blob: c8701ba53cbbc801f6d29a33dd1ad57235ef785e [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;
42 private final Cursor mCursor;
43 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
Grace Kloba22ac16e2009-10-07 18:00:23 -070048 public DownloadTouchIcon(Tab tab, ContentResolver cr, Cursor c, WebView view) {
49 mTab = tab;
Patrick Scott3918d442009-08-04 13:22:29 -040050 mContentResolver = cr;
51 mCursor = c;
52 // Store these in case they change.
53 mOriginalUrl = view.getOriginalUrl();
54 mUrl = view.getUrl();
55 mUserAgent = view.getSettings().getUserAgentString();
56 }
57
58 public DownloadTouchIcon(ContentResolver cr, Cursor c, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070059 mTab = null;
Patrick Scott3918d442009-08-04 13:22:29 -040060 mContentResolver = cr;
61 mCursor = c;
62 mOriginalUrl = null;
63 mUrl = url;
64 mUserAgent = null;
65 }
66
67 @Override
Patrick Scotta74f6962009-11-10 13:06:47 -050068 public Void doInBackground(String... values) {
Patrick Scott3918d442009-08-04 13:22:29 -040069 String url = values[0];
70
71 AndroidHttpClient client = AndroidHttpClient.newInstance(
72 mUserAgent);
73 HttpGet request = new HttpGet(url);
74
75 // Follow redirects
76 HttpClientParams.setRedirecting(client.getParams(), true);
77
78 try {
79 HttpResponse response = client.execute(request);
80
81 if (response.getStatusLine().getStatusCode() == 200) {
82 HttpEntity entity = response.getEntity();
83 if (entity != null) {
84 InputStream content = entity.getContent();
85 if (content != null) {
86 Bitmap icon = BitmapFactory.decodeStream(
87 content, null, null);
Patrick Scotta74f6962009-11-10 13:06:47 -050088 storeIcon(icon);
Patrick Scott3918d442009-08-04 13:22:29 -040089 }
90 }
91 }
92 } catch (IllegalArgumentException ex) {
93 request.abort();
94 } catch (IOException ex) {
95 request.abort();
96 } finally {
97 client.close();
98 }
Patrick Scott8e9fe322010-03-04 14:29:31 -050099 if (mCursor != null) {
100 mCursor.close();
101 }
Patrick Scott3918d442009-08-04 13:22:29 -0400102 return null;
103 }
104
105 @Override
Patrick Scott59ce8302009-09-18 16:29:38 -0400106 protected void onCancelled() {
107 if (mCursor != null) {
108 mCursor.close();
109 }
110 }
111
Patrick Scotta74f6962009-11-10 13:06:47 -0500112 private void storeIcon(Bitmap icon) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400113 // Do this first in case the download failed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700114 if (mTab != null) {
Patrick Scott59ce8302009-09-18 16:29:38 -0400115 // Remove the touch icon loader from the BrowserActivity.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700116 mTab.mTouchIconLoader = null;
Patrick Scott59ce8302009-09-18 16:29:38 -0400117 }
118
119 if (icon == null || mCursor == null || isCancelled()) {
Patrick Scott3918d442009-08-04 13:22:29 -0400120 return;
121 }
Patrick Scott59ce8302009-09-18 16:29:38 -0400122
Patrick Scott3918d442009-08-04 13:22:29 -0400123 final ByteArrayOutputStream os = new ByteArrayOutputStream();
124 icon.compress(Bitmap.CompressFormat.PNG, 100, os);
125 ContentValues values = new ContentValues();
126 values.put(Browser.BookmarkColumns.TOUCH_ICON,
127 os.toByteArray());
128
129 if (mCursor.moveToFirst()) {
130 do {
131 mContentResolver.update(ContentUris.withAppendedId(
132 Browser.BOOKMARKS_URI, mCursor.getInt(0)),
133 values, null, null);
134 } while (mCursor.moveToNext());
135 }
Patrick Scott3918d442009-08-04 13:22:29 -0400136 }
137}