blob: fe139e89f8d0b66f65da8bb2fe01153034ced41d [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Patrick Scott3918d442009-08-04 13:22:29 -040018
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080037
Bijan Amirzada41242f22014-03-21 12:12:18 -070038import com.android.browser.platformsupport.BrowserContract;
39import com.android.browser.platformsupport.BrowserContract.Images;
40import com.android.browser.reflect.ReflectHelper;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080041
42import org.codeaurora.swe.WebView;
Patrick Scott3918d442009-08-04 13:22:29 -040043
Patrick Scott3918d442009-08-04 13:22:29 -040044import java.io.ByteArrayOutputStream;
Patrick Scott3918d442009-08-04 13:22:29 -040045import java.io.InputStream;
46
Patrick Scotta74f6962009-11-10 13:06:47 -050047class DownloadTouchIcon extends AsyncTask<String, Void, Void> {
John Reckbf6b54d2011-02-17 16:15:44 -080048
Patrick Scott3918d442009-08-04 13:22:29 -040049 private final ContentResolver mContentResolver;
Leon Scrogginsc8393d92010-04-23 14:58:16 -040050 private Cursor mCursor;
Patrick Scott3918d442009-08-04 13:22:29 -040051 private final String mOriginalUrl;
52 private final String mUrl;
Ben Murdoche322f562010-07-01 12:21:19 +010053 private final String mUserAgent; // Sites may serve a different icon to different UAs
54 private Message mMessage;
55
Henrik Baard980e9952010-09-06 18:13:23 +020056 private final Context mContext;
Grace Kloba22ac16e2009-10-07 18:00:23 -070057 /* package */ Tab mTab;
Patrick Scott3918d442009-08-04 13:22:29 -040058
Ben Murdoche322f562010-07-01 12:21:19 +010059 /**
60 * Use this ctor to store the touch icon in the bookmarks database for
61 * the originalUrl so we take account of redirects. Used when the user
62 * bookmarks a page from outside the bookmarks activity.
63 */
Henrik Baard980e9952010-09-06 18:13:23 +020064 public DownloadTouchIcon(Tab tab, Context ctx, ContentResolver cr, WebView view) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070065 mTab = tab;
Ben Murdoch914c5592011-08-01 13:58:47 +010066 mContext = ctx.getApplicationContext();
Patrick Scott3918d442009-08-04 13:22:29 -040067 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040068 // Store these in case they change.
69 mOriginalUrl = view.getOriginalUrl();
70 mUrl = view.getUrl();
71 mUserAgent = view.getSettings().getUserAgentString();
72 }
73
Ben Murdoche322f562010-07-01 12:21:19 +010074 /**
75 * Use this ctor to download the touch icon and update the bookmarks database
76 * entry for the given url. Used when the user creates a bookmark from
77 * within the bookmarks activity and there haven't been any redirects.
78 * TODO: Would be nice to set the user agent here so that there is no
79 * potential for the three different ctors here to return different icons.
80 */
Henrik Baard980e9952010-09-06 18:13:23 +020081 public DownloadTouchIcon(Context ctx, ContentResolver cr, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070082 mTab = null;
Ben Murdoch914c5592011-08-01 13:58:47 +010083 mContext = ctx.getApplicationContext();
Patrick Scott3918d442009-08-04 13:22:29 -040084 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040085 mOriginalUrl = null;
86 mUrl = url;
87 mUserAgent = null;
88 }
89
Ben Murdoche322f562010-07-01 12:21:19 +010090 /**
91 * Use this ctor to not store the touch icon in a database, rather add it to
Leon Scrogginsbc922852010-10-22 12:15:27 -040092 * the passed Message's data bundle with the key
93 * {@link BrowserContract.Bookmarks#TOUCH_ICON} and then send the message.
Ben Murdoche322f562010-07-01 12:21:19 +010094 */
Bjorn Bringert67832032010-10-12 08:24:17 +010095 public DownloadTouchIcon(Context context, Message msg, String userAgent) {
Ben Murdoche322f562010-07-01 12:21:19 +010096 mMessage = msg;
Ben Murdoch914c5592011-08-01 13:58:47 +010097 mContext = context.getApplicationContext();
Ben Murdoche322f562010-07-01 12:21:19 +010098 mContentResolver = null;
99 mOriginalUrl = null;
100 mUrl = null;
101 mUserAgent = userAgent;
102 }
103
Patrick Scott3918d442009-08-04 13:22:29 -0400104 @Override
Patrick Scotta74f6962009-11-10 13:06:47 -0500105 public Void doInBackground(String... values) {
Ben Murdoche322f562010-07-01 12:21:19 +0100106 if (mContentResolver != null) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700107 mCursor = Bookmarks.queryCombinedForUrl(mContentResolver,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500108 mOriginalUrl, mUrl);
Ben Murdoche322f562010-07-01 12:21:19 +0100109 }
Patrick Scott3918d442009-08-04 13:22:29 -0400110
Jeff Hamilton1a805652010-09-07 12:36:30 -0700111 boolean inDatabase = mCursor != null && mCursor.getCount() > 0;
Ben Murdoche322f562010-07-01 12:21:19 +0100112
113 String url = values[0];
114
Jeff Hamilton1a805652010-09-07 12:36:30 -0700115 if (inDatabase || mMessage != null) {
John Reckbf6b54d2011-02-17 16:15:44 -0800116 AndroidHttpClient client = null;
117 HttpGet request = null;
Patrick Scott3918d442009-08-04 13:22:29 -0400118
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400119 try {
John Reckbf6b54d2011-02-17 16:15:44 -0800120 client = AndroidHttpClient.newInstance(mUserAgent);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800121 //HttpHost httpHost = Proxy.getPreferredHttpHost(mContext, url);
122 Object[] params = { mContext, url};
123 Class[] type = new Class[] {android.content.Context.class, String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -0700124 HttpHost httpHost = (HttpHost) ReflectHelper.invokeMethod(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800125 "android.net.Proxy", "getPreferredHttpHost",
126 type, params);
John Reckbf6b54d2011-02-17 16:15:44 -0800127 if (httpHost != null) {
128 ConnRouteParams.setDefaultProxy(client.getParams(), httpHost);
129 }
130
131 request = new HttpGet(url);
132
133 // Follow redirects
134 HttpClientParams.setRedirecting(client.getParams(), true);
135
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400136 HttpResponse response = client.execute(request);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400137 if (response.getStatusLine().getStatusCode() == 200) {
138 HttpEntity entity = response.getEntity();
139 if (entity != null) {
140 InputStream content = entity.getContent();
141 if (content != null) {
142 Bitmap icon = BitmapFactory.decodeStream(
143 content, null, null);
Jeff Hamilton1a805652010-09-07 12:36:30 -0700144 if (inDatabase) {
Ben Murdoche322f562010-07-01 12:21:19 +0100145 storeIcon(icon);
146 } else if (mMessage != null) {
147 Bundle b = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400148 b.putParcelable(BrowserContract.Bookmarks.TOUCH_ICON, icon);
Ben Murdoche322f562010-07-01 12:21:19 +0100149 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400150 }
Patrick Scott3918d442009-08-04 13:22:29 -0400151 }
152 }
John Reckbf6b54d2011-02-17 16:15:44 -0800153 } catch (Exception ex) {
154 if (request != null) {
155 request.abort();
156 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400157 } finally {
John Reckbf6b54d2011-02-17 16:15:44 -0800158 if (client != null) {
159 client.close();
160 }
Patrick Scott3918d442009-08-04 13:22:29 -0400161 }
Patrick Scott3918d442009-08-04 13:22:29 -0400162 }
Ben Murdoche322f562010-07-01 12:21:19 +0100163
Patrick Scott8e9fe322010-03-04 14:29:31 -0500164 if (mCursor != null) {
165 mCursor.close();
166 }
Ben Murdoche322f562010-07-01 12:21:19 +0100167
168 if (mMessage != null) {
169 mMessage.sendToTarget();
170 }
171
Patrick Scott3918d442009-08-04 13:22:29 -0400172 return null;
173 }
174
175 @Override
Patrick Scott59ce8302009-09-18 16:29:38 -0400176 protected void onCancelled() {
177 if (mCursor != null) {
178 mCursor.close();
179 }
180 }
181
Patrick Scotta74f6962009-11-10 13:06:47 -0500182 private void storeIcon(Bitmap icon) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400183 // Do this first in case the download failed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700184 if (mTab != null) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400185 // Remove the touch icon loader from the BrowserActivity.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700186 mTab.mTouchIconLoader = null;
Patrick Scott59ce8302009-09-18 16:29:38 -0400187 }
188
189 if (icon == null || mCursor == null || isCancelled()) {
Patrick Scott3918d442009-08-04 13:22:29 -0400190 return;
191 }
Patrick Scott59ce8302009-09-18 16:29:38 -0400192
Patrick Scott3918d442009-08-04 13:22:29 -0400193 if (mCursor.moveToFirst()) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700194 final ByteArrayOutputStream os = new ByteArrayOutputStream();
195 icon.compress(Bitmap.CompressFormat.PNG, 100, os);
196
197 ContentValues values = new ContentValues();
198 values.put(Images.TOUCH_ICON, os.toByteArray());
Jeff Hamilton1a805652010-09-07 12:36:30 -0700199
Patrick Scott3918d442009-08-04 13:22:29 -0400200 do {
John Reck9e7686e2011-02-17 13:25:59 -0800201 values.put(Images.URL, mCursor.getString(0));
Jeff Hamilton1a805652010-09-07 12:36:30 -0700202 mContentResolver.update(Images.CONTENT_URI, values, null, null);
Patrick Scott3918d442009-08-04 13:22:29 -0400203 } while (mCursor.moveToNext());
204 }
Patrick Scott3918d442009-08-04 13:22:29 -0400205 }
206}