Skip to content

Commit f03d620

Browse files
committed
Fix persisting wifi state on setWifiEnabled() call
When somebody makes a quick setWifiEnabled calls in back to back succession, we were missing setting the last state because we were only doing that when wifi was in a particular state from a state machine's perspective. This was done to handle the interaction b/w airplane and wifi and was done in the wrong way. That part is now moved to the code which detects airplane mode changes. In the longer term, I want to move the whole persisting code as part of wifi state machine which is more aware of the exact states wifi is in. Bug: 6504534 Change-Id: I452f3f4efdeb84458dcfd280269e09ffa3844f05
1 parent ae14715 commit f03d620

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

services/java/com/android/server/WifiService.java

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,7 @@ public void handleMessage(Message msg) {
376376
@Override
377377
public void onReceive(Context context, Intent intent) {
378378
mAirplaneModeOn.set(isAirplaneModeOn());
379-
/* On airplane mode disable, restore wifi state if necessary */
380-
if (!mAirplaneModeOn.get() && (testAndClearWifiSavedState() ||
381-
mPersistWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE)) {
382-
persistWifiState(true);
383-
}
379+
handleAirplaneModeToggled();
384380
updateWifiState();
385381
}
386382
},
@@ -447,7 +443,10 @@ public void checkAndStartWifi() {
447443
boolean wifiEnabled = shouldWifiBeEnabled() || testAndClearWifiSavedState();
448444
Slog.i(TAG, "WifiService starting up with Wi-Fi " +
449445
(wifiEnabled ? "enabled" : "disabled"));
450-
setWifiEnabled(wifiEnabled);
446+
447+
// If we are already disabled (could be due to airplane mode), avoid changing persist
448+
// state here
449+
if (wifiEnabled) setWifiEnabled(wifiEnabled);
451450

452451
mWifiWatchdogStateMachine = WifiWatchdogStateMachine.
453452
makeWifiWatchdogStateMachine(mContext);
@@ -485,26 +484,43 @@ private boolean shouldWifiBeEnabled() {
485484
}
486485
}
487486

488-
private void persistWifiState(boolean enabled) {
489-
final ContentResolver cr = mContext.getContentResolver();
487+
private void handleWifiToggled(boolean enabled) {
490488
boolean airplane = mAirplaneModeOn.get() && isAirplaneToggleable();
491489
if (enabled) {
492490
if (airplane) {
493-
mPersistWifiState.set(WIFI_ENABLED_AIRPLANE_OVERRIDE);
491+
persistWifiState(WIFI_ENABLED_AIRPLANE_OVERRIDE);
494492
} else {
495-
mPersistWifiState.set(WIFI_ENABLED);
493+
persistWifiState(WIFI_ENABLED);
496494
}
497495
} else {
498-
if (airplane) {
499-
mPersistWifiState.set(WIFI_DISABLED_AIRPLANE_ON);
500-
} else {
501-
mPersistWifiState.set(WIFI_DISABLED);
502-
}
496+
// When wifi state is disabled, we do not care
497+
// if airplane mode is on or not. The scenario of
498+
// wifi being disabled due to airplane mode being turned on
499+
// is handled handleAirplaneModeToggled()
500+
persistWifiState(WIFI_DISABLED);
503501
}
502+
}
504503

505-
Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, mPersistWifiState.get());
504+
private void handleAirplaneModeToggled() {
505+
if (mAirplaneModeOn.get()) {
506+
// Wifi disabled due to airplane on
507+
if (mWifiEnabled) {
508+
persistWifiState(WIFI_DISABLED_AIRPLANE_ON);
509+
}
510+
} else {
511+
/* On airplane mode disable, restore wifi state if necessary */
512+
if (testAndClearWifiSavedState() ||
513+
mPersistWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE) {
514+
persistWifiState(WIFI_ENABLED);
515+
}
516+
}
506517
}
507518

519+
private void persistWifiState(int state) {
520+
final ContentResolver cr = mContext.getContentResolver();
521+
mPersistWifiState.set(state);
522+
Settings.Secure.putInt(cr, Settings.Secure.WIFI_ON, state);
523+
}
508524

509525
/**
510526
* see {@link android.net.wifi.WifiManager#pingSupplicant()}
@@ -576,12 +592,9 @@ public synchronized boolean setWifiEnabled(boolean enable) {
576592
* only CHANGE_WIFI_STATE is enforced
577593
*/
578594

579-
/* Avoids overriding of airplane state when wifi is already in the expected state */
580-
if (enable != mWifiEnabled) {
581-
long ident = Binder.clearCallingIdentity();
582-
persistWifiState(enable);
583-
Binder.restoreCallingIdentity(ident);
584-
}
595+
long ident = Binder.clearCallingIdentity();
596+
handleWifiToggled(enable);
597+
Binder.restoreCallingIdentity(ident);
585598

586599
if (enable) {
587600
if (!mIsReceiverRegistered) {

0 commit comments

Comments
 (0)