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