FileDocCategorySizeDatePackage
WifiSettingsStore.javaAPI DocAndroid 5.1 API7455Thu Mar 12 22:22:52 GMT 2015com.android.server.wifi

WifiSettingsStore

public final class WifiSettingsStore extends Object

Fields Summary
private static final int
WIFI_DISABLED
private static final int
WIFI_ENABLED
private static final int
WIFI_ENABLED_AIRPLANE_OVERRIDE
private static final int
WIFI_DISABLED_AIRPLANE_ON
private int
mPersistWifiState
private boolean
mAirplaneModeOn
private boolean
mScanAlwaysAvailable
private final android.content.Context
mContext
private boolean
mCheckSavedStateAtBoot
Constructors Summary
WifiSettingsStore(android.content.Context context)


      
        mContext = context;
        mAirplaneModeOn = getPersistedAirplaneModeOn();
        mPersistWifiState = getPersistedWifiState();
        mScanAlwaysAvailable = getPersistedScanAlwaysAvailable();
    
Methods Summary
voiddump(java.io.FileDescriptor fd, java.io.PrintWriter pw, java.lang.String[] args)

        pw.println("mPersistWifiState " + mPersistWifiState);
        pw.println("mAirplaneModeOn " + mAirplaneModeOn);
    
private booleangetPersistedAirplaneModeOn()

        return Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
    
private booleangetPersistedScanAlwaysAvailable()

        return Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE,
                0) == 1;
    
private intgetPersistedWifiState()

        final ContentResolver cr = mContext.getContentResolver();
        try {
            return Settings.Global.getInt(cr, Settings.Global.WIFI_ON);
        } catch (Settings.SettingNotFoundException e) {
            Settings.Global.putInt(cr, Settings.Global.WIFI_ON, WIFI_DISABLED);
            return WIFI_DISABLED;
        }
    
synchronized booleanhandleAirplaneModeToggled()

        // Is Wi-Fi sensitive to airplane mode changes ?
        if (!isAirplaneSensitive()) {
            return false;
        }

        mAirplaneModeOn = getPersistedAirplaneModeOn();
        if (mAirplaneModeOn) {
            // Wifi disabled due to airplane on
            if (mPersistWifiState == WIFI_ENABLED) {
                persistWifiState(WIFI_DISABLED_AIRPLANE_ON);
            }
        } else {
            /* On airplane mode disable, restore wifi state if necessary */
            if (testAndClearWifiSavedState() ||
                    mPersistWifiState == WIFI_ENABLED_AIRPLANE_OVERRIDE) {
                persistWifiState(WIFI_ENABLED);
            }
        }
        return true;
    
synchronized voidhandleWifiScanAlwaysAvailableToggled()

        mScanAlwaysAvailable = getPersistedScanAlwaysAvailable();
    
synchronized booleanhandleWifiToggled(boolean wifiEnabled)

        // Can Wi-Fi be toggled in airplane mode ?
        if (mAirplaneModeOn && !isAirplaneToggleable()) {
            return false;
        }

        if (wifiEnabled) {
            if (mAirplaneModeOn) {
                persistWifiState(WIFI_ENABLED_AIRPLANE_OVERRIDE);
            } else {
                persistWifiState(WIFI_ENABLED);
            }
        } else {
            // When wifi state is disabled, we do not care
            // if airplane mode is on or not. The scenario of
            // wifi being disabled due to airplane mode being turned on
            // is handled handleAirplaneModeToggled()
            persistWifiState(WIFI_DISABLED);
        }
        return true;
    
synchronized booleanisAirplaneModeOn()
Returns true if airplane mode is currently on.

return
{@code true} if airplane mode is on.

       return mAirplaneModeOn;
    
private booleanisAirplaneSensitive()

        String airplaneModeRadios = Settings.Global.getString(mContext.getContentResolver(),
                Settings.Global.AIRPLANE_MODE_RADIOS);
        return airplaneModeRadios == null
                || airplaneModeRadios.contains(Settings.Global.RADIO_WIFI);
    
private booleanisAirplaneToggleable()

        String toggleableRadios = Settings.Global.getString(mContext.getContentResolver(),
                Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
        return toggleableRadios != null
                && toggleableRadios.contains(Settings.Global.RADIO_WIFI);
    
synchronized booleanisScanAlwaysAvailable()

        return !mAirplaneModeOn && mScanAlwaysAvailable;
    
synchronized booleanisWifiToggleEnabled()

        if (!mCheckSavedStateAtBoot) {
            mCheckSavedStateAtBoot = true;
            if (testAndClearWifiSavedState()) return true;
        }

        if (mAirplaneModeOn) {
            return mPersistWifiState == WIFI_ENABLED_AIRPLANE_OVERRIDE;
        } else {
            return mPersistWifiState != WIFI_DISABLED;
        }
    
private voidpersistWifiState(int state)

        final ContentResolver cr = mContext.getContentResolver();
        mPersistWifiState = state;
        Settings.Global.putInt(cr, Settings.Global.WIFI_ON, state);
    
private booleantestAndClearWifiSavedState()

        final ContentResolver cr = mContext.getContentResolver();
        int wifiSavedState = 0;
        try {
            wifiSavedState = Settings.Global.getInt(cr, Settings.Global.WIFI_SAVED_STATE);
            if(wifiSavedState == 1)
                Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 0);
        } catch (Settings.SettingNotFoundException e) {
            ;
        }
        return (wifiSavedState == 1);