Fix case for not reverting country code on cellular loss
am: a32e200002

Change-Id: Ifb5e2c811e59e69e1b3743112e26249c789944f0
diff --git a/service/java/com/android/server/wifi/WifiCountryCode.java b/service/java/com/android/server/wifi/WifiCountryCode.java
index e69fb8e..66a035f 100644
--- a/service/java/com/android/server/wifi/WifiCountryCode.java
+++ b/service/java/com/android/server/wifi/WifiCountryCode.java
@@ -83,11 +83,9 @@
     public synchronized void simCardRemoved() {
         if (DBG) Log.d(TAG, "SIM Card Removed");
         // SIM card is removed, we need to reset the country code to phone default.
-        if (mRevertCountryCodeOnCellularLoss) {
-            mTelephonyCountryCode = null;
-            if (mReady) {
-                updateCountryCode();
-            }
+        mTelephonyCountryCode = null;
+        if (mReady) {
+            updateCountryCode();
         }
     }
 
@@ -98,12 +96,9 @@
      */
     public synchronized void airplaneModeEnabled() {
         if (DBG) Log.d(TAG, "Airplane Mode Enabled");
-        mTelephonyCountryCode = null;
         // Airplane mode is enabled, we need to reset the country code to phone default.
-        if (mRevertCountryCodeOnCellularLoss) {
-            mTelephonyCountryCode = null;
-            // Country code will be set upon when wpa_supplicant starts next time.
-        }
+        // Country code will be set upon when wpa_supplicant starts next time.
+        mTelephonyCountryCode = null;
     }
 
     /**
@@ -133,8 +128,10 @@
         if (DBG) Log.d(TAG, "Receive set country code request: " + countryCode);
         // Empty country code.
         if (TextUtils.isEmpty(countryCode)) {
-            if (DBG) Log.d(TAG, "Received empty country code, reset to default country code");
-            mTelephonyCountryCode = null;
+            if (mRevertCountryCodeOnCellularLoss) {
+                if (DBG) Log.d(TAG, "Received empty country code, reset to default country code");
+                mTelephonyCountryCode = null;
+            }
         } else {
             mTelephonyCountryCode = countryCode.toUpperCase();
         }
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiCountryCodeTest.java b/tests/wifitests/src/com/android/server/wifi/WifiCountryCodeTest.java
index 33aab60..fb4e71e 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiCountryCodeTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiCountryCodeTest.java
@@ -169,7 +169,8 @@
     }
 
     /**
-     * Test if we can reset to the default country code when phone is out of service.
+     * Test if we can reset to the default country code when phone is out of service, when
+     * |config_wifi_revert_country_code_on_cellular_loss| is set to true;
      * Telephony service calls |setCountryCode| with an empty string when phone is out of service.
      * In this case we should fall back to the default country code.
      * @throws Exception
@@ -184,4 +185,28 @@
         assertEquals(mDefaultCountryCode, mWifiCountryCode.getCountryCode());
     }
 
+    /**
+     * Test if we can keep using the last known country code when phone is out of service, when
+     * |config_wifi_revert_country_code_on_cellular_loss| is set to false;
+     * Telephony service calls |setCountryCode| with an empty string when phone is out of service.
+     * In this case we should keep using the last known country code.
+     * @throws Exception
+     */
+    @Test
+    public void doNotResetCountryCodeWhenOutOfService() throws Exception {
+        // Refresh mWifiCountryCode with |config_wifi_revert_country_code_on_cellular_loss|
+        // setting to false.
+        mWifiCountryCode = new WifiCountryCode(
+                mWifiNative,
+                mDefaultCountryCode,
+                false /* config_wifi_revert_country_code_on_cellular_loss */);
+
+        assertEquals(mDefaultCountryCode, mWifiCountryCode.getCountryCode());
+        mWifiCountryCode.setCountryCode(mTelephonyCountryCode);
+        assertEquals(mTelephonyCountryCode, mWifiCountryCode.getCountryCode());
+        // Out of service.
+        mWifiCountryCode.setCountryCode("");
+        assertEquals(mTelephonyCountryCode, mWifiCountryCode.getCountryCode());
+    }
+
 }