blob: 7bb93dc596973760e3c68ce04b5cd948e1027402 [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
Ben Murdochccb5de02010-07-19 18:38:17 +010026import android.app.Activity;
Patrick Scott3918d442009-08-04 13:22:29 -040027import android.content.ContentResolver;
28import android.content.ContentUris;
29import android.content.ContentValues;
30import android.database.Cursor;
31import android.graphics.Bitmap;
32import android.graphics.BitmapFactory;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020033import android.net.Proxy;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050034import android.net.http.AndroidHttpClient;
Patrick Scott3918d442009-08-04 13:22:29 -040035import android.os.AsyncTask;
Ben Murdoche322f562010-07-01 12:21:19 +010036import android.os.Bundle;
37import android.os.Message;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050038import android.provider.BrowserContract;
Jeff Hamilton1a805652010-09-07 12:36:30 -070039import android.provider.BrowserContract.Images;
Patrick Scott3918d442009-08-04 13:22:29 -040040import android.webkit.WebView;
41
Patrick Scott3918d442009-08-04 13:22:29 -040042import java.io.ByteArrayOutputStream;
43import java.io.IOException;
44import java.io.InputStream;
45
Patrick Scotta74f6962009-11-10 13:06:47 -050046class DownloadTouchIcon extends AsyncTask<String, Void, Void> {
Patrick Scott3918d442009-08-04 13:22:29 -040047 private final ContentResolver mContentResolver;
Leon Scrogginsc8393d92010-04-23 14:58:16 -040048 private Cursor mCursor;
Patrick Scott3918d442009-08-04 13:22:29 -040049 private final String mOriginalUrl;
50 private final String mUrl;
Ben Murdoche322f562010-07-01 12:21:19 +010051 private final String mUserAgent; // Sites may serve a different icon to different UAs
52 private Message mMessage;
53
Ben Murdochccb5de02010-07-19 18:38:17 +010054 private final Activity mActivity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055 /* package */ Tab mTab;
Patrick Scott3918d442009-08-04 13:22:29 -040056
Ben Murdoche322f562010-07-01 12:21:19 +010057 /**
58 * Use this ctor to store the touch icon in the bookmarks database for
59 * the originalUrl so we take account of redirects. Used when the user
60 * bookmarks a page from outside the bookmarks activity.
61 */
Andreas Sandbladd159ec52010-06-16 13:10:39 +020062 public DownloadTouchIcon(Tab tab, BrowserActivity activity, ContentResolver cr, WebView view) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070063 mTab = tab;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020064 mActivity = activity;
Patrick Scott3918d442009-08-04 13:22:29 -040065 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040066 // Store these in case they change.
67 mOriginalUrl = view.getOriginalUrl();
68 mUrl = view.getUrl();
69 mUserAgent = view.getSettings().getUserAgentString();
70 }
71
Ben Murdoche322f562010-07-01 12:21:19 +010072 /**
73 * Use this ctor to download the touch icon and update the bookmarks database
74 * entry for the given url. Used when the user creates a bookmark from
75 * within the bookmarks activity and there haven't been any redirects.
76 * TODO: Would be nice to set the user agent here so that there is no
77 * potential for the three different ctors here to return different icons.
78 */
Ben Murdochccb5de02010-07-19 18:38:17 +010079 public DownloadTouchIcon(AddBookmarkPage activity, ContentResolver cr, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070080 mTab = null;
Ben Murdochccb5de02010-07-19 18:38:17 +010081 mActivity = activity;
Patrick Scott3918d442009-08-04 13:22:29 -040082 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040083 mOriginalUrl = null;
84 mUrl = url;
85 mUserAgent = null;
86 }
87
Ben Murdoche322f562010-07-01 12:21:19 +010088 /**
89 * Use this ctor to not store the touch icon in a database, rather add it to
90 * the passed Message's data bundle with the key "touchIcon" and then send
91 * the message.
92 */
Ben Murdochccb5de02010-07-19 18:38:17 +010093 public DownloadTouchIcon(BrowserActivity activity, Message msg, String userAgent) {
Ben Murdoche322f562010-07-01 12:21:19 +010094 mMessage = msg;
Ben Murdochccb5de02010-07-19 18:38:17 +010095 mActivity = activity;
Ben Murdoche322f562010-07-01 12:21:19 +010096 mContentResolver = null;
97 mOriginalUrl = null;
98 mUrl = null;
99 mUserAgent = userAgent;
100 }
101
Patrick Scott3918d442009-08-04 13:22:29 -0400102 @Override
Patrick Scotta74f6962009-11-10 13:06:47 -0500103 public Void doInBackground(String... values) {
Ben Murdoche322f562010-07-01 12:21:19 +0100104 if (mContentResolver != null) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700105 mCursor = Bookmarks.queryCombinedForUrl(mContentResolver,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500106 mOriginalUrl, mUrl);
Ben Murdoche322f562010-07-01 12:21:19 +0100107 }
Patrick Scott3918d442009-08-04 13:22:29 -0400108
Jeff Hamilton1a805652010-09-07 12:36:30 -0700109 boolean inDatabase = mCursor != null && mCursor.getCount() > 0;
Ben Murdoche322f562010-07-01 12:21:19 +0100110
111 String url = values[0];
112
Jeff Hamilton1a805652010-09-07 12:36:30 -0700113 if (inDatabase || mMessage != null) {
Ben Murdoche322f562010-07-01 12:21:19 +0100114 AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent);
Andreas Sandbladd159ec52010-06-16 13:10:39 +0200115 HttpHost httpHost = Proxy.getPreferredHttpHost(mActivity, url);
116 if (httpHost != null) {
117 ConnRouteParams.setDefaultProxy(client.getParams(), httpHost);
118 }
119
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400120 HttpGet request = new HttpGet(url);
Patrick Scott3918d442009-08-04 13:22:29 -0400121
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400122 // Follow redirects
123 HttpClientParams.setRedirecting(client.getParams(), true);
Patrick Scott3918d442009-08-04 13:22:29 -0400124
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400125 try {
126 HttpResponse response = client.execute(request);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400127 if (response.getStatusLine().getStatusCode() == 200) {
128 HttpEntity entity = response.getEntity();
129 if (entity != null) {
130 InputStream content = entity.getContent();
131 if (content != null) {
132 Bitmap icon = BitmapFactory.decodeStream(
133 content, null, null);
Jeff Hamilton1a805652010-09-07 12:36:30 -0700134 if (inDatabase) {
Ben Murdoche322f562010-07-01 12:21:19 +0100135 storeIcon(icon);
136 } else if (mMessage != null) {
137 Bundle b = mMessage.getData();
138 b.putParcelable("touchIcon", icon);
139 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400140 }
Patrick Scott3918d442009-08-04 13:22:29 -0400141 }
142 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400143 } catch (IllegalArgumentException ex) {
144 request.abort();
145 } catch (IOException ex) {
146 request.abort();
147 } finally {
148 client.close();
Patrick Scott3918d442009-08-04 13:22:29 -0400149 }
Patrick Scott3918d442009-08-04 13:22:29 -0400150 }
Ben Murdoche322f562010-07-01 12:21:19 +0100151
Patrick Scott8e9fe322010-03-04 14:29:31 -0500152 if (mCursor != null) {
153 mCursor.close();
154 }
Ben Murdoche322f562010-07-01 12:21:19 +0100155
156 if (mMessage != null) {
157 mMessage.sendToTarget();
158 }
159
Patrick Scott3918d442009-08-04 13:22:29 -0400160 return null;
161 }
162
163 @Override
Patrick Scott59ce8302009-09-18 16:29:38 -0400164 protected void onCancelled() {
165 if (mCursor != null) {
166 mCursor.close();
167 }
168 }
169
Patrick Scotta74f6962009-11-10 13:06:47 -0500170 private void storeIcon(Bitmap icon) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400171 // Do this first in case the download failed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700172 if (mTab != null) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400173 // Remove the touch icon loader from the BrowserActivity.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700174 mTab.mTouchIconLoader = null;
Patrick Scott59ce8302009-09-18 16:29:38 -0400175 }
176
177 if (icon == null || mCursor == null || isCancelled()) {
Patrick Scott3918d442009-08-04 13:22:29 -0400178 return;
179 }
Patrick Scott59ce8302009-09-18 16:29:38 -0400180
Patrick Scott3918d442009-08-04 13:22:29 -0400181 if (mCursor.moveToFirst()) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700182 final ByteArrayOutputStream os = new ByteArrayOutputStream();
183 icon.compress(Bitmap.CompressFormat.PNG, 100, os);
184
185 ContentValues values = new ContentValues();
186 values.put(Images.TOUCH_ICON, os.toByteArray());
187 values.put(Images.URL, mCursor.getString(0));
188
Patrick Scott3918d442009-08-04 13:22:29 -0400189 do {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700190 mContentResolver.update(Images.CONTENT_URI, values, null, null);
Patrick Scott3918d442009-08-04 13:22:29 -0400191 } while (mCursor.moveToNext());
192 }
Patrick Scott3918d442009-08-04 13:22:29 -0400193 }
194}