1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* -*- 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.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.Nullable;
import android.support.v4.net.ConnectivityManagerCompat;
import android.util.Log;
import com.keepsafe.switchboard.SwitchBoard;
import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.GeckoProfile;
import org.mozilla.gecko.GeckoSharedPrefs;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.feeds.action.FeedAction;
import org.mozilla.gecko.feeds.action.CheckForUpdatesAction;
import org.mozilla.gecko.feeds.action.EnrollSubscriptionsAction;
import org.mozilla.gecko.feeds.action.SetupAlarmsAction;
import org.mozilla.gecko.feeds.action.SubscribeToFeedAction;
import org.mozilla.gecko.feeds.action.WithdrawSubscriptionsAction;
import org.mozilla.gecko.preferences.GeckoPreferences;
import org.mozilla.gecko.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_SETUP = AppConstants.ANDROID_PACKAGE_NAME + ".FEEDS.SETUP";
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 setup(Context context) {
Intent intent = new Intent(context, FeedService.class);
intent.setAction(ACTION_SETUP);
context.startService(intent);
}
public static void subscribe(Context context, String feedUrl) {
Intent intent = new Intent(context, FeedService.class);
intent.setAction(ACTION_SUBSCRIBE);
intent.putExtra(SubscribeToFeedAction.EXTRA_FEED_URL, feedUrl);
context.startService(intent);
}
public FeedService() {
super(LOGTAG);
}
private BrowserDB browserDB;
@Override
public void onCreate() {
super.onCreate();
browserDB = BrowserDB.from(this);
}
@Override
protected void onHandleIntent(Intent intent) {
try {
if (intent == null) {
return;
}
Log.d(LOGTAG, "Service started with action: " + intent.getAction());
if (!isInExperiment(this)) {
Log.d(LOGTAG, "Not in content notifications experiment. Skipping.");
return;
}
FeedAction action = createActionForIntent(intent);
if (action == null) {
Log.d(LOGTAG, "No action to process");
return;
}
if (action.requiresPreferenceEnabled() && !isPreferenceEnabled()) {
Log.d(LOGTAG, "Preference is disabled. Skipping.");
return;
}
if (action.requiresNetwork() && !isConnectedToUnmeteredNetwork()) {
// For now just skip if we are not connected or the network is metered. We do not want
// to use precious mobile traffic.
Log.d(LOGTAG, "Not connected to a network or network is metered. Skipping.");
return;
}
action.perform(browserDB, intent);
} finally {
FeedAlarmReceiver.completeWakefulIntent(intent);
}
Log.d(LOGTAG, "Done.");
}
@Nullable
private FeedAction createActionForIntent(Intent intent) {
final Context context = getApplicationContext();
switch (intent.getAction()) {
case ACTION_SETUP:
return new SetupAlarmsAction(context);
case ACTION_SUBSCRIBE:
return new SubscribeToFeedAction(context);
case ACTION_CHECK:
return new CheckForUpdatesAction(context);
case ACTION_ENROLL:
return new EnrollSubscriptionsAction(context);
case ACTION_WITHDRAW:
return new WithdrawSubscriptionsAction(context);
default:
throw new AssertionError("Unknown action: " + intent.getAction());
}
}
private boolean isConnectedToUnmeteredNetwork() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
return false;
}
return !ConnectivityManagerCompat.isActiveNetworkMetered(manager);
}
public static boolean isInExperiment(Context context) {
return SwitchBoard.isInExperiment(context, Experiments.CONTENT_NOTIFICATIONS_12HRS) ||
SwitchBoard.isInExperiment(context, Experiments.CONTENT_NOTIFICATIONS_5PM) ||
SwitchBoard.isInExperiment(context, Experiments.CONTENT_NOTIFICATIONS_8AM);
}
public static String getEnabledExperiment(Context context) {
String experiment = null;
if (SwitchBoard.isInExperiment(context, Experiments.CONTENT_NOTIFICATIONS_12HRS)) {
experiment = Experiments.CONTENT_NOTIFICATIONS_12HRS;
} else if (SwitchBoard.isInExperiment(context, Experiments.CONTENT_NOTIFICATIONS_8AM)) {
experiment = Experiments.CONTENT_NOTIFICATIONS_8AM;
} else if (SwitchBoard.isInExperiment(context, Experiments.CONTENT_NOTIFICATIONS_5PM)) {
experiment = Experiments.CONTENT_NOTIFICATIONS_5PM;
}
return experiment;
}
private boolean isPreferenceEnabled() {
return GeckoSharedPrefs.forApp(this).getBoolean(GeckoPreferences.PREFS_NOTIFICATIONS_CONTENT, true);
}
}
|