@@ -138,10 +138,6 @@ public class WifiStateMachine extends StateMachine {
138138 private boolean mScanResultIsPending = false ;
139139 /* Tracks if the current scan settings are active */
140140 private boolean mSetScanActive = false ;
141- /* High perf mode is true if an app has held a high perf Wifi Lock */
142- private boolean mHighPerfMode = false ;
143- /* Tracks if user has disabled suspend optimizations through settings */
144- private AtomicBoolean mSuspendOptEnabled = new AtomicBoolean (true );
145141
146142 private boolean mBluetoothConnectionActive = false ;
147143
@@ -338,10 +334,8 @@ public class WifiStateMachine extends StateMachine {
338334 static final int CMD_START_PACKET_FILTERING = BASE + 84 ;
339335 /* Clear packet filter */
340336 static final int CMD_STOP_PACKET_FILTERING = BASE + 85 ;
341- /* Set suspend mode optimizations in the driver */
342- static final int CMD_SET_SUSPEND_OPTIMIZATIONS = BASE + 86 ;
343- /* Clear suspend mode optimizations in the driver */
344- static final int CMD_CLEAR_SUSPEND_OPTIMIZATIONS = BASE + 87 ;
337+ /* Enable suspend mode optimizations in the driver */
338+ static final int CMD_SET_SUSPEND_OPT_ENABLED = BASE + 86 ;
345339 /* When there are no saved networks, we do a periodic scan to notify user of
346340 * an open network */
347341 static final int CMD_NO_NETWORKS_PERIODIC_SCAN = BASE + 88 ;
@@ -386,8 +380,19 @@ public class WifiStateMachine extends StateMachine {
386380 */
387381 private static final int DEFAULT_MAX_DHCP_RETRIES = 9 ;
388382
389- /* Tracks if power save is enabled in driver */
390- private boolean mPowerSaveEnabled = true ;;
383+ /* Tracks if suspend optimizations need to be disabled by DHCP,
384+ * screen or due to high perf mode.
385+ * When any of them needs to disable it, we keep the suspend optimizations
386+ * disabled
387+ */
388+ private int mSuspendOptNeedsDisabled = 0 ;
389+
390+ private static final int SUSPEND_DUE_TO_DHCP = 1 ;
391+ private static final int SUSPEND_DUE_TO_HIGH_PERF = 1 <<1 ;
392+ private static final int SUSPEND_DUE_TO_SCREEN = 1 <<2 ;
393+
394+ /* Tracks if user has enabled suspend optimizations through settings */
395+ private AtomicBoolean mUserWantsSuspendOpt = new AtomicBoolean (true );
391396
392397 /**
393398 * Default framework scan interval in milliseconds. This is used in the scenario in which
@@ -599,7 +604,7 @@ public WifiStateMachine(Context context, String wlanInterface) {
599604 mPrimaryDeviceType = mContext .getResources ().getString (
600605 com .android .internal .R .string .config_wifi_p2p_device_type );
601606
602- mSuspendOptEnabled .set (Settings .Secure .getInt (mContext .getContentResolver (),
607+ mUserWantsSuspendOpt .set (Settings .Secure .getInt (mContext .getContentResolver (),
603608 Settings .Secure .WIFI_SUSPEND_OPTIMIZATIONS_ENABLED , 1 ) == 1 );
604609
605610 mContext .registerReceiver (
@@ -637,20 +642,20 @@ public void onReceive(Context context, Intent intent) {
637642 enableBackgroundScanCommand (false );
638643 }
639644 enableAllNetworks ();
640- if (mSuspendOptEnabled .get ()) {
645+ if (mUserWantsSuspendOpt .get ()) {
641646 if (DBG ) log ("Clear suspend optimizations" );
642- sendMessage (CMD_CLEAR_SUSPEND_OPTIMIZATIONS );
647+ sendMessage (obtainMessage ( CMD_SET_SUSPEND_OPT_ENABLED , 0 , 0 ) );
643648 }
644649 } else if (action .equals (Intent .ACTION_SCREEN_OFF )) {
645650 enableRssiPolling (false );
646651 if (mBackgroundScanSupported ) {
647652 enableBackgroundScanCommand (true );
648653 }
649- if (mSuspendOptEnabled .get ()) {
654+ if (mUserWantsSuspendOpt .get ()) {
650655 if (DBG ) log ("Enable suspend optimizations" );
651656 //Allow 2s for suspend optimizations to be set
652657 mSuspendWakeLock .acquire (2000 );
653- sendMessage (CMD_SET_SUSPEND_OPTIMIZATIONS );
658+ sendMessage (obtainMessage ( CMD_SET_SUSPEND_OPT_ENABLED , 1 , 0 ) );
654659 }
655660 }
656661 }
@@ -671,7 +676,7 @@ public void onReceive(Context context, Intent intent) {
671676 new ContentObserver (getHandler ()) {
672677 @ Override
673678 public void onChange (boolean selfChange ) {
674- mSuspendOptEnabled .set (Settings .Secure .getInt (mContext .getContentResolver (),
679+ mUserWantsSuspendOpt .set (Settings .Secure .getInt (mContext .getContentResolver (),
675680 Settings .Secure .WIFI_SUSPEND_OPTIMIZATIONS_ENABLED , 1 ) == 1 );
676681 }
677682 });
@@ -1172,8 +1177,8 @@ public String toString() {
11721177 sb .append ("mLastNetworkId " ).append (mLastNetworkId ).append (LS );
11731178 sb .append ("mReconnectCount " ).append (mReconnectCount ).append (LS );
11741179 sb .append ("mIsScanMode " ).append (mIsScanMode ).append (LS );
1175- sb .append ("mHighPerfMode " ).append (mHighPerfMode ).append (LS );
1176- sb .append ("mSuspendOptEnabled " ).append (mSuspendOptEnabled ).append (LS );
1180+ sb .append ("mUserWantsSuspendOpt " ).append (mUserWantsSuspendOpt ).append (LS );
1181+ sb .append ("mSuspendOptNeedsDisabled " ).append (mSuspendOptNeedsDisabled ).append (LS );
11771182 sb .append ("Supplicant status" ).append (LS )
11781183 .append (mWifiNative .status ()).append (LS ).append (LS );
11791184
@@ -1191,8 +1196,7 @@ protected boolean recordLogRec(Message msg) {
11911196 case CMD_START_DRIVER :
11921197 case CMD_SET_SCAN_MODE :
11931198 case CMD_SET_HIGH_PERF_MODE :
1194- case CMD_SET_SUSPEND_OPTIMIZATIONS :
1195- case CMD_CLEAR_SUSPEND_OPTIMIZATIONS :
1199+ case CMD_SET_SUSPEND_OPT_ENABLED :
11961200 case CMD_ENABLE_BACKGROUND_SCAN :
11971201 case CMD_ENABLE_ALL_NETWORKS :
11981202 return false ;
@@ -1324,6 +1328,32 @@ private void setFrequencyBand() {
13241328 setFrequencyBand (band , false );
13251329 }
13261330
1331+ private void setSuspendOptimizationsNative (int reason , boolean enabled ) {
1332+ if (DBG ) log ("setSuspendOptimizationsNative: " + reason + " " + enabled );
1333+ if (enabled ) {
1334+ mSuspendOptNeedsDisabled &= ~reason ;
1335+ /* None of dhcp, screen or highperf need it disabled and user wants it enabled */
1336+ if (mSuspendOptNeedsDisabled == 0 && mUserWantsSuspendOpt .get ()) {
1337+ mWifiNative .setSuspendOptimizations (true );
1338+ if (DBG ) log ("Enabled, mSuspendOptNeedsDisabled " + mSuspendOptNeedsDisabled );
1339+ }
1340+ } else {
1341+ mSuspendOptNeedsDisabled |= reason ;
1342+ mWifiNative .setSuspendOptimizations (false );
1343+ if (DBG ) log ("Disabled, mSuspendOptNeedsDisabled " + mSuspendOptNeedsDisabled );
1344+ }
1345+ }
1346+
1347+ private void setSuspendOptimizations (int reason , boolean enabled ) {
1348+ if (DBG ) log ("setSuspendOptimizations: " + reason + " " + enabled );
1349+ if (enabled ) {
1350+ mSuspendOptNeedsDisabled &= ~reason ;
1351+ } else {
1352+ mSuspendOptNeedsDisabled |= reason ;
1353+ }
1354+ if (DBG ) log ("mSuspendOptNeedsDisabled " + mSuspendOptNeedsDisabled );
1355+ }
1356+
13271357 private void setWifiState (int wifiState ) {
13281358 final int previousWifiState = mWifiState .get ();
13291359
@@ -1736,18 +1766,16 @@ void handlePreDhcpSetup() {
17361766 mWifiNative .BLUETOOTH_COEXISTENCE_MODE_DISABLED );
17371767 }
17381768
1739- /* Disable power save during DHCP */
1740- if (mPowerSaveEnabled ) {
1741- mPowerSaveEnabled = false ;
1742- mWifiNative .setPowerSave (mPowerSaveEnabled );
1743- }
1769+ /* Disable power save and suspend optimizations during DHCP */
1770+ mWifiNative .setPowerSave (false );
1771+ setSuspendOptimizationsNative (SUSPEND_DUE_TO_DHCP , false );
17441772 }
17451773
17461774
17471775 void handlePostDhcpSetup () {
1748- /* Restore power save */
1749- mPowerSaveEnabled = true ;
1750- mWifiNative . setPowerSave ( mPowerSaveEnabled );
1776+ /* Restore power save and suspend optimizations */
1777+ mWifiNative . setPowerSave ( true ) ;
1778+ setSuspendOptimizationsNative ( SUSPEND_DUE_TO_DHCP , true );
17511779
17521780 // Set the coexistence mode back to its default value
17531781 mWifiNative .setBluetoothCoexistenceMode (
@@ -1880,7 +1908,11 @@ public boolean processMessage(Message message) {
18801908 mEnableBackgroundScan = (message .arg1 == 1 );
18811909 break ;
18821910 case CMD_SET_HIGH_PERF_MODE :
1883- mHighPerfMode = (message .arg1 == 1 );
1911+ if (message .arg1 == 1 ) {
1912+ setSuspendOptimizations (SUSPEND_DUE_TO_HIGH_PERF , false );
1913+ } else {
1914+ setSuspendOptimizations (SUSPEND_DUE_TO_HIGH_PERF , true );
1915+ }
18841916 break ;
18851917 /* Discard */
18861918 case CMD_LOAD_DRIVER :
@@ -1927,14 +1959,18 @@ public boolean processMessage(Message message) {
19271959 case CMD_RESPONSE_AP_CONFIG :
19281960 case WifiWatchdogStateMachine .POOR_LINK_DETECTED :
19291961 case WifiWatchdogStateMachine .GOOD_LINK_DETECTED :
1930- case CMD_CLEAR_SUSPEND_OPTIMIZATIONS :
19311962 case CMD_NO_NETWORKS_PERIODIC_SCAN :
19321963 break ;
19331964 case DhcpStateMachine .CMD_ON_QUIT :
19341965 mDhcpStateMachine = null ;
19351966 break ;
1936- case CMD_SET_SUSPEND_OPTIMIZATIONS :
1937- mSuspendWakeLock .release ();
1967+ case CMD_SET_SUSPEND_OPT_ENABLED :
1968+ if (message .arg1 == 1 ) {
1969+ mSuspendWakeLock .release ();
1970+ setSuspendOptimizations (SUSPEND_DUE_TO_SCREEN , true );
1971+ } else {
1972+ setSuspendOptimizations (SUSPEND_DUE_TO_SCREEN , false );
1973+ }
19381974 break ;
19391975 case WifiMonitor .DRIVER_HUNG_EVENT :
19401976 setWifiEnabled (false );
@@ -2670,7 +2706,7 @@ public void enter() {
26702706 mWifiNative .stopFilteringMulticastV4Packets ();
26712707 }
26722708
2673- mWifiNative .setPowerSave (mPowerSaveEnabled );
2709+ mWifiNative .setPowerSave (true );
26742710
26752711 if (mIsScanMode ) {
26762712 mWifiNative .setScanResultHandling (SCAN_ONLY_MODE );
@@ -2797,20 +2833,19 @@ public boolean processMessage(Message message) {
27972833 loge ("Illegal arugments to CMD_STOP_PACKET_FILTERING" );
27982834 }
27992835 break ;
2800- case CMD_SET_SUSPEND_OPTIMIZATIONS :
2801- if (!mHighPerfMode ) {
2802- mWifiNative .setSuspendOptimizations (true );
2836+ case CMD_SET_SUSPEND_OPT_ENABLED :
2837+ if (message .arg1 == 1 ) {
2838+ setSuspendOptimizationsNative (SUSPEND_DUE_TO_SCREEN , true );
2839+ mSuspendWakeLock .release ();
2840+ } else {
2841+ setSuspendOptimizationsNative (SUSPEND_DUE_TO_SCREEN , false );
28032842 }
2804- mSuspendWakeLock .release ();
2805- break ;
2806- case CMD_CLEAR_SUSPEND_OPTIMIZATIONS :
2807- mWifiNative .setSuspendOptimizations (false );
28082843 break ;
28092844 case CMD_SET_HIGH_PERF_MODE :
2810- mHighPerfMode = (message .arg1 == 1 );
2811- if ( mHighPerfMode ) {
2812- //Disable any suspend optimizations
2813- mWifiNative . setSuspendOptimizations ( false );
2845+ if (message .arg1 == 1 ) {
2846+ setSuspendOptimizationsNative ( SUSPEND_DUE_TO_HIGH_PERF , false );
2847+ } else {
2848+ setSuspendOptimizationsNative ( SUSPEND_DUE_TO_HIGH_PERF , true );
28142849 }
28152850 break ;
28162851 default :
0 commit comments