diff options
author | Quang Luong <qal@google.com> | 2020-05-04 10:39:39 -0700 |
---|---|---|
committer | Quang Luong <qal@google.com> | 2020-05-04 13:08:03 -0700 |
commit | 53d7744eb5d911c34a42316b527fd87db3f7f965 (patch) | |
tree | 5a1301755fe5ead5cb27ffba8f739da2661fa744 | |
parent | 329152acb062ea374bb5181d9eb8592cfe80ae42 (diff) |
[WifiTrackerLib] Make scanner run on single thread
Scanner should run on single thread to prevent race condition where
onStop() is called on the main thread while a msg is being processed on
the worker thread. This would result in a looping msg being sent after
the msg queue is cleared, which keeps the scan loop going even after
onStop().
Bug: 155425309
Test: build
Change-Id: Iecbab79b511bddf30515de27a60ff8eaf431d10e
-rw-r--r-- | libs/WifiTrackerLib/src/com/android/wifitrackerlib/BaseWifiTracker.java | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/BaseWifiTracker.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/BaseWifiTracker.java index 2f5b56eea..18611e242 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/BaseWifiTracker.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/BaseWifiTracker.java @@ -86,6 +86,7 @@ public class BaseWifiTracker implements LifecycleObserver { return BaseWifiTracker.sVerboseLogging; } + // Registered on the worker thread private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override @WorkerThread @@ -224,9 +225,9 @@ public class BaseWifiTracker implements LifecycleObserver { mWifiNetworkScoreCache, NetworkScoreManager.SCORE_FILTER_SCAN_RESULTS); if (mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) { - mScanner.start(); + mWorkerHandler.post(mScanner::start); } else { - mScanner.stop(); + mWorkerHandler.post(mScanner::stop); } mWorkerHandler.post(this::handleOnStart); } @@ -237,7 +238,7 @@ public class BaseWifiTracker implements LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_STOP) @MainThread public void onStop() { - mScanner.stop(); + mWorkerHandler.post(mScanner::stop); mContext.unregisterReceiver(mBroadcastReceiver); mConnectivityManager.unregisterNetworkCallback(mNetworkCallback); mNetworkScoreManager.unregisterNetworkScoreCache(NetworkKey.TYPE_WIFI, @@ -335,6 +336,7 @@ public class BaseWifiTracker implements LifecycleObserver { /** * Scanner to handle starting scans every SCAN_INTERVAL_MILLIS */ + @WorkerThread private class Scanner extends Handler { private static final int SCAN_RETRY_TIMES = 3; @@ -344,15 +346,13 @@ public class BaseWifiTracker implements LifecycleObserver { super(looper); } - @AnyThread private void start() { if (isVerboseLoggingEnabled()) { Log.v(mTag, "Scanner start"); } - post(this::postScan); + postScan(); } - @AnyThread private void stop() { if (isVerboseLoggingEnabled()) { Log.v(mTag, "Scanner stop"); @@ -361,7 +361,6 @@ public class BaseWifiTracker implements LifecycleObserver { removeCallbacksAndMessages(null); } - @WorkerThread private void postScan() { if (mWifiManager.startScan()) { mRetry = 0; |