Bug 671131 - (Part 2) Make observer to update history tab when history item is removed. r=bnicholson
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Mon, 14 May 2012 11:44:37 -0700
changeset 93879 a02adac1eaf3b406b488ab8380f26f6176db6d13
parent 93878 95dfc63ba18159ad3a57694e5e68a617c3f1610e
child 93880 d0f6caf941d106a9ccab935b8f4069b99e121afd
push id9364
push usermleibovic@mozilla.com
push dateMon, 14 May 2012 18:45:01 +0000
treeherdermozilla-inbound@d0f6caf941d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbnicholson
bugs671131
milestone15.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 671131 - (Part 2) Make observer to update history tab when history item is removed. r=bnicholson
mobile/android/base/AwesomeBarTabs.java
mobile/android/base/Tab.java
mobile/android/base/db/AndroidBrowserDB.java
mobile/android/base/db/BrowserDB.java
mobile/android/base/db/LocalBrowserDB.java
--- a/mobile/android/base/AwesomeBarTabs.java
+++ b/mobile/android/base/AwesomeBarTabs.java
@@ -36,16 +36,17 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 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.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Color;
 import android.graphics.LightingColorFilter;
 import android.graphics.drawable.Drawable;
 import android.os.AsyncTask;
 import android.os.SystemClock;
@@ -97,16 +98,17 @@ public class AwesomeBarTabs extends TabH
 
     private Context mContext;
     private boolean mInflated;
     private LayoutInflater mInflater;
     private OnUrlOpenListener mUrlOpenListener;
     private View.OnTouchListener mListTouchListener;
     private JSONArray mSearchEngines;
     private ContentResolver mContentResolver;
+    private ContentObserver mContentObserver;
 
     private BookmarksQueryTask mBookmarksQueryTask;
     private HistoryQueryTask mHistoryQueryTask;
     
     private AwesomeBarCursorAdapter mAllPagesCursorAdapter;
     private BookmarksListAdapter mBookmarksAdapter;
     private SimpleExpandableListAdapter mHistoryAdapter;
 
@@ -576,16 +578,27 @@ public class AwesomeBarTabs extends TabH
                 mContext,
                 result.first,
                 R.layout.awesomebar_header_row,
                 new String[] { URLColumns.TITLE },
                 new int[] { R.id.title },
                 result.second
             );
 
