Bug 1241810 - Do not do anything if not connected to a network or network is metered. r=mcomella draft
authorSebastian Kaspari <s.kaspari@gmail.com>
Wed, 24 Feb 2016 16:10:47 -0800
changeset 343933 3ff13f5f955fa5f2958a183b02b6816be8d99574
parent 343932 1dbd295f975eff15ba374de793c4d13e6203bcb1
child 343934 e5205c505298e79f1e787f6cc5e7407a84449b07
push id13713
push users.kaspari@gmail.com
push dateWed, 23 Mar 2016 15:10:41 +0000
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));
+    }
 }