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

WifiController

public class WifiController extends com.android.internal.util.StateMachine

Fields Summary
private static final String
TAG
private static final boolean
DBG
private android.content.Context
mContext
private boolean
mScreenOff
private boolean
mDeviceIdle
private int
mPluggedType
private int
mStayAwakeConditions
private long
mIdleMillis
private int
mSleepPolicy
private boolean
mFirstUserSignOnSeen
private android.app.AlarmManager
mAlarmManager
private android.app.PendingIntent
mIdleIntent
private static final int
IDLE_REQUEST
private static final long
DEFAULT_IDLE_MS
See {@link Settings.Global#WIFI_IDLE_MS}. This is the default value if a Settings.Global value is not present. This timeout value is chosen as the approximate point at which the battery drain caused by Wi-Fi being enabled but not active exceeds the battery drain caused by re-establishing a connection to the mobile data network.
private static final long
DEFAULT_REENABLE_DELAY_MS
See {@link Settings.Global#WIFI_REENABLE_DELAY_MS}. This is the default value if a Settings.Global value is not present. This is the minimum time after wifi is disabled we'll act on an enable. Enable requests received before this delay will be deferred.
private static final long
DEFER_MARGIN_MS
android.net.NetworkInfo
mNetworkInfo
private static final String
ACTION_DEVICE_IDLE
final WifiStateMachine
mWifiStateMachine
final WifiSettingsStore
mSettingsStore
final com.android.server.wifi.WifiServiceImpl.LockList
mLocks
private final android.os.WorkSource
mTmpWorkSource
Temporary for computing UIDS that are responsible for starting WIFI. Protected by mWifiStateTracker lock.
private long
mReEnableDelayMillis
private static final int
BASE
static final int
CMD_EMERGENCY_MODE_CHANGED
static final int
CMD_SCREEN_ON
static final int
CMD_SCREEN_OFF
static final int
CMD_BATTERY_CHANGED
static final int
CMD_DEVICE_IDLE
static final int
CMD_LOCKS_CHANGED
static final int
CMD_SCAN_ALWAYS_MODE_CHANGED
static final int
CMD_WIFI_TOGGLED
static final int
CMD_AIRPLANE_TOGGLED
static final int
CMD_SET_AP
static final int
CMD_DEFERRED_TOGGLE
static final int
CMD_USER_PRESENT
private DefaultState
mDefaultState
private StaEnabledState
mStaEnabledState
private ApStaDisabledState
mApStaDisabledState
private StaDisabledWithScanState
mStaDisabledWithScanState
private ApEnabledState
mApEnabledState
private DeviceActiveState
mDeviceActiveState
private DeviceInactiveState
mDeviceInactiveState
private ScanOnlyLockHeldState
mScanOnlyLockHeldState
private FullLockHeldState
mFullLockHeldState
private FullHighPerfLockHeldState
mFullHighPerfLockHeldState
private NoLockHeldState
mNoLockHeldState
private EcmState
mEcmState
Constructors Summary
WifiController(android.content.Context context, WifiServiceImpl service, android.os.Looper looper)


          
        super(TAG, looper);
        mContext = context;
        mWifiStateMachine = service.mWifiStateMachine;
        mSettingsStore = service.mSettingsStore;
        mLocks = service.mLocks;

        mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
        Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
        mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);

        addState(mDefaultState);
            addState(mApStaDisabledState, mDefaultState);
            addState(mStaEnabledState, mDefaultState);
                addState(mDeviceActiveState, mStaEnabledState);
                addState(mDeviceInactiveState, mStaEnabledState);
                    addState(mScanOnlyLockHeldState, mDeviceInactiveState);
                    addState(mFullLockHeldState, mDeviceInactiveState);
                    addState(mFullHighPerfLockHeldState, mDeviceInactiveState);
                    addState(mNoLockHeldState, mDeviceInactiveState);
            addState(mStaDisabledWithScanState, mDefaultState);
            addState(mApEnabledState, mDefaultState);
            addState(mEcmState, mDefaultState);

        boolean isAirplaneModeOn = mSettingsStore.isAirplaneModeOn();
        boolean isWifiEnabled = mSettingsStore.isWifiToggleEnabled();
        boolean isScanningAlwaysAvailable = mSettingsStore.isScanAlwaysAvailable();

        log("isAirplaneModeOn = " + isAirplaneModeOn +
                ", isWifiEnabled = " + isWifiEnabled +
                ", isScanningAvailable = " + isScanningAlwaysAvailable);

        if (isScanningAlwaysAvailable) {
            setInitialState(mStaDisabledWithScanState);
        } else {
            setInitialState(mApStaDisabledState);
        }

        setLogRecSize(100);
        setLogOnlyTransitions(false);

        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_DEVICE_IDLE);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        mContext.registerReceiver(
                new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        String action = intent.getAction();
                        if (action.equals(ACTION_DEVICE_IDLE)) {
                            sendMessage(CMD_DEVICE_IDLE);
                        } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                            mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
                                    WifiManager.EXTRA_NETWORK_INFO);
                        }
                    }
                },
                new IntentFilter(filter));

        initializeAndRegisterForSettingsChange(looper);
    
