Merge "Avoid quick shutdown after a driver start" into ics-mr1

This commit is contained in:
Irfan Sheriff
2011-11-04 11:47:27 -07:00
committed by Android (Google) Code Review
2 changed files with 51 additions and 43 deletions

View File

@@ -921,18 +921,14 @@ public class WifiService extends IWifiManager.Stub {
Slog.d(TAG, "ACTION_SCREEN_ON");
}
mAlarmManager.cancel(mIdleIntent);
mDeviceIdle = false;
mScreenOff = false;
// Once the screen is on, we are not keeping WIFI running
// because of any locks so clear that tracking immediately.
reportStartWorkSource();
evaluateTrafficStatsPolling();
mWifiStateMachine.enableRssiPolling(true);
if (mBackgroundScanSupported) {
mWifiStateMachine.enableBackgroundScanCommand(false);
}
mWifiStateMachine.enableAllNetworks();
updateWifiState();
setDeviceIdleAndUpdateWifi(false);
} else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
if (DBG) {
Slog.d(TAG, "ACTION_SCREEN_OFF");
@@ -950,36 +946,17 @@ public class WifiService extends IWifiManager.Stub {
* or plugged in to AC).
*/
if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) {
WifiInfo info = mWifiStateMachine.syncRequestConnectionInfo();
if (info.getSupplicantState() != SupplicantState.COMPLETED) {
// we used to go to sleep immediately, but this caused some race conditions
// we don't have time to track down for this release. Delay instead,
// but not as long as we would if connected (below)
// TODO - fix the race conditions and switch back to the immediate turn-off
long triggerTime = System.currentTimeMillis() + (2*60*1000); // 2 min
if (DBG) {
Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for 120,000 ms");
}
mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
// // do not keep Wifi awake when screen is off if Wifi is not associated
// mDeviceIdle = true;
// updateWifiState();
//Delayed shutdown if wifi is connected
if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED) {
if (DBG) Slog.d(TAG, "setting ACTION_DEVICE_IDLE: " + idleMillis + " ms");
mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ idleMillis, mIdleIntent);
} else {
long triggerTime = System.currentTimeMillis() + idleMillis;
if (DBG) {
Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis
+ "ms");
}
mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent);
setDeviceIdleAndUpdateWifi(true);
}
}
} else if (action.equals(ACTION_DEVICE_IDLE)) {
if (DBG) {
Slog.d(TAG, "got ACTION_DEVICE_IDLE");
}
mDeviceIdle = true;
reportStartWorkSource();
updateWifiState();
setDeviceIdleAndUpdateWifi(true);
} else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
/*
* Set a timer to put Wi-Fi to sleep, but only if the screen is off
@@ -1056,6 +1033,12 @@ public class WifiService extends IWifiManager.Stub {
}
};
private void setDeviceIdleAndUpdateWifi(boolean deviceIdle) {
mDeviceIdle = deviceIdle;
reportStartWorkSource();
updateWifiState();
}
private synchronized void reportStartWorkSource() {
mTmpWorkSource.clear();
if (mDeviceIdle) {

View File

@@ -211,7 +211,7 @@ public class WifiStateMachine extends StateMachine {
static final int CMD_STOP_SUPPLICANT = BASE + 12;
/* Start the driver */
static final int CMD_START_DRIVER = BASE + 13;
/* Start the driver */
/* Stop the driver */
static final int CMD_STOP_DRIVER = BASE + 14;
/* Indicates Static IP succeded */
static final int CMD_STATIC_IP_SUCCESS = BASE + 15;
@@ -219,6 +219,9 @@ public class WifiStateMachine extends StateMachine {
static final int CMD_STATIC_IP_FAILURE = BASE + 16;
/* Indicates supplicant stop failed */
static final int CMD_STOP_SUPPLICANT_FAILED = BASE + 17;
/* Delayed stop to avoid shutting down driver too quick*/
static final int CMD_DELAYED_STOP_DRIVER = BASE + 18;
/* Start the soft access point */
static final int CMD_START_AP = BASE + 21;
@@ -389,6 +392,13 @@ public class WifiStateMachine extends StateMachine {
private static final int MIN_INTERVAL_ENABLE_ALL_NETWORKS_MS = 10 * 60 * 1000; /* 10 minutes */
private long mLastEnableAllNetworksTime;
/**
* Starting and shutting down driver too quick causes problems leading to driver
* being in a bad state. Delay driver stop.
*/
private static final int DELAYED_DRIVER_STOP_MS = 2 * 60 * 1000; /* 2 minutes */
private int mDelayedStopCounter;
private boolean mInDelayedStop = false;
private static final int MIN_RSSI = -200;
private static final int MAX_RSSI = 256;
@@ -1780,6 +1790,7 @@ public class WifiStateMachine extends StateMachine {
case CMD_STOP_SUPPLICANT_FAILED:
case CMD_START_DRIVER:
case CMD_STOP_DRIVER:
case CMD_DELAYED_STOP_DRIVER:
case CMD_START_AP:
case CMD_START_AP_SUCCESS:
case CMD_START_AP_FAILURE:
@@ -2442,6 +2453,7 @@ public class WifiStateMachine extends StateMachine {
EventLog.writeEvent(EVENTLOG_WIFI_STATE_CHANGED, getName());
mIsRunning = true;
mInDelayedStop = false;
updateBatteryWorkSource(null);
/**
@@ -2521,6 +2533,30 @@ public class WifiStateMachine extends StateMachine {
WifiNative.setBluetoothCoexistenceScanModeCommand(mBluetoothConnectionActive);
break;
case CMD_STOP_DRIVER:
/* Already doing a delayed stop */
if (mInDelayedStop) {
if (DBG) log("Already in delayed stop");
break;
}
mInDelayedStop = true;
mDelayedStopCounter++;
if (DBG) log("Delayed stop message " + mDelayedStopCounter);
sendMessageDelayed(obtainMessage(CMD_DELAYED_STOP_DRIVER, mDelayedStopCounter,
0), DELAYED_DRIVER_STOP_MS);
break;
case CMD_START_DRIVER:
if (mInDelayedStop) {
mInDelayedStop = false;
mDelayedStopCounter++;
if (DBG) log("Delayed stop ignored due to start");
}
break;
case CMD_DELAYED_STOP_DRIVER:
if (message.arg1 != mDelayedStopCounter) break;
if (getCurrentState() != mDisconnectedState) {
WifiNative.disconnectCommand();
handleNetworkDisconnect();
}
mWakeLock.acquire();
WifiNative.stopDriverCommand();
transitionTo(mDriverStoppingState);
@@ -2879,10 +2915,6 @@ public class WifiStateMachine extends StateMachine {
/* Ignore */
case WifiMonitor.NETWORK_CONNECTION_EVENT:
break;
case CMD_STOP_DRIVER:
sendMessage(CMD_DISCONNECT);
deferMessage(message);
break;
case CMD_SET_SCAN_MODE:
if (message.arg1 == SCAN_ONLY_MODE) {
sendMessage(CMD_DISCONNECT);
@@ -2937,10 +2969,6 @@ public class WifiStateMachine extends StateMachine {
WifiNative.disconnectCommand();
transitionTo(mDisconnectingState);
break;
case CMD_STOP_DRIVER:
sendMessage(CMD_DISCONNECT);
deferMessage(message);
break;
case CMD_REQUEST_CM_WAKELOCK:
checkAndSetConnectivityInstance();
mCm.requestNetworkTransitionWakelock(TAG);
@@ -3036,9 +3064,6 @@ public class WifiStateMachine extends StateMachine {
public boolean processMessage(Message message) {
if (DBG) log(getName() + message.toString() + "\n");
switch (message.what) {
case CMD_STOP_DRIVER: /* Stop driver only after disconnect handled */
deferMessage(message);
break;
case CMD_SET_SCAN_MODE:
if (message.arg1 == SCAN_ONLY_MODE) {
deferMessage(message);