[automerger skipped] Merge "Revert "Fixed that OK button is not localized"" am: 4895135e54 -s ours

am skip reason: Merged-In Iaccf51e669dbf76ae4c9fcf149cec665f9090c97 with SHA-1 07666003e2 is already in history. Merged-In was found from reverted change.

Reverted change: https://android-review.googlesource.com/c/platform/packages/services/Telephony/+/1289450

Original change: https://android-review.googlesource.com/c/platform/packages/services/Telephony/+/2464552

Change-Id: I9637cb21002fde9f66f4252af9e56f9dbbdbbce6
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 41ff216..a427bac 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -193,6 +193,7 @@
             android:label="@string/emergencyDialerIconLabel"
             android:theme="@style/EmergencyDialerTheme"
             android:screenOrientation="portrait"
+            android:showWhenLocked="true"
             android:exported="true"
             android:resizeableActivity="false">
             <intent-filter>
@@ -509,7 +510,6 @@
         <activity android:name="com.android.phone.settings.VoicemailSettingsActivity"
             android:label="@string/voicemail"
             android:configChanges="orientation|screenSize|keyboardHidden|screenLayout"
-            android:screenOrientation="portrait"
             android:exported="true"
             android:theme="@style/CallSettingsWithoutDividerTheme">
             <intent-filter >
diff --git a/src/com/android/phone/EmergencyDialer.java b/src/com/android/phone/EmergencyDialer.java
index 9b7a43e..5fe8708 100644
--- a/src/com/android/phone/EmergencyDialer.java
+++ b/src/com/android/phone/EmergencyDialer.java
@@ -259,8 +259,6 @@
         mEntryType = getIntent().getIntExtra(EXTRA_ENTRY_TYPE, ENTRY_TYPE_UNKNOWN);
         Log.d(LOG_TAG, "Launched from " + entryTypeToString(mEntryType));
 
-        // Allow this activity to be displayed in front of the keyguard / lockscreen.
-        setShowWhenLocked(true);
         // Allow turning screen on
         setTurnScreenOn(true);
 
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 06d6849..a5a0783 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -50,6 +50,7 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
+import android.os.LocaleList;
 import android.os.Looper;
 import android.os.Message;
 import android.os.Messenger;
@@ -1414,6 +1415,7 @@
                     request = (MainThreadRequest) ar.userObj;
                     ResultReceiver result = (ResultReceiver) request.argument;
                     int error = 0;
+                    ModemActivityInfo ret = null;
                     if (mLastModemActivityInfo == null) {
                         mLastModemActivitySpecificInfo = new ActivityStatsTechSpecificInfo[1];
                         mLastModemActivitySpecificInfo[0] =
@@ -1432,12 +1434,14 @@
                         if (isModemActivityInfoValid(info)) {
                             mergeModemActivityInfo(info);
                         }
-                        mLastModemActivityInfo =
-                                new ModemActivityInfo(
-                                        mLastModemActivityInfo.getTimestampMillis(),
-                                        mLastModemActivityInfo.getSleepTimeMillis(),
-                                        mLastModemActivityInfo.getIdleTimeMillis(),
-                                        mLastModemActivitySpecificInfo);
+                        // This is needed to decouple ret from mLastModemActivityInfo
+                        // We don't want to return mLastModemActivityInfo which is updated
+                        // inside mergeModemActivityInfo()
+                        ret = new ModemActivityInfo(
+                                mLastModemActivityInfo.getTimestampMillis(),
+                                mLastModemActivityInfo.getSleepTimeMillis(),
+                                mLastModemActivityInfo.getIdleTimeMillis(),
+                                deepCopyModemActivitySpecificInfo(mLastModemActivitySpecificInfo));
 
                     } else {
                         if (ar.result == null) {
@@ -1455,10 +1459,10 @@
                         }
                     }
                     Bundle bundle = new Bundle();
-                    if (mLastModemActivityInfo != null) {
+                    if (ret != null) {
                         bundle.putParcelable(
                                 TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
-                                mLastModemActivityInfo);
+                                ret);
                     } else {
                         bundle.putInt(TelephonyManager.EXCEPTION_RESULT_KEY, error);
                     }
@@ -6326,8 +6330,14 @@
 
     private SecurityException checkNetworkRequestForSanitizedLocationAccess(
             NetworkScanRequest request, int subId, String callingPackage) {
-        boolean hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
-                == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
+        boolean hasCarrierPriv;
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
+                    == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
         boolean hasNetworkScanPermission =
                 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
                 == PERMISSION_GRANTED;
@@ -7691,7 +7701,7 @@
                 if (!localeFromDefaultSim.getCountry().isEmpty()) {
                     if (DBG) log("Using locale from subId: " + subId + " locale: "
                             + localeFromDefaultSim);
-                    return localeFromDefaultSim.toLanguageTag();
+                    return matchLocaleFromSupportedLocaleList(localeFromDefaultSim);
                 } else {
                     simLanguage = localeFromDefaultSim.getLanguage();
                 }
@@ -7704,7 +7714,7 @@
             final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
             if (mccLocale != null) {
                 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
-                return mccLocale.toLanguageTag();
+                return matchLocaleFromSupportedLocaleList(mccLocale);
             }
 
             if (DBG) log("No locale found - returning null");
@@ -7714,6 +7724,21 @@
         }
     }
 
+    @VisibleForTesting
+    String matchLocaleFromSupportedLocaleList(@NonNull Locale inputLocale) {
+        String[] supportedLocale = com.android.internal.app.LocalePicker.getSupportedLocales(
+                getDefaultPhone().getContext());
+        for (String localeTag : supportedLocale) {
+            if (LocaleList.matchesLanguageAndScript(
+                    inputLocale, Locale.forLanguageTag(localeTag))
+                    && inputLocale.getCountry().equals(
+                    Locale.forLanguageTag(localeTag).getCountry())) {
+                return localeTag;
+            }
+        }
+        return inputLocale.toLanguageTag();
+    }
+
     private List<SubscriptionInfo> getAllSubscriptionInfoList() {
         return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName(),
                 mApp.getAttributionTag());
@@ -7805,7 +7830,7 @@
     */
     private void mergeModemActivityInfo(ModemActivityInfo info) {
         List<ActivityStatsTechSpecificInfo> merged = new ArrayList<>();
-        ActivityStatsTechSpecificInfo mDeltaSpecificInfo;
+        ActivityStatsTechSpecificInfo deltaSpecificInfo;
         boolean matched;
         for (int i = 0; i < info.getSpecificInfoLength(); i++) {
             matched = false;
@@ -7830,13 +7855,13 @@
             }
 
             if (!matched) {
-                mDeltaSpecificInfo =
+                deltaSpecificInfo =
                         new ActivityStatsTechSpecificInfo(
                                 rat,
                                 freq,
                                 info.getTransmitTimeMillis(rat, freq),
                                 (int) info.getReceiveTimeMillis(rat, freq));
-                merged.addAll(Arrays.asList(mDeltaSpecificInfo));
+                merged.addAll(Arrays.asList(deltaSpecificInfo));
             }
         }
         merged.addAll(Arrays.asList(mLastModemActivitySpecificInfo));
@@ -7851,6 +7876,26 @@
         mLastModemActivityInfo.setIdleTimeMillis(
                 info.getIdleTimeMillis()
                 + mLastModemActivityInfo.getIdleTimeMillis());
+
+        mLastModemActivityInfo =
+                 new ModemActivityInfo(
+                         mLastModemActivityInfo.getTimestampMillis(),
+                         mLastModemActivityInfo.getSleepTimeMillis(),
+                         mLastModemActivityInfo.getIdleTimeMillis(),
+                         mLastModemActivitySpecificInfo);
+    }
+
+    private ActivityStatsTechSpecificInfo[] deepCopyModemActivitySpecificInfo(
+            ActivityStatsTechSpecificInfo[] info) {
+        int infoSize = info.length;
+        ActivityStatsTechSpecificInfo[] ret = new ActivityStatsTechSpecificInfo[infoSize];
+        for (int i = 0; i < infoSize; i++) {
+            ret[i] = new ActivityStatsTechSpecificInfo(
+                    info[i].getRat(), info[i].getFrequencyRange(),
+                    info[i].getTransmitTimeMillis(),
+                    (int) info[i].getReceiveTimeMillis());
+        }
+        return ret;
     }
 
     /**
diff --git a/src/com/android/phone/PhoneUtils.java b/src/com/android/phone/PhoneUtils.java
index 8832e73..d0aad4a 100644
--- a/src/com/android/phone/PhoneUtils.java
+++ b/src/com/android/phone/PhoneUtils.java
@@ -98,7 +98,6 @@
     private static final int THEME = com.android.internal.R.style.Theme_DeviceDefault_Dialog_Alert;
 
     /** USSD information used to aggregate all USSD messages */
-    private static AlertDialog sUssdDialog = null;
     private static StringBuilder sUssdMsg = new StringBuilder();
 
     private static final ComponentName PSTN_CONNECTION_SERVICE_COMPONENT =
@@ -588,39 +587,36 @@
         // displaying system alert dialog on the screen instead of
         // using another activity to display the message.  This
         // places the message at the forefront of the UI.
+        AlertDialog ussdDialog = new AlertDialog.Builder(context, THEME)
+                .setPositiveButton(R.string.ok, null)
+                .setCancelable(true)
+                .setOnDismissListener(new DialogInterface.OnDismissListener() {
+                    @Override
+                    public void onDismiss(DialogInterface dialog) {
+                        sUssdMsg.setLength(0);
+                    }
+                })
+                .create();
 
-        if (sUssdDialog == null) {
-            sUssdDialog = new AlertDialog.Builder(context, THEME)
-                    .setPositiveButton(R.string.ok, null)
-                    .setCancelable(true)
-                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
-                        @Override
-                        public void onDismiss(DialogInterface dialog) {
-                            sUssdMsg.setLength(0);
-                        }
-                    })
-                    .create();
+        ussdDialog.getWindow().setType(windowType);
+        ussdDialog.getWindow().addFlags(
+                WindowManager.LayoutParams.FLAG_DIM_BEHIND);
 
-            sUssdDialog.getWindow().setType(windowType);
-            sUssdDialog.getWindow().addFlags(
-                    WindowManager.LayoutParams.FLAG_DIM_BEHIND);
-        }
         if (sUssdMsg.length() != 0) {
-            sUssdMsg
-                    .insert(0, "\n")
+            sUssdMsg.insert(0, "\n")
                     .insert(0, app.getResources().getString(R.string.ussd_dialog_sep))
                     .insert(0, "\n");
         }
         if (phone != null && phone.getCarrierName() != null) {
-            sUssdDialog.setTitle(app.getResources().getString(R.string.carrier_mmi_msg_title,
+            ussdDialog.setTitle(app.getResources().getString(R.string.carrier_mmi_msg_title,
                     phone.getCarrierName()));
         } else {
-            sUssdDialog
+            ussdDialog
                     .setTitle(app.getResources().getString(R.string.default_carrier_mmi_msg_title));
         }
         sUssdMsg.insert(0, text);
-        sUssdDialog.setMessage(sUssdMsg.toString());
-        sUssdDialog.show();
+        ussdDialog.setMessage(sUssdMsg.toString());
+        ussdDialog.show();
     }
 
     /**
diff --git a/tests/src/com/android/TelephonyTestBase.java b/tests/src/com/android/TelephonyTestBase.java
index 09abb15..ffda81b 100644
--- a/tests/src/com/android/TelephonyTestBase.java
+++ b/tests/src/com/android/TelephonyTestBase.java
@@ -24,7 +24,9 @@
 
 import com.android.internal.telephony.PhoneConfigurationManager;
 
-import org.mockito.MockitoAnnotations;
+import org.junit.Rule;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
 
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executor;
@@ -34,11 +36,11 @@
  * Helper class to load Mockito Resources into a test.
  */
 public class TelephonyTestBase {
+    @Rule public final MockitoRule mocks = MockitoJUnit.rule();
 
     protected TestContext mContext;
 
     public void setUp() throws Exception {
-        MockitoAnnotations.initMocks(this);
         mContext = spy(new TestContext());
         // Set up the looper if it does not exist on the test thread.
         if (Looper.myLooper() == null) {
diff --git a/tests/src/com/android/phone/PhoneInterfaceManagerTest.java b/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
index fe5d013..ffc0177 100644
--- a/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
+++ b/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
@@ -16,6 +16,7 @@
 
 package com.android.phone;
 
+import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
@@ -35,6 +36,9 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.Locale;
 
 /**
  * Unit Test for CarrierConfigLoader.
@@ -54,6 +58,7 @@
     @UiThreadTest
     public void setUp() throws Exception {
         super.setUp();
+        MockitoAnnotations.initMocks(this);
         doReturn(mPackageManager).when(mPhoneGlobals).getPackageManager();
         doReturn(false).when(mPackageManager).hasSystemFeature(
                 PackageManager.FEATURE_TELEPHONY_IMS);
@@ -83,4 +88,19 @@
         verify(mPhone, never()).setAllowedNetworkTypes(
                 TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER, defaultNetworkType, null);
     }
+
+    @Test
+    public void matchLocaleFromSupportedLocaleList_inputLocaleChangeToSupportedLocale() {
+        // Input zh-TW, then look up the matched supported locale, zh-Hant-TW, instead.
+        String result1 = mPhoneInterfaceManager.matchLocaleFromSupportedLocaleList(
+                Locale.forLanguageTag("zh-TW"));
+
+        assertEquals(result1, "zh-Hant-TW");
+
+        // Input ff-BF, then find the matched supported locale, ff-Latn-BF, instead.
+        String result2 = mPhoneInterfaceManager.matchLocaleFromSupportedLocaleList(
+                Locale.forLanguageTag("ff-BF"));
+
+        assertEquals(result2, "ff-Latn-BF");
+    }
 }