Bug 913457 - Part 2: Handle unpinning of removed sites. r=lucasr
authorChenxia Liu <liuche@mozilla.com>
Tue, 20 May 2014 14:02:58 -0700
changeset 184101 41548309c22f1df6c19c3172c22c5795d9941ff1
parent 184100 e23b323393bc036f5fd17631dc6050c3ec39eb6e
child 184102 70eebd17c5673fbe908d4695960ed86bbac7d301
push id26811
push usercbook@mozilla.com
push dateWed, 21 May 2014 11:58:32 +0000
treeherdermozilla-central@97943ef127f4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslucasr
bugs913457
milestone32.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 913457 - Part 2: Handle unpinning of removed sites. r=lucasr
mobile/android/base/home/HomeFragment.java
mobile/android/base/home/TopSitesPanel.java
--- a/mobile/android/base/home/HomeFragment.java
+++ b/mobile/android/base/home/HomeFragment.java
@@ -12,16 +12,17 @@ import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.ReaderModeUtils;
 import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.db.BrowserContract.Combined;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.favicons.Favicons;
+import org.mozilla.gecko.home.TopSitesGridView.TopSitesGridContextMenuInfo;
 import org.mozilla.gecko.util.ThreadUtils;
 import org.mozilla.gecko.util.UiAsyncTask;
 
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.res.Configuration;
 import android.net.Uri;
