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; |
Andreas Sandblad | d159ec5 | 2010-06-16 13:10:39 +0200 | [diff] [blame] | 26 | import android.net.Proxy; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 27 | import android.os.AsyncTask; |
| 28 | import android.provider.Browser; |
| 29 | import android.webkit.WebView; |
| 30 | |
Paul Westbrook | c6343ad | 2009-12-11 15:35:38 -0800 | [diff] [blame] | 31 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 32 | import org.apache.http.HttpEntity; |
Andreas Sandblad | d159ec5 | 2010-06-16 13:10:39 +0200 | [diff] [blame] | 33 | import org.apache.http.HttpHost; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 34 | import org.apache.http.HttpResponse; |
| 35 | import org.apache.http.client.methods.HttpGet; |
| 36 | import org.apache.http.client.params.HttpClientParams; |
Andreas Sandblad | d159ec5 | 2010-06-16 13:10:39 +0200 | [diff] [blame] | 37 | import org.apache.http.conn.params.ConnRouteParams; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 38 | |
| 39 | import java.io.ByteArrayOutputStream; |
| 40 | import java.io.IOException; |
| 41 | import java.io.InputStream; |
| 42 | |
Patrick Scott | a74f696 | 2009-11-10 13:06:47 -0500 | [diff] [blame] | 43 | class DownloadTouchIcon extends AsyncTask<String, Void, Void> { |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 44 | private final ContentResolver mContentResolver; |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 45 | private Cursor mCursor; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 46 | private final String mOriginalUrl; |
| 47 | private final String mUrl; |
| 48 | private final String mUserAgent; |
Andreas Sandblad | d159ec5 | 2010-06-16 13:10:39 +0200 | [diff] [blame] | 49 | private final BrowserActivity mActivity; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 50 | /* package */ Tab mTab; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 51 | |
Andreas Sandblad | d159ec5 | 2010-06-16 13:10:39 +0200 | [diff] [blame] | 52 | public DownloadTouchIcon(Tab tab, BrowserActivity activity, ContentResolver cr, WebView view) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 53 | mTab = tab; |
Andreas Sandblad | d159ec5 | 2010-06-16 13:10:39 +0200 | [diff] [blame] | 54 | mActivity = activity; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 55 | mContentResolver = cr; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 56 | // Store these in case they change. |
| 57 | mOriginalUrl = view.getOriginalUrl(); |
| 58 | mUrl = view.getUrl(); |
| 59 | mUserAgent = view.getSettings().getUserAgentString(); |
| 60 | } |
| 61 | |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 62 | public DownloadTouchIcon(ContentResolver cr, String url) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 63 | mTab = null; |
Andreas Sandblad | d159ec5 | 2010-06-16 13:10:39 +0200 | [diff] [blame] | 64 | mActivity = null; |
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 | mOriginalUrl = null; |
| 67 | mUrl = url; |
| 68 | mUserAgent = null; |
| 69 | } |
| 70 | |
| 71 | @Override |
Patrick Scott | a74f696 | 2009-11-10 13:06:47 -0500 | [diff] [blame] | 72 | public Void doInBackground(String... values) { |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 73 | mCursor = BrowserBookmarksAdapter.queryBookmarksForUrl(mContentResolver, |
| 74 | mOriginalUrl, mUrl, true); |
| 75 | if (mCursor != null && mCursor.getCount() > 0) { |
| 76 | String url = values[0]; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 77 | |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 78 | AndroidHttpClient client = AndroidHttpClient.newInstance( |
| 79 | mUserAgent); |
Andreas Sandblad | d159ec5 | 2010-06-16 13:10:39 +0200 | [diff] [blame] | 80 | HttpHost httpHost = Proxy.getPreferredHttpHost(mActivity, url); |
| 81 | if (httpHost != null) { |
| 82 | ConnRouteParams.setDefaultProxy(client.getParams(), httpHost); |
| 83 | } |
| 84 | |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 85 | HttpGet request = new HttpGet(url); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 86 | |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 87 | // Follow redirects |
| 88 | HttpClientParams.setRedirecting(client.getParams(), true); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 89 | |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 90 | try { |
| 91 | HttpResponse response = client.execute(request); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 92 | |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 93 | if (response.getStatusLine().getStatusCode() == 200) { |
| 94 | HttpEntity entity = response.getEntity(); |
| 95 | if (entity != null) { |
| 96 | InputStream content = entity.getContent(); |
| 97 | if (content != null) { |
| 98 | Bitmap icon = BitmapFactory.decodeStream( |
| 99 | content, null, null); |
| 100 | storeIcon(icon); |
| 101 | } |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 102 | } |
| 103 | } |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 104 | } catch (IllegalArgumentException ex) { |
| 105 | request.abort(); |
| 106 | } catch (IOException ex) { |
| 107 | request.abort(); |
| 108 | } finally { |
| 109 | client.close(); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 110 | } |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 111 | } |
Patrick Scott | 8e9fe32 | 2010-03-04 14:29:31 -0500 | [diff] [blame] | 112 | if (mCursor != null) { |
| 113 | mCursor.close(); |
| 114 | } |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 115 | return null; |
| 116 | } |
| 117 | |
| 118 | @Override |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 119 | protected void onCancelled() { |
| 120 | if (mCursor != null) { |
| 121 | mCursor.close(); |
| 122 | } |
| 123 | } |
| 124 | |
Patrick Scott | a74f696 | 2009-11-10 13:06:47 -0500 | [diff] [blame] | 125 | private void storeIcon(Bitmap icon) { |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 126 | // Do this first in case the download failed. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 127 | if (mTab != null) { |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 128 | // Remove the touch icon loader from the BrowserActivity. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 129 | mTab.mTouchIconLoader = null; |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (icon == null || mCursor == null || isCancelled()) { |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 133 | return; |
| 134 | } |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 135 | |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 136 | final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 137 | icon.compress(Bitmap.CompressFormat.PNG, 100, os); |
| 138 | ContentValues values = new ContentValues(); |
| 139 | values.put(Browser.BookmarkColumns.TOUCH_ICON, |
| 140 | os.toByteArray()); |
| 141 | |
| 142 | if (mCursor.moveToFirst()) { |
| 143 | do { |
| 144 | mContentResolver.update(ContentUris.withAppendedId( |
| 145 | Browser.BOOKMARKS_URI, mCursor.getInt(0)), |
| 146 | values, null, null); |
| 147 | } while (mCursor.moveToNext()); |
| 148 | } |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 149 | } |
| 150 | } |