From 95427e4189c9198e609f9ca9d4e94a4ec45301ea Mon Sep 17 00:00:00 2001 From: Brian Nicholson Date: Tue, 14 Feb 2012 16:23:06 -0800 Subject: [PATCH] Bug 722413 - Use custom observer for BookmarksListAdapter. r=mfinkle f=lucasr --- mobile/android/base/AwesomeBar.java | 1 - mobile/android/base/AwesomeBarTabs.java | 45 ++++++++++++++++++------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/mobile/android/base/AwesomeBar.java b/mobile/android/base/AwesomeBar.java index bcddb555789a..ef4a7f43c318 100644 --- a/mobile/android/base/AwesomeBar.java +++ b/mobile/android/base/AwesomeBar.java @@ -502,7 +502,6 @@ public class AwesomeBar extends Activity implements GeckoEventListener { GeckoApp.mAppContext.mMainHandler.post(new Runnable() { public void run() { - mAwesomeTabs.refreshBookmarks(); Toast.makeText(AwesomeBar.this, R.string.bookmark_removed, Toast.LENGTH_SHORT).show(); } diff --git a/mobile/android/base/AwesomeBarTabs.java b/mobile/android/base/AwesomeBarTabs.java index b8883ff40c8e..d8d50d40a274 100644 --- a/mobile/android/base/AwesomeBarTabs.java +++ b/mobile/android/base/AwesomeBarTabs.java @@ -40,6 +40,7 @@ package org.mozilla.gecko; import android.content.ContentResolver; import android.content.Context; import android.content.res.Resources; +import android.database.ContentObserver; import android.database.Cursor; import android.database.MatrixCursor; import android.graphics.Bitmap; @@ -71,6 +72,7 @@ import android.widget.TextView; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.lang.reflect.Field; import java.util.Date; import java.util.HashMap; import java.util.LinkedList; @@ -100,6 +102,7 @@ public class AwesomeBarTabs extends TabHost { private View.OnTouchListener mListTouchListener; private JSONArray mSearchEngines; private ContentResolver mContentResolver; + private ContentObserver mContentObserver; private AwesomeBarCursorAdapter mAllPagesCursorAdapter; private BookmarksListAdapter mBookmarksAdapter; @@ -247,18 +250,6 @@ public class AwesomeBarTabs extends TabHost { } } - // The group cursor doesn't ever need to change because it just holds the - // mobile/desktop folders, but we do need to update the children cursors. - public void refreshBookmarks() { - Cursor groupCursor = mBookmarksAdapter.getCursor(); - groupCursor.moveToPosition(-1); - while (groupCursor.moveToNext()) { - String guid = groupCursor.getString(groupCursor.getColumnIndexOrThrow(Bookmarks.GUID)); - // We need to do this in a AsyncTask because we're on the main thread - new RefreshChildrenCursorTask(groupCursor.getPosition()).execute(guid); - } - } - private class BookmarksQueryTask extends AsyncTask { protected Cursor doInBackground(Void... arg0) { // Make our own cursor to group mobile bookmarks and desktop bookmarks. @@ -291,6 +282,32 @@ public class AwesomeBarTabs extends TabHost { new int[] { R.id.title, R.id.url, R.id.favicon } ); + try { + // use reflection to disable auto-requery + Class cls = Class.forName("android.widget.CursorTreeAdapter"); + Field field = cls.getDeclaredField("mAutoRequery"); + field.setAccessible(true); + field.set(mBookmarksAdapter, false); + + // register an asynchronous custom observer to replace the synchronous auto-requery + mContentObserver = new ContentObserver(GeckoAppShell.getHandler()) { + public void onChange(boolean selfChange) { + // The group cursor doesn't ever need to change because it just holds the + // mobile/desktop folders, but we do need to update the children cursors. + Cursor groupCursor = mBookmarksAdapter.getCursor(); + groupCursor.moveToPosition(-1); + while (groupCursor.moveToNext()) { + String guid = groupCursor.getString(groupCursor.getColumnIndexOrThrow(Bookmarks.GUID)); + // We need to do this in a AsyncTask because we're on the main thread + new RefreshChildrenCursorTask(groupCursor.getPosition()).execute(guid); + } + } + }; + BrowserDB.registerBookmarkObserver(mContentResolver, mContentObserver); + } catch (Exception e) { + Log.e(LOGTAG, "could not disable auto-requery for BookmarksListAdapter"); + } + mBookmarksAdapter.setViewBinder(new AwesomeCursorViewBinder()); final ExpandableListView bookmarksList = (ExpandableListView) findViewById(R.id.bookmarks_list); @@ -618,6 +635,7 @@ public class AwesomeBarTabs extends TabHost { mInflated = false; mSearchEngines = new JSONArray(); mContentResolver = context.getContentResolver(); + mContentObserver = null; } @Override @@ -827,6 +845,9 @@ public class AwesomeBarTabs extends TabHost { if (bookmarksCursor != null) bookmarksCursor.close(); } + + if (mContentObserver != null) + BrowserDB.unregisterBookmarkObserver(mContentResolver, mContentObserver); } public void filter(String searchTerm) {