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