diff options
author | John Reck <jreck@google.com> | 2011-11-01 11:23:39 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2011-11-01 11:23:39 -0700 |
commit | 91c61a5b3bd8de65e30a0ead6d1f634a6dc7a79f (patch) | |
tree | 2f0c3fbea7cbc940c264821cab3ca7a18d60f7be /src/com/android | |
parent | 36b2aac54e5a9f90addf8092c7b5358822d2b1c4 (diff) | |
parent | 4eadc34fc528b1b63ac8d87bfd7f06e50b738b6d (diff) | |
download | packages_apps_Browser-91c61a5b3bd8de65e30a0ead6d1f634a6dc7a79f.zip packages_apps_Browser-91c61a5b3bd8de65e30a0ead6d1f634a6dc7a79f.tar.gz packages_apps_Browser-91c61a5b3bd8de65e30a0ead6d1f634a6dc7a79f.tar.bz2 |
Merge "Switch Tab to DataController" into ics-mr1
Diffstat (limited to 'src/com/android')
-rw-r--r-- | src/com/android/browser/DataController.java | 92 | ||||
-rw-r--r-- | src/com/android/browser/Tab.java | 84 |
2 files changed, 95 insertions, 81 deletions
diff --git a/src/com/android/browser/DataController.java b/src/com/android/browser/DataController.java index a56fce8..909c2a3 100644 --- a/src/com/android/browser/DataController.java +++ b/src/com/android/browser/DataController.java @@ -23,12 +23,17 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteException; +import android.graphics.Bitmap; +import android.net.Uri; import android.os.Handler; import android.os.Message; import android.provider.BrowserContract; import android.provider.BrowserContract.History; import android.util.Log; +import com.android.browser.provider.BrowserProvider2.Thumbnails; + +import java.nio.ByteBuffer; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @@ -37,12 +42,16 @@ public class DataController { // Message IDs private static final int HISTORY_UPDATE_VISITED = 100; private static final int HISTORY_UPDATE_TITLE = 101; - public static final int QUERY_URL_IS_BOOKMARK = 200; + private static final int QUERY_URL_IS_BOOKMARK = 200; + private static final int TAB_LOAD_THUMBNAIL = 201; + private static final int TAB_SAVE_THUMBNAIL = 202; + private static final int TAB_DELETE_THUMBNAIL = 203; private static DataController sInstance; private Context mContext; private DataControllerHandler mDataHandler; private Handler mCbHandler; // To respond on the UI thread + private ByteBuffer mBuffer; // to capture thumbnails /* package */ static interface OnQueryUrlIsBookmark { void onQueryUrlIsBookmark(String url, boolean isBookmark); @@ -72,7 +81,6 @@ public class DataController { private DataController(Context c) { mContext = c.getApplicationContext(); mDataHandler = new DataControllerHandler(); - mDataHandler.setDaemon(true); mDataHandler.start(); mCbHandler = new Handler() { @Override @@ -108,15 +116,31 @@ public class DataController { mDataHandler.sendMessage(QUERY_URL_IS_BOOKMARK, url.trim(), replyTo); } + public void loadThumbnail(Tab tab) { + mDataHandler.sendMessage(TAB_LOAD_THUMBNAIL, tab); + } + + public void deleteThumbnail(Tab tab) { + mDataHandler.sendMessage(TAB_DELETE_THUMBNAIL, tab.getId()); + } + + public void saveThumbnail(Tab tab) { + mDataHandler.sendMessage(TAB_SAVE_THUMBNAIL, tab); + } + // The standard Handler and Message classes don't allow the queue manipulation // we want (such as peeking). So we use our own queue. class DataControllerHandler extends Thread { private BlockingQueue<DCMessage> mMessageQueue = new LinkedBlockingQueue<DCMessage>(); + public DataControllerHandler() { + super("DataControllerHandler"); + } + @Override public void run() { - super.run(); + setPriority(Thread.MIN_PRIORITY); while (true) { try { handleMessage(mMessageQueue.take()); @@ -152,6 +176,67 @@ public class DataController { // multiple callbacks querying the same URL) doQueryBookmarkStatus((String) msg.obj, msg.replyTo); break; + case TAB_LOAD_THUMBNAIL: + doLoadThumbnail((Tab) msg.obj); + break; + case TAB_DELETE_THUMBNAIL: + ContentResolver cr = mContext.getContentResolver(); + try { + cr.delete(ContentUris.withAppendedId( + Thumbnails.CONTENT_URI, (Long)msg.obj), + null, null); + } catch (Throwable t) {} + break; + case TAB_SAVE_THUMBNAIL: + doSaveThumbnail((Tab)msg.obj); + break; + } + } + + private byte[] getCaptureBlob(Tab tab) { + synchronized (tab) { + Bitmap capture = tab.getScreenshot(); + if (capture == null) { + return null; + } + if (mBuffer == null || mBuffer.limit() < capture.getByteCount()) { + mBuffer = ByteBuffer.allocate(capture.getByteCount()); + } + capture.copyPixelsToBuffer(mBuffer); + mBuffer.rewind(); + return mBuffer.array(); + } + } + + private void doSaveThumbnail(Tab tab) { + byte[] blob = getCaptureBlob(tab); + if (blob == null) { + return; + } + ContentResolver cr = mContext.getContentResolver(); + ContentValues values = new ContentValues(); + values.put(Thumbnails._ID, tab.getId()); + values.put(Thumbnails.THUMBNAIL, blob); + cr.insert(Thumbnails.CONTENT_URI, values); + } + + private void doLoadThumbnail(Tab tab) { + ContentResolver cr = mContext.getContentResolver(); + Cursor c = null; + try { + Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, tab.getId()); + c = cr.query(uri, new String[] {Thumbnails._ID, + Thumbnails.THUMBNAIL}, null, null, null); + if (c.moveToFirst()) { + byte[] data = c.getBlob(1); + if (data != null && data.length > 0) { + tab.updateCaptureFromBlob(data); + } + } + } finally { + if (c != null) { + c.close(); + } } } @@ -184,7 +269,6 @@ public class DataController { } private void doQueryBookmarkStatus(String url, Object replyTo) { - ContentResolver cr = mContext.getContentResolver(); // Check to see if the site is bookmarked Cursor cursor = null; boolean isBookmark = false; diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java index 8a3febe..dd97960 100644 --- a/src/com/android/browser/Tab.java +++ b/src/com/android/browser/Tab.java @@ -20,13 +20,11 @@ import android.app.Activity; import android.app.AlertDialog; import android.app.SearchManager; import android.content.ContentResolver; -import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.content.Intent; -import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; @@ -73,7 +71,6 @@ import android.widget.Toast; import com.android.browser.TabControl.OnThumbnailUpdatedListener; import com.android.browser.homepages.HomeProvider; -import com.android.browser.provider.BrowserProvider2.Thumbnails; import com.android.browser.provider.SnapshotProvider.Snapshots; import com.android.common.speech.LoggingEvents; @@ -2013,7 +2010,7 @@ class Tab implements PictureListener { mCurrentState.mTitle = title; synchronized (Tab.this) { if (mCapture != null) { - BackgroundHandler.execute(mLoadThumbnail); + DataController.getInstance(mContext).loadThumbnail(this); } } } @@ -2093,6 +2090,9 @@ class Tab implements PictureListener { protected void capture() { if (mMainView == null || mCapture == null) return; + if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) { + return; + } Canvas c = new Canvas(mCapture); final int left = mMainView.getScrollX(); final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight(); @@ -2170,14 +2170,14 @@ class Tab implements PictureListener { } protected void persistThumbnail() { - BackgroundHandler.execute(mSaveThumbnail); + DataController.getInstance(mContext).saveThumbnail(this); } protected void deleteThumbnail() { - BackgroundHandler.execute(mDeleteThumbnail); + DataController.getInstance(mContext).deleteThumbnail(this); } - private void updateCaptureFromBlob(byte[] blob) { + void updateCaptureFromBlob(byte[] blob) { synchronized (Tab.this) { if (mCapture == null) { return; @@ -2194,76 +2194,6 @@ class Tab implements PictureListener { } } - private static final ThreadLocal<ByteBuffer> sBuffer = new ThreadLocal<ByteBuffer>(); - - private byte[] getCaptureBlob() { - synchronized (Tab.this) { - if (mCapture == null) { - return null; - } - ByteBuffer buffer = sBuffer.get(); - if (buffer == null || buffer.limit() < mCapture.getByteCount()) { - buffer = ByteBuffer.allocate(mCapture.getByteCount()); - sBuffer.set(buffer); - } - mCapture.copyPixelsToBuffer(buffer); - buffer.rewind(); - return buffer.array(); - } - } - - private Runnable mSaveThumbnail = new Runnable() { - - @Override - public void run() { - byte[] blob = getCaptureBlob(); - if (blob == null) { - return; - } - ContentResolver cr = mContext.getContentResolver(); - ContentValues values = new ContentValues(); - values.put(Thumbnails._ID, mId); - values.put(Thumbnails.THUMBNAIL, blob); - cr.insert(Thumbnails.CONTENT_URI, values); - } - }; - - private Runnable mDeleteThumbnail = new Runnable() { - - @Override - public void run() { - ContentResolver cr = mContext.getContentResolver(); - try { - cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId), - null, null); - } catch (Throwable t) {} - } - }; - - private Runnable mLoadThumbnail = new Runnable() { - - @Override - public void run() { - ContentResolver cr = mContext.getContentResolver(); - Cursor c = null; - try { - Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId); - c = cr.query(uri, new String[] {Thumbnails._ID, - Thumbnails.THUMBNAIL}, null, null, null); - if (c.moveToFirst()) { - byte[] data = c.getBlob(1); - if (data != null && data.length > 0) { - updateCaptureFromBlob(data); - } - } - } finally { - if (c != null) { - c.close(); - } - } - } - }; - @Override public String toString() { StringBuilder builder = new StringBuilder(100); |