Merge "Change unregisterCallback to destroy." into mnc-dev
diff --git a/res/values/strings.xml b/res/values/strings.xml
index b09cdce..d166cb6 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -75,7 +75,7 @@
 
     <!-- Message indicating that the user is not allowed to make non-emergency outgoing phone calls
          due to a user restriction -->
-    <string name="outgoing_call_not_allowed">Only emergency calls are allowed by the device owner</string>
+    <string name="outgoing_call_not_allowed">Only emergency calls are allowed by the device owner.</string>
 
     <!-- Call failure message displayed in an error dialog used to indicate that a phone number was not provided -->
     <string name="outgoing_call_error_no_phone_number_supplied">To place a call, enter a valid number.</string>
diff --git a/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java b/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java
index c1cf7f8..a3f56b7 100644
--- a/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java
+++ b/src/com/android/server/telecom/NewOutgoingCallIntentBroadcaster.java
@@ -16,6 +16,7 @@
 
 package com.android.server.telecom;
 
+import android.app.AppOpsManager;
 import com.android.server.telecom.components.UserCallIntentProcessor;
 
 import android.app.Activity;
@@ -53,9 +54,6 @@
  * prior to sending ACTION_NEW_OUTGOING_CALL and cannot be redirected nor prevented.
  */
 class NewOutgoingCallIntentBroadcaster {
-    /** Required permission for any app that wants to consume ACTION_NEW_OUTGOING_CALL. */
-    private static final String PERMISSION = android.Manifest.permission.PROCESS_OUTGOING_CALLS;
-
     private static final String EXTRA_ACTUAL_NUMBER_TO_DIAL =
             "android.telecom.extra.ACTUAL_NUMBER_TO_DIAL";
 
@@ -289,7 +287,8 @@
         mContext.sendOrderedBroadcastAsUser(
                 broadcastIntent,
                 UserHandle.CURRENT,
-                PERMISSION,
+                android.Manifest.permission.PROCESS_OUTGOING_CALLS,
+                AppOpsManager.OP_PROCESS_OUTGOING_CALLS,
                 receiverRequired ? new NewOutgoingCallBroadcastIntentReceiver() : null,
                 null,  // scheduler
                 Activity.RESULT_OK,  // initialCode
diff --git a/src/com/android/server/telecom/TelecomServiceImpl.java b/src/com/android/server/telecom/TelecomServiceImpl.java
index a012e5f..63e9e49 100644
--- a/src/com/android/server/telecom/TelecomServiceImpl.java
+++ b/src/com/android/server/telecom/TelecomServiceImpl.java
@@ -358,7 +358,7 @@
         public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number,
                 String callingPackage) {
             synchronized (mLock) {
-                if (!isDefaultDialerCalling()
+                if (!isPrivilegedDialerCalling(callingPackage)
                         && !canReadPhoneState(callingPackage, "isVoiceMailNumber")) {
                     return false;
                 }
@@ -382,7 +382,7 @@
         @Override
         public String getVoiceMailNumber(PhoneAccountHandle accountHandle, String callingPackage) {
             synchronized (mLock) {
-                if (!isDefaultDialerCalling()
+                if (!isPrivilegedDialerCalling(callingPackage)
                         && !canReadPhoneState(callingPackage, "getVoiceMailNumber")) {
                     return null;
                 }
@@ -411,7 +411,7 @@
          */
         @Override
         public String getLine1Number(PhoneAccountHandle accountHandle, String callingPackage) {
-            if (!isDefaultDialerCalling()
+            if (!isPrivilegedDialerCalling(callingPackage)
                     && !canReadPhoneState(callingPackage, "getLine1Number")) {
                 return null;
             }
@@ -436,9 +436,9 @@
          * @see android.telecom.TelecomManager#silenceRinger
          */
         @Override
-        public void silenceRinger() {
+        public void silenceRinger(String callingPackage) {
             synchronized (mLock) {
-                enforceModifyPermissionOrDefaultDialer();
+                enforceModifyPermissionOrPrivilegedDialer(callingPackage);
                 mCallsManager.getRinger().silence();
             }
         }
@@ -465,12 +465,7 @@
          */
         @Override
         public String getDefaultDialerPackage() {
-            final ComponentName defaultDialer =
-                    DefaultDialerManager.getDefaultDialerApplication(mContext);
-            if (defaultDialer != null) {
-                return defaultDialer.getPackageName();
-            }
-            return null;
+            return DefaultDialerManager.getDefaultDialerApplication(mContext);
         }
 
         /**
@@ -548,7 +543,7 @@
          */
         @Override
         public void showInCallScreen(boolean showDialpad, String callingPackage) {
-            if (!isDefaultDialerCalling()
+            if (!isPrivilegedDialerCalling(callingPackage)
                     && !canReadPhoneState(callingPackage, "showInCallScreen")) {
                 return;
             }
@@ -562,9 +557,9 @@
          * @see android.telecom.TelecomManager#cancelMissedCallsNotification
          */
         @Override
-        public void cancelMissedCallsNotification() {
+        public void cancelMissedCallsNotification(String callingPackage) {
             synchronized (mLock) {
-                enforceModifyPermissionOrDefaultDialer();
+                enforceModifyPermissionOrPrivilegedDialer(callingPackage);
                 mCallsManager.getMissedCallNotifier().clearMissedCalls();
             }
         }
@@ -573,9 +568,9 @@
          * @see android.telecom.TelecomManager#handleMmi
          */
         @Override
-        public boolean handlePinMmi(String dialString) {
+        public boolean handlePinMmi(String dialString, String callingPackage) {
             synchronized (mLock) {
-                enforceModifyPermissionOrDefaultDialer();
+                enforceModifyPermissionOrPrivilegedDialer(callingPackage);
 
                 // Switch identity so that TelephonyManager checks Telecom's permissions instead.
                 long token = Binder.clearCallingIdentity();
@@ -596,9 +591,10 @@
         @Override
         public boolean handlePinMmiForPhoneAccount(
                 PhoneAccountHandle accountHandle,
-                String dialString) {
+                String dialString,
+                String callingPackage) {
             synchronized (mLock) {
-                enforceModifyPermissionOrDefaultDialer();
+                enforceModifyPermissionOrPrivilegedDialer(callingPackage);
 
                 if (!isVisibleToCaller(accountHandle)) {
                     Log.w(this, "%s is not visible for the calling user", accountHandle);
@@ -624,9 +620,10 @@
          * @see android.telecom.TelecomManager#getAdnUriForPhoneAccount
          */
         @Override
-        public Uri getAdnUriForPhoneAccount(PhoneAccountHandle accountHandle) {
+        public Uri getAdnUriForPhoneAccount(PhoneAccountHandle accountHandle,
+                String callingPackage) {
             synchronized (mLock) {
-                enforceModifyPermissionOrDefaultDialer();
+                enforceModifyPermissionOrPrivilegedDialer(callingPackage);
 
                 if (!isVisibleToCaller(accountHandle)) {
                     Log.w(this, "%s is not visible for the calling user", accountHandle);
@@ -744,10 +741,12 @@
                         + " is not allowed to place phone calls");
             }
             synchronized (mLock) {
+                final UserHandle userHandle = Binder.getCallingUserHandle();
                 long token = Binder.clearCallingIdentity();
                 final Intent intent = new Intent(Intent.ACTION_CALL, handle);
                 intent.putExtras(extras);
-                new UserCallIntentProcessor(mContext).processIntent(intent, callingPackage);
+                new UserCallIntentProcessor(mContext, userHandle).processIntent(intent,
+                        callingPackage);
                 Binder.restoreCallingIdentity(token);
             }
         }
@@ -952,9 +951,15 @@
         }
     }
 
-    private void enforceModifyPermissionOrDefaultDialer() {
-        if (!isDefaultDialerCalling()) {
-            enforceModifyPermission();
+    private void enforceModifyPermissionOrPrivilegedDialer(String packageName) {
+        if (!isPrivilegedDialerCalling(packageName)) {
+            try {
+                enforceModifyPermission();
+            } catch (SecurityException e) {
+                Log.e(this, e, "Caller must be the default or system dialer, or have the system"
+                        + " only permission MODIFY_PHONE_STATE to perform this operation.");
+                throw e;
+            }
         }
     }
 
@@ -1038,25 +1043,9 @@
         return false;
     }
 
-    private boolean isDefaultDialerCalling() {
-        ComponentName defaultDialerComponent = getDefaultPhoneAppInternal();
-        if (defaultDialerComponent != null) {
-            try {
-                mAppOpsManager.checkPackage(
-                        Binder.getCallingUid(), defaultDialerComponent.getPackageName());
-                return true;
-            } catch (SecurityException e) {
-                Log.i(this, "Calling uid %d is not the default dialer.", Binder.getCallingUid());
-            }
-        }
-        return false;
-    }
-
-    private ComponentName getDefaultPhoneAppInternal() {
-        Resources resources = mContext.getResources();
-        return new ComponentName(
-                resources.getString(R.string.ui_default_package),
-                resources.getString(R.string.dialer_default_class));
+    private boolean isPrivilegedDialerCalling(String callingPackage) {
+        mAppOpsManager.checkPackage(Binder.getCallingUid(), callingPackage);
+        return DefaultDialerManager.isDefaultOrSystemDialer(mContext, callingPackage);
     }
 
     private TelephonyManager getTelephonyManager() {
diff --git a/src/com/android/server/telecom/components/UserCallActivity.java b/src/com/android/server/telecom/components/UserCallActivity.java
index ccff468..ae9004c 100644
--- a/src/com/android/server/telecom/components/UserCallActivity.java
+++ b/src/com/android/server/telecom/components/UserCallActivity.java
@@ -21,8 +21,11 @@
 import com.android.server.telecom.TelecomSystem;
 
 import android.app.Activity;
+import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
+import android.os.UserHandle;
+import android.os.UserManager;
 import android.telecom.TelecomManager;
 
 // TODO: Needed for move to system service: import com.android.internal.R;
@@ -53,7 +56,10 @@
         // See OutgoingCallBroadcaster in services/Telephony for more.
         Intent intent = getIntent();
         verifyCallAction(intent);
-        new UserCallIntentProcessor(this).processIntent(getIntent(), getCallingPackage());
+        final UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
+        final UserHandle userHandle = new UserHandle(userManager.getUserHandle());
+        new UserCallIntentProcessor(this, userHandle).processIntent(getIntent(),
+                getCallingPackage());
         finish();
     }
 
diff --git a/src/com/android/server/telecom/components/UserCallIntentProcessor.java b/src/com/android/server/telecom/components/UserCallIntentProcessor.java
index d50073d..d19a260 100644
--- a/src/com/android/server/telecom/components/UserCallIntentProcessor.java
+++ b/src/com/android/server/telecom/components/UserCallIntentProcessor.java
@@ -21,13 +21,11 @@
 import com.android.server.telecom.R;
 import com.android.server.telecom.TelephonyUtil;
 
-import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.net.Uri;
 import android.os.UserHandle;
 import android.os.UserManager;
-import android.provider.Settings;
 import android.telecom.PhoneAccount;
 import android.telecom.TelecomManager;
 import android.telecom.VideoProfile;
@@ -57,9 +55,11 @@
 public class UserCallIntentProcessor {
 
     private final Context mContext;
+    private final UserHandle mUserHandle;
 
-    public UserCallIntentProcessor(Context context) {
+    public UserCallIntentProcessor(Context context, UserHandle userHandle) {
         mContext = context;
+        mUserHandle = userHandle;
     }
 
     /**
@@ -93,15 +93,12 @@
         }
 
         UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
-        if (userManager.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS)
+        if (userManager.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS, mUserHandle)
                 && !TelephonyUtil.shouldProcessAsEmergency(mContext, handle)) {
             // Only emergency calls are allowed for users with the DISALLOW_OUTGOING_CALLS
             // restriction.
-            Toast.makeText(
-                    mContext,
-                    mContext.getResources().getString(R.string.outgoing_call_not_allowed),
-                    Toast.LENGTH_SHORT).show();
-            Log.d(this, "Rejecting non-emergency phone call due to DISALLOW_OUTGOING_CALLS "
+            showErrorDialogForRestrictedOutgoingCall(mContext);
+            Log.w(this, "Rejecting non-emergency phone call due to DISALLOW_OUTGOING_CALLS "
                     + "restriction");
             return;
         }
@@ -173,4 +170,12 @@
         mContext.sendBroadcastAsUser(intent, UserHandle.OWNER);
         return true;
     }
+
+    private static void showErrorDialogForRestrictedOutgoingCall(Context context) {
+        final Intent intent = new Intent(context, ErrorDialogActivity.class);
+        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        intent.putExtra(ErrorDialogActivity.ERROR_MESSAGE_ID_EXTRA,
+                R.string.outgoing_call_not_allowed);
+        context.startActivityAsUser(intent, UserHandle.CURRENT);
+    }
 }
diff --git a/testapps/res/layout/testdialer_main.xml b/testapps/res/layout/testdialer_main.xml
index 4f2d6bb..2c3e5e4 100644
--- a/testapps/res/layout/testdialer_main.xml
+++ b/testapps/res/layout/testdialer_main.xml
@@ -39,4 +39,9 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/testVoicemailButton" />
+    <Button
+        android:id="@+id/cancel_missed_button"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="@string/cancelMissedButton" />
 </LinearLayout>
diff --git a/testapps/res/values/donottranslate_strings.xml b/testapps/res/values/donottranslate_strings.xml
index 6a163d4..43c302d 100644
--- a/testapps/res/values/donottranslate_strings.xml
+++ b/testapps/res/values/donottranslate_strings.xml
@@ -33,4 +33,8 @@
     <!-- String for button in TestDialerActivity that performs voicemail requests to verify
             voicemail permissions -->
     <string name="testVoicemailButton">Exercise voicemail permissions</string>
+
+    <!-- String for button in TestDialerActivity that tries to exercise the
+            TelecomManager.cancelMissedCallNotifications() functionality -->
+    <string name="cancelMissedButton">Cancel missed calls</string>
 </resources>
diff --git a/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java b/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
index 0e67627..1da8906 100644
--- a/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
+++ b/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
@@ -163,6 +163,7 @@
             capabilities |= CAPABILITY_MUTE;
             capabilities |= CAPABILITY_SUPPORT_HOLD;
             capabilities |= CAPABILITY_HOLD;
+            capabilities |= CAPABILITY_RESPOND_VIA_TEXT;
             setConnectionCapabilities(capabilities);
 
             LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(
diff --git a/testapps/src/com/android/server/telecom/testapps/TestDialerActivity.java b/testapps/src/com/android/server/telecom/testapps/TestDialerActivity.java
index dcef96a..a756284 100644
--- a/testapps/src/com/android/server/telecom/testapps/TestDialerActivity.java
+++ b/testapps/src/com/android/server/telecom/testapps/TestDialerActivity.java
@@ -2,11 +2,13 @@
 
 import android.app.Activity;
 import android.content.ContentValues;
+import android.content.Context;
 import android.content.Intent;
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.CallLog.Calls;
 import android.telecom.PhoneAccount;
+import android.telecom.TelecomManager;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.EditText;
@@ -27,6 +29,7 @@
                 setDefault();
             }
         });
+
         findViewById(R.id.place_call_button).setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
@@ -41,6 +44,13 @@
             }
         });
 
+        findViewById(R.id.cancel_missed_button).setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                cancelMissedCallNotification();
+            }
+        });
+
         mNumberView = (EditText) findViewById(R.id.number);
         updateEditTextWithNumber();
     }
@@ -82,4 +92,15 @@
         }
         Toast.makeText(this, "Permission check succeeded", Toast.LENGTH_SHORT).show();
     }
+
+    private void cancelMissedCallNotification() {
+        try {
+            final TelecomManager tm = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
+            tm.cancelMissedCallsNotification();
+        } catch (SecurityException e) {
+            Toast.makeText(this, "Privileged dialer operation failed", Toast.LENGTH_SHORT).show();
+            return;
+        }
+        Toast.makeText(this, "Privileged dialer operation succeeded", Toast.LENGTH_SHORT).show();
+    }
 }