blob: 6662e09051fd27a6fe2a7e377093440b5238af8d [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;
25import android.net.http.AndroidHttpClient;
26import android.os.AsyncTask;
27import android.provider.Browser;
28import android.webkit.WebView;
29
30import org.apache.http.HttpEntity;
31import org.apache.http.HttpResponse;
32import org.apache.http.client.methods.HttpGet;
33import org.apache.http.client.params.HttpClientParams;
34
35import java.io.ByteArrayOutputStream;
36import java.io.IOException;
37import java.io.InputStream;
38
39class DownloadTouchIcon extends AsyncTask<String, Void, Bitmap> {
40 private final ContentResolver mContentResolver;
41 private final Cursor mCursor;
42 private final String mOriginalUrl;
43 private final String mUrl;
44 private final String mUserAgent;
45
46 public DownloadTouchIcon(ContentResolver cr, Cursor c, WebView view) {
47 mContentResolver = cr;
48 mCursor = c;
49 // Store these in case they change.
50 mOriginalUrl = view.getOriginalUrl();
51 mUrl = view.getUrl();
52 mUserAgent = view.getSettings().getUserAgentString();
53 }
54
55 public DownloadTouchIcon(ContentResolver cr, Cursor c, String url) {
56 mContentResolver = cr;
57 mCursor = c;
58 mOriginalUrl = null;
59 mUrl = url;
60 mUserAgent = null;
61 }
62
63 @Override
64 public Bitmap doInBackground(String... values) {
65 String url = values[0];
66
67 AndroidHttpClient client = AndroidHttpClient.newInstance(
68 mUserAgent);
69 HttpGet request = new HttpGet(url);
70
71 // Follow redirects
72 HttpClientParams.setRedirecting(client.getParams(), true);
73
74 try {
75 HttpResponse response = client.execute(request);
76
77 if (response.getStatusLine().getStatusCode() == 200) {
78 HttpEntity entity = response.getEntity();
79 if (entity != null) {
80 InputStream content = entity.getContent();
81 if (content != null) {
82 Bitmap icon = BitmapFactory.decodeStream(
83 content, null, null);
84 return icon;
85 }
86 }
87 }
88 } catch (IllegalArgumentException ex) {
89 request.abort();
90 } catch (IOException ex) {
91 request.abort();
92 } finally {
93 client.close();
94 }
95 return null;
96 }
97
98 @Override
99 public void onPostExecute(Bitmap icon) {
100 if (icon == null || mCursor == null) {
101 return;
102 }
103 final ByteArrayOutputStream os = new ByteArrayOutputStream();
104 icon.compress(Bitmap.CompressFormat.PNG, 100, os);
105 ContentValues values = new ContentValues();
106 values.put(Browser.BookmarkColumns.TOUCH_ICON,
107 os.toByteArray());
108
109 if (mCursor.moveToFirst()) {
110 do {
111 mContentResolver.update(ContentUris.withAppendedId(
112 Browser.BOOKMARKS_URI, mCursor.getInt(0)),
113 values, null, null);
114 } while (mCursor.moveToNext());
115 }
116 mCursor.close();
117 }
118}