blob: 14404ff06ddb53fc065ba8b29b8c1e0e44183b55 [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
19import android.content.ContentResolver;
20import android.content.ContentUris;
21import android.content.ContentValues;
22import android.database.Cursor;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
Dianne Hackborn385effd2010-02-24 20:03:04 -080025import android.net.http.AndroidHttpClient;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020026import android.net.Proxy;
Patrick Scott3918d442009-08-04 13:22:29 -040027import android.os.AsyncTask;
28import android.provider.Browser;
29import android.webkit.WebView;
30
Paul Westbrookc6343ad2009-12-11 15:35:38 -080031
Patrick Scott3918d442009-08-04 13:22:29 -040032import org.apache.http.HttpEntity;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020033import org.apache.http.HttpHost;
Patrick Scott3918d442009-08-04 13:22:29 -040034import org.apache.http.HttpResponse;
35import org.apache.http.client.methods.HttpGet;
36import org.apache.http.client.params.HttpClientParams;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020037import org.apache.http.conn.params.ConnRouteParams;
Patrick Scott3918d442009-08-04 13:22:29 -040038
39import java.io.ByteArrayOutputStream;
40import java.io.IOException;
41import java.io.InputStream;
42
Patrick Scotta74f6962009-11-10 13:06:47 -050043class DownloadTouchIcon extends AsyncTask<String, Void, Void> {
Patrick Scott3918d442009-08-04 13:22:29 -040044 private final ContentResolver mContentResolver;
Leon Scrogginsc8393d92010-04-23 14:58:16 -040045 private Cursor mCursor;
Patrick Scott3918d442009-08-04 13:22:29 -040046 private final String mOriginalUrl;
47 private final String mUrl;
48 private final String mUserAgent;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020049 private final BrowserActivity mActivity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070050 /* package */ Tab mTab;
Patrick Scott3918d442009-08-04 13:22:29 -040051
Andreas Sandbladd159ec52010-06-16 13:10:39 +020052 public DownloadTouchIcon(Tab tab, BrowserActivity activity, ContentResolver cr, WebView view) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070053 mTab = tab;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020054 mActivity = activity;
Patrick Scott3918d442009-08-04 13:22:29 -040055 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040056 // Store these in case they change.
57 mOriginalUrl = view.getOriginalUrl();
58 mUrl = view.getUrl();
59 mUserAgent = view.getSettings().getUserAgentString();
60 }
61
Leon Scrogginsc8393d92010-04-23 14:58:16 -040062 public DownloadTouchIcon(ContentResolver cr, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070063 mTab = null;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020064 mActivity = null;
Patrick Scott3918d442009-08-04 13:22:29 -040065 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040066 mOriginalUrl = null;
67 mUrl = url;
68 mUserAgent = null;
69 }
70
71 @Override
Patrick Scotta74f6962009-11-10 13:06:47 -050072 public Void doInBackground(String... values) {
Leon Scrogginsc8393d92010-04-23 14:58:16 -040073 mCursor = BrowserBookmarksAdapter.queryBookmarksForUrl(mContentResolver,
74 mOriginalUrl, mUrl, true);
75 if (mCursor != null && mCursor.getCount() > 0) {
76 String url = values[0];
Patrick Scott3918d442009-08-04 13:22:29 -040077
Leon Scrogginsc8393d92010-04-23 14:58:16 -040078 AndroidHttpClient client = AndroidHttpClient.newInstance(
79 mUserAgent);
Andreas Sandbladd159ec52010-06-16 13:10:39 +020080 HttpHost httpHost = Proxy.getPreferredHttpHost(mActivity, url);
81 if (httpHost != null) {
82 ConnRouteParams.setDefaultProxy(client.getParams(), httpHost);
83 }
84
Leon Scrogginsc8393d92010-04-23 14:58:16 -040085 HttpGet request = new HttpGet(url);
Patrick Scott3918d442009-08-04 13:22:29 -040086
Leon Scrogginsc8393d92010-04-23 14:58:16 -040087 // Follow redirects
88 HttpClientParams.setRedirecting(client.getParams(), true);
Patrick Scott3918d442009-08-04 13:22:29 -040089
Leon Scrogginsc8393d92010-04-23 14:58:16 -040090 try {
91 HttpResponse response = client.execute(request);
Patrick Scott3918d442009-08-04 13:22:29 -040092
Leon Scrogginsc8393d92010-04-23 14:58:16 -040093 if (response.getStatusLine().getStatusCode() == 200) {
94 HttpEntity entity = response.getEntity();
95 if (entity != null) {
96 InputStream content = entity.getContent();
97 if (content != null) {
98 Bitmap icon = BitmapFactory.decodeStream(
99 content, null, null);
100 storeIcon(icon);
101 }
Patrick Scott3918d442009-08-04 13:22:29 -0400102 }
103 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400104 } catch (IllegalArgumentException ex) {
105 request.abort();
106 } catch (IOException ex) {
107 request.abort();
108 } finally {
109 client.close();
Patrick Scott3918d442009-08-04 13:22:29 -0400110 }
Patrick Scott3918d442009-08-04 13:22:29 -0400111 }
Patrick Scott8e9fe322010-03-04 14:29:31 -0500112 if (mCursor != null) {
113 mCursor.close();
114 }
Patrick Scott3918d442009-08-04 13:22:29 -0400115 return null;
116 }
117
118 @Override
Patrick Scott59ce8302009-09-18 16:29:38 -0400119 protected void onCancelled() {
120 if (mCursor != null) {
121 mCursor.close();
122 }
123 }
124
Patrick Scotta74f6962009-11-10 13:06:47 -0500125 private void storeIcon(Bitmap icon) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400126 // Do this first in case the download failed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700127 if (mTab != null) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400128 // Remove the touch icon loader from the BrowserActivity.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700129 mTab.mTouchIconLoader = null;
Patrick Scott59ce8302009-09-18 16:29:38 -0400130 }
131
132 if (icon == null || mCursor == null || isCancelled()) {
Patrick Scott3918d442009-08-04 13:22:29 -0400133 return;
134 }
Patrick Scott59ce8302009-09-18 16:29:38 -0400135
Patrick Scott3918d442009-08-04 13:22:29 -0400136 final ByteArrayOutputStream os = new ByteArrayOutputStream();
137 icon.compress(Bitmap.CompressFormat.PNG, 100, os);
138 ContentValues values = new ContentValues();
139 values.put(Browser.BookmarkColumns.TOUCH_ICON,
140 os.toByteArray());
141
142 if (mCursor.moveToFirst()) {
143 do {
144 mContentResolver.update(ContentUris.withAppendedId(
145 Browser.BOOKMARKS_URI, mCursor.getInt(0)),
146 values, null, null);
147 } while (mCursor.moveToNext());
148 }
Patrick Scott3918d442009-08-04 13:22:29 -0400149 }
150}