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

Commit 5401f0b8 authored by Irfan Sheriff's avatar Irfan Sheriff
Browse files

Fix wifi bring up at boot up

Bug: 5719176
Change-Id: Iac879fc634f6ab6289b73c46931d754bc0af2f87
parent 641a5624
Loading
Loading
Loading
Loading
+31 −18
Original line number Diff line number Diff line
@@ -152,8 +152,12 @@ public class WifiService extends IWifiManager.Stub {
    /* Wifi disabled due to airplane mode on */
    private static final int WIFI_DISABLED_AIRPLANE_ON      = 3;

    private AtomicInteger mWifiState = new AtomicInteger(WIFI_DISABLED);
    /* Persisted state that tracks the wifi & airplane interaction from settings */
    private AtomicInteger mPersistWifiState = new AtomicInteger(WIFI_DISABLED);
    /* Tracks current airplane mode state */
    private AtomicBoolean mAirplaneModeOn = new AtomicBoolean(false);
    /* Tracks whether wifi is enabled from WifiStateMachine's perspective */
    private boolean mWifiEnabled;

    private boolean mIsReceiverRegistered = false;

@@ -373,8 +377,8 @@ public class WifiService extends IWifiManager.Stub {
                        mAirplaneModeOn.set(isAirplaneModeOn());
                        /* On airplane mode disable, restore wifi state if necessary */
                        if (!mAirplaneModeOn.get() && (testAndClearWifiSavedState() ||
                            mWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE)) {
                                persistWifiEnabled(true);
                            mPersistWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE)) {
                                persistWifiState(true);
                        }
                        updateWifiState();
                    }
@@ -391,6 +395,11 @@ public class WifiService extends IWifiManager.Stub {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                                    WifiManager.WIFI_STATE_DISABLED);

                            mWifiEnabled = (wifiState == WifiManager.WIFI_STATE_ENABLED);

                           // reset & clear notification on any wifi state change
                            resetNotification();
                        } else if (intent.getAction().equals(
@@ -435,7 +444,7 @@ public class WifiService extends IWifiManager.Stub {
     */
    public void checkAndStartWifi() {
        mAirplaneModeOn.set(isAirplaneModeOn());
        mWifiState.set(getPersistedWifiState());
        mPersistWifiState.set(getPersistedWifiState());
        /* Start if Wi-Fi should be enabled or the saved state indicates Wi-Fi was on */
        boolean wifiEnabled = shouldWifiBeEnabled() || testAndClearWifiSavedState();
        Slog.i(TAG, "WifiService starting up with Wi-Fi " +
@@ -472,29 +481,30 @@ public class WifiService extends IWifiManager.Stub {

    private boolean shouldWifiBeEnabled() {
        if (mAirplaneModeOn.get()) {
            return mWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE;
            return mPersistWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE;
        } else {
            return mWifiState.get() != WIFI_DISABLED;
            return mPersistWifiState.get() != WIFI_DISABLED;
        }
    }

    private void persistWifiEnabled(boolean enabled) {
    private void persistWifiState(boolean enabled) {
        final ContentResolver cr = mContext.getContentResolver();
        boolean airplane = mAirplaneModeOn.get() && isAirplaneToggleable();
        if (enabled) {
            if (airplane) {
                mWifiState.set(WIFI_ENABLED_AIRPLANE_OVERRIDE);
                mPersistWifiState.set(WIFI_ENABLED_AIRPLANE_OVERRIDE);
            } else {
                mWifiState.set(WIFI_ENABLED);
                mPersistWifiState.set(WIFI_ENABLED);
            }
        } else {
            if (airplane) {
                mWifiState.set(WIFI_DISABLED_AIRPLANE_ON);
                mPersistWifiState.set(WIFI_DISABLED_AIRPLANE_ON);
            } else {
                mWifiState.set(WIFI_DISABLED);
                mPersistWifiState.set(WIFI_DISABLED);
            }
        }
        Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, mWifiState.get());

        Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, mPersistWifiState.get());
    }


@@ -545,7 +555,6 @@ public class WifiService extends IWifiManager.Stub {
     */
    public synchronized boolean setWifiEnabled(boolean enable) {
        enforceChangePermission();

        if (DBG) {
            Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
        }
@@ -559,9 +568,13 @@ public class WifiService extends IWifiManager.Stub {
         * Caller might not have WRITE_SECURE_SETTINGS,
         * only CHANGE_WIFI_STATE is enforced
         */

        /* Avoids overriding of airplane state when wifi is already in the expected state */
        if (enable != mWifiEnabled) {
            long ident = Binder.clearCallingIdentity();
        persistWifiEnabled(enable);
            persistWifiState(enable);
            Binder.restoreCallingIdentity(ident);
        }

        if (enable) {
            if (!mIsReceiverRegistered) {