Clear calling identity before calling TelephonyManager

Clear calling identity, when calling TelephonyManager, because callerUid must
be the process, where TelephonyManager was instantiated. Otherwise AppOps
check will fail due to process UID mismatch.

We also need to enforce READ_PHONE_STATE permission and do the app op check,
before making calls under new identity.

Bug: 21726452
Bug: 21878243
Change-Id: I26ac122eb2c2940c89dfbf89d6260a938f829217
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index ec8ce40..f18fc0c 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -2201,16 +2201,26 @@
     }
 
     @Override
-    public String[] getMergedSubscriberIds() {
+    public String[] getMergedSubscriberIds(String callingPackage) {
+        if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
+            return null;
+        }
         final Context context = mPhone.getContext();
         final TelephonyManager tele = TelephonyManager.from(context);
         final SubscriptionManager sub = SubscriptionManager.from(context);
 
         // Figure out what subscribers are currently active
         final ArraySet<String> activeSubscriberIds = new ArraySet<>();
-        final int[] subIds = sub.getActiveSubscriptionIdList();
-        for (int subId : subIds) {
-            activeSubscriberIds.add(tele.getSubscriberId(subId));
+        // Clear calling identity, when calling TelephonyManager, because callerUid must be
+        // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
+        final long identity  = Binder.clearCallingIdentity();
+        try {
+            final int[] subIds = sub.getActiveSubscriptionIdList();
+            for (int subId : subIds) {
+                activeSubscriberIds.add(tele.getSubscriberId(subId));
+            }
+        } finally {
+            Binder.restoreCallingIdentity(identity);
         }
 
         // First pass, find a number override for an active subscriber