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
--- 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));