PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/WebVox/src/com/marvin/webvox/DownloadTouchIcon.java

http://eyes-free.googlecode.com/
Java | 138 lines | 72 code | 13 blank | 53 comment | 11 complexity | 6a35c69f275eb634c1779b1a8efe7e20 MD5 | raw file
  1/*
  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.marvin.webvox;
 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;
 25//import 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    /* package */ BrowserActivity mActivity;
 46
 47    public DownloadTouchIcon(BrowserActivity activity, ContentResolver cr,
 48            Cursor c, WebView view) {
 49        mActivity = activity;
 50        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) {
 59        mActivity = null;
 60        mContentResolver = cr;
 61        mCursor = c;
 62        mOriginalUrl = null;
 63        mUrl = url;
 64        mUserAgent = null;
 65    }
 66
 67    @Override
 68    public Bitmap doInBackground(String... values) {
 69/*        
 70        String url = values[0];
 71
 72        AndroidHttpClient client = AndroidHttpClient.newInstance(
 73                mUserAgent);
 74        HttpGet request = new HttpGet(url);
 75
 76        // Follow redirects
 77        HttpClientParams.setRedirecting(client.getParams(), true);
 78
 79        try {
 80            HttpResponse response = client.execute(request);
 81
 82            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                        return icon;
 90                    }
 91                }
 92            }
 93        } catch (IllegalArgumentException ex) {
 94            request.abort();
 95        } catch (IOException ex) {
 96            request.abort();
 97        } finally {
 98            client.close();
 99        }
100*/        
101        return null;
102    }
103
104    @Override
105    protected void onCancelled() {
106        if (mCursor != null) {
107            mCursor.close();
108        }
109    }
110
111    @Override
112    public void onPostExecute(Bitmap icon) {
113        // Do this first in case the download failed.
114        if (mActivity != null) {
115            // Remove the touch icon loader from the BrowserActivity.
116            mActivity.mTouchIconLoader = null;
117        }
118
119        if (icon == null || mCursor == null || isCancelled()) {
120            return;
121        }
122
123        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        }
136        mCursor.close();
137    }
138}