blob: e8a912cd82cec4d6781b0c85bcdef1caaf954e79 [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;
Henrik Baard980e9952010-09-06 18:13:23 +020022import android.content.Context;
Patrick Scott3918d442009-08-04 13:22:29 -040023import android.database.Cursor;
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
Dianne Hackborn385effd2010-02-24 20:03:04 -080026import android.net.http.AndroidHttpClient;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020027import android.net.Proxy;
Patrick Scott3918d442009-08-04 13:22:29 -040028import android.os.AsyncTask;
29import android.provider.Browser;
30import android.webkit.WebView;
31
Paul Westbrookc6343ad2009-12-11 15:35:38 -080032
Patrick Scott3918d442009-08-04 13:22:29 -040033import org.apache.http.HttpEntity;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020034import org.apache.http.HttpHost;
Patrick Scott3918d442009-08-04 13:22:29 -040035import org.apache.http.HttpResponse;
36import org.apache.http.client.methods.HttpGet;
37import org.apache.http.client.params.HttpClientParams;
Andreas Sandbladd159ec52010-06-16 13:10:39 +020038import org.apache.http.conn.params.ConnRouteParams;
Patrick Scott3918d442009-08-04 13:22:29 -040039
40import 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;
49 private final String mUserAgent;
Henrik Baard980e9952010-09-06 18:13:23 +020050 private final Context mContext;
Grace Kloba22ac16e2009-10-07 18:00:23 -070051 /* package */ Tab mTab;
Patrick Scott3918d442009-08-04 13:22:29 -040052
Henrik Baard980e9952010-09-06 18:13:23 +020053 public DownloadTouchIcon(Tab tab, Context ctx, ContentResolver cr, WebView view) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070054 mTab = tab;
Henrik Baard980e9952010-09-06 18:13:23 +020055 mContext = ctx;
Patrick Scott3918d442009-08-04 13:22:29 -040056 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040057 // Store these in case they change.
58 mOriginalUrl = view.getOriginalUrl();
59 mUrl = view.getUrl();
60 mUserAgent = view.getSettings().getUserAgentString();
61 }
62
Henrik Baard980e9952010-09-06 18:13:23 +020063 public DownloadTouchIcon(Context ctx, ContentResolver cr, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070064 mTab = null;
Henrik Baard980e9952010-09-06 18:13:23 +020065 mContext = ctx;
Patrick Scott3918d442009-08-04 13:22:29 -040066 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040067 mOriginalUrl = null;
68 mUrl = url;
69 mUserAgent = null;
70 }
71
72 @Override
Patrick Scotta74f6962009-11-10 13:06:47 -050073 public Void doInBackground(String... values) {
Leon Scrogginsc8393d92010-04-23 14:58:16 -040074 mCursor = BrowserBookmarksAdapter.queryBookmarksForUrl(mContentResolver,
75 mOriginalUrl, mUrl, true);
76 if (mCursor != null && mCursor.getCount() > 0) {
77 String url = values[0];
Patrick Scott3918d442009-08-04 13:22:29 -040078
Leon Scrogginsc8393d92010-04-23 14:58:16 -040079 AndroidHttpClient client = AndroidHttpClient.newInstance(
80 mUserAgent);
Henrik Baard980e9952010-09-06 18:13:23 +020081 HttpHost httpHost = Proxy.getPreferredHttpHost(mContext, url);
Andreas Sandbladd159ec52010-06-16 13:10:39 +020082 if (httpHost != null) {
83 ConnRouteParams.setDefaultProxy(client.getParams(), httpHost);
84 }
85
Leon Scrogginsc8393d92010-04-23 14:58:16 -040086 HttpGet request = new HttpGet(url);
Patrick Scott3918d442009-08-04 13:22:29 -040087
Leon Scrogginsc8393d92010-04-23 14:58:16 -040088 // Follow redirects
89 HttpClientParams.setRedirecting(client.getParams(), true);
Patrick Scott3918d442009-08-04 13:22:29 -040090
Leon Scrogginsc8393d92010-04-23 14:58:16 -040091 try {
92 HttpResponse response = client.execute(request);
Patrick Scott3918d442009-08-04 13:22:29 -040093
Leon Scrogginsc8393d92010-04-23 14:58:16 -040094 if (response.getStatusLine().getStatusCode() == 200) {
95 HttpEntity entity = response.getEntity();
96 if (entity != null) {
97 InputStream content = entity.getContent();
98 if (content != null) {
99 Bitmap icon = BitmapFactory.decodeStream(
100 content, null, null);
101 storeIcon(icon);
102 }
Patrick Scott3918d442009-08-04 13:22:29 -0400103 }
104 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400105 } catch (IllegalArgumentException ex) {
106 request.abort();
107 } catch (IOException ex) {
108 request.abort();
109 } finally {
110 client.close();
Patrick Scott3918d442009-08-04 13:22:29 -0400111 }
Patrick Scott3918d442009-08-04 13:22:29 -0400112 }
Patrick Scott8e9fe322010-03-04 14:29:31 -0500113 if (mCursor != null) {
114 mCursor.close();
115 }
Patrick Scott3918d442009-08-04 13:22:29 -0400116 return null;
117 }
118
119 @Override
Patrick Scott59ce8302009-09-18 16:29:38 -0400120 protected void onCancelled() {
121 if (mCursor != null) {
122 mCursor.close();
123 }
124 }
125
Patrick Scotta74f6962009-11-10 13:06:47 -0500126 private void storeIcon(Bitmap icon) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400127 // Do this first in case the download failed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700128 if (mTab != null) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400129 // Remove the touch icon loader from the BrowserActivity.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700130 mTab.mTouchIconLoader = null;
Patrick Scott59ce8302009-09-18 16:29:38 -0400131 }
132
133 if (icon == null || mCursor == null || isCancelled()) {
Patrick Scott3918d442009-08-04 13:22:29 -0400134 return;
135 }
Patrick Scott59ce8302009-09-18 16:29:38 -0400136
Patrick Scott3918d442009-08-04 13:22:29 -0400137 final ByteArrayOutputStream os = new ByteArrayOutputStream();
138 icon.compress(Bitmap.CompressFormat.PNG, 100, os);
139 ContentValues values = new ContentValues();
140 values.put(Browser.BookmarkColumns.TOUCH_ICON,
141 os.toByteArray());
142
143 if (mCursor.moveToFirst()) {
144 do {
145 mContentResolver.update(ContentUris.withAppendedId(
146 Browser.BOOKMARKS_URI, mCursor.getInt(0)),
147 values, null, null);
148 } while (mCursor.moveToNext());
149 }
Patrick Scott3918d442009-08-04 13:22:29 -0400150 }
151}