Fix "No service" is not shown without SIM

"Emergency calls only" is shown instead of "No service" in statusbar
even if within the area specified by
"config_display_no_service_when_sim_unready" in the following cases.

1. Power on the device without SIM.
   When updateSpnDisplay() is called, the country code may not be
   updated yet. So the plmn keeps as "Emergency calls only".

2. Remove SIM card from the device which supports hot swap SIM.
   After removing the SIM, the UiccApplication becomes null.
   If UiccApplication is null, mIsSimReady is not updated to false.
   "No service" is not shown when mIsSimReady is true,
   so the problem occur.

How to fix the problem:
1. When last known country code is changed, update plmn by
   updateSpnDisplay().
2. If UiccApplication is null, update mIsSimReady to false.

Test: manual - "No service" is shown in lock screen and statusbar
without SIM.
Test: auto - Passed ServiceStateTrackerTest.
Bug: 151881495

Merged-in: I8115fdd0b4760069a309a55ddd8b579785d7ce46
Change-Id: I8115fdd0b4760069a309a55ddd8b579785d7ce46
(cherry picked from commit 250fb97f09dc82b4f9015d12522b35c8fed7abe5)
diff --git a/src/java/com/android/internal/telephony/ServiceStateTracker.java b/src/java/com/android/internal/telephony/ServiceStateTracker.java
index 8fd9cb5..13c44eb 100755
--- a/src/java/com/android/internal/telephony/ServiceStateTracker.java
+++ b/src/java/com/android/internal/telephony/ServiceStateTracker.java
@@ -529,6 +529,8 @@
     /** To identify whether EVENT_SIM_READY is received or not */
     private boolean mIsSimReady = false;
 
+    private String mLastKnownNetworkCountry = "";
+
     @UnsupportedAppUsage
     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
         @Override
@@ -554,6 +556,12 @@
             } else if (intent.getAction().equals(ACTION_RADIO_OFF)) {
                 mAlarmSwitch = false;
                 powerOffRadioSafely();
+            } else if (intent.getAction().equals(TelephonyManager.ACTION_NETWORK_COUNTRY_CHANGED)) {
+                String lastKnownNetworkCountry = intent.getStringExtra(
+                        TelephonyManager.EXTRA_LAST_KNOWN_NETWORK_COUNTRY);
+                if (!mLastKnownNetworkCountry.equals(lastKnownNetworkCountry)) {
+                    updateSpnDisplay();
+                }
             }
         }
     };
@@ -675,6 +683,9 @@
         filter = new IntentFilter();
         filter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
         context.registerReceiver(mIntentReceiver, filter);
+        filter = new IntentFilter();
+        filter.addAction(TelephonyManager.ACTION_NETWORK_COUNTRY_CHANGED);
+        context.registerReceiver(mIntentReceiver, filter);
 
         mPhone.notifyOtaspChanged(TelephonyManager.OTASP_UNINITIALIZED);
 
@@ -1160,8 +1171,8 @@
                     mCdnr.updateEfFromUsim(null /* Usim */);
                 }
                 onUpdateIccAvailability();
-                if (mUiccApplcation != null
-                        && mUiccApplcation.getState() != AppState.APPSTATE_READY) {
+                if (mUiccApplcation == null
+                        || mUiccApplcation.getState() != AppState.APPSTATE_READY) {
                     mIsSimReady = false;
                     updateSpnDisplay();
                 }
@@ -2854,9 +2865,9 @@
         if (ArrayUtils.isEmpty(countriesWithNoService)) {
             return false;
         }
-        String currentCountry = mLocaleTracker.getCurrentCountry();
+        mLastKnownNetworkCountry = mLocaleTracker.getLastKnownCountryIso();
         for (String country : countriesWithNoService) {
-            if (country.equalsIgnoreCase(currentCountry)) {
+            if (country.equalsIgnoreCase(mLastKnownNetworkCountry)) {
                 return true;
             }
         }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/ServiceStateTrackerTest.java b/tests/telephonytests/src/com/android/internal/telephony/ServiceStateTrackerTest.java
index 55b7a2a..44e30ac 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/ServiceStateTrackerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/ServiceStateTrackerTest.java
@@ -2564,7 +2564,7 @@
     @Test
     public void testShouldForceDisplayNoService_forceBasedOnLocale() {
         // set up unaffected locale (US) and clear the resource
-        doReturn("us").when(mLocaleTracker).getCurrentCountry();
+        doReturn("us").when(mLocaleTracker).getLastKnownCountryIso();
         mContextFixture.putStringArrayResource(
                 com.android.internal.R.array.config_display_no_service_when_sim_unready,
                 new String[0]);
@@ -2574,11 +2574,11 @@
         mContextFixture.putStringArrayResource(
                 com.android.internal.R.array.config_display_no_service_when_sim_unready,
                 new String[]{"de"});
-        doReturn("us").when(mLocaleTracker).getCurrentCountry();
+        doReturn("us").when(mLocaleTracker).getLastKnownCountryIso();
         assertFalse(sst.shouldForceDisplayNoService());
 
         // mock the locale to Germany
-        doReturn("de").when(mLocaleTracker).getCurrentCountry();
+        doReturn("de").when(mLocaleTracker).getLastKnownCountryIso();
         assertTrue(sst.shouldForceDisplayNoService());
     }