Methods Summary
private voidcheckLocksAndTransitionWhenDeviceIdle()

        if (mLocks.hasLocks()) {
            switch (mLocks.getStrongestLockMode()) {
                case WIFI_MODE_FULL:
                    transitionTo(mFullLockHeldState);
                    break;
                case WIFI_MODE_FULL_HIGH_PERF:
                    transitionTo(mFullHighPerfLockHeldState);
                    break;
                case WIFI_MODE_SCAN_ONLY:
                    transitionTo(mScanOnlyLockHeldState);
                    break;
                default:
                    loge("Illegal lock " + mLocks.getStrongestLockMode());
            }
        } else {
            if (mSettingsStore.isScanAlwaysAvailable()) {
                transitionTo(mScanOnlyLockHeldState);
            } else {
                transitionTo(mNoLockHeldState);
            }
        }
    
public voiddump(java.io.FileDescriptor fd, java.io.PrintWriter pw, java.lang.String[] args)

        super.dump(fd, pw, args);

        pw.println("mScreenOff " + mScreenOff);
        pw.println("mDeviceIdle " + mDeviceIdle);
        pw.println("mPluggedType " + mPluggedType);
        pw.println("mIdleMillis " + mIdleMillis);
        pw.println("mSleepPolicy " + mSleepPolicy);
    
private voidinitializeAndRegisterForSettingsChange(android.os.Looper looper)

        Handler handler = new Handler(looper);
        readStayAwakeConditions();
        registerForStayAwakeModeChange(handler);
        readWifiIdleTime();
        registerForWifiIdleTimeChange(handler);
        readWifiSleepPolicy();
        registerForWifiSleepPolicyChange(handler);
        readWifiReEnableDelay();
    
private voidreadStayAwakeConditions()

        mStayAwakeConditions = Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0);
    
private voidreadWifiIdleTime()

        mIdleMillis = Settings.Global.getLong(mContext.getContentResolver(),
                Settings.Global.WIFI_IDLE_MS, DEFAULT_IDLE_MS);
    
private voidreadWifiReEnableDelay()

        mReEnableDelayMillis = Settings.Global.getLong(mContext.getContentResolver(),
                Settings.Global.WIFI_REENABLE_DELAY_MS, DEFAULT_REENABLE_DELAY_MS);
    
private voidreadWifiSleepPolicy()

        mSleepPolicy = Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.WIFI_SLEEP_POLICY,
                Settings.Global.WIFI_SLEEP_POLICY_NEVER);
    
private voidregisterForStayAwakeModeChange(android.os.Handler handler)
Observes settings changes to scan always mode.

        ContentObserver contentObserver = new ContentObserver(handler) {
            @Override
            public void onChange(boolean selfChange) {
                readStayAwakeConditions();
            }
        };

        mContext.getContentResolver().registerContentObserver(
                Settings.Global.getUriFor(Settings.Global.STAY_ON_WHILE_PLUGGED_IN),
                false, contentObserver);
    
private voidregisterForWifiIdleTimeChange(android.os.Handler handler)
Observes settings changes to scan always mode.

        ContentObserver contentObserver = new ContentObserver(handler) {
            @Override
            public void onChange(boolean selfChange) {
                readWifiIdleTime();
            }
        };

        mContext.getContentResolver().registerContentObserver(
                Settings.Global.getUriFor(Settings.Global.WIFI_IDLE_MS),
                false, contentObserver);
    
private voidregisterForWifiSleepPolicyChange(android.os.Handler handler)
Observes changes to wifi sleep policy

        ContentObserver contentObserver = new ContentObserver(handler) {
            @Override
            public void onChange(boolean selfChange) {
                readWifiSleepPolicy();
            }
        };
        mContext.getContentResolver().registerContentObserver(
                Settings.Global.getUriFor(Settings.Global.WIFI_SLEEP_POLICY),
                false, contentObserver);
    
private booleanshouldDeviceStayAwake(int pluggedType)
Determine whether the bit value corresponding to {@code pluggedType} is set in the bit string mStayAwakeConditions. This determines whether the device should stay awake based on the current plugged type.

param
pluggedType the type of plug (USB, AC, or none) for which the check is being made
return
{@code true} if {@code pluggedType} indicates that the device is supposed to stay awake, {@code false} otherwise.

        return (mStayAwakeConditions & pluggedType) != 0;
    
private booleanshouldWifiStayAwake(int pluggedType)
Determines whether the Wi-Fi chipset should stay awake or be put to sleep. Looks at the setting for the sleep policy and the current conditions.

see
#shouldDeviceStayAwake(int)

        if (mSleepPolicy == Settings.Global.WIFI_SLEEP_POLICY_NEVER) {
            // Never sleep
            return true;
        } else if ((mSleepPolicy == Settings.Global.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
                (pluggedType != 0)) {
            // Never sleep while plugged, and we're plugged
            return true;
        } else {
            // Default
            return shouldDeviceStayAwake(pluggedType);
        }
    
private voidupdateBatteryWorkSource()

        mTmpWorkSource.clear();
        if (mDeviceIdle) {
            mLocks.updateWorkSource(mTmpWorkSource);
        }
        mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource);