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 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 40 class DownloadTouchIcon extends AsyncTask<String, Void, Void> { 41 private final ContentResolver mContentResolver; 42 private Cursor mCursor; 43 private final String mOriginalUrl; 44 private final String mUrl; 45 private final String mUserAgent; 46 /* package */ Tab mTab; 47 48 public DownloadTouchIcon(Tab tab, ContentResolver cr, WebView view) { 49 mTab = tab; 50 mContentResolver = cr; 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, String url) { 58 mTab = null; 59 mContentResolver = cr; 60 mOriginalUrl = null; 61 mUrl = url; 62 mUserAgent = null; 63 } 64 65 @Override 66 public Void doInBackground(String... values) { 67 mCursor = BrowserBookmarksAdapter.queryBookmarksForUrl(mContentResolver, 68 mOriginalUrl, mUrl, true); 69 if (mCursor != null && mCursor.getCount() > 0) { 70 String url = values[0]; 71 72 AndroidHttpClient client = AndroidHttpClient.newInstance( 73 mUserAgent); 74 HttpGet request = new HttpGet(url); 75 76 // Follow redirects 77 HttpClientParams.setRedirecting(client.getParams(), true); 78 79 try { 80 HttpResponse response = client.execute(request); 81 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 } 91 } 92 } 93 } catch (IllegalArgumentException ex) { 94 request.abort(); 95 } catch (IOException ex) { 96 request.abort(); 97 } finally { 98 client.close(); 99 } 100 } 101 if (mCursor != null) { 102 mCursor.close(); 103 } 104 return null; 105 } 106 107 @Override 108 protected void onCancelled() { 109 if (mCursor != null) { 110 mCursor.close(); 111 } 112 } 113 114 private void storeIcon(Bitmap icon) { 115 // Do this first in case the download failed. 116 if (mTab != null) { 117 // Remove the touch icon loader from the BrowserActivity. 118 mTab.mTouchIconLoader = null; 119 } 120 121 if (icon == null || mCursor == null || isCancelled()) { 122 return; 123 } 124 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 } 138 } 139 } 140