Add activity to handle default dialer requests

* ChangeDefaultDialerDialog provides the UI that can be used by
apps to request that the user select their package as the default
dialer.

* Add various manifest entries, strings etc for use in the UI

* Exercise TelecomManager.ACTION_CHANGE_DEFAULT_DIALER in
TestDialerActivity

Bug: 20304411

Change-Id: Ibc6678c44290f5c9aa00c294b55db74c2c6e09eb
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();
+        }
+    }
+}