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