Bug 1175355 - Add BroadcastScanneredHandler to CellScanner. r=nalexander
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 24 Jun 2015 17:20:42 -0700
changeset 281160 54453e0bbdc828ca3963d769b0b53912afd1e019
parent 281159 58334ada143a33f9e4043c12a17ef2cd98c26ae3
child 281161 5b03881a38de08924bd0926637e4b9882aeb3abc
push id4932
push userjlund@mozilla.com
push dateMon, 10 Aug 2015 18:23:06 +0000
treeherdermozilla-beta@6dd5a4f5f745 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnalexander
bugs1175355
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1175355 - Add BroadcastScanneredHandler to CellScanner. r=nalexander
mobile/android/stumbler/java/org/mozilla/mozstumbler/service/stumblerthread/scanners/cellscanner/CellScanner.java
--- a/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/stumblerthread/scanners/cellscanner/CellScanner.java
+++ b/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/stumblerthread/scanners/cellscanner/CellScanner.java
@@ -12,16 +12,17 @@ import android.os.Handler;
 import android.os.Message;
 import android.support.v4.content.LocalBroadcastManager;
 import android.telephony.TelephonyManager;
 import android.util.Log;
 
 import org.mozilla.mozstumbler.service.AppGlobals;
 import org.mozilla.mozstumbler.service.AppGlobals.ActiveOrPassiveStumbling;
 
+import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.atomic.AtomicBoolean;
 import org.mozilla.mozstumbler.service.AppGlobals.ActiveOrPassiveStumbling;
@@ -67,23 +68,17 @@ public class CellScanner {
         if (mCellScanTimer != null) {
             return;
         }
 
         LocalBroadcastManager.getInstance(mContext).registerReceiver(mReportFlushedReceiver,
                 new IntentFilter(Reporter.ACTION_NEW_BUNDLE));
 
         // This is to ensure the broadcast happens from the same thread the CellScanner start() is on
-        mBroadcastScannedHandler = new Handler() {
-            @Override
-            public void handleMessage(Message msg) {
-                Intent intent = (Intent) msg.obj;
-                LocalBroadcastManager.getInstance(mContext).sendBroadcastSync(intent);
-            }
-        };
+        mBroadcastScannedHandler = new BroadcastScannedHandler(this);
 
         mCellScannerImplementation.start();
 
         mCellScanTimer = new Timer();
 
         mCellScanTimer.schedule(new TimerTask() {
             int mPassiveScanCount;
             @Override
@@ -154,9 +149,30 @@ public class CellScanner {
     }
 
     private class ReportFlushedReceiver extends BroadcastReceiver {
         @Override
         public void onReceive(Context c, Intent i) {
             mReportWasFlushed.set(true);
         }
     }
+
+    // Note: this reimplements org.mozilla.gecko.util.WeakReferenceHandler because it's not available here.
+    private static class BroadcastScannedHandler extends Handler {
+        private WeakReference<CellScanner> mTarget;
+
+        public BroadcastScannedHandler(final CellScanner that) {
+            super();
+            mTarget = new WeakReference<>(that);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            final CellScanner that = mTarget.get();
+            if (that == null) {
+                return;
+            }
+
+            final Intent intent = (Intent) msg.obj;
+            LocalBroadcastManager.getInstance(that.mContext).sendBroadcastSync(intent);
+        }
+    };
 }