Bug 1062257 - Handle HomeFragment deletions by panel/type instead of universally. r=margaret, a=sledru
authorChenxia Liu <liuche@mozilla.com>
Fri, 19 Sep 2014 10:13:00 -0400
changeset 216807 ae87b325401d
parent 216806 389dd23d771c
child 216808 d6aa05e710f2
push id3921
push userryanvm@gmail.com
push date2014-09-22 15:03 +0000
treeherdermozilla-beta@f8eec8fe1b2b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret, sledru
bugs1062257
milestone33.0
Bug 1062257 - Handle HomeFragment deletions by panel/type instead of universally. r=margaret, a=sledru
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.BrowserDB;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
+import org.mozilla.gecko.home.HomeContextMenuInfo.RemoveItemType;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
-import org.mozilla.gecko.util.ThreadUtils;
 
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.database.Cursor;
 import android.os.Bundle;
@@ -112,16 +112,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.AdapterView.Adapte
 public class HomeContextMenuInfo extends AdapterContextMenuInfo {
 
     public String url;
     public String title;
     public boolean isFolder = false;
     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
@@ -17,16 +17,17 @@ 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.Combined;
 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.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;
 
 import android.content.ContentResolver;
@@ -219,25 +220,21 @@ abstract class HomeFragment extends Frag
 
         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()) {
@@ -261,17 +258,16 @@ abstract class HomeFragment extends Frag
         mCanLoadHint = canLoadHint;
         loadIfVisible();
     }
 
     boolean getCanLoadHint() {
         return mCanLoadHint;
     }
 
-
     protected abstract void load();
 
     protected boolean canLoad() {
         return (mCanLoadHint && isVisible() && getUserVisibleHint());
     }
 
     protected void loadIfVisible() {
         if (!canLoad() || mIsLoaded) {
@@ -280,64 +276,69 @@ abstract class HomeFragment extends Frag
 
         load();
         mIsLoaded = true;
     }
 
     private static class RemoveItemByUrlTask extends UiAsyncTask<Void, Void, 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(Void... params) {
             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);
-
-            BrowserDB.removeReadingListItemWithURL(cr, mUrl);
+            switch(mType) {
+                case BOOKMARKS:
+                    BrowserDB.removeBookmarksWithURL(cr, mUrl);
+                    break;
+                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.BrowserDB;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
+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.database.Cursor;
 import android.os.Bundle;
 import android.support.v4.app.LoaderManager.LoaderCallbacks;
@@ -116,16 +117,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;
 
 /**
@@ -274,11 +272,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
@@ -18,16 +18,17 @@ import org.mozilla.gecko.TelemetryContra
 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.db.URLMetadataTable;
 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 static org.mozilla.gecko.db.URLMetadataTable.TILE_IMAGE_URL_COLUMN;
@@ -198,16 +199,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);
                 }