blob: 64a7316f00406798119ea2d7b0f334efe03c2871 [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;
Patrick Scott3918d442009-08-04 13:22:29 -040042import java.io.InputStream;
43
Patrick Scotta74f6962009-11-10 13:06:47 -050044class DownloadTouchIcon extends AsyncTask<String, Void, Void> {
John Reckbf6b54d2011-02-17 16:15:44 -080045
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) {
John Reckbf6b54d2011-02-17 16:15:44 -0800113 AndroidHttpClient client = null;
114 HttpGet request = null;
Patrick Scott3918d442009-08-04 13:22:29 -0400115
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400116 try {
John Reckbf6b54d2011-02-17 16:15:44 -0800117 client = AndroidHttpClient.newInstance(mUserAgent);
118 HttpHost httpHost = Proxy.getPreferredHttpHost(mContext, url);
119 if (httpHost != null) {
120 ConnRouteParams.setDefaultProxy(client.getParams(), httpHost);
121 }
122
123 request = new HttpGet(url);
124
125 // Follow redirects
126 HttpClientParams.setRedirecting(client.getParams(), true);
127
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400128 HttpResponse response = client.execute(request);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400129 if (response.getStatusLine().getStatusCode() == 200) {
130 HttpEntity entity = response.getEntity();
131 if (entity != null) {
132 InputStream content = entity.getContent();
133 if (content != null) {
134 Bitmap icon = BitmapFactory.decodeStream(
135 content, null, null);
Jeff Hamilton1a805652010-09-07 12:36:30 -0700136 if (inDatabase) {
Ben Murdoche322f562010-07-01 12:21:19 +0100137 storeIcon(icon);
138 } else if (mMessage != null) {
139 Bundle b = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400140 b.putParcelable(BrowserContract.Bookmarks.TOUCH_ICON, icon);
Ben Murdoche322f562010-07-01 12:21:19 +0100141 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400142 }
Patrick Scott3918d442009-08-04 13:22:29 -0400143 }
144 }
John Reckbf6b54d2011-02-17 16:15:44 -0800145 } catch (Exception ex) {
146 if (request != null) {
147 request.abort();
148 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400149 } finally {
John Reckbf6b54d2011-02-17 16:15:44 -0800150 if (client != null) {
151 client.close();
152 }
Patrick Scott3918d442009-08-04 13:22:29 -0400153 }
Patrick Scott3918d442009-08-04 13:22:29 -0400154 }
Ben Murdoche322f562010-07-01 12:21:19 +0100155
Patrick Scott8e9fe322010-03-04 14:29:31 -0500156 if (mCursor != null) {
157 mCursor.close();
158 }
Ben Murdoche322f562010-07-01 12:21:19 +0100159
160 if (mMessage != null) {
161 mMessage.sendToTarget();
162 }
163
Patrick Scott3918d442009-08-04 13:22:29 -0400164 return null;
165 }
166
167 @Override
Patrick Scott59ce8302009-09-18 16:29:38 -0400168 protected void onCancelled() {
169 if (mCursor != null) {
170 mCursor.close();
171 }
172 }
173
Patrick Scotta74f6962009-11-10 13:06:47 -0500174 private void storeIcon(Bitmap icon) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400175 // Do this first in case the download failed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700176 if (mTab != null) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400177 // Remove the touch icon loader from the BrowserActivity.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700178 mTab.mTouchIconLoader = null;
Patrick Scott59ce8302009-09-18 16:29:38 -0400179 }
180
181 if (icon == null || mCursor == null || isCancelled()) {
Patrick Scott3918d442009-08-04 13:22:29 -0400182 return;
183 }
Patrick Scott59ce8302009-09-18 16:29:38 -0400184
Patrick Scott3918d442009-08-04 13:22:29 -0400185 if (mCursor.moveToFirst()) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700186 final ByteArrayOutputStream os = new ByteArrayOutputStream();
187 icon.compress(Bitmap.CompressFormat.PNG, 100, os);
188
189 ContentValues values = new ContentValues();
190 values.put(Images.TOUCH_ICON, os.toByteArray());
Jeff Hamilton1a805652010-09-07 12:36:30 -0700191
Patrick Scott3918d442009-08-04 13:22:29 -0400192 do {
John Reck9e7686e2011-02-17 13:25:59 -0800193 values.put(Images.URL, mCursor.getString(0));
Jeff Hamilton1a805652010-09-07 12:36:30 -0700194 mContentResolver.update(Images.CONTENT_URI, values, null, null);
Patrick Scott3918d442009-08-04 13:22:29 -0400195 } while (mCursor.moveToNext());
196 }
Patrick Scott3918d442009-08-04 13:22:29 -0400197 }
198}