Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 86cc9b7f authored by Sundeep Ghuman's avatar Sundeep Ghuman
Browse files

Execute all callbacks on the MainThread.

This is a precursory step which will allow us to delete the double
handlers that currently exist in WifiTracker.

Bug: 37674366
Test: runtest --path
frameworks/base/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java

Change-Id: Ieea1af712b80ea6ede358d7a1004e489e11c6009
parent 04f7f34a
Loading
Loading
Loading
Loading
+34 −1
Original line number Diff line number Diff line
@@ -220,7 +220,7 @@ public class WifiTracker implements LifecycleObserver, OnStart, OnStop, OnDestro
        mWifiManager = wifiManager;
        mIncludeSaved = includeSaved;
        mIncludeScans = includeScans;
        mListener = wifiListener;
        mListener = new WifiListenerWrapper(wifiListener);
        mConnectivityManager = connectivityManager;

        // check if verbose logging has been turned on or off
@@ -1052,6 +1052,39 @@ public class WifiTracker implements LifecycleObserver, OnStart, OnStop, OnDestro
        }
    }

    /**
     * Wraps the given {@link WifiListener} instance and executes it's methods on the Main Thread.
     *
     * <p>This mechanism allows us to no longer need a separate MainHandler and WorkHandler, which
     * were previously both performing work, while avoiding errors which occur from executing
     * callbacks which manipulate UI elements from a different thread than the MainThread.
     */
    private static class WifiListenerWrapper implements WifiListener {

        private final Handler mHandler;
        private final WifiListener mDelegatee;

        public WifiListenerWrapper(WifiListener listener) {
            mHandler = new Handler(Looper.getMainLooper());
            mDelegatee = listener;
        }

        @Override
        public void onWifiStateChanged(int state) {
            mHandler.post(() -> mDelegatee.onWifiStateChanged(state));
        }

        @Override
        public void onConnectedChanged() {
            mHandler.post(() -> mDelegatee.onConnectedChanged());
        }

        @Override
        public void onAccessPointsChanged() {
            mHandler.post(() -> mDelegatee.onAccessPointsChanged());
        }
    }

    public interface WifiListener {
        /**
         * Called when the state of Wifi has changed, the state will be one of