OSDN Git Service

Save to database in non UI thread.
authorLeon Scroggins <scroggo@google.com>
Mon, 4 Jan 2010 19:30:13 +0000 (14:30 -0500)
committerLeon Scroggins <scroggo@google.com>
Mon, 4 Jan 2010 19:30:13 +0000 (14:30 -0500)
Work to prevent ANR reported in http://b/issue?id=2322540 category 1.

src/com/android/browser/AddBookmarkPage.java

index a9c7217..f5b1ff9 100644 (file)
@@ -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;