Merge "Add Call.Detail.PROPERTY_* values." into mnc-dev
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 31b2f84..5547624 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -224,6 +224,18 @@
                 android:process=":ui">
         </activity>
 
+        <activity android:name=".components.ChangeDefaultDialerDialog"
+                  android:label="@string/change_default_dialer_dialog_title"
+                  android:excludeFromRecents="true"
+                  android:theme="@*android:style/Theme.Material.Light.Dialog.Alert"
+                  android:priority="1000"
+                  android:process=":ui" >
+            <intent-filter>
+                <action android:name="android.telecom.action.CHANGE_DEFAULT_DIALER" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+
         <receiver android:name=".components.PrimaryCallReceiver"
                 android:exported="true"
                 android:permission="android.permission.MODIFY_PHONE_STATE"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index b09cdce..a2295b5 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>
@@ -94,6 +94,15 @@
     <!-- Button label on the "Missing voicemail number" dialog -->
     <string name="add_vm_number_str">Add number</string>
 
+    <!-- Title of dialog used to comfirm whether the user intends to change the default dialer
+            application [CHAR LIMIT=55]-->
+    <string name="change_default_dialer_dialog_title">Change default Dialer app?</string>
+    <!-- Text in dialog used to confirm whether or not the user intends to change the default dialer, if a different default dialer has been previously set. -->
+    <string name="change_default_dialer_with_previous_app_set_text">Use <xliff:g id="new_app">%1$s</xliff:g> instead of <xliff:g id="current_app">%2$s</xliff:g> as your default dialer app?</string>
+    <!-- Text in dialog used to confirm whether or not the user intends to change the default dialer, if a different default dialer has not been previously set. -->
+    <string name="change_default_dialer_no_previous_app_set_text">Use <xliff:g id="new_app">%s</xliff:g> as your default dialer app?</string>
+
+
     <!-- DO NOT TRANSLATE. Label for test Subscription 0. -->
     <string name="test_account_0_label">Q Mobile</string>
     <!-- DO NOT TRANSLATE. Label for test Subscription 1. -->
diff --git a/src/com/android/server/telecom/CallIntentProcessor.java b/src/com/android/server/telecom/CallIntentProcessor.java
index b354bf5..ef1a931 100644
--- a/src/com/android/server/telecom/CallIntentProcessor.java
+++ b/src/com/android/server/telecom/CallIntentProcessor.java
@@ -167,9 +167,9 @@
         }
         if (errorMessageId != -1) {
             errorIntent.putExtra(ErrorDialogActivity.ERROR_MESSAGE_ID_EXTRA, errorMessageId);
+            errorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            context.startActivityAsUser(errorIntent, UserHandle.CURRENT);
         }
