blob: 992284f21de438815867bb15b30d8e943499efab [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;
Jeff Hamilton1a805652010-09-07 12:36:30 -070037import android.provider.BrowserContract.Images;
Patrick Scott3918d442009-08-04 13:22:29 -040038import android.webkit.WebView;
39
Patrick Scott3918d442009-08-04 13:22:29 -040040import java.io.ByteArrayOutputStream;
41import java.io.IOException;
42import java.io.InputStream;
43
Patrick Scotta74f6962009-11-10 13:06:47 -050044class DownloadTouchIcon extends AsyncTask<String, Void, Void> {
Patrick Scott3918d442009-08-04 13:22:29 -040045 private final ContentResolver mContentResolver;
Leon Scrogginsc8393d92010-04-23 14:58:16 -040046 private Cursor mCursor;
Patrick Scott3918d442009-08-04 13:22:29 -040047 private final String mOriginalUrl;
48 private final String mUrl;
Ben Murdoche322f562010-07-01 12:21:19 +010049 private final String mUserAgent; // Sites may serve a different icon to different UAs
50 private Message mMessage;
51
Henrik Baard980e9952010-09-06 18:13:23 +020052 private final Context mContext;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053 /* package */ Tab mTab;
Patrick Scott3918d442009-08-04 13:22:29 -040054
Ben Murdoche322f562010-07-01 12:21:19 +010055 /**
56 * Use this ctor to store the touch icon in the bookmarks database for
57 * the originalUrl so we take account of redirects. Used when the user
58 * bookmarks a page from outside the bookmarks activity.
59 */
Henrik Baard980e9952010-09-06 18:13:23 +020060 public DownloadTouchIcon(Tab tab, Context ctx, ContentResolver cr, WebView view) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070061 mTab = tab;
Henrik Baard980e9952010-09-06 18:13:23 +020062 mContext = ctx;
Patrick Scott3918d442009-08-04 13:22:29 -040063 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040064 // Store these in case they change.
65 mOriginalUrl = view.getOriginalUrl();
66 mUrl = view.getUrl();
67 mUserAgent = view.getSettings().getUserAgentString();
68 }
69
Ben Murdoche322f562010-07-01 12:21:19 +010070 /**
71 * Use this ctor to download the touch icon and update the bookmarks database
72 * entry for the given url. Used when the user creates a bookmark from
73 * within the bookmarks activity and there haven't been any redirects.
74 * TODO: Would be nice to set the user agent here so that there is no
75 * potential for the three different ctors here to return different icons.
76 */
Henrik Baard980e9952010-09-06 18:13:23 +020077 public DownloadTouchIcon(Context ctx, ContentResolver cr, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070078 mTab = null;
Henrik Baard980e9952010-09-06 18:13:23 +020079 mContext = ctx;
Patrick Scott3918d442009-08-04 13:22:29 -040080 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040081 mOriginalUrl = null;
82 mUrl = url;
83 mUserAgent = null;
84 }
85
Ben Murdoche322f562010-07-01 12:21:19 +010086 /**
87 * Use this ctor to not store the touch icon in a database, rather add it to
88 * the passed Message's data bundle with the key "touchIcon" and then send
89 * the message.
90 */
Bjorn Bringert67832032010-10-12 08:24:17 +010091 public DownloadTouchIcon(Context context, Message msg, String userAgent) {
Ben Murdoche322f562010-07-01 12:21:19 +010092 mMessage = msg;
Bjorn Bringert67832032010-10-12 08:24:17 +010093 mContext = context;
Ben Murdoche322f562010-07-01 12:21:19 +010094 mContentResolver = null;
95 mOriginalUrl = null;
96 mUrl = null;
97 mUserAgent = userAgent;
98 }
99
Patrick Scott3918d442009-08-04 13:22:29 -0400100 @Override
Patrick Scotta74f6962009-11-10 13:06:47 -0500101 public Void doInBackground(String... values) {
Ben Murdoche322f562010-07-01 12:21:19 +0100102 if (mContentResolver != null) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700103 mCursor = Bookmarks.queryCombinedForUrl(mContentResolver,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500104 mOriginalUrl, mUrl);
Ben Murdoche322f562010-07-01 12:21:19 +0100105 }
Patrick Scott3918d442009-08-04 13:22:29 -0400106
Jeff Hamilton1a805652010-09-07 12:36:30 -0700107 boolean inDatabase = mCursor != null && mCursor.getCount() > 0;
Ben Murdoche322f562010-07-01 12:21:19 +0100108
109 String url = values[0];
110
Jeff Hamilton1a805652010-09-07 12:36:30 -0700111 if (inDatabase || mMessage != null) {
Ben Murdoche322f562010-07-01 12:21:19 +0100112 AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent);
Henrik Baard980e9952010-09-06 18:13:23 +0200113 HttpHost httpHost = Proxy.getPreferredHttpHost(mContext, url);
Andreas Sandbladd159ec52010-06-16 13:10:39 +0200114 if (httpHost != null) {
115 ConnRouteParams.setDefaultProxy(client.getParams(), httpHost);
116 }
117
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400118 HttpGet request = new HttpGet(url);
Patrick Scott3918d442009-08-04 13:22:29 -0400119
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400120 // Follow redirects
121 HttpClientParams.setRedirecting(client.getParams(), true);
Patrick Scott3918d442009-08-04 13:22:29 -0400122
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400123 try {
124 HttpResponse response = client.execute(request);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400125 if (response.getStatusLine().getStatusCode() == 200) {
126 HttpEntity entity = response.getEntity();
127 if (entity != null) {
128 InputStream content = entity.getContent();
129 if (content != null) {
130 Bitmap icon = BitmapFactory.decodeStream(
131 content, null, null);
Jeff Hamilton1a805652010-09-07 12:36:30 -0700132 if (inDatabase) {
Ben Murdoche322f562010-07-01 12:21:19 +0100133 storeIcon(icon);
134 } else if (mMessage != null) {
135 Bundle b = mMessage.getData();
136 b.putParcelable("touchIcon", icon);
137 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400138 }
Patrick Scott3918d442009-08-04 13:22:29 -0400139 }
140 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400141 } catch (IllegalArgumentException ex) {
142 request.abort();
143 } catch (IOException ex) {
144 request.abort();
145 } finally {
146 client.close();
Patrick Scott3918d442009-08-04 13:22:29 -0400147 }
Patrick Scott3918d442009-08-04 13:22:29 -0400148 }
Ben Murdoche322f562010-07-01 12:21:19 +0100149
Patrick Scott8e9fe322010-03-04 14:29:31 -0500150 if (mCursor != null) {
151 mCursor.close();
152 }
Ben Murdoche322f562010-07-01 12:21:19 +0100153
154 if (mMessage != null) {
155 mMessage.sendToTarget();
156 }
157
Patrick Scott3918d442009-08-04 13:22:29 -0400158 return null;
159 }
160
161 @Override
Patrick Scott59ce8302009-09-18 16:29:38 -0400162 protected void onCancelled() {
163 if (mCursor != null) {
164 mCursor.close();
165 }
166 }
167
Patrick Scotta74f6962009-11-10 13:06:47 -0500168 private void storeIcon(Bitmap icon) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400169 // Do this first in case the download failed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700170 if (mTab != null) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400171 // Remove the touch icon loader from the BrowserActivity.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700172 mTab.mTouchIconLoader = null;
Patrick Scott59ce8302009-09-18 16:29:38 -0400173 }
174
175 if (icon == null || mCursor == null || isCancelled()) {
Patrick Scott3918d442009-08-04 13:22:29 -0400176 return;
177 }
Patrick Scott59ce8302009-09-18 16:29:38 -0400178
Patrick Scott3918d442009-08-04 13:22:29 -0400179 if (mCursor.moveToFirst()) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700180 final ByteArrayOutputStream os = new ByteArrayOutputStream();
181 icon.compress(Bitmap.CompressFormat.PNG, 100, os);
182
183 ContentValues values = new ContentValues();
184 values.put(Images.TOUCH_ICON, os.toByteArray());
185 values.put(Images.URL, mCursor.getString(0));
186
Patrick Scott3918d442009-08-04 13:22:29 -0400187 do {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700188 mContentResolver.update(Images.CONTENT_URI, values, null, null);
Patrick Scott3918d442009-08-04 13:22:29 -0400189 } while (mCursor.moveToNext());
190 }
Patrick Scott3918d442009-08-04 13:22:29 -0400191 }
192}