blob: ad161315fac1743ca9da18523db300a8ed458e45 [file] [log] [blame]
Patrick Scott3918d442009-08-04 13:22:29 -04001/*
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
17package com.android.browser;
18
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050019import org.apache.http.HttpEntity;
20import org.apache.http.HttpHost;
21import org.apache.http.HttpResponse;
22import org.apache.http.client.methods.HttpGet;
23import org.apache.http.client.params.HttpClientParams;
24import org.apache.http.conn.params.ConnRouteParams;
25
Patrick Scott3918d442009-08-04 13:22:29 -040026import android.content.ContentResolver;
Patrick Scott3918d442009-08-04 13:22:29 -040027import android.content.ContentValues;
Bjorn Bringert67832032010-10-12 08:24:17 +010028import android.content.Context;
Patrick Scott3918d442009-08-04 13:22:29 -040029import android.database.Cursor;
30import android.graphics.Bitmap;
31import android.graphics.BitmapFactory;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020032import android.net.Proxy;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050033import android.net.http.AndroidHttpClient;
Patrick Scott3918d442009-08-04 13:22:29 -040034import android.os.AsyncTask;
Ben Murdoche322f562010-07-01 12:21:19 +010035import android.os.Bundle;
36import android.os.Message;
Leon Scrogginsbc922852010-10-22 12:15:27 -040037import android.provider.BrowserContract;
Jeff Hamilton1a805652010-09-07 12:36:30 -070038import android.provider.BrowserContract.Images;
Patrick Scott3918d442009-08-04 13:22:29 -040039import android.webkit.WebView;
40
Patrick Scott3918d442009-08-04 13:22:29 -040041import java.io.ByteArrayOutputStream;
42import java.io.IOException;
43import java.io.InputStream;
44
Patrick Scotta74f6962009-11-10 13:06:47 -050045class DownloadTouchIcon extends AsyncTask<String, Void, Void> {
Patrick Scott3918d442009-08-04 13:22:29 -040046 private final ContentResolver mContentResolver;
Leon Scrogginsc8393d92010-04-23 14:58:16 -040047 private Cursor mCursor;
Patrick Scott3918d442009-08-04 13:22:29 -040048 private final String mOriginalUrl;
49 private final String mUrl;
Ben Murdoche322f562010-07-01 12:21:19 +010050 private final String mUserAgent; // Sites may serve a different icon to different UAs
51 private Message mMessage;
52
Henrik Baard980e9952010-09-06 18:13:23 +020053 private final Context mContext;
Grace Kloba22ac16e2009-10-07 18:00:23 -070054 /* package */ Tab mTab;
Patrick Scott3918d442009-08-04 13:22:29 -040055
Ben Murdoche322f562010-07-01 12:21:19 +010056 /**
57 * Use this ctor to store the touch icon in the bookmarks database for
58 * the originalUrl so we take account of redirects. Used when the user
59 * bookmarks a page from outside the bookmarks activity.
60 */
Henrik Baard980e9952010-09-06 18:13:23 +020061 public DownloadTouchIcon(Tab tab, Context ctx, ContentResolver cr, WebView view) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070062 mTab = tab;
Henrik Baard980e9952010-09-06 18:13:23 +020063 mContext = ctx;
Patrick Scott3918d442009-08-04 13:22:29 -040064 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040065 // Store these in case they change.
66 mOriginalUrl = view.getOriginalUrl();
67 mUrl = view.getUrl();
68 mUserAgent = view.getSettings().getUserAgentString();
69 }
70
Ben Murdoche322f562010-07-01 12:21:19 +010071 /**
72 * Use this ctor to download the touch icon and update the bookmarks database
73 * entry for the given url. Used when the user creates a bookmark from
74 * within the bookmarks activity and there haven't been any redirects.
75 * TODO: Would be nice to set the user agent here so that there is no
76 * potential for the three different ctors here to return different icons.
77 */
Henrik Baard980e9952010-09-06 18:13:23 +020078 public DownloadTouchIcon(Context ctx, ContentResolver cr, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070079 mTab = null;
Henrik Baard980e9952010-09-06 18:13:23 +020080 mContext = ctx;
Patrick Scott3918d442009-08-04 13:22:29 -040081 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040082 mOriginalUrl = null;
83 mUrl = url;
84 mUserAgent = null;
85 }
86
Ben Murdoche322f562010-07-01 12:21:19 +010087 /**
88 * Use this ctor to not store the touch icon in a database, rather add it to
Leon Scrogginsbc922852010-10-22 12:15:27 -040089 * the passed Message's data bundle with the key
90 * {@link BrowserContract.Bookmarks#TOUCH_ICON} and then send the message.
Ben Murdoche322f562010-07-01 12:21:19 +010091 */
Bjorn Bringert67832032010-10-12 08:24:17 +010092 public DownloadTouchIcon(Context context, Message msg, String userAgent) {
Ben Murdoche322f562010-07-01 12:21:19 +010093 mMessage = msg;
Bjorn Bringert67832032010-10-12 08:24:17 +010094 mContext = context;
Ben Murdoche322f562010-07-01 12:21:19 +010095 mContentResolver = null;
96 mOriginalUrl = null;
97 mUrl = null;
98 mUserAgent = userAgent;
99 }
100
Patrick Scott3918d442009-08-04 13:22:29 -0400101 @Override
Patrick Scotta74f6962009-11-10 13:06:47 -0500102 public Void doInBackground(String... values) {
Ben Murdoche322f562010-07-01 12:21:19 +0100103 if (mContentResolver != null) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700104 mCursor = Bookmarks.queryCombinedForUrl(mContentResolver,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500105 mOriginalUrl, mUrl);
Ben Murdoche322f562010-07-01 12:21:19 +0100106 }
Patrick Scott3918d442009-08-04 13:22:29 -0400107
Jeff Hamilton1a805652010-09-07 12:36:30 -0700108 boolean inDatabase = mCursor != null && mCursor.getCount() > 0;
Ben Murdoche322f562010-07-01 12:21:19 +0100109
110 String url = values[0];
111
Jeff Hamilton1a805652010-09-07 12:36:30 -0700112 if (inDatabase || mMessage != null) {
Ben Murdoche322f562010-07-01 12:21:19 +0100113 AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent);
Henrik Baard980e9952010-09-06 18:13:23 +0200114 HttpHost httpHost = Proxy.getPreferredHttpHost(mContext, url);
Andreas Sandbladd159ec52010-06-16 13:10:39 +0200115 if (httpHost != null) {
116 ConnRouteParams.setDefaultProxy(client.getParams(), httpHost);
117 }
118
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400119 HttpGet request = new HttpGet(url);
Patrick Scott3918d442009-08-04 13:22:29 -0400120
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400121 // Follow redirects
122 HttpClientParams.setRedirecting(client.getParams(), true);
Patrick Scott3918d442009-08-04 13:22:29 -0400123
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400124 try {
125 HttpResponse response = client.execute(request);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400126 if (response.getStatusLine().getStatusCode() == 200) {
127 HttpEntity entity = response.getEntity();
128 if (entity != null) {
129 InputStream content = entity.getContent();
130 if (content != null) {
131 Bitmap icon = BitmapFactory.decodeStream(
132 content, null, null);
Jeff Hamilton1a805652010-09-07 12:36:30 -0700133 if (inDatabase) {
Ben Murdoche322f562010-07-01 12:21:19 +0100134 storeIcon(icon);
135 } else if (mMessage != null) {
136 Bundle b = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400137 b.putParcelable(BrowserContract.Bookmarks.TOUCH_ICON, icon);
Ben Murdoche322f562010-07-01 12:21:19 +0100138 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400139 }
Patrick Scott3918d442009-08-04 13:22:29 -0400140 }
141 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400142 } catch (IllegalArgumentException ex) {
143 request.abort();
144 } catch (IOException ex) {
145 request.abort();
146 } finally {
147 client.close();
Patrick Scott3918d442009-08-04 13:22:29 -0400148 }
Patrick Scott3918d442009-08-04 13:22:29 -0400149 }
Ben Murdoche322f562010-07-01 12:21:19 +0100150
Patrick Scott8e9fe322010-03-04 14:29:31 -0500151 if (mCursor != null) {
152 mCursor.close();
153 }
Ben Murdoche322f562010-07-01 12:21:19 +0100154
155 if (mMessage != null) {
156 mMessage.sendToTarget();
157 }
158
Patrick Scott3918d442009-08-04 13:22:29 -0400159 return null;
160 }
161
162 @Override
Patrick Scott59ce8302009-09-18 16:29:38 -0400163 protected void onCancelled() {
164 if (mCursor != null) {
165 mCursor.close();
166 }
167 }
168
Patrick Scotta74f6962009-11-10 13:06:47 -0500169 private void storeIcon(Bitmap icon) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400170 // Do this first in case the download failed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700171 if (mTab != null) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400172 // Remove the touch icon loader from the BrowserActivity.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700173 mTab.mTouchIconLoader = null;
Patrick Scott59ce8302009-09-18 16:29:38 -0400174 }
175
176 if (icon == null || mCursor == null || isCancelled()) {
Patrick Scott3918d442009-08-04 13:22:29 -0400177 return;
178 }
Patrick Scott59ce8302009-09-18 16:29:38 -0400179
Patrick Scott3918d442009-08-04 13:22:29 -0400180 if (mCursor.moveToFirst()) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700181 final ByteArrayOutputStream os = new ByteArrayOutputStream();
182 icon.compress(Bitmap.CompressFormat.PNG, 100, os);
183
184 ContentValues values = new ContentValues();
185 values.put(Images.TOUCH_ICON, os.toByteArray());
Jeff Hamilton1a805652010-09-07 12:36:30 -0700186
Patrick Scott3918d442009-08-04 13:22:29 -0400187 do {
John Reck9e7686e2011-02-17 13:25:59 -0800188 values.put(Images.URL, mCursor.getString(0));
Jeff Hamilton1a805652010-09-07 12:36:30 -0700189 mContentResolver.update(Images.CONTENT_URI, values, null, null);
Patrick Scott3918d442009-08-04 13:22:29 -0400190 } while (mCursor.moveToNext());
191 }
Patrick Scott3918d442009-08-04 13:22:29 -0400192 }
193}