Persist and use country code reliably

It looks like we never persisted country code if it wasn't
set on the driver; this is not correct.

Also fixed initial sequence - the cached country code is now
trashed on reboot if config_wifi_revert_country_code_on_cellular_loss
is set. New country codes will be set if telephony stacks
retrieve them; or else, ro.boot.wificountrycode will stick.

Note that there are possible race conditions between telephony
finding country codes and starting Wifi functionality (like SoftAP)
so it is possible that Wifi uses more conservative country code
from ro.boot.wificountrycode even if telephony finds a better
country code later. The situation will recover eventually.

Bug: 24553903

Change-Id: I37bb2f64947ab04a82a0392eb85f521a08147e29
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java
index 341d3f1..e33c6ce 100644
--- a/service/java/com/android/server/wifi/WifiStateMachine.java
+++ b/service/java/com/android/server/wifi/WifiStateMachine.java
@@ -1197,7 +1197,12 @@
                     "but there is no default country code!! Resetting ...");
             mRevertCountryCodeOnCellularLoss = false;
         } else if (mRevertCountryCodeOnCellularLoss) {
-            logd("will revert to " + mDefaultCountryCode + " on MCC loss");
+            logd("initializing with and will revert to " + mDefaultCountryCode + " on MCC loss");
+        }
+
+        if (mRevertCountryCodeOnCellularLoss) {
+            Settings.Global.putString(mContext.getContentResolver(),
+                    Settings.Global.WIFI_COUNTRY_CODE, mDefaultCountryCode);
         }
 
         mUserWantsSuspendOpt.set(Settings.Global.getInt(mContext.getContentResolver(),
@@ -3708,16 +3713,7 @@
      * Set the country code from the system setting value, if any.
      */
     private void initializeCountryCode() {
-        String countryCode;
-        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);
-            countryCode = mDefaultCountryCode;
-        }
-
+        String countryCode = getCurrentCountryCode();
         if (countryCode != null && !countryCode.isEmpty()) {
             setCountryCode(countryCode, false);
         } else {
@@ -6117,11 +6113,12 @@
                         } else {
                             loge("Failed to set country code " + country);
                         }
-                        if (persist) {
-                            Settings.Global.putString(mContext.getContentResolver(),
-                                    Settings.Global.WIFI_COUNTRY_CODE,
-                                    country == null ? "" : country);
-                        }
+                    }
+
+                    if (persist) {
+                        Settings.Global.putString(mContext.getContentResolver(),
+                                Settings.Global.WIFI_COUNTRY_CODE,
+                                country == null ? "" : country);
                     }
 
                     mWifiP2pChannel.sendMessage(WifiP2pServiceImpl.SET_COUNTRY_CODE, country);