Bug 1241810 - Add background service and action to check feeds for updates. r=mcomella

MozReview-Commit-ID: 5hD4I65OGX3

--HG--
extra : rebase_source : d0e5bb5fd336de8f73ddef0bf1dc603edb5e2853
extra : histedit_source : f97137b389593ac8a3c71167b4f0721c15ce4497
This commit is contained in:
Sebastian Kaspari 2016-02-24 14:38:44 -08:00
Родитель 10e2d0b90f
Коммит b54b9b35e9
6 изменённых файлов: 170 добавлений и 0 удалений

Просмотреть файл

@ -341,6 +341,11 @@
android:name="org.mozilla.gecko.dlc.DownloadContentService">
</service>
<service
android:exported="false"
android:name="org.mozilla.gecko.feeds.FeedService">
</service>
<service
android:name="org.mozilla.gecko.telemetry.TelemetryUploadService"
android:exported="false"/>

Просмотреть файл

@ -0,0 +1,62 @@
/* -*- 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;
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.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_CHECK = AppConstants.ANDROID_PACKAGE_NAME + ".FEEDS.CHECK";
private SubscriptionStorage storage;
public FeedService() {
super(LOGTAG);
}
@Override
public void onCreate() {
super.onCreate();
storage = new SubscriptionStorage(getApplicationContext());
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent == null) {
return;
}
if (!SwitchBoard.isInExperiment(this, Experiments.CONTENT_NOTIFICATIONS)) {
Log.d(LOGTAG, "Not in content notifications experiment. Skipping.");
return;
}
switch (intent.getAction()) {
case ACTION_CHECK:
new CheckAction(this, storage).perform();
break;
default:
Log.e(LOGTAG, "Unknown action: " + intent.getAction());
}
storage.persistChanges();
}
}

Просмотреть файл

@ -0,0 +1,97 @@
/* -*- 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.app.Notification;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import org.mozilla.gecko.BrowserApp;
import org.mozilla.gecko.R;
import org.mozilla.gecko.feeds.FeedFetcher;
import org.mozilla.gecko.feeds.parser.Feed;
import org.mozilla.gecko.feeds.subscriptions.FeedSubscription;
import org.mozilla.gecko.feeds.subscriptions.SubscriptionStorage;
import java.util.List;
/**
* CheckAction: Check if feeds we subscribed to have new content available.
*/
public class CheckAction {
private static final String LOGTAG = "FeedCheckAction";
private Context context;
private SubscriptionStorage storage;
public CheckAction(Context context, SubscriptionStorage storage) {
this.context = context;
this.storage = storage;
}
public void perform() {
final List<FeedSubscription> subscriptions = storage.getSubscriptions();
Log.d(LOGTAG, "Checking feeds for updates (" + subscriptions.size() + " feeds) ..");
for (FeedSubscription subscription : subscriptions) {
Log.i(LOGTAG, "Checking feed: " + subscription.getFeedTitle());
FeedFetcher.FeedResponse response = fetchFeed(subscription);
if (response == null) {
continue;
}
if (subscription.isNewer(response)) {
Log.d(LOGTAG, "* Feed has changed. New item: " + response.feed.getLastItem().getTitle());
storage.updateSubscription(subscription, response);
notify(response.feed);
}
}
}
private void notify(Feed feed) {
NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle()
.bigText(feed.getLastItem().getTitle())
.setBigContentTitle(feed.getTitle())
.setSummaryText(feed.getLastItem().getURL());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setComponent(new ComponentName(context, BrowserApp.class));
intent.setData(Uri.parse(feed.getLastItem().getURL()));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_status_logo)
.setContentTitle(feed.getTitle())
.setContentText(feed.getLastItem().getTitle())
.setStyle(style)
.setColor(ContextCompat.getColor(context, R.color.link_blue))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
NotificationManagerCompat.from(context).notify(R.id.websiteContentNotification, notification);
}
private FeedFetcher.FeedResponse fetchFeed(FeedSubscription subscription) {
return FeedFetcher.fetchAndParseFeedIfModified(
subscription.getFeedUrl(),
subscription.getETag(),
subscription.getLastModified()
);
}
}

Просмотреть файл

@ -31,6 +31,9 @@ public class Experiments {
// Show a system notification linking to a "What's New" page on app update.
public static final String WHATSNEW_NOTIFICATION = "whatsnew-notification";
// Subscribe to known, bookmarked sites and show a notification if new content is available.
public static final String CONTENT_NOTIFICATIONS = "content-notifications";
// Onboarding: "Features and Story". These experiments are determined
// on the client, they are not part of the server config.
public static final String ONBOARDING2_A = "onboarding2-a"; // Control: Single (blue) welcome screen

Просмотреть файл

@ -273,7 +273,9 @@ gbjar.sources += ['java/org/mozilla/gecko/' + x for x in [
'favicons/LoadFaviconTask.java',
'favicons/OnFaviconLoadedListener.java',
'favicons/RemoteFavicon.java',
'feeds/action/CheckAction.java',
'feeds/FeedFetcher.java',
'feeds/FeedService.java',
'feeds/parser/Feed.java',
'feeds/parser/Item.java',
'feeds/parser/SimpleFeedParser.java',

Просмотреть файл

@ -17,5 +17,6 @@
<item type="id" name="pref_header_privacy"/>
<item type="id" name="pref_header_search"/>
<item type="id" name="updateServicePermissionNotification" />
<item type="id" name="websiteContentNotification" />
</resources>