Bug 1241810 - Do not do anything if not connected to a network or network is metered. r=mcomella
authorSebastian Kaspari <s.kaspari@gmail.com>
Wed, 24 Feb 2016 16:10:47 -0800
changeset 290379 9cb1ee777a66bce0f353a7527e16ef365371a859
parent 290378 9f74cd8ed6144b740ae03163130a68c0f5bcae3d
child 290380 fb1c859114bd5dc4f0031aaa7a9cc04246e221b8
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella
bugs1241810
milestone48.0a1
Bug 1241810 - Do not do anything if not connected to a network or network is metered. r=mcomella MozReview-Commit-ID: LilXNpxQhyI
mobile/android/base/java/org/mozilla/gecko/feeds/FeedService.java
--- 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));
+    }
 }