Bug 1241810 - Add WithdrawAction for removing subscriptions of bookmarks that do not exist anymore. r?mcomella draft
authorSebastian Kaspari <s.kaspari@gmail.com>
Wed, 24 Feb 2016 15:20:12 -0800
changeset 335177 f9286e50f22eec067e5620e6c7e10be03ef6526e
parent 335176 b6991c3f1fa2fc076b0e6bfe132d5ae5e92ef827
child 335178 c08a3bd83b516a959be1f163b1fd14d9b9ac929f
push id11746
push users.kaspari@gmail.com
push dateSat, 27 Feb 2016 23:17:50 +0000
reviewersmcomella
bugs1241810
milestone47.0a1
Bug 1241810 - Add WithdrawAction for removing subscriptions of bookmarks that do not exist anymore. r?mcomella MozReview-Commit-ID: 9jFnIgLDgws
mobile/android/base/java/org/mozilla/gecko/db/BrowserDB.java
mobile/android/base/java/org/mozilla/gecko/db/LocalBrowserDB.java
mobile/android/base/java/org/mozilla/gecko/db/StubBrowserDB.java
mobile/android/base/java/org/mozilla/gecko/feeds/FeedService.java
mobile/android/base/java/org/mozilla/gecko/feeds/action/WithdrawAction.java
mobile/android/base/moz.build
--- a/mobile/android/base/java/org/mozilla/gecko/db/BrowserDB.java
+++ b/mobile/android/base/java/org/mozilla/gecko/db/BrowserDB.java
@@ -114,16 +114,17 @@ public interface BrowserDB {
 
     public abstract boolean isBookmark(ContentResolver cr, String uri);
     public abstract boolean addBookmark(ContentResolver cr, String title, String uri);
     public abstract Cursor getBookmarkForUrl(ContentResolver cr, String url);
     public abstract Cursor getBookmarksForPartialUrl(ContentResolver cr, String partialUrl);
     public abstract void removeBookmarksWithURL(ContentResolver cr, String uri);
     public abstract void registerBookmarkObserver(ContentResolver cr, ContentObserver observer);
     public abstract void updateBookmark(ContentResolver cr, int id, String uri, String title, String keyword);
+    public abstract boolean hasBookmarkWithGuid(ContentResolver cr, String guid);
 
     /**
      * Can return <code>null</code>.
      */
     public abstract Cursor getBookmarksInFolder(ContentResolver cr, long folderId);
 
     /**
      * Get the favicon from the database, if any, associated with the given favicon URL. (That is,
--- a/mobile/android/base/java/org/mozilla/gecko/db/LocalBrowserDB.java
+++ b/mobile/android/base/java/org/mozilla/gecko/db/LocalBrowserDB.java
@@ -1054,16 +1054,33 @@ public class LocalBrowserDB implements B
         values.put(Bookmarks.DATE_MODIFIED, System.currentTimeMillis());
 
         cr.update(mBookmarksUriWithProfile,
                   values,
                   Bookmarks._ID + " = ?",
                   new String[] { String.valueOf(id) });
     }
 
+    @Override
+    public boolean hasBookmarkWithGuid(ContentResolver cr, String guid) {
+        Cursor c = cr.query(bookmarksUriWithLimit(1),
+                new String[] { Bookmarks.GUID },
+                Bookmarks.GUID + " = ?",
+                new String[] { guid },
+                null);
+
+        try {
+            return c != null && c.getCount() > 0;
+        } finally {
+            if (c != null) {
+                c.close();
+            }
+        }
+    }
+
     /**
      * Get the favicon from the database, if any, associated with the given favicon URL. (That is,
      * the URL of the actual favicon image, not the URL of the page with which the favicon is associated.)
      * @param cr The ContentResolver to use.
      * @param faviconURL The URL of the favicon to fetch from the database.
      * @return The decoded Bitmap from the database, if any. null if none is stored.
      */
     @Override
--- a/mobile/android/base/java/org/mozilla/gecko/db/StubBrowserDB.java
+++ b/mobile/android/base/java/org/mozilla/gecko/db/StubBrowserDB.java
@@ -375,16 +375,21 @@ public class StubBrowserDB implements Br
         return null;
     }
 
     @Override
     public Cursor getBookmarksForPartialUrl(ContentResolver cr, String partialUrl) {
         return null;
     }
 
+    @Override
+    public boolean hasBookmarkWithGuid(ContentResolver cr, String guid) {
+        return false;
+    }
+
     public void setSuggestedSites(SuggestedSites suggestedSites) {
     }
 
     public boolean hasSuggestedImageUrl(String url) {
         return false;
     }
 
     public String getSuggestedImageUrlForUrl(String url) {
--- a/mobile/android/base/java/org/mozilla/gecko/feeds/FeedService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/feeds/FeedService.java
@@ -11,28 +11,30 @@ import android.content.Intent;
 import android.util.Log;
 
 import com.keepsafe.switchboard.SwitchBoard;
 
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.feeds.action.CheckAction;
 import org.mozilla.gecko.feeds.action.EnrollAction;
 import org.mozilla.gecko.feeds.action.SubscribeAction;
+import org.mozilla.gecko.feeds.action.WithdrawAction;
 import org.mozilla.gecko.feeds.subscriptions.SubscriptionStorage;
 import org.mozilla.gecko.util.Experiments;
 
 /**
  * Background service for subscribing to and checking website feeds to notify the user about updates.
  */
 public class FeedService extends IntentService {
     private static final String LOGTAG = "GeckoFeedService";
 
     public static final String ACTION_SUBSCRIBE = AppConstants.ANDROID_PACKAGE_NAME + ".FEEDS.SUBSCRIBE";
     public static final String ACTION_CHECK = AppConstants.ANDROID_PACKAGE_NAME + ".FEEDS.CHECK";
     public static final String ACTION_ENROLL = AppConstants.ANDROID_PACKAGE_NAME + ".FEEDS.ENROLL";
+    public static final String ACTION_WITHDRAW = AppConstants.ANDROID_PACKAGE_NAME + ".FEEDS.WITHDRAW";
 
     public static void subscribe(Context context, String guid, String feedUrl) {
         Intent intent = new Intent(context, FeedService.class);
         intent.setAction(ACTION_SUBSCRIBE);
         intent.putExtra(SubscribeAction.EXTRA_GUID, guid);
         intent.putExtra(SubscribeAction.EXTRA_FEED_URL, feedUrl);
         context.startService(intent);
     }
@@ -69,15 +71,19 @@ public class FeedService extends IntentS
             case ACTION_CHECK:
                 new CheckAction(this, storage).perform();
                 break;
 
             case ACTION_ENROLL:
                 new EnrollAction(this).perform();
                 break;
 
+            case ACTION_WITHDRAW:
+                new WithdrawAction(this, storage).perform();
+                break;
+
             default:
                 Log.e(LOGTAG, "Unknown action: " + intent.getAction());
         }
 
         storage.persistChanges();
     }
 }
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/feeds/action/WithdrawAction.java
@@ -0,0 +1,51 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * 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.feeds.action;
+
+import android.content.Context;
+import android.util.Log;
+
+import org.mozilla.gecko.GeckoProfile;
+import org.mozilla.gecko.db.BrowserDB;
+import org.mozilla.gecko.feeds.subscriptions.FeedSubscription;
+import org.mozilla.gecko.feeds.subscriptions.SubscriptionStorage;
+
+import java.util.List;
+
+/**
+ *
+ */
+public class WithdrawAction {
+    private static final String LOGTAG = "FeedWithdrawAction";
+
+    private Context context;
+    private SubscriptionStorage storage;
+
+    public WithdrawAction(Context context, SubscriptionStorage storage) {
+        this.context = context;
+        this.storage = storage;
+    }
+
+    public void perform() {
+        BrowserDB db = GeckoProfile.get(context).getDB();
+
+        List<FeedSubscription> subscriptions = storage.getSubscriptions();
+
+        Log.d(LOGTAG, "Checking " + subscriptions.size() + " subscriptions");
+
+        for (FeedSubscription subscription : subscriptions) {
+            if (!db.hasBookmarkWithGuid(context.getContentResolver(), subscription.getBookmarkGUID())) {
+                unsubscribe(subscription);
+            }
+        }
+    }
+
+    private void unsubscribe(FeedSubscription subscription) {
+        Log.d(LOGTAG, "Unsubscribing from: (" + subscription.getBookmarkGUID() + ") " + subscription.getFeedUrl());
+
+        storage.removeSubscription(subscription);
+    }
+}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -273,16 +273,17 @@ gbjar.sources += ['java/org/mozilla/geck
     'favicons/decoders/LoadFaviconResult.java',
     'favicons/Favicons.java',
     'favicons/LoadFaviconTask.java',
     'favicons/OnFaviconLoadedListener.java',
     'favicons/RemoteFavicon.java',
     'feeds/action/CheckAction.java',
     'feeds/action/EnrollAction.java',
     'feeds/action/SubscribeAction.java',
+    'feeds/action/WithdrawAction.java',
     'feeds/FeedFetcher.java',
     'feeds/FeedService.java',
     'feeds/knownsites/KnownSite.java',
     'feeds/knownsites/KnownSiteBlogger.java',
     'feeds/knownsites/KnownSiteMedium.java',
     'feeds/parser/Feed.java',
     'feeds/parser/Item.java',
     'feeds/parser/SimpleFeedParser.java',