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