Bug 1155237 - Part 1: Remove contextless access to NetworkUtils, causes NPE. r=rnewman, a=sledru
authorGarvan Keeley <gkeeley@mozilla.com>
Tue, 21 Apr 2015 10:45:03 -0400
changeset 260241 1ec2ee773b51
parent 260240 92a269ca564d
child 260242 645fc5aa6a49
push id724
push userryanvm@gmail.com
push date2015-04-23 01:08 +0000
treeherdermozilla-release@db41e8e267ed [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman, sledru
bugs1155237
milestone38.0
Bug 1155237 - Part 1: Remove contextless access to NetworkUtils, causes NPE. r=rnewman, a=sledru
mobile/android/stumbler/java/org/mozilla/mozstumbler/service/stumblerthread/StumblerService.java
mobile/android/stumbler/java/org/mozilla/mozstumbler/service/uploadthread/AsyncUploader.java
mobile/android/stumbler/java/org/mozilla/mozstumbler/service/uploadthread/UploadAlarmReceiver.java
mobile/android/stumbler/java/org/mozilla/mozstumbler/service/utils/NetworkUtils.java
--- a/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/stumblerthread/StumblerService.java
+++ b/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/stumblerthread/StumblerService.java
@@ -13,17 +13,16 @@ import java.io.IOException;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.mozilla.mozstumbler.service.AppGlobals;
 import org.mozilla.mozstumbler.service.Prefs;
 import org.mozilla.mozstumbler.service.stumblerthread.blocklist.WifiBlockListInterface;
 import org.mozilla.mozstumbler.service.stumblerthread.datahandling.DataStorageManager;
 import org.mozilla.mozstumbler.service.stumblerthread.scanners.ScanManager;
 import org.mozilla.mozstumbler.service.uploadthread.UploadAlarmReceiver;
-import org.mozilla.mozstumbler.service.utils.NetworkUtils;
 import org.mozilla.mozstumbler.service.utils.PersistentIntentService;
 
 // In stand-alone service mode (a.k.a passive scanning mode), this is created from PassiveServiceReceiver (by calling startService).
 // The StumblerService is a sticky unbound service in this usage.
 //
 public class StumblerService extends PersistentIntentService
         implements DataStorageManager.StorageIsEmptyTracker {
     private static final String LOG_TAG = AppGlobals.makeLogTag(StumblerService.class.getSimpleName());
@@ -113,17 +112,16 @@ public class StumblerService extends Per
     }
 
     // Previously this was done in onCreate(). Moved out of that so that in the passive standalone service
     // use (i.e. Fennec), init() can be called from this class's dedicated thread.
     // Safe to call more than once, ensure added code complies with that intent.
     protected void init() {
         // Ensure Prefs is created, so internal utility code can use getInstanceWithoutContext
         Prefs.getInstance(this);
-        NetworkUtils.createGlobalInstance(this);
         DataStorageManager.createGlobalInstance(this, this);
 
         mReporter.startup(this);
     }
 
     // Called from the main thread.
     @Override
     public void onCreate() {
--- a/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/uploadthread/AsyncUploader.java
+++ b/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/uploadthread/AsyncUploader.java
@@ -20,39 +20,43 @@ import org.mozilla.mozstumbler.service.u
 * it will return immediately, and SyncResult is null.
 *
 * Threading:
 * Uploads on a separate thread. ONLY DataStorageManager is thread-safe, do not call
 * preferences, do not call any code that isn't thread-safe. You will cause suffering.
 * An exception is made for AppGlobals.isDebug, a false reading is of no consequence. */
 public class AsyncUploader extends AsyncTask<Void, Void, SyncSummary> {
     private static final String LOG_TAG = AppGlobals.makeLogTag(AsyncUploader.class.getSimpleName());
-    private final UploadSettings mSettings;
+    private final AsyncUploadArgs mUploadArgs;
     private final Object mListenerLock = new Object();
     private AsyncUploaderListener mListener;
     private static final AtomicBoolean sIsUploading = new AtomicBoolean();
     private String mNickname;
 
     public interface AsyncUploaderListener {
         public void onUploadComplete(SyncSummary result);
         public void onUploadProgress();
     }
 
-    public static class UploadSettings {
+    public static class AsyncUploadArgs {
+        public final NetworkUtils mNetworkUtils;
         public final boolean mShouldIgnoreWifiStatus;
         public final boolean mUseWifiOnly;
-        public UploadSettings(boolean shouldIgnoreWifiStatus, boolean useWifiOnly) {
+        public AsyncUploadArgs(NetworkUtils networkUtils,
+                               boolean shouldIgnoreWifiStatus,
+                               boolean useWifiOnly) {
+            mNetworkUtils = networkUtils;
             mShouldIgnoreWifiStatus = shouldIgnoreWifiStatus;
             mUseWifiOnly = useWifiOnly;
         }
     }
 
-    public AsyncUploader(UploadSettings settings, AsyncUploaderListener listener) {
+    public AsyncUploader(AsyncUploadArgs args, AsyncUploaderListener listener) {
         mListener = listener;
-        mSettings = settings;
+        mUploadArgs = args;
     }
 
     public void setNickname(String name) {
         mNickname = name;
     }
 
     public void clearListener() {
         synchronized (mListenerLock) {
@@ -141,17 +145,18 @@ public class AsyncUploader extends Async
         }
     }
 
     private void uploadReports(AbstractCommunicator.SyncSummary syncResult, Runnable progressListener) {
         long uploadedObservations = 0;
         long uploadedCells = 0;
         long uploadedWifis = 0;
 
-        if (!mSettings.mShouldIgnoreWifiStatus && mSettings.mUseWifiOnly && !NetworkUtils.getInstance().isWifiAvailable()) {
+        if (!mUploadArgs.mShouldIgnoreWifiStatus && mUploadArgs.mUseWifiOnly &&
+               mUploadArgs.mNetworkUtils.isWifiAvailable()) {
             if (AppGlobals.isDebug) {
                 Log.d(LOG_TAG, "not on WiFi, not sending");
             }
             syncResult.numIoExceptions += 1;
             return;
         }
 
         Submitter submitter = new Submitter();
--- a/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/uploadthread/UploadAlarmReceiver.java
+++ b/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/uploadthread/UploadAlarmReceiver.java
@@ -71,21 +71,24 @@ public class UploadAlarmReceiver extends
                 long msPerWeek = 604800 * 1000;
                 if (currentTime - oldestMs > maxWeeks * msPerWeek) {
                     DataStorageManager.getInstance().deleteAll();
                     UploadAlarmReceiver.cancelAlarm(this, isRepeating);
                     return;
                 }
             }
 
-            if (NetworkUtils.getInstance().isWifiAvailable() &&
+            NetworkUtils networkUtils = new NetworkUtils(this);
+            if (networkUtils.isWifiAvailable() &&
                 !AsyncUploader.isUploading()) {
                 Log.d(LOG_TAG, "Alarm upload(), call AsyncUploader");
-                AsyncUploader.UploadSettings settings =
-                    new AsyncUploader.UploadSettings(Prefs.getInstance(this).getWifiScanAlways(), Prefs.getInstance(this).getUseWifiOnly());
+                AsyncUploader.AsyncUploadArgs settings =
+                    new AsyncUploader.AsyncUploadArgs(networkUtils,
+                            Prefs.getInstance(this).getWifiScanAlways(),
+                            Prefs.getInstance(this).getUseWifiOnly());
                 AsyncUploader uploader = new AsyncUploader(settings, null);
                 uploader.setNickname(Prefs.getInstance(this).getNickname());
                 uploader.execute();
                 // we could listen for completion and cancel, instead, cancel on next alarm when db empty
             }
         }
     }
 
@@ -125,9 +128,9 @@ public class UploadAlarmReceiver extends
         }
     }
 
     @Override
     public void onReceive(final Context context, Intent intent) {
         Intent startServiceIntent = new Intent(context, UploadAlarmService.class);
         context.startService(startServiceIntent);
     }
-}
\ No newline at end of file
+}
--- a/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/utils/NetworkUtils.java
+++ b/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/utils/NetworkUtils.java
@@ -9,28 +9,19 @@ import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.util.Log;
 import org.mozilla.mozstumbler.service.AppGlobals;
 
 public final class NetworkUtils {
     private static final String LOG_TAG = AppGlobals.makeLogTag(NetworkUtils.class.getSimpleName());
 
     ConnectivityManager mConnectivityManager;
-    static NetworkUtils sInstance;
 
-    /* Created at startup by app, or service, using a context. */
-    static public void createGlobalInstance(Context context) {
-        sInstance = new NetworkUtils();
-        sInstance.mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
-    }
-
-    /* If accessed before singleton instantiation will abort. */
-    public static NetworkUtils getInstance() {
-        assert(sInstance != null);
-        return sInstance;
+    public NetworkUtils(Context context) {
+        mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     }
 
     public synchronized boolean isWifiAvailable() {
         if (mConnectivityManager == null) {
             Log.e(LOG_TAG, "ConnectivityManager is null!");
             return false;
         }