diff options
author | Leon Scroggins <scroggo@google.com> | 2010-01-04 14:30:13 -0500 |
---|---|---|
committer | Leon Scroggins <scroggo@google.com> | 2010-01-04 14:30:13 -0500 |
commit | 02065b07aea6b4392c344a4172c3ab56019225ed (patch) | |
tree | 69698ee6eda667abefee72294e37a3e764c91442 /src/com/android/browser/AddBookmarkPage.java | |
parent | 3b2b810a099b7bce65d18fc2a21ae17d78488eb0 (diff) | |
download | packages_apps_Browser-02065b07aea6b4392c344a4172c3ab56019225ed.zip packages_apps_Browser-02065b07aea6b4392c344a4172c3ab56019225ed.tar.gz packages_apps_Browser-02065b07aea6b4392c344a4172c3ab56019225ed.tar.bz2 |
Save to database in non UI thread.
Work to prevent ANR reported in http://b/issue?id=2322540 category 1.
Diffstat (limited to 'src/com/android/browser/AddBookmarkPage.java')
-rw-r--r-- | src/com/android/browser/AddBookmarkPage.java | 68 |
1 files changed, 41 insertions, 27 deletions
diff --git a/src/com/android/browser/AddBookmarkPage.java b/src/com/android/browser/AddBookmarkPage.java index a9c7217..f5b1ff9 100644 --- a/src/com/android/browser/AddBookmarkPage.java +++ b/src/com/android/browser/AddBookmarkPage.java @@ -111,6 +111,43 @@ public class AddBookmarkPage extends Activity { } } + /** + * Runnable to save a bookmark, so it can be performed in its own thread. + */ + private class SaveBookmarkRunnable implements Runnable { + private Message mMessage; + public SaveBookmarkRunnable(Message msg) { + mMessage = msg; + } + public void run() { + // Unbundle bookmark data. + Bundle bundle = mMessage.getData(); + String title = bundle.getString("title"); + String url = bundle.getString("url"); + boolean invalidateThumbnail = bundle.getBoolean( + "invalidateThumbnail"); + Bitmap thumbnail = invalidateThumbnail ? null + : (Bitmap) bundle.getParcelable("thumbnail"); + String touchIconUrl = bundle.getString("touchIconUrl"); + + // Save to the bookmarks DB. + try { + final ContentResolver cr = getContentResolver(); + Bookmarks.addBookmark(null, cr, url, title, thumbnail, true); + if (touchIconUrl != null) { + final Cursor c + = BrowserBookmarksAdapter.queryBookmarksForUrl(cr, + null, url, true); + new DownloadTouchIcon(cr, c, url).execute(mTouchIconUrl); + } + mMessage.arg1 = 1; + } catch (IllegalStateException e) { + mMessage.arg1 = 0; + } + mMessage.sendToTarget(); + } + } + private void createHandler() { if (mHandler == null) { mHandler = new Handler() { @@ -118,17 +155,7 @@ public class AddBookmarkPage extends Activity { public void handleMessage(Message msg) { switch (msg.what) { case SAVE_BOOKMARK: - // Unbundle bookmark data. - Bundle bundle = msg.getData(); - String title = bundle.getString("title"); - String url = bundle.getString("url"); - boolean invalidateThumbnail = bundle.getBoolean("invalidateThumbnail"); - Bitmap thumbnail = invalidateThumbnail - ? null : (Bitmap) bundle.getParcelable("thumbnail"); - String touchIconUrl = bundle.getString("touchIconUrl"); - - // Save to the bookmarks DB. - if (updateBookmarksDB(title, url, thumbnail, touchIconUrl)) { + if (1 == msg.arg1) { Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved, Toast.LENGTH_LONG).show(); } else { @@ -142,21 +169,6 @@ public class AddBookmarkPage extends Activity { } } - private boolean updateBookmarksDB(String title, String url, Bitmap thumbnail, String touchIconUrl) { - try { - final ContentResolver cr = getContentResolver(); - Bookmarks.addBookmark(null, cr, url, title, thumbnail, true); - if (touchIconUrl != null) { - final Cursor c = - BrowserBookmarksAdapter.queryBookmarksForUrl(cr, null, url, true); - new DownloadTouchIcon(cr, c, url).execute(mTouchIconUrl); - } - } catch (IllegalStateException e) { - return false; - } - return true; - } - /** * Parse the data entered in the dialog and post a message to update the bookmarks database. */ @@ -227,7 +239,9 @@ public class AddBookmarkPage extends Activity { bundle.putString("touchIconUrl", mTouchIconUrl); Message msg = Message.obtain(mHandler, SAVE_BOOKMARK); msg.setData(bundle); - mHandler.sendMessage(msg); + // Start a new thread so as to not slow down the UI + Thread t = new Thread(new SaveBookmarkRunnable(msg)); + t.start(); setResult(RESULT_OK); } return true; |