@@ -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()}
@@ -578,12 +594,9 @@ public synchronized boolean setWifiEnabled(boolean enable) {
578594 * only CHANGE_WIFI_STATE is enforced
579595 */
580596
581- /* Avoids overriding of airplane state when wifi is already in the expected state */
582- if (enable != mWifiEnabled ) {
583- long ident = Binder .clearCallingIdentity ();
584- persistWifiState (enable );
585- Binder .restoreCallingIdentity (ident );
586- }
597+ long ident = Binder .clearCallingIdentity ();
598+ handleWifiToggled (enable );
599+ Binder .restoreCallingIdentity (ident );
587600
588601 if (enable ) {
589602 if (!mIsReceiverRegistered ) {
0 commit comments