Don't reset country code on mobile signal loss

Since mobile signal is lost too often; resetting country
code on this trigger causes too many changes. This change
restricts resetting country code only on two triggers

1. When airplane mode is turned ON
2. When SIM is removed

all other triggeres to reset country code are ignored.

Bug: 24363012
Change-Id: I5bc4bd8af7461da916c9a7638cf9cb457c73ac8b
diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java
index 269000f..2b83b68 100644
--- a/service/java/com/android/server/wifi/WifiServiceImpl.java
+++ b/service/java/com/android/server/wifi/WifiServiceImpl.java
@@ -69,6 +69,7 @@
 
 import com.android.internal.R;
 import com.android.internal.app.IBatteryStats;
+import com.android.internal.telephony.IccCardConstants;
 import com.android.internal.telephony.TelephonyIntents;
 import com.android.internal.util.AsyncChannel;
 import com.android.server.am.BatteryStatsService;
@@ -358,10 +359,27 @@
                         if (mSettingsStore.handleAirplaneModeToggled()) {
                             mWifiController.sendMessage(CMD_AIRPLANE_TOGGLED);
                         }
+                        if (mSettingsStore.isAirplaneModeOn()) {
+                            Log.d(TAG, "resetting country code because Airplane mode is ON");
+                            mWifiStateMachine.resetCountryCode();
+                        }
                     }
                 },
                 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
 
+        mContext.registerReceiver(
+                new BroadcastReceiver() {
+                    @Override
+                    public void onReceive(Context context, Intent intent) {
+                        String state = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
+                        if (state.equals(IccCardConstants.INTENT_VALUE_ICC_ABSENT)) {
+                            Log.d(TAG, "resetting country code because SIM is removed");
+                            mWifiStateMachine.resetCountryCode();
+                        }
+                    }
+                },
+                new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED));
+
         // Adding optimizations of only receiving broadcasts when wifi is enabled
         // can result in race conditions when apps toggle wifi in the background
         // without active user involvement. Always receive broadcasts.
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java
index 87ed29c..341d3f1 100644
--- a/service/java/com/android/server/wifi/WifiStateMachine.java
+++ b/service/java/com/android/server/wifi/WifiStateMachine.java
@@ -2434,24 +2434,32 @@
         // wifi connection is terminated; ignore resetting of code
         // for now (it is unclear what the chipset should do when
         // country code is reset)
+
         // if mCountryCodeSequence == 0, it is the first time to set country code, always set
         // else only when the new country code is different from the current one to set
-        int countryCodeSequence = mCountryCodeSequence.get();
-        String currentCountryCode = getCurrentCountryCode();
 
-        if (countryCodeSequence == 0
-                || TextUtils.equals(countryCode, currentCountryCode) == false) {
+        if (TextUtils.isEmpty(countryCode)) {
+            if (DBG) log("Ignoring resetting of country code");
+        } else {
+            int countryCodeSequence = mCountryCodeSequence.get();
+            String currentCountryCode = getCurrentCountryCode();
+            if (countryCodeSequence == 0
+                    || TextUtils.equals(countryCode, currentCountryCode) == false) {
 
-            countryCodeSequence = mCountryCodeSequence.incrementAndGet();
-            if (TextUtils.isEmpty(countryCode)) {
-                if (mRevertCountryCodeOnCellularLoss && !TextUtils.isEmpty(mDefaultCountryCode)) {
-                    if (DBG) Log.d(TAG,"Reverting wifi country code to default of "
-                                    + mDefaultCountryCode);
-                    countryCode = mDefaultCountryCode;
-                }
+                countryCodeSequence = mCountryCodeSequence.incrementAndGet();
+                sendMessage(CMD_SET_COUNTRY_CODE, countryCodeSequence, persist ? 1 : 0, 
+                        countryCode);
             }
-            sendMessage(CMD_SET_COUNTRY_CODE, countryCodeSequence, persist ? 1 : 0,
-                    countryCode);
+        }
+    }
+
+    /**
+     * reset the country code to default
+     */
+    public synchronized void resetCountryCode() {
+        if (mRevertCountryCodeOnCellularLoss && TextUtils.isEmpty(mDefaultCountryCode) == false) {
+            logd("resetting country code to " + mDefaultCountryCode);
+            setCountryCode(mDefaultCountryCode, /* persist = */ true);
         }
     }
 
@@ -2470,14 +2478,13 @@
     /**
      * Get the country code
      *
-     * @param countryCode following ISO 3166 format
+     * @return countryCode following ISO 3166 format
      */
     public String getCurrentCountryCode() {
         return Settings.Global.getString(
                 mContext.getContentResolver(), Settings.Global.WIFI_COUNTRY_CODE);
     }
 
-
     /**
      * Set the operational frequency band
      *
@@ -3705,9 +3712,9 @@
         if (!mRevertCountryCodeOnCellularLoss || TextUtils.isEmpty(mDefaultCountryCode) ) {
             countryCode = Settings.Global.getString(mContext.getContentResolver(),
                     Settings.Global.WIFI_COUNTRY_CODE);
+            Log.d(TAG,"Initialize wifi country code to persisted value of " + countryCode);
         } else {
-            Log.d(TAG,"Initialize wifi country code to default of "
-                                    + mDefaultCountryCode);
+            Log.d(TAG,"Initialize wifi country code to default of " + mDefaultCountryCode);
             countryCode = mDefaultCountryCode;
         }
 
@@ -3735,8 +3742,6 @@
         }
     }
 
-
-
     private void setSuspendOptimizationsNative(int reason, boolean enabled) {
         if (DBG) {
             log("setSuspendOptimizationsNative: " + reason + " " + enabled