-        errorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        context.startActivityAsUser(errorIntent, UserHandle.CURRENT);
     }
 
     /**
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/ChangeDefaultDialerDialog.java b/src/com/android/server/telecom/components/ChangeDefaultDialerDialog.java
new file mode 100644
index 0000000..253d0ab
--- /dev/null
+++ b/src/com/android/server/telecom/components/ChangeDefaultDialerDialog.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.telecom.components;
+
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.os.Bundle;
+import android.telecom.DefaultDialerManager;
+import android.telecom.TelecomManager;
+import android.telephony.TelephonyManager;
+import android.text.TextUtils;
+import android.util.Log;
+
+import com.android.internal.app.AlertActivity;
+import com.android.internal.app.AlertController;
+import com.android.server.telecom.R;
+
+public class ChangeDefaultDialerDialog extends AlertActivity implements
+        DialogInterface.OnClickListener{
+    private static final String TAG = ChangeDefaultDialerDialog.class.getSimpleName();
+    private String mNewPackage;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        final String packageName = getIntent().getStringExtra(
+                TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME);
+
+        if (!buildDialog(packageName)) {
+            setResult(RESULT_CANCELED);
+            finish();
+        }
+    }
+
+    @Override
+    public void onClick(DialogInterface dialog, int which) {
+        switch (which) {
+            case BUTTON_POSITIVE:
+                DefaultDialerManager.setDefaultDialerApplication(ChangeDefaultDialerDialog.this,
+                        mNewPackage);
+                setResult(RESULT_OK);
+                break;
+            case BUTTON_NEGATIVE:
+                setResult(RESULT_CANCELED);
+                break;
+        }
+    }
+
+    private boolean buildDialog(String newPackage) {
+        mNewPackage = newPackage;
+        final TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
+        if (!tm.isVoiceCapable()) {
+            Log.w(TAG, "Dialog launched but device is not voice capable.");
+            return false;
+        }
+
+        if (!DefaultDialerManager.getInstalledDialerApplications(this).contains(mNewPackage)) {
+            Log.w(TAG, "Provided package name does not correspond to an installed Dialer "
+                    + "application.");
+            return false;
+        }
+
+        final String oldPackage = DefaultDialerManager.getDefaultDialerApplication(this);
+        if (!TextUtils.isEmpty(oldPackage) && TextUtils.equals(oldPackage, mNewPackage)) {
+            Log.w(TAG, "Provided package name is already the current default Dialer application.");
+            return false;
+        }
+
+        final PackageManager pm = getPackageManager();
+        final String newPackageLabel =
+                getApplicationLabelForPackageName(pm, mNewPackage);
+        final AlertController.AlertParams p = mAlertParams;
+        p.mTitle = getString(R.string.change_default_dialer_dialog_title);
+        if (!TextUtils.isEmpty(oldPackage)) {
+            String oldPackageLabel =
+                    getApplicationLabelForPackageName(pm, oldPackage);
+            p.mMessage = getString(R.string.change_default_dialer_with_previous_app_set_text,
+                    newPackageLabel,
+                    oldPackageLabel);
+        } else {
+            p.mMessage = getString(R.string.change_default_dialer_no_previous_app_set_text,
+                    newPackageLabel);
+        }
+        p.mPositiveButtonText = getString(android.R.string.yes);
+        p.mNegativeButtonText = getString(android.R.string.no);
+        p.mPositiveButtonListener = this;
+        p.mNegativeButtonListener = this;
+        setupAlert();
+
+        return true;
+    }
+
+    /**
+     * Returns the application label that corresponds to the given package name
+     *
+     * @param pm An instance of a {@link PackageManager}.
+     * @param packageName A valid package name.
+     *
+     * @return Application label for the given package name, or null if not found.
+     */
+    private String getApplicationLabelForPackageName(PackageManager pm, String packageName) {
+        ApplicationInfo info = null;
+        try {
+            info = pm.getApplicationInfo(packageName, 0);
+        } catch (NameNotFoundException e) {
+            Log.w(TAG, "Application info not found for packageName " + packageName);
+        }
+        if (info == null) {
+            return packageName;
+        } else {
+            return info.loadLabel(pm).toString();
+        }
+    }
+}
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/TestCallList.java b/testapps/src/com/android/server/telecom/testapps/TestCallList.java
index 7bb7a91..dabc21b 100644
--- a/testapps/src/com/android/server/telecom/testapps/TestCallList.java
+++ b/testapps/src/com/android/server/telecom/testapps/TestCallList.java
@@ -120,7 +120,7 @@
         mCalls.clear();
         for (Call call : mVideoCallListeners.keySet()) {
             if (call.getVideoCall() != null) {
-                call.getVideoCall().unregisterCallback();
+                call.getVideoCall().destroy();
             }
         }
         mVideoCallListeners.clear();
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..4153ee1 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;
@@ -15,6 +17,8 @@
 import com.android.server.telecom.testapps.R;
 
 public class TestDialerActivity extends Activity {
+    private static final int REQUEST_CODE_SET_DEFAULT_DIALER = 1;
+
     private EditText mNumberView;
 
     @Override
@@ -27,6 +31,7 @@
                 setDefault();
             }
         });
+
         findViewById(R.id.place_call_button).setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
@@ -41,11 +46,29 @@
             }
         });
 
+        findViewById(R.id.cancel_missed_button).setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                cancelMissedCallNotification();
+            }
+        });
+
         mNumberView = (EditText) findViewById(R.id.number);
         updateEditTextWithNumber();
     }
 
     @Override
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        if (requestCode == REQUEST_CODE_SET_DEFAULT_DIALER) {
+            if (resultCode == RESULT_OK) {
+                showToast("User accepted request to become default dialer");
+            } else if (resultCode == RESULT_CANCELED) {
+                showToast("User declined request to become default dialer");
+            }
+        }
+    }
+
+    @Override
     protected void onNewIntent(Intent intent) {
         super.onNewIntent(intent);
         updateEditTextWithNumber();
@@ -59,7 +82,9 @@
     }
 
     private void setDefault() {
-        // TODO: Send a request to become the default dialer application
+        final Intent intent = new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER);
+        intent.putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, getPackageName());
+        startActivityForResult(intent, REQUEST_CODE_SET_DEFAULT_DIALER);
     }
 
     private void placeCall() {
@@ -77,9 +102,24 @@
             values.put(Calls.CACHED_NAME, "hello world");
             getContentResolver().update(Calls.CONTENT_URI_WITH_VOICEMAIL, values, "1=0", null);
         } catch (SecurityException e) {
-            Toast.makeText(this, "Permission check failed", Toast.LENGTH_SHORT).show();
+            showToast("Permission check failed");
             return;
         }
-        Toast.makeText(this, "Permission check succeeded", Toast.LENGTH_SHORT).show();
+        showToast("Permission check succeeded");
+    }
+
+    private void showToast(String message) {
+        Toast.makeText(this, message, 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();
     }
 }