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