blob: b5369aeb3d8adeaefe18d836b67a8803ec5c2b4d [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;
Patrick Scott3918d442009-08-04 13:22:29 -040026import android.os.AsyncTask;
27import android.provider.Browser;
28import android.webkit.WebView;
29
Paul Westbrookc6343ad2009-12-11 15:35:38 -080030
Patrick Scott3918d442009-08-04 13:22:29 -040031import org.apache.http.HttpEntity;
32import org.apache.http.HttpResponse;
33import org.apache.http.client.methods.HttpGet;
34import org.apache.http.client.params.HttpClientParams;
35
36import java.io.ByteArrayOutputStream;
37import java.io.IOException;
38import java.io.InputStream;
39
Patrick Scotta74f6962009-11-10 13:06:47 -050040class DownloadTouchIcon extends AsyncTask<String, Void, Void> {
Patrick Scott3918d442009-08-04 13:22:29 -040041 private final ContentResolver mContentResolver;
Leon Scrogginsc8393d92010-04-23 14:58:16 -040042 private Cursor mCursor;
Patrick Scott3918d442009-08-04 13:22:29 -040043 private final String mOriginalUrl;
44 private final String mUrl;
45 private final String mUserAgent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046 /* package */ Tab mTab;
Patrick Scott3918d442009-08-04 13:22:29 -040047
Leon Scrogginsc8393d92010-04-23 14:58:16 -040048 public DownloadTouchIcon(Tab tab, ContentResolver cr, WebView view) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070049 mTab = tab;
Patrick Scott3918d442009-08-04 13:22:29 -040050 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040051 // Store these in case they change.
52 mOriginalUrl = view.getOriginalUrl();
53 mUrl = view.getUrl();
54 mUserAgent = view.getSettings().getUserAgentString();
55 }
56
Leon Scrogginsc8393d92010-04-23 14:58:16 -040057 public DownloadTouchIcon(ContentResolver cr, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070058 mTab = null;
Patrick Scott3918d442009-08-04 13:22:29 -040059 mContentResolver = cr;
Patrick Scott3918d442009-08-04 13:22:29 -040060 mOriginalUrl = null;
61 mUrl = url;
62 mUserAgent = null;
63 }
64
65 @Override
Patrick Scotta74f6962009-11-10 13:06:47 -050066 public Void doInBackground(String... values) {
Leon Scrogginsc8393d92010-04-23 14:58:16 -040067 mCursor = BrowserBookmarksAdapter.queryBookmarksForUrl(mContentResolver,
68 mOriginalUrl, mUrl, true);
69 if (mCursor != null && mCursor.getCount() > 0) {
70 String url = values[0];
Patrick Scott3918d442009-08-04 13:22:29 -040071
Leon Scrogginsc8393d92010-04-23 14:58:16 -040072 AndroidHttpClient client = AndroidHttpClient.newInstance(
73 mUserAgent);
74 HttpGet request = new HttpGet(url);
Patrick Scott3918d442009-08-04 13:22:29 -040075
Leon Scrogginsc8393d92010-04-23 14:58:16 -040076 // Follow redirects
77 HttpClientParams.setRedirecting(client.getParams(), true);
Patrick Scott3918d442009-08-04 13:22:29 -040078
Leon Scrogginsc8393d92010-04-23 14:58:16 -040079 try {
80 HttpResponse response = client.execute(request);
Patrick Scott3918d442009-08-04 13:22:29 -040081
Leon Scrogginsc8393d92010-04-23 14:58:16 -040082 if (response.getStatusLine().getStatusCode() == 200) {
83 HttpEntity entity = response.getEntity();
84 if (entity != null) {
85 InputStream content = entity.getContent();
86 if (content != null) {
87 Bitmap icon = BitmapFactory.decodeStream(
88 content, null, null);
89 storeIcon(icon);
90 }
Patrick Scott3918d442009-08-04 13:22:29 -040091 }
92 }
Leon Scrogginsc8393d92010-04-23 14:58:16 -040093 } catch (IllegalArgumentException ex) {
94 request.abort();
95 } catch (IOException ex) {
96 request.abort();
97 } finally {
98 client.close();
Patrick Scott3918d442009-08-04 13:22:29 -040099 }
Patrick Scott3918d442009-08-04 13:22:29 -0400100 }
Patrick Scott8e9fe322010-03-04 14:29:31 -0500101 if (mCursor != null) {
102 mCursor.close();
103 }
Patrick Scott3918d442009-08-04 13:22:29 -0400104 return null;
105 }
106
107 @Override
Patrick Scott59ce8302009-09-18 16:29:38 -0400108 protected void onCancelled() {
109 if (mCursor != null) {
110 mCursor.close();
111 }
112 }
113
Patrick Scotta74f6962009-11-10 13:06:47 -0500114 private void storeIcon(Bitmap icon) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400115 // Do this first in case the download failed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700116 if (mTab != null) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400117 // Remove the touch icon loader from the BrowserActivity.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700118 mTab.mTouchIconLoader = null;
Patrick Scott59ce8302009-09-18 16:29:38 -0400119 }
120
121 if (icon == null || mCursor == null || isCancelled()) {
Patrick Scott3918d442009-08-04 13:22:29 -0400122 return;
123 }
Patrick Scott59ce8302009-09-18 16:29:38 -0400124
Patrick Scott3918d442009-08-04 13:22:29 -0400125 final ByteArrayOutputStream os = new ByteArrayOutputStream();
126 icon.compress(Bitmap.CompressFormat.PNG, 100, os);
127 ContentValues values = new ContentValues();
128 values.put(Browser.BookmarkColumns.TOUCH_ICON,
129 os.toByteArray());
130
131 if (mCursor.moveToFirst()) {
132 do {
133 mContentResolver.update(ContentUris.withAppendedId(
134 Browser.BOOKMARKS_URI, mCursor.getInt(0)),
135 values, null, null);
136 } while (mCursor.moveToNext());
137 }
Patrick Scott3918d442009-08-04 13:22:29 -0400138 }
139}