+            if (mContentObserver == null) {
+                // Register an observer to update the history tab contents if they change.
+                mContentObserver = new ContentObserver(GeckoAppShell.getHandler()) {
+                    public void onChange(boolean selfChange) {
+                        mHistoryQueryTask = new HistoryQueryTask();
+                        mHistoryQueryTask.execute();
+                    }
+                };
+                BrowserDB.registerHistoryObserver(mContentResolver, mContentObserver);
+            }
+
             final ExpandableListView historyList =
                     (ExpandableListView) findViewById(R.id.history_list);
 
             // Hack: force this to the main thread, even though it should already be on it
             GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
                 public void run() {
                     historyList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                         public boolean onChildClick(ExpandableListView parent, View view,
@@ -733,16 +746,17 @@ public class AwesomeBarTabs extends TabH
         super(context, attrs);
 
         Log.d(LOGTAG, "Creating AwesomeBarTabs");
 
         mContext = context;
         mInflated = false;
         mSearchEngines = new JSONArray();
         mContentResolver = context.getContentResolver();
+        mContentObserver = null;
         mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     }
 
     @Override
     protected void onFinishInflate() {
         super.onFinishInflate();
 
         // HACK: Without this, the onFinishInflate is called twice
@@ -996,16 +1010,19 @@ public class AwesomeBarTabs extends TabH
         if (allPagesCursor != null)
             allPagesCursor.close();
 
         if (mBookmarksAdapter != null) {
             Cursor bookmarksCursor = mBookmarksAdapter.getCursor();
             if (bookmarksCursor != null)
                 bookmarksCursor.close();
         }
+
+        if (mContentObserver != null)
+            BrowserDB.unregisterContentObserver(mContentResolver, mContentObserver);
     }
 
     public void filter(String searchTerm) {
         // Don't let the tab's content steal focus on tab switch
         setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
 
         // Ensure the 'All Pages' tab is selected
         setCurrentTabByTag(ALL_PAGES_TAB);
--- a/mobile/android/base/Tab.java
+++ b/mobile/android/base/Tab.java
@@ -134,17 +134,17 @@ public final class Tab {
                 updateBookmark();
             }
         };
         BrowserDB.registerBookmarkObserver(mContentResolver, mContentObserver);
     }
 
     public void onDestroy() {
         mDoorHangers = new HashMap<String, DoorHanger>();
-        BrowserDB.unregisterBookmarkObserver(mContentResolver, mContentObserver);
+        BrowserDB.unregisterContentObserver(mContentResolver, mContentObserver);
     }
 
     public int getId() {
         return mId;
     }
 
     public int getParentId() {
         return mParentId;
--- a/mobile/android/base/db/AndroidBrowserDB.java
+++ b/mobile/android/base/db/AndroidBrowserDB.java
@@ -308,16 +308,20 @@ public class AndroidBrowserDB implements
 
     public void registerBookmarkObserver(ContentResolver cr, ContentObserver observer) {
         if (Build.VERSION.SDK_INT >= 11)
             registerBookmarkObserverPost11(cr, observer);
         else
             registerBookmarkObserverPre11(cr, observer);
     }
 
+    public void registerHistoryObserver(ContentResolver cr, ContentObserver observer) {
+        // Not implemented
+    }
+
     public BitmapDrawable getFaviconForUrl(ContentResolver cr, String uri) {
         Cursor c = cr.query(Browser.BOOKMARKS_URI,
                             new String[] { Browser.BookmarkColumns.FAVICON },
                             Browser.BookmarkColumns.URL + " = ?",
                             new String[] { uri },
                             null);
 
         if (!c.moveToFirst()) {
--- a/mobile/android/base/db/BrowserDB.java
+++ b/mobile/android/base/db/BrowserDB.java
@@ -97,16 +97,18 @@ public class BrowserDB {
 
         public void updateFaviconForUrl(ContentResolver cr, String uri, BitmapDrawable favicon);
 
         public void updateThumbnailForUrl(ContentResolver cr, String uri, BitmapDrawable thumbnail);
 
         public byte[] getThumbnailForUrl(ContentResolver cr, String uri);
 
         public void registerBookmarkObserver(ContentResolver cr, ContentObserver observer);
+
+        public void registerHistoryObserver(ContentResolver cr, ContentObserver observer);
     }
 
     static {
         // Forcing local DB no option to switch to Android DB for now
         sDb = new LocalBrowserDB(BrowserContract.DEFAULT_PROFILE);
     }
 
     public static void invalidateCachedState() {
@@ -193,12 +195,16 @@ public class BrowserDB {
     public static byte[] getThumbnailForUrl(ContentResolver cr, String uri) {
         return sDb.getThumbnailForUrl(cr, uri);
     }
 
     public static void registerBookmarkObserver(ContentResolver cr, ContentObserver observer) {
         sDb.registerBookmarkObserver(cr, observer);
     }
 
-    public static void unregisterBookmarkObserver(ContentResolver cr, ContentObserver observer) {
+    public static void registerHistoryObserver(ContentResolver cr, ContentObserver observer) {
+        sDb.registerHistoryObserver(cr, observer);
+    }
+
+    public static void unregisterContentObserver(ContentResolver cr, ContentObserver observer) {
         cr.unregisterContentObserver(observer);
     }
 }
--- a/mobile/android/base/db/LocalBrowserDB.java
+++ b/mobile/android/base/db/LocalBrowserDB.java
@@ -503,18 +503,21 @@ public class LocalBrowserDB implements B
         bumpParents(cr, Bookmarks.URL, uri);
 
         final String[] urlArgs = new String[] { uri };
         final String urlEquals = Bookmarks.URL + " = ?";
         cr.delete(contentUri, urlEquals, urlArgs);
     }
 
     public void registerBookmarkObserver(ContentResolver cr, ContentObserver observer) {
-        Uri uri = mBookmarksUriWithProfile;
-        cr.registerContentObserver(uri, false, observer);
+        cr.registerContentObserver(mBookmarksUriWithProfile, false, observer);
+    }
+
+    public void registerHistoryObserver(ContentResolver cr, ContentObserver observer) {
+        cr.registerContentObserver(mHistoryUriWithProfile, false, observer);
     }
 
     public void updateBookmark(ContentResolver cr, int id, String uri, String title, String keyword) {
         ContentValues values = new ContentValues();
         values.put(Browser.BookmarkColumns.TITLE, title);
         values.put(Bookmarks.URL, uri);
         values.put(Bookmarks.KEYWORD, keyword);