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