Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 19 | import org.apache.http.HttpEntity; |
| 20 | import org.apache.http.HttpHost; |
| 21 | import org.apache.http.HttpResponse; |
| 22 | import org.apache.http.client.methods.HttpGet; |
| 23 | import org.apache.http.client.params.HttpClientParams; |
| 24 | import org.apache.http.conn.params.ConnRouteParams; |
| 25 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 26 | import android.content.ContentResolver; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 27 | import android.content.ContentValues; |
Bjorn Bringert | 6783203 | 2010-10-12 08:24:17 +0100 | [diff] [blame^] | 28 | import android.content.Context; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 29 | import android.database.Cursor; |
| 30 | import android.graphics.Bitmap; |
| 31 | import android.graphics.BitmapFactory; |
Andreas Sandblad | d159ec5 | 2010-06-16 13:10:39 +0200 | [diff] [blame] | 32 | import android.net.Proxy; |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 33 | import android.net.http.AndroidHttpClient; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 34 | import android.os.AsyncTask; |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 35 | import android.os.Bundle; |
| 36 | import android.os.Message; |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 37 | import android.provider.BrowserContract.Images; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 38 | import android.webkit.WebView; |
| 39 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 40 | import java.io.ByteArrayOutputStream; |
| 41 | import java.io.IOException; |
| 42 | import java.io.InputStream; |
| 43 | |
Patrick Scott | a74f696 | 2009-11-10 13:06:47 -0500 | [diff] [blame] | 44 | class DownloadTouchIcon extends AsyncTask<String, Void, Void> { |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 45 | private final ContentResolver mContentResolver; |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 46 | private Cursor mCursor; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 47 | private final String mOriginalUrl; |
| 48 | private final String mUrl; |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 49 | private final String mUserAgent; // Sites may serve a different icon to different UAs |
| 50 | private Message mMessage; |
| 51 | |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 52 | private final Context mContext; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 53 | /* package */ Tab mTab; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 54 | |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 55 | /** |
| 56 | * Use this ctor to store the touch icon in the bookmarks database for |
| 57 | * the originalUrl so we take account of redirects. Used when the user |
| 58 | * bookmarks a page from outside the bookmarks activity. |
| 59 | */ |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 60 | public DownloadTouchIcon(Tab tab, Context ctx, ContentResolver cr, WebView view) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 61 | mTab = tab; |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 62 | mContext = ctx; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 63 | mContentResolver = cr; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 64 | // Store these in case they change. |
| 65 | mOriginalUrl = view.getOriginalUrl(); |
| 66 | mUrl = view.getUrl(); |
| 67 | mUserAgent = view.getSettings().getUserAgentString(); |
| 68 | } |
| 69 | |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 70 | /** |
| 71 | * Use this ctor to download the touch icon and update the bookmarks database |
| 72 | * entry for the given url. Used when the user creates a bookmark from |
| 73 | * within the bookmarks activity and there haven't been any redirects. |
| 74 | * TODO: Would be nice to set the user agent here so that there is no |
| 75 | * potential for the three different ctors here to return different icons. |
| 76 | */ |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 77 | public DownloadTouchIcon(Context ctx, ContentResolver cr, String url) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 78 | mTab = null; |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 79 | mContext = ctx; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 80 | mContentResolver = cr; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 81 | mOriginalUrl = null; |
| 82 | mUrl = url; |
| 83 | mUserAgent = null; |
| 84 | } |
| 85 | |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 86 | /** |
| 87 | * Use this ctor to not store the touch icon in a database, rather add it to |
| 88 | * the passed Message's data bundle with the key "touchIcon" and then send |
| 89 | * the message. |
| 90 | */ |
Bjorn Bringert | 6783203 | 2010-10-12 08:24:17 +0100 | [diff] [blame^] | 91 | public DownloadTouchIcon(Context context, Message msg, String userAgent) { |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 92 | mMessage = msg; |
Bjorn Bringert | 6783203 | 2010-10-12 08:24:17 +0100 | [diff] [blame^] | 93 | mContext = context; |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 94 | mContentResolver = null; |
| 95 | mOriginalUrl = null; |
| 96 | mUrl = null; |
| 97 | mUserAgent = userAgent; |
| 98 | } |
| 99 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 100 | @Override |
Patrick Scott | a74f696 | 2009-11-10 13:06:47 -0500 | [diff] [blame] | 101 | public Void doInBackground(String... values) { |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 102 | if (mContentResolver != null) { |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 103 | mCursor = Bookmarks.queryCombinedForUrl(mContentResolver, |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 104 | mOriginalUrl, mUrl); |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 105 | } |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 106 | |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 107 | boolean inDatabase = mCursor != null && mCursor.getCount() > 0; |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 108 | |
| 109 | String url = values[0]; |
| 110 | |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 111 | if (inDatabase || mMessage != null) { |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 112 | AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent); |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 113 | HttpHost httpHost = Proxy.getPreferredHttpHost(mContext, url); |
Andreas Sandblad | d159ec5 | 2010-06-16 13:10:39 +0200 | [diff] [blame] | 114 | if (httpHost != null) { |
| 115 | ConnRouteParams.setDefaultProxy(client.getParams(), httpHost); |
| 116 | } |
| 117 | |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 118 | HttpGet request = new HttpGet(url); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 119 | |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 120 | // Follow redirects |
| 121 | HttpClientParams.setRedirecting(client.getParams(), true); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 122 | |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 123 | try { |
| 124 | HttpResponse response = client.execute(request); |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 125 | if (response.getStatusLine().getStatusCode() == 200) { |
| 126 | HttpEntity entity = response.getEntity(); |
| 127 | if (entity != null) { |
| 128 | InputStream content = entity.getContent(); |
| 129 | if (content != null) { |
| 130 | Bitmap icon = BitmapFactory.decodeStream( |
| 131 | content, null, null); |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 132 | if (inDatabase) { |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 133 | storeIcon(icon); |
| 134 | } else if (mMessage != null) { |
| 135 | Bundle b = mMessage.getData(); |
| 136 | b.putParcelable("touchIcon", icon); |
| 137 | } |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 138 | } |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 139 | } |
| 140 | } |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 141 | } catch (IllegalArgumentException ex) { |
| 142 | request.abort(); |
| 143 | } catch (IOException ex) { |
| 144 | request.abort(); |
| 145 | } finally { |
| 146 | client.close(); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 147 | } |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 148 | } |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 149 | |
Patrick Scott | 8e9fe32 | 2010-03-04 14:29:31 -0500 | [diff] [blame] | 150 | if (mCursor != null) { |
| 151 | mCursor.close(); |
| 152 | } |
Ben Murdoch | e322f56 | 2010-07-01 12:21:19 +0100 | [diff] [blame] | 153 | |
| 154 | if (mMessage != null) { |
| 155 | mMessage.sendToTarget(); |
| 156 | } |
| 157 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 158 | return null; |
| 159 | } |
| 160 | |
| 161 | @Override |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 162 | protected void onCancelled() { |
| 163 | if (mCursor != null) { |
| 164 | mCursor.close(); |
| 165 | } |
| 166 | } |
| 167 | |
Patrick Scott | a74f696 | 2009-11-10 13:06:47 -0500 | [diff] [blame] | 168 | private void storeIcon(Bitmap icon) { |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 169 | // Do this first in case the download failed. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 170 | if (mTab != null) { |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 171 | // Remove the touch icon loader from the BrowserActivity. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 172 | mTab.mTouchIconLoader = null; |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | if (icon == null || mCursor == null || isCancelled()) { |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 176 | return; |
| 177 | } |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 178 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 179 | if (mCursor.moveToFirst()) { |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 180 | final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 181 | icon.compress(Bitmap.CompressFormat.PNG, 100, os); |
| 182 | |
| 183 | ContentValues values = new ContentValues(); |
| 184 | values.put(Images.TOUCH_ICON, os.toByteArray()); |
| 185 | values.put(Images.URL, mCursor.getString(0)); |
| 186 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 187 | do { |
Jeff Hamilton | 1a80565 | 2010-09-07 12:36:30 -0700 | [diff] [blame] | 188 | mContentResolver.update(Images.CONTENT_URI, values, null, null); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 189 | } while (mCursor.moveToNext()); |
| 190 | } |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 191 | } |
| 192 | } |