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; 45 /* package */ BrowserActivity mActivity; 46 DownloadTouchIcon(BrowserActivity activity, ContentResolver cr, Cursor c, WebView view)47 public DownloadTouchIcon(BrowserActivity activity, ContentResolver cr, 48 Cursor c, WebView view) { 49 mActivity = activity; 50 mContentResolver = cr; 51 mCursor = c; 52 // Store these in case they change. 53 mOriginalUrl = view.getOriginalUrl(); 54 mUrl = view.getUrl(); 55 mUserAgent = view.getSettings().getUserAgentString(); 56 } 57 DownloadTouchIcon(ContentResolver cr, Cursor c, String url)58 public DownloadTouchIcon(ContentResolver cr, Cursor c, String url) { 59 mActivity = null; 60 mContentResolver = cr; 61 mCursor = c; 62 mOriginalUrl = null; 63 mUrl = url; 64 mUserAgent = null; 65 } 66 67 @Override doInBackground(String... values)68 public Bitmap doInBackground(String... values) { 69 String url = values[0]; 70 71 AndroidHttpClient client = AndroidHttpClient.newInstance( 72 mUserAgent); 73 HttpGet request = new HttpGet(url); 74 75 // Follow redirects 76 HttpClientParams.setRedirecting(client.getParams(), true); 77 78 try { 79 HttpResponse response = client.execute(request); 80 81 if (response.getStatusLine().getStatusCode() == 200) { 82 HttpEntity entity = response.getEntity(); 83 if (entity != null) { 84 InputStream content = entity.getContent(); 85 if (content != null) { 86 Bitmap icon = BitmapFactory.decodeStream( 87 content, null, null); 88 return icon; 89 } 90 } 91 } 92 } catch (IllegalArgumentException ex) { 93 request.abort(); 94 } catch (IOException ex) { 95 request.abort(); 96 } finally { 97 client.close(); 98 } 99 return null; 100 } 101 102 @Override onCancelled()103 protected void onCancelled() { 104 if (mCursor != null) { 105 mCursor.close(); 106 } 107 } 108 109 @Override onPostExecute(Bitmap icon)110 public void onPostExecute(Bitmap icon) { 111 // Do this first in case the download failed. 112 if (mActivity != null) { 113 // Remove the touch icon loader from the BrowserActivity. 114 mActivity.mTouchIconLoader = null; 115 } 116 117 if (icon == null || mCursor == null || isCancelled()) { 118 return; 119 } 120 121 final ByteArrayOutputStream os = new ByteArrayOutputStream(); 122 icon.compress(Bitmap.CompressFormat.PNG, 100, os); 123 ContentValues values = new ContentValues(); 124 values.put(Browser.BookmarkColumns.TOUCH_ICON, 125 os.toByteArray()); 126 127 if (mCursor.moveToFirst()) { 128 do { 129 mContentResolver.update(ContentUris.withAppendedId( 130 Browser.BOOKMARKS_URI, mCursor.getInt(0)), 131 values, null, null); 132 } while (mCursor.moveToNext()); 133 } 134 mCursor.close(); 135 } 136 } 137