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