blob: 14426837b7963d698df07ed34dc4d946dba0b01f [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;
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
Ben Murdochccb5de02010-07-19 18:38:17 +010053 private final Activity mActivity;
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 */
Andreas Sandbladd159ec52010-06-16 13:10:39 +020061 public DownloadTouchIcon(Tab tab, BrowserActivity activity, ContentResolver cr, WebView view) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070062 mTab = tab;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020063 mActivity = activity;
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 */
Ben Murdochccb5de02010-07-19 18:38:17 +010078 public DownloadTouchIcon(AddBookmarkPage activity, ContentResolver cr, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070079 mTab = null;
Ben Murdochccb5de02010-07-19 18:38:17 +010080 mActivity = activity;
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
89 * the passed Message's data bundle with the key "touchIcon" and then send
90 * the message.
91 */
Ben Murdochccb5de02010-07-19 18:38:17 +010092 public DownloadTouchIcon(BrowserActivity activity, Message msg, String userAgent) {
Ben Murdoche322f562010-07-01 12:21:19 +010093 mMessage = msg;
Ben Murdochccb5de02010-07-19 18:38:17 +010094 mActivity = activity;
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 Hamilton84029622010-08-05 14:29:28 -0500104 mCursor = Bookmarks.queryBookmarksForUrl(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
Ben Murdoche322f562010-07-01 12:21:19 +0100108 boolean inBookmarksDatabase = mCursor != null && mCursor.getCount() > 0;
109
110 String url = values[0];
111
112 if (inBookmarksDatabase || mMessage != null) {
113 AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent);
Andreas Sandbladd159ec52010-06-16 13:10:39 +0200114 HttpHost httpHost = Proxy.getPreferredHttpHost(mActivity, url);
115 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);
Ben Murdoche322f562010-07-01 12:21:19 +0100133 if (inBookmarksDatabase) {
134 storeIcon(icon);
135 } else if (mMessage != null) {
136 Bundle b = mMessage.getData();
137 b.putParcelable("touchIcon", icon);
138 }
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 final ByteArrayOutputStream os = new ByteArrayOutputStream();
181 icon.compress(Bitmap.CompressFormat.PNG, 100, os);
182 ContentValues values = new ContentValues();
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500183 values.put(BrowserContract.Bookmarks.TOUCH_ICON,
Patrick Scott3918d442009-08-04 13:22:29 -0400184 os.toByteArray());
185
186 if (mCursor.moveToFirst()) {
187 do {
188 mContentResolver.update(ContentUris.withAppendedId(
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500189 BrowserContract.Bookmarks.CONTENT_URI, mCursor.getLong(0)),
Patrick Scott3918d442009-08-04 13:22:29 -0400190 values, null, null);
191 } while (mCursor.moveToNext());
192 }
Patrick Scott3918d442009-08-04 13:22:29 -0400193 }
194}