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

Commit c591a8b9 authored by Irfan Sheriff's avatar Irfan Sheriff
Browse files

Add reminder about scan settings on wifi off

Notify the user that scans will be available when
wifi is turned off

Bug: 8732391
Change-Id: If232bfb9f6c3976059bde11280318901c35161e7
parent 2774bc44
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -20,10 +20,12 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.NetworkInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.UserHandle;
import android.provider.Settings;
import android.widget.CompoundButton;
import android.widget.Switch;
@@ -38,6 +40,7 @@ public class WifiEnabler implements CompoundButton.OnCheckedChangeListener {
    private final Context mContext;
    private Switch mSwitch;
    private AtomicBoolean mConnected = new AtomicBoolean(false);
    private AtomicBoolean mNotifyScanMode = new AtomicBoolean(true);

    private final WifiManager mWifiManager;
    private boolean mStateMachineEvent;
@@ -72,6 +75,7 @@ public class WifiEnabler implements CompoundButton.OnCheckedChangeListener {
        // The order matters! We really should not depend on this. :(
        mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        registerForNotifyUserOnScanModeChange();
    }

    public void resume() {
@@ -98,6 +102,29 @@ public class WifiEnabler implements CompoundButton.OnCheckedChangeListener {
        mSwitch.setEnabled(isEnabled || isDisabled);
    }

    private void getPersistedNotifyScanMode() {
        mNotifyScanMode.set(Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.WIFI_NOTIFY_SCAN_ALWAYS_AVAILABLE, 1) == 1);
    }

    /**
     * Observes settings changes to notify the user when scan mode is active and
     * Wi-Fi is turned off
     */
    private void registerForNotifyUserOnScanModeChange() {
        ContentObserver contentObserver = new ContentObserver(null) {
            @Override
            public void onChange(boolean selfChange) {
                getPersistedNotifyScanMode();
            }
        };

        mContext.getContentResolver().registerContentObserver(
                Settings.Global.getUriFor(Settings.Global.WIFI_NOTIFY_SCAN_ALWAYS_AVAILABLE),
                false, contentObserver);
        getPersistedNotifyScanMode();
    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //Do nothing if called as a result of a state machine event
        if (mStateMachineEvent) {
@@ -117,6 +144,13 @@ public class WifiEnabler implements CompoundButton.OnCheckedChangeListener {
            mWifiManager.setWifiApEnabled(null, false);
        }

        if (isChecked == false)  {
            if (mWifiManager.isScanAlwaysAvailable() && mNotifyScanMode.get()) {
                Intent intent = new Intent(WifiManager.ACTION_NOTIFY_SCAN_ALWAYS_AVAILABLE);
                mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
            }
        }

        if (mWifiManager.setWifiEnabled(isChecked)) {
            // Intent has been taken into account, disable until new state is active
            mSwitch.setEnabled(false);