@@ -186,44 +187,25 @@ abstract class HomeFragment extends Frag
 
         if (itemId == R.id.home_open_in_reader) {
             final String url = ReaderModeUtils.getAboutReaderForUrl(info.url);
             Tabs.getInstance().loadUrl(url, Tabs.LOADURL_NONE);
             return true;
         }
 
         if (itemId == R.id.home_remove) {
-            // Track notification queuing of removal so we don't notify multiple times.
-            boolean notifyQueued = false;
-            final String url = info.url;
-
-            // This might be a reading list item. Try removing it by url.
-            (new RemoveReadingListItemTask(context, url)).execute();
-
-            if (info.isInReadingList()) {
-                // For reading list, this may still double-notify because reading list is in Gecko.
-                notifyQueued = true;
+            if (info instanceof TopSitesGridContextMenuInfo) {
+                (new RemoveItemByUrlTask(context, info.url, info.position)).execute();
+                return true;
             }
 
-            if (info.hasBookmarkId()) {
-                new RemoveBookmarkTask(context, info.bookmarkId, !notifyQueued).execute();
-                notifyQueued = true;
-            } else {
-                new RemoveBookmarkTask(context, url, false).execute();
+            if (info.isInReadingList() || info.hasBookmarkId() || info.hasHistoryId()) {
+                (new RemoveItemByUrlTask(context, info.url)).execute();
+                return true;
             }
-
-            if (info.hasHistoryId()) {
-                new RemoveHistoryTask(context, info.historyId, !notifyQueued).execute();
-                notifyQueued = true;
-            } else {
-                // We can't know for sure if there is also a history item, but try anyways.
-                new RemoveHistoryTask(context, url, false).execute();
-            }
-
-            return notifyQueued;
         }
 
         return false;
     }
 
     @Override
     public void setUserVisibleHint (boolean isVisibleToUser) {
         if (isVisibleToUser == getUserVisibleHint()) {
@@ -279,115 +261,56 @@ abstract class HomeFragment extends Frag
         if (!canLoad() || mIsLoaded) {
             return;
         }
 
         load();
         mIsLoaded = true;
     }
 
-    private static class RemoveBookmarkTask extends UiAsyncTask<Void, Void, Void> {
+    private static class RemoveItemByUrlTask extends UiAsyncTask<Void, Void, Void> {
         private final Context mContext;
-        private final int mId;
         private final String mUrl;
-        private final boolean mNotify;
+        private final int mPosition;
 
-        public RemoveBookmarkTask(Context context, int id, String url, boolean notify) {
+        /**
+         * Remove bookmark/history/reading list item by url.
+         */
+        public RemoveItemByUrlTask(Context context, String url) {
+            this(context, url, -1);
+        }
+
+        /**
+         * Remove bookmark/history/reading list item by url, and also unpin the
+         * Top Sites grid item at index <code>position</code>.
+         */
+        public RemoveItemByUrlTask(Context context, String url, int position) {
             super(ThreadUtils.getBackgroundHandler());
 
             mContext = context;
-            mId = id;
             mUrl = url;
-            mNotify = notify;
-        }
-
-        public RemoveBookmarkTask(Context context, int id, boolean notify) {
-            this(context, id, null, notify);
-        }
-
-        public RemoveBookmarkTask(Context context, String url, boolean notify) {
-            this(context, -1, url, notify);
+            mPosition = position;
         }
 
         @Override
         public Void doInBackground(Void... params) {
             ContentResolver cr = mContext.getContentResolver();
-            if (mId > 0) {
-                BrowserDB.removeBookmark(cr, mId);
-            } else {
-                BrowserDB.removeBookmarksWithURL(cr, mUrl);
+
+            if (mPosition > -1) {
+                BrowserDB.unpinSite(cr, mPosition);
             }
 
+            BrowserDB.removeBookmarksWithURL(cr, mUrl);
+            BrowserDB.removeHistoryEntry(cr, mUrl);
+
+            BrowserDB.removeReadingListItemWithURL(cr, mUrl);
+            GeckoEvent e = GeckoEvent.createBroadcastEvent("Reader:Remove", mUrl);
+            GeckoAppShell.sendEventToGecko(e);
+
             return null;
         }
 
         @Override
         public void onPostExecute(Void result) {
-            if (mNotify) {
-                Toast.makeText(mContext, R.string.page_removed, Toast.LENGTH_SHORT).show();
-            }
-        }
-    }
-
-
-    private static class RemoveReadingListItemTask extends UiAsyncTask<Void, Void, Void> {
-        private final String mUrl;
-        private final Context mContext;
-
-        public RemoveReadingListItemTask(Context context, String url) {
-            super(ThreadUtils.getBackgroundHandler());
-            mUrl = url;
-            mContext = context;
-        }
-
-        @Override
-        public Void doInBackground(Void... params) {
-            ContentResolver cr = mContext.getContentResolver();
-            BrowserDB.removeReadingListItemWithURL(cr, mUrl);
-
-            GeckoEvent e = GeckoEvent.createBroadcastEvent("Reader:Remove", mUrl);
-            GeckoAppShell.sendEventToGecko(e);
-
-            return null;
-        }
-    }
-
-    private static class RemoveHistoryTask extends UiAsyncTask<Void, Void, Void> {
-        private final Context mContext;
-        private final int mId;
-        private final String mUrl;
-        private final boolean mNotify;
-
-        public RemoveHistoryTask(Context context, int id, boolean notify) {
-            this(context, id, null, notify);
-        }
-
-        public RemoveHistoryTask(Context context, String url, boolean notify) {
-            this(context, -1, url, notify);
-        }
-
-        public RemoveHistoryTask(Context context, int id, String url, boolean notify) {
-            super(ThreadUtils.getBackgroundHandler());
-
-            mContext = context;
-            mId = id;
-            mUrl = url;
-            mNotify = notify;
-        }
-
-        @Override
-        public Void doInBackground(Void... params) {
-            if (mId > 0) {
-                BrowserDB.removeHistoryEntry(mContext.getContentResolver(), mId);
-            } else {
-                BrowserDB.removeHistoryEntry(mContext.getContentResolver(), mUrl);
-            }
-            return null;
-        }
-
-        @Override
-        public void onPostExecute(Void result) {
-            if (mNotify) {
-                Toast.makeText(mContext, R.string.page_removed, Toast.LENGTH_SHORT).show();
-            }
+            Toast.makeText(mContext, R.string.page_removed, Toast.LENGTH_SHORT).show();
         }
     }
 }
--- a/mobile/android/base/home/TopSitesPanel.java
+++ b/mobile/android/base/home/TopSitesPanel.java
@@ -339,16 +339,17 @@ public class TopSitesPanel extends HomeF
             ThreadUtils.postToBackgroundThread(new Runnable() {
                 @Override
                 public void run() {
                     BrowserDB.unpinSite(context.getContentResolver(), position);
                 }
             });
 
             Telemetry.sendUIEvent(TelemetryContract.Event.TOP_SITES_UNPIN);
+
             return true;
         }
 
         if (itemId == R.id.top_sites_edit) {
             // Decode "user-entered" URLs before showing them.
             mEditPinnedSiteListener.onEditPinnedSite(info.position, decodeUserEnteredUrl(info.url));
 
             Telemetry.sendUIEvent(TelemetryContract.Event.TOP_SITES_EDIT);