OSDN Git Service

Use AsyncTask to edit history in the background.
authorLeon Scroggins <scroggo@google.com>
Thu, 11 Mar 2010 14:33:11 +0000 (09:33 -0500)
committerLeon Scroggins <scroggo@google.com>
Thu, 11 Mar 2010 17:39:13 +0000 (12:39 -0500)
Part of http://b/issue?id=2485421

Change-Id: I19db62679bd0e8df63294b2ad126c7b5c3ff9f52

src/com/android/browser/Tab.java

index 5d17691..409f68c 100644 (file)
@@ -931,45 +931,60 @@ class Tab {
         }
 
         @Override
-        public void onReceivedTitle(WebView view, String title) {
-            String url = view.getUrl();
+        public void onReceivedTitle(WebView view, final String title) {
+            final String pageUrl = view.getUrl();
             if (mInForeground) {
                 // here, if url is null, we want to reset the title
-                mActivity.setUrlTitle(url, title);
+                mActivity.setUrlTitle(pageUrl, title);
             }
-            if (url == null ||
-                url.length() >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
+            if (pageUrl == null || pageUrl.length()
+                    >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
                 return;
             }
-            // See if we can find the current url in our history database and
-            // add the new title to it.
-            if (url.startsWith("http://www.")) {
-                url = url.substring(11);
-            } else if (url.startsWith("http://")) {
-                url = url.substring(4);
-            }
-            try {
-                final ContentResolver cr = mActivity.getContentResolver();
-                url = "%" + url;
-                String [] selArgs = new String[] { url };
-                String where = Browser.BookmarkColumns.URL + " LIKE ? AND "
-                        + Browser.BookmarkColumns.BOOKMARK + " = 0";
-                Cursor c = cr.query(Browser.BOOKMARKS_URI,
-                        Browser.HISTORY_PROJECTION, where, selArgs, null);
-                if (c.moveToFirst()) {
-                    // Current implementation of database only has one entry per
-                    // url.
-                    ContentValues map = new ContentValues();
-                    map.put(Browser.BookmarkColumns.TITLE, title);
-                    cr.update(Browser.BOOKMARKS_URI, map, "_id = "
-                            + c.getInt(0), null);
+            new AsyncTask<Void, Void, Void>() {
+                protected Void doInBackground(Void... unused) {
+                    // See if we can find the current url in our history
+                    // database and add the new title to it.
+                    String url = pageUrl;
+                    if (url.startsWith("http://www.")) {
+                        url = url.substring(11);
+                    } else if (url.startsWith("http://")) {
+                        url = url.substring(4);
+                    }
+                    Cursor c = null;
+                    try {
+                        final ContentResolver cr
+                                = mActivity.getContentResolver();
+                        url = "%" + url;
+                        String [] selArgs = new String[] { url };
+                        String where = Browser.BookmarkColumns.URL
+                                + " LIKE ? AND "
+                                + Browser.BookmarkColumns.BOOKMARK + " = 0";
+                        c = cr.query(Browser.BOOKMARKS_URI, new String[]
+                                { Browser.BookmarkColumns._ID }, where, selArgs,
+                                null);
+                        if (c.moveToFirst()) {
+                            // Current implementation of database only has one
+                            // entry per url.
+                            ContentValues map = new ContentValues();
+                            map.put(Browser.BookmarkColumns.TITLE, title);
+                            String[] projection = new String[]
+                                    { Integer.valueOf(c.getInt(0)).toString() };
+                            cr.update(Browser.BOOKMARKS_URI, map, "_id = ?",
+                                    projection);
+                        }
+                    } catch (IllegalStateException e) {
+                        Log.e(LOGTAG, "Tab onReceived title", e);
+                    } catch (SQLiteException ex) {
+                        Log.e(LOGTAG,
+                                "onReceivedTitle() caught SQLiteException: ",
+                                ex);
+                    } finally {
+                        if (c != null) c.close();
+                    }
+                    return null;
                 }
-                c.close();
-            } catch (IllegalStateException e) {
-                Log.e(LOGTAG, "Tab onReceived title", e);
-            } catch (SQLiteException ex) {
-                Log.e(LOGTAG, "onReceivedTitle() caught SQLiteException: ", ex);
-            }
+            }.execute();
         }
 
         @Override