Bug 1261836 - Content notifications: Check if new content is already in history. r=mcomella,mfinkle draft
authorSebastian Kaspari <s.kaspari@gmail.com>
Wed, 13 Apr 2016 13:01:52 +0200
changeset 354340 4613cd1f70e3e40f4ec3a8a87ed653c91e1eab29
parent 354339 dff5be5628175185fa736bed9bf789fb2ff42504
child 518978 dada9e6e2ba8eb99f8097ec79bfa381f001137ba
push id16051
push users.kaspari@gmail.com
push dateWed, 20 Apr 2016 18:09:37 +0000
reviewersmcomella, mfinkle
bugs1261836
milestone48.0a1
Bug 1261836 - Content notifications: Check if new content is already in history. r=mcomella,mfinkle If the URL of new content is already in the user's history then we won't show a notification for it. MozReview-Commit-ID: B26SBvXOnxY
mobile/android/base/java/org/mozilla/gecko/feeds/action/CheckForUpdatesAction.java
--- a/mobile/android/base/java/org/mozilla/gecko/feeds/action/CheckForUpdatesAction.java
+++ b/mobile/android/base/java/org/mozilla/gecko/feeds/action/CheckForUpdatesAction.java
@@ -20,16 +20,17 @@ import android.text.format.DateFormat;
 
 import org.json.JSONException;
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.BrowserApp;
 import org.mozilla.gecko.GeckoApp;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
+import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.UrlAnnotations;
 import org.mozilla.gecko.feeds.FeedFetcher;
 import org.mozilla.gecko.feeds.FeedService;
 import org.mozilla.gecko.feeds.parser.Feed;
 import org.mozilla.gecko.feeds.subscriptions.FeedSubscription;
 import org.mozilla.gecko.preferences.GeckoPreferences;
 import org.mozilla.gecko.util.StringUtils;
@@ -69,17 +70,28 @@ public class CheckForUpdatesAction exten
         }
 
         try {
             while (cursor.moveToNext()) {
                 FeedSubscription subscription = FeedSubscription.fromCursor(cursor);
 
                 FeedFetcher.FeedResponse response = checkFeedForUpdates(subscription);
                 if (response != null) {
-                    updatedFeeds.add(response.feed);
+                    final Feed feed = response.feed;
+
+                    if (!hasBeenVisited(browserDB, feed.getLastItem().getURL())) {
+                        // Only notify about this update if the last item hasn't been visited yet.
+                        updatedFeeds.add(feed);
+                    } else {
+                        Telemetry.startUISession(TelemetryContract.Session.EXPERIMENT, FeedService.getEnabledExperiment(context));
+                        Telemetry.sendUIEvent(TelemetryContract.Event.CANCEL,
+                                TelemetryContract.Method.SERVICE,
+                                "content_update");
+                        Telemetry.stopUISession(TelemetryContract.Session.EXPERIMENT, FeedService.getEnabledExperiment(context));
+                    }
 
                     urlAnnotations.updateFeedSubscription(resolver, subscription);
                 }
             }
         } catch (JSONException e) {
             log("Could not deserialize subscription", e);
         } finally {
             cursor.close();
@@ -103,16 +115,39 @@ public class CheckForUpdatesAction exten
 
             return response;
 
         }
 
         return null;
     }
 
+    /**
+     * Returns true if this URL has been visited before.
+     *
+     * We do an exact match. So this can fail if the feed uses a different URL and redirects to
+     * content. But it's better than no checks at all.
+     */
+    private boolean hasBeenVisited(final BrowserDB browserDB, final String url) {
+        final Cursor cursor = browserDB.getHistoryForURL(context.getContentResolver(), url);
+        if (cursor == null) {
+            return false;
+        }
+
+        try {
+            if (cursor.moveToFirst()) {
+                return cursor.getInt(cursor.getColumnIndex(BrowserContract.History.VISITS)) > 0;
+            }
+        } finally {
+            cursor.close();
+        }
+
+        return false;
+    }
+
     private void showNotification(List<Feed> updatedFeeds) {
         final int feedCount = updatedFeeds.size();
         if (feedCount == 0) {
             return;
         }
 
         if (feedCount == 1) {
             showNotificationForSingleUpdate(updatedFeeds.get(0));