@@ -152,8 +152,12 @@ public class WifiService extends IWifiManager.Stub {
152152 /* Wifi disabled due to airplane mode on */
153153 private static final int WIFI_DISABLED_AIRPLANE_ON = 3 ;
154154
155- private AtomicInteger mWifiState = new AtomicInteger (WIFI_DISABLED );
155+ /* Persisted state that tracks the wifi & airplane interaction from settings */
156+ private AtomicInteger mPersistWifiState = new AtomicInteger (WIFI_DISABLED );
157+ /* Tracks current airplane mode state */
156158 private AtomicBoolean mAirplaneModeOn = new AtomicBoolean (false );
159+ /* Tracks whether wifi is enabled from WifiStateMachine's perspective */
160+ private boolean mWifiEnabled ;
157161
158162 private boolean mIsReceiverRegistered = false ;
159163
@@ -373,8 +377,8 @@ public void onReceive(Context context, Intent intent) {
373377 mAirplaneModeOn .set (isAirplaneModeOn ());
374378 /* On airplane mode disable, restore wifi state if necessary */
375379 if (!mAirplaneModeOn .get () && (testAndClearWifiSavedState () ||
376- mWifiState .get () == WIFI_ENABLED_AIRPLANE_OVERRIDE )) {
377- persistWifiEnabled (true );
380+ mPersistWifiState .get () == WIFI_ENABLED_AIRPLANE_OVERRIDE )) {
381+ persistWifiState (true );
378382 }
379383 updateWifiState ();
380384 }
@@ -391,7 +395,12 @@ public void onReceive(Context context, Intent intent) {
391395 @ Override
392396 public void onReceive (Context context , Intent intent ) {
393397 if (intent .getAction ().equals (WifiManager .WIFI_STATE_CHANGED_ACTION )) {
394- // reset & clear notification on any wifi state change
398+ int wifiState = intent .getIntExtra (WifiManager .EXTRA_WIFI_STATE ,
399+ WifiManager .WIFI_STATE_DISABLED );
400+
401+ mWifiEnabled = (wifiState == WifiManager .WIFI_STATE_ENABLED );
402+
403+ // reset & clear notification on any wifi state change
395404 resetNotification ();
396405 } else if (intent .getAction ().equals (
397406 WifiManager .NETWORK_STATE_CHANGED_ACTION )) {
@@ -435,7 +444,7 @@ public void onReceive(Context context, Intent intent) {
435444 */
436445 public void checkAndStartWifi () {
437446 mAirplaneModeOn .set (isAirplaneModeOn ());
438- mWifiState .set (getPersistedWifiState ());
447+ mPersistWifiState .set (getPersistedWifiState ());
439448 /* Start if Wi-Fi should be enabled or the saved state indicates Wi-Fi was on */
440449 boolean wifiEnabled = shouldWifiBeEnabled () || testAndClearWifiSavedState ();
441450 Slog .i (TAG , "WifiService starting up with Wi-Fi " +
@@ -472,29 +481,30 @@ private int getPersistedWifiState() {
472481
473482 private boolean shouldWifiBeEnabled () {
474483 if (mAirplaneModeOn .get ()) {
475- return mWifiState .get () == WIFI_ENABLED_AIRPLANE_OVERRIDE ;
484+ return mPersistWifiState .get () == WIFI_ENABLED_AIRPLANE_OVERRIDE ;
476485 } else {
477- return mWifiState .get () != WIFI_DISABLED ;
486+ return mPersistWifiState .get () != WIFI_DISABLED ;
478487 }
479488 }
480489
481- private void persistWifiEnabled (boolean enabled ) {
490+ private void persistWifiState (boolean enabled ) {
482491 final ContentResolver cr = mContext .getContentResolver ();
483492 boolean airplane = mAirplaneModeOn .get () && isAirplaneToggleable ();
484493 if (enabled ) {
485494 if (airplane ) {
486- mWifiState .set (WIFI_ENABLED_AIRPLANE_OVERRIDE );
495+ mPersistWifiState .set (WIFI_ENABLED_AIRPLANE_OVERRIDE );
487496 } else {
488- mWifiState .set (WIFI_ENABLED );
497+ mPersistWifiState .set (WIFI_ENABLED );
489498 }
490499 } else {
491500 if (airplane ) {
492- mWifiState .set (WIFI_DISABLED_AIRPLANE_ON );
501+ mPersistWifiState .set (WIFI_DISABLED_AIRPLANE_ON );
493502 } else {
494- mWifiState .set (WIFI_DISABLED );
503+ mPersistWifiState .set (WIFI_DISABLED );
495504 }
496505 }
497- Settings .Secure .putInt (cr , Settings .Secure .WIFI_ON , mWifiState .get ());
506+
507+ Settings .Secure .putInt (cr , Settings .Secure .WIFI_ON , mPersistWifiState .get ());
498508 }
499509
500510
@@ -545,7 +555,6 @@ private void enforceMulticastChangePermission() {
545555 */
546556 public synchronized boolean setWifiEnabled (boolean enable ) {
547557 enforceChangePermission ();
548-
549558 if (DBG ) {
550559 Slog .e (TAG , "Invoking mWifiStateMachine.setWifiEnabled\n " );
551560 }
@@ -559,16 +568,20 @@ public synchronized boolean setWifiEnabled(boolean enable) {
559568 * Caller might not have WRITE_SECURE_SETTINGS,
560569 * only CHANGE_WIFI_STATE is enforced
561570 */
562- long ident = Binder .clearCallingIdentity ();
563- persistWifiEnabled (enable );
564- Binder .restoreCallingIdentity (ident );
571+
572+ /* Avoids overriding of airplane state when wifi is already in the expected state */
573+ if (enable != mWifiEnabled ) {
574+ long ident = Binder .clearCallingIdentity ();
575+ persistWifiState (enable );
576+ Binder .restoreCallingIdentity (ident );
577+ }
565578
566579 if (enable ) {
567580 if (!mIsReceiverRegistered ) {
568581 registerForBroadcasts ();
569582 mIsReceiverRegistered = true ;
570583 }
571- } else if (mIsReceiverRegistered ){
584+ } else if (mIsReceiverRegistered ) {
572585 mContext .unregisterReceiver (mReceiver );
573586 mIsReceiverRegistered = false ;
574587 }
0 commit comments