Bug 1241810 - Do not do anything if not connected to a network or network is metered. r=mcomella
MozReview-Commit-ID: LilXNpxQhyI
--- a/mobile/android/base/java/org/mozilla/gecko/feeds/FeedService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/feeds/FeedService.java
@@ -3,16 +3,19 @@
* 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.v4.net.ConnectivityManagerCompat;
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.EnrollAction;
import org.mozilla.gecko.feeds.action.SetupAction;
@@ -66,16 +69,23 @@ public class FeedService extends IntentS
return;
}
if (!SwitchBoard.isInExperiment(this, Experiments.CONTENT_NOTIFICATIONS)) {
Log.d(LOGTAG, "Not in content notifications experiment. Skipping.");
return;
}
+ if (!isConnectedToNetwork() || isActiveNetworkMetered()) {
+ // 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;
+ }
+
switch (intent.getAction()) {
case ACTION_SETUP:
new SetupAction(this).perform();
break;
case ACTION_SUBSCRIBE:
new SubscribeAction(storage).perform(intent);
break;
@@ -95,9 +105,20 @@ public class FeedService extends IntentS
default:
Log.e(LOGTAG, "Unknown action: " + intent.getAction());
}
storage.persistChanges();
FeedAlarmReceiver.completeWakefulIntent(intent);
}
+
+ private boolean isConnectedToNetwork() {
+ ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
+ NetworkInfo networkInfo = manager.getActiveNetworkInfo();
+ return networkInfo != null && networkInfo.isConnected();
+ }
+
+ private boolean isActiveNetworkMetered() {
+ return ConnectivityManagerCompat.isActiveNetworkMetered(
+ (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE));
+ }
}