Bug 1241810 - Add action for subscribing to new feeds. r?mcomella draft
authorSebastian Kaspari <s.kaspari@gmail.com>
Wed, 24 Feb 2016 14:47:29 -0800
changeset 335787 e8e8b9d50bae2152085e209e12c81bd016f8471f
parent 335786 ba7be9bbd3c13a4354e102106c58e3e4cd90f1cd
child 335788 251b333d918afa2da4d659da6f1b7c80a57e37f9
push id11875
push users.kaspari@gmail.com
push dateTue, 01 Mar 2016 15:14:45 +0000
reviewersmcomella
bugs1241810
milestone47.0a1
Bug 1241810 - Add action for subscribing to new feeds. r?mcomella MozReview-Commit-ID: 9H4qsEe6P4b
mobile/android/base/java/org/mozilla/gecko/feeds/FeedService.java
mobile/android/base/java/org/mozilla/gecko/feeds/action/SubscribeAction.java
mobile/android/base/moz.build
--- a/mobile/android/base/java/org/mozilla/gecko/feeds/FeedService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/feeds/FeedService.java
@@ -8,25 +8,27 @@ package org.mozilla.gecko.feeds;
 import android.app.IntentService;
 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.SubscribeAction;
 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";
 
     private SubscriptionStorage storage;
 
     public FeedService() {
         super(LOGTAG);
     }
 
@@ -44,16 +46,20 @@ public class FeedService extends IntentS
         }
 
         if (!SwitchBoard.isInExperiment(this, Experiments.CONTENT_NOTIFICATIONS)) {
             Log.d(LOGTAG, "Not in content notifications experiment. Skipping.");
             return;
         }
 
         switch (intent.getAction()) {
+            case ACTION_SUBSCRIBE:
+                new SubscribeAction(storage).perform(intent);
+                break;
+
             case ACTION_CHECK:
                 new CheckAction(this, storage).perform();
                 break;
 
             default:
                 Log.e(LOGTAG, "Unknown action: " + intent.getAction());
         }
 
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/feeds/action/SubscribeAction.java
@@ -0,0 +1,63 @@
+/* -*- 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.Intent;
+import android.os.Bundle;
+import android.util.Log;
+
+import org.mozilla.gecko.feeds.FeedFetcher;
+import org.mozilla.gecko.feeds.subscriptions.FeedSubscription;
+import org.mozilla.gecko.feeds.subscriptions.SubscriptionStorage;
+
+import java.util.UUID;
+
+/**
+ * SubscribeAction: Try to fetch a feed and create a subscription if successful.
+ */
+public class SubscribeAction {
+    private static final String LOGTAG = "FeedSubscribeAction";
+
+    public static final String EXTRA_GUID = "guid";
+    public static final String EXTRA_FEED_URL = "feed_url";
+
+    private SubscriptionStorage storage;
+
+    public SubscribeAction(SubscriptionStorage storage) {
+        this.storage = storage;
+    }
+
+    public void perform(Intent intent) {
+        Log.d(LOGTAG, "Subscribing to feed..");
+
+        final Bundle extras = intent.getExtras();
+
+        // TODO: Using a random UUID as fallback just so that I can subscribe for things that are not bookmarks (testing)
+        final String guid = extras.getString(EXTRA_GUID, UUID.randomUUID().toString());
+        final String feedUrl = intent.getStringExtra(EXTRA_FEED_URL);
+
+        if (storage.hasSubscriptionForBookmark(guid)) {
+            Log.d(LOGTAG, "Already subscribed to " + feedUrl + ". Skipping.");
+            return;
+        }
+
+        subscribe(guid, feedUrl);
+    }
+
+    private void subscribe(String guid, String feedUrl) {
+        FeedFetcher.FeedResponse response = FeedFetcher.fetchAndParseFeed(feedUrl);
+        if (response == null) {
+            Log.w(LOGTAG, String.format("Could not fetch feed (%s). Not subscribing for now.", feedUrl));
+            return;
+        }
+
+        Log.d(LOGTAG, "Subscribing to feed: " + response.feed.getTitle());
+        Log.d(LOGTAG, "               GUID: " + guid);
+        Log.d(LOGTAG, "          Last item: " + response.feed.getLastItem().getTitle());
+
+        storage.addSubscription(FeedSubscription.create(guid, feedUrl, response));
+    }
+}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -271,16 +271,17 @@ gbjar.sources += ['java/org/mozilla/geck
     'favicons/decoders/ICODecoder.java',
     'favicons/decoders/IconDirectoryEntry.java',
     'favicons/decoders/LoadFaviconResult.java',
     'favicons/Favicons.java',
     'favicons/LoadFaviconTask.java',
     'favicons/OnFaviconLoadedListener.java',
     'favicons/RemoteFavicon.java',
     'feeds/action/CheckAction.java',
+    'feeds/action/SubscribeAction.java',
     'feeds/FeedFetcher.java',
     'feeds/FeedService.java',
     'feeds/parser/Feed.java',
     'feeds/parser/Item.java',
     'feeds/parser/SimpleFeedParser.java',
     'feeds/subscriptions/FeedSubscription.java',
     'feeds/subscriptions/SubscriptionStorage.java',
     'FilePicker.java',