Merge "DO NOT MERGE Fix NPE in Telecom" into lmp-mr1-dev
diff --git a/src/com/android/server/telecom/TelecomService.java b/src/com/android/server/telecom/TelecomService.java
index 0e0f0a1..e914f32 100644
--- a/src/com/android/server/telecom/TelecomService.java
+++ b/src/com/android/server/telecom/TelecomService.java
@@ -657,11 +657,16 @@
         public void addNewIncomingCall(PhoneAccountHandle phoneAccountHandle, Bundle extras) {
             Log.i(this, "Adding new incoming call with phoneAccountHandle %s", phoneAccountHandle);
             if (phoneAccountHandle != null && phoneAccountHandle.getComponentName() != null) {
-                mAppOpsManager.checkPackage(
-                        Binder.getCallingUid(), phoneAccountHandle.getComponentName().getPackageName());
-
-                // Make sure it doesn't cross the UserHandle boundary
-                enforceUserHandleMatchesCaller(phoneAccountHandle);
+                // TODO(sail): Add unit tests for adding incoming calls from a SIM call manager.
+                if (isCallerSimCallManager() && TelephonyUtil.isPstnComponentName(
+                        phoneAccountHandle.getComponentName())) {
+                    Log.v(this, "Allowing call manager to add incoming call with PSTN handle");
+                } else {
+                    mAppOpsManager.checkPackage(Binder.getCallingUid(),
+                            phoneAccountHandle.getComponentName().getPackageName());
+                    // Make sure it doesn't cross the UserHandle boundary
+                    enforceUserHandleMatchesCaller(phoneAccountHandle);
+                }
 
                 Intent intent = new Intent(TelecomManager.ACTION_INCOMING_CALL);
                 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
@@ -932,6 +937,19 @@
         }
     }
 
+    private boolean isCallerSimCallManager() {
+        PhoneAccountHandle accountHandle = mPhoneAccountRegistrar.getSimCallManager();
+        if (accountHandle != null) {
+            try {
+                mAppOpsManager.checkPackage(
+                        Binder.getCallingUid(), accountHandle.getComponentName().getPackageName());
+                return true;
+            } catch (SecurityException e) {
+            }
+        }
+        return false;
+    }
+
     private boolean isDefaultDialerCalling() {
         ComponentName defaultDialerComponent = getDefaultPhoneAppInternal();
         if (defaultDialerComponent != null) {