Bug 1062257 - Handle HomeFragment deletions by panel/type instead of universally. r=margaret
authorChenxia Liu <liuche@mozilla.com>
Fri, 12 Sep 2014 17:12:33 -0700
changeset 205066 f6c986420f6729e039742d62165ccdec8bb92c10
parent 205065 1e0e069b5cc790084e098e5ae2bc65494da03ac0
child 205067 155871851e7d2d48f891c9a4b0044c83fa9ca6d3
push id27476
push userphilringnalda@gmail.com
push dateSat, 13 Sep 2014 16:22:53 +0000
treeherdermozilla-central@a6d02cff43d3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs1062257
milestone35.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 1062257 - Handle HomeFragment deletions by panel/type instead of universally. r=margaret
mobile/android/base/home/BookmarksPanel.java
mobile/android/base/home/HistoryPanel.java
mobile/android/base/home/HomeAdapter.java
mobile/android/base/home/HomeContextMenuInfo.java
mobile/android/base/home/HomeFragment.java
mobile/android/base/home/ReadingListPanel.java
mobile/android/base/home/TopSitesGridView.java
mobile/android/base/home/TopSitesPanel.java
--- a/mobile/android/base/home/BookmarksPanel.java
+++ b/mobile/android/base/home/BookmarksPanel.java
@@ -8,22 +8,23 @@ package org.mozilla.gecko.home;
 import java.util.List;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.home.BookmarksListAdapter.FolderInfo;
 import org.mozilla.gecko.home.BookmarksListAdapter.OnRefreshFolderListener;
 import org.mozilla.gecko.home.BookmarksListAdapter.RefreshType;
+import org.mozilla.gecko.home.HomeContextMenuInfo.RemoveItemType;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 
 import android.app.Activity;
 import android.content.Context;
+import android.content.res.Configuration;
 import android.content.res.Resources;
-import android.content.res.Configuration;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.v4.app.LoaderManager.LoaderCallbacks;
 import android.support.v4.content.Loader;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewStub;
@@ -73,16 +74,17 @@ public class BookmarksPanel extends Home
                 if (type == Bookmarks.TYPE_FOLDER) {
                     // We don't show a context menu for folders
                     return null;
                 }
                 final HomeContextMenuInfo info = new HomeContextMenuInfo(view, position, id);
                 info.url = cursor.getString(cursor.getColumnIndexOrThrow(Bookmarks.URL));
                 info.title = cursor.getString(cursor.getColumnIndexOrThrow(Bookmarks.TITLE));
                 info.bookmarkId = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID));
+                info.itemType = RemoveItemType.BOOKMARKS;
                 return info;
             }
         });
 
         return view;
     }
 
     @Override
--- a/mobile/android/base/home/HistoryPanel.java
+++ b/mobile/android/base/home/HistoryPanel.java
@@ -13,18 +13,18 @@ import org.json.JSONObject;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoEvent;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.db.BrowserContract.Combined;
 import org.mozilla.gecko.db.BrowserContract.History;
 import org.mozilla.gecko.db.BrowserDB;
+import org.mozilla.gecko.home.HomeContextMenuInfo.RemoveItemType;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
-import org.mozilla.gecko.util.ThreadUtils;
 
 import android.app.AlertDialog;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.v4.app.LoaderManager.LoaderCallbacks;
@@ -90,16 +90,17 @@ public class HistoryPanel extends HomeFr
 
         mList.setContextMenuInfoFactory(new HomeContextMenuInfo.Factory() {
             @Override
             public HomeContextMenuInfo makeInfoForCursor(View view, int position, long id, Cursor cursor) {
                 final HomeContextMenuInfo info = new HomeContextMenuInfo(view, position, id);
                 info.url = cursor.getString(cursor.getColumnIndexOrThrow(Combined.URL));
                 info.title = cursor.getString(cursor.getColumnIndexOrThrow(Combined.TITLE));
                 info.historyId = cursor.getInt(cursor.getColumnIndexOrThrow(Combined.HISTORY_ID));
+                info.itemType = RemoveItemType.HISTORY;
                 final int bookmarkIdCol = cursor.getColumnIndexOrThrow(Combined.BOOKMARK_ID);
                 if (cursor.isNull(bookmarkIdCol)) {
                     // If this is a combined cursor, we may get a history item without a
                     // bookmark, in which case the bookmarks ID column value will be null.
                     info.bookmarkId =  -1;
                 } else {
                     info.bookmarkId = cursor.getInt(bookmarkIdCol);
                 }
--- a/mobile/android/base/home/HomeAdapter.java
+++ b/mobile/android/base/home/HomeAdapter.java
@@ -2,17 +2,16 @@
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.home;
 
 import org.mozilla.gecko.home.HomeConfig.PanelConfig;
 import org.mozilla.gecko.home.HomeConfig.PanelType;
-import org.mozilla.gecko.home.HomePager;
 
 import android.content.Context;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentStatePagerAdapter;
 import android.view.ViewGroup;
 
--- a/mobile/android/base/home/HomeContextMenuInfo.java
+++ b/mobile/android/base/home/HomeContextMenuInfo.java
@@ -19,16 +19,22 @@ import android.widget.ExpandableListAdap
 public class HomeContextMenuInfo extends AdapterContextMenuInfo {
 
     public String url;
     public String title;
     public boolean isFolder;
     public int historyId = -1;
     public int bookmarkId = -1;
     public int readingListItemId = -1;
+    public RemoveItemType itemType = null;
+
+    // Item type to be handled with "Remove" selection.
+    public static enum RemoveItemType {
+        BOOKMARKS, HISTORY, READING_LIST
+    }
 
     public HomeContextMenuInfo(View targetView, int position, long id) {
         super(targetView, position, id);
     }
 
     public boolean hasBookmarkId() {
         return bookmarkId > -1;
     }
--- a/mobile/android/base/home/HomeFragment.java
+++ b/mobile/android/base/home/HomeFragment.java
@@ -16,16 +16,17 @@ import org.mozilla.gecko.R;
 import org.mozilla.gecko.ReaderModeUtils;
 import org.mozilla.gecko.Tab;
 import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.db.BrowserContract.SuggestedSites;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.favicons.Favicons;
+import org.mozilla.gecko.home.HomeContextMenuInfo.RemoveItemType;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 import org.mozilla.gecko.home.TopSitesGridView.TopSitesGridContextMenuInfo;
 import org.mozilla.gecko.util.Clipboard;
 import org.mozilla.gecko.util.StringUtils;
 import org.mozilla.gecko.util.ThreadUtils;
 import org.mozilla.gecko.util.UIAsyncTask;
 import org.mozilla.gecko.widget.ButtonToast;
 
@@ -243,25 +244,21 @@ public abstract class HomeFragment exten
 
         if (itemId == R.id.home_edit_bookmark) {
             // UI Dialog associates to the activity context, not the applications'.
             new EditBookmarkDialog(context).show(info.url);
             return true;
         }
 
         if (itemId == R.id.home_remove) {
-            if (info instanceof TopSitesGridContextMenuInfo) {
-                (new RemoveItemByUrlTask(context, info.url, info.position)).execute();
-                return true;
-            }
+            // For Top Sites grid items, position is required in case item is Pinned.
+            final int position = info instanceof TopSitesGridContextMenuInfo ? info.position : -1;
 
-            if (info.isInReadingList() || info.hasBookmarkId() || info.hasHistoryId()) {
-                (new RemoveItemByUrlTask(context, info.url)).execute();
-                return true;
-            }
+            (new RemoveItemByUrlTask(context, info.url, info.itemType, position)).execute();
+            return true;
         }
 
         return false;
     }
 
     @Override
     public void setUserVisibleHint (boolean isVisibleToUser) {
         if (isVisibleToUser == getUserVisibleHint()) {
@@ -311,83 +308,89 @@ public abstract class HomeFragment exten
         mCanLoadHint = canLoadHint;
         loadIfVisible();
     }
 
     boolean getCanLoadHint() {
         return mCanLoadHint;
     }
 
-
     protected abstract void load();
 
     protected boolean canLoad() {
         return (mCanLoadHint && isVisible() && getUserVisibleHint());
     }
 
     protected void loadIfVisible() {
         if (!canLoad() || mIsLoaded) {
             return;
         }
 
         load();
         mIsLoaded = true;
     }
 
-    private static class RemoveItemByUrlTask extends UIAsyncTask.WithoutParams<Void> {
+    protected static class RemoveItemByUrlTask extends UIAsyncTask.WithoutParams<Void> {
         private final Context mContext;
         private final String mUrl;
+        private final RemoveItemType mType;
         private final int mPosition;
 
         /**
-         * 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
+         * Remove bookmark/history/reading list type item by url, and also unpin the
          * Top Sites grid item at index <code>position</code>.
          */
-        public RemoveItemByUrlTask(Context context, String url, int position) {
+        public RemoveItemByUrlTask(Context context, String url, RemoveItemType type, int position) {
             super(ThreadUtils.getBackgroundHandler());
 
             mContext = context;
             mUrl = url;
+            mType = type;
             mPosition = position;
         }
 
         @Override
         public Void doInBackground() {
             ContentResolver cr = mContext.getContentResolver();
 
             if (mPosition > -1) {
                 BrowserDB.unpinSite(cr, mPosition);
                 if (BrowserDB.hideSuggestedSite(mUrl)) {
                     cr.notifyChange(SuggestedSites.CONTENT_URI, null);
                 }
             }
 
-            BrowserDB.removeBookmarksWithURL(cr, mUrl);
-            BrowserDB.removeHistoryEntry(cr, mUrl);
+            switch(mType) {
+                case BOOKMARKS:
+                    BrowserDB.removeBookmarksWithURL(cr, mUrl);
+                    break;
 
-            BrowserDB.removeReadingListItemWithURL(cr, mUrl);
+                case HISTORY:
+                    BrowserDB.removeHistoryEntry(cr, mUrl);
+                    break;
+
+                case READING_LIST:
+                    BrowserDB.removeReadingListItemWithURL(cr, mUrl);
 
-            final JSONObject json = new JSONObject();
-            try {
-                json.put("url", mUrl);
-                json.put("notify", false);
-            } catch (JSONException e) {
-                Log.e(LOGTAG, "error building JSON arguments");
+                    final JSONObject json = new JSONObject();
+                    try {
+                        json.put("url", mUrl);
+                        json.put("notify", false);
+                    } catch (JSONException e) {
+                        Log.e(LOGTAG, "error building JSON arguments");
+                    }
+
+                    GeckoEvent e = GeckoEvent.createBroadcastEvent("Reader:Remove", json.toString());
+                    GeckoAppShell.sendEventToGecko(e);
+                    break;
+
+                default:
+                    Log.e(LOGTAG, "Can't remove item type " + mType.toString());
+                    break;
             }
-
-            GeckoEvent e = GeckoEvent.createBroadcastEvent("Reader:Remove", json.toString());
-            GeckoAppShell.sendEventToGecko(e);
-
             return null;
         }
 
         @Override
         public void onPostExecute(Void result) {
             Toast.makeText(mContext, R.string.page_removed, Toast.LENGTH_SHORT).show();
         }
     }
--- a/mobile/android/base/home/ReadingListPanel.java
+++ b/mobile/android/base/home/ReadingListPanel.java
@@ -9,16 +9,17 @@ import java.util.EnumSet;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.ReaderModeUtils;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.db.BrowserContract.ReadingListItems;
 import org.mozilla.gecko.db.BrowserContract.URLColumns;
 import org.mozilla.gecko.db.BrowserDB;
+import org.mozilla.gecko.home.HomeContextMenuInfo.RemoveItemType;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 
 import android.content.Context;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.v4.app.LoaderManager.LoaderCallbacks;
 import android.support.v4.content.Loader;
 import android.support.v4.widget.CursorAdapter;
@@ -92,16 +93,17 @@ public class ReadingListPanel extends Ho
 
         mList.setContextMenuInfoFactory(new HomeContextMenuInfo.Factory() {
             @Override
             public HomeContextMenuInfo makeInfoForCursor(View view, int position, long id, Cursor cursor) {
                 final HomeContextMenuInfo info = new HomeContextMenuInfo(view, position, id);
                 info.url = cursor.getString(cursor.getColumnIndexOrThrow(ReadingListItems.URL));
                 info.title = cursor.getString(cursor.getColumnIndexOrThrow(ReadingListItems.TITLE));
                 info.readingListItemId = cursor.getInt(cursor.getColumnIndexOrThrow(ReadingListItems._ID));
+                info.itemType = RemoveItemType.READING_LIST;
                 return info;
             }
         });
         registerForContextMenu(mList);
     }
 
     @Override
     public void onDestroyView() {
--- a/mobile/android/base/home/TopSitesGridView.java
+++ b/mobile/android/base/home/TopSitesGridView.java
@@ -7,25 +7,23 @@ package org.mozilla.gecko.home;
 
 import java.util.EnumSet;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.ThumbnailHelper;
 import org.mozilla.gecko.db.BrowserContract.TopSites;
-import org.mozilla.gecko.db.TopSitesCursorWrapper;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 import org.mozilla.gecko.util.StringUtils;
 
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.database.Cursor;
 import android.graphics.Rect;
-import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.view.ContextMenu.ContextMenuInfo;
 import android.view.View;
 import android.widget.AbsListView;
 import android.widget.AdapterView;
 import android.widget.GridView;
 
 /**
@@ -272,11 +270,12 @@ public class TopSitesGridView extends Gr
     /**
      * Stores information regarding the creation of the context menu for a GridView item.
      */
     public static class TopSitesGridContextMenuInfo extends HomeContextMenuInfo {
         public int type = -1;
 
         public TopSitesGridContextMenuInfo(View targetView, int position, long id) {
             super(targetView, position, id);
+            this.itemType = RemoveItemType.HISTORY;
         }
     }
 }
--- a/mobile/android/base/home/TopSitesPanel.java
+++ b/mobile/android/base/home/TopSitesPanel.java
@@ -20,16 +20,17 @@ import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.db.BrowserContract.Thumbnails;
 import org.mozilla.gecko.db.BrowserContract.TopSites;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.URLMetadata;
 import org.mozilla.gecko.favicons.Favicons;
 import org.mozilla.gecko.favicons.OnFaviconLoadedListener;
 import org.mozilla.gecko.gfx.BitmapUtils;
+import org.mozilla.gecko.home.HomeContextMenuInfo.RemoveItemType;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 import org.mozilla.gecko.home.PinSiteDialog.OnSiteSelectedListener;
 import org.mozilla.gecko.home.TopSitesGridView.OnEditPinnedSiteListener;
 import org.mozilla.gecko.home.TopSitesGridView.TopSitesGridContextMenuInfo;
 import org.mozilla.gecko.util.StringUtils;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import android.app.Activity;
@@ -174,16 +175,17 @@ public class TopSitesPanel extends HomeF
 
         mList.setContextMenuInfoFactory(new HomeContextMenuInfo.Factory() {
             @Override
             public HomeContextMenuInfo makeInfoForCursor(View view, int position, long id, Cursor cursor) {
                 final HomeContextMenuInfo info = new HomeContextMenuInfo(view, position, id);
                 info.url = cursor.getString(cursor.getColumnIndexOrThrow(TopSites.URL));
                 info.title = cursor.getString(cursor.getColumnIndexOrThrow(TopSites.TITLE));
                 info.historyId = cursor.getInt(cursor.getColumnIndexOrThrow(TopSites.HISTORY_ID));
+                info.itemType = RemoveItemType.HISTORY;
                 final int bookmarkIdCol = cursor.getColumnIndexOrThrow(TopSites.BOOKMARK_ID);
                 if (cursor.isNull(bookmarkIdCol)) {
                     // If this is a combined cursor, we may get a history item without a
                     // bookmark, in which case the bookmarks ID column value will be null.
                     info.bookmarkId =  -1;
                 } else {
                     info.bookmarkId = cursor.getInt(bookmarkIdCol);
                 }