blob: 95b81b1942591b6d544b8c8b9b1b20491f800c42 [file] [log] [blame]
Yorke Leeb5c5d442015-04-27 15:21:53 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.telecom.components;
18
19import android.content.Context;
20import android.content.DialogInterface;
Yorke Lee7d3416d2015-05-29 15:07:42 -070021import android.content.Intent;
Yorke Leeb5c5d442015-04-27 15:21:53 -070022import android.content.pm.ApplicationInfo;
23import android.content.pm.PackageManager;
24import android.content.pm.PackageManager.NameNotFoundException;
25import android.os.Bundle;
26import android.telecom.DefaultDialerManager;
27import android.telecom.TelecomManager;
28import android.telephony.TelephonyManager;
29import android.text.TextUtils;
30import android.util.Log;
31
32import com.android.internal.app.AlertActivity;
33import com.android.internal.app.AlertController;
34import com.android.server.telecom.R;
35
Yorke Lee7d3416d2015-05-29 15:07:42 -070036/**
37 * Activity that shows a dialog for the user to confirm whether or not the default dialer should
38 * be changed.
39 *
40 * This dialog can be skipped directly for CTS tests using the adb command:
41 * adb shell am start -a android.telecom.action.CHANGE_DEFAULT_DIALER_PRIVILEGED -e android.telecom.extra.CHANGE_DEFAULT_DIALER_PACKAGE_NAME <packageName>
42 */
Yorke Leeb5c5d442015-04-27 15:21:53 -070043public class ChangeDefaultDialerDialog extends AlertActivity implements
44 DialogInterface.OnClickListener{
45 private static final String TAG = ChangeDefaultDialerDialog.class.getSimpleName();
46 private String mNewPackage;
47
48 @Override
49 protected void onCreate(Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
51
Yorke Lee7d3416d2015-05-29 15:07:42 -070052 final String oldPackage = DefaultDialerManager.getDefaultDialerApplication(this);
53 mNewPackage = getIntent().getStringExtra(
Yorke Leeb5c5d442015-04-27 15:21:53 -070054 TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME);
Yorke Lee7d3416d2015-05-29 15:07:42 -070055 if (!canChangeToProvidedPackage(oldPackage, mNewPackage)) {
Yorke Leeb5c5d442015-04-27 15:21:53 -070056 setResult(RESULT_CANCELED);
57 finish();
58 }
Yorke Lee7d3416d2015-05-29 15:07:42 -070059
Yorke Lee71734c22015-06-02 14:22:56 -070060 // Show dialog to require user confirmation.
61 buildDialog(oldPackage, mNewPackage);
Yorke Leeb5c5d442015-04-27 15:21:53 -070062 }
63
64 @Override
65 public void onClick(DialogInterface dialog, int which) {
66 switch (which) {
67 case BUTTON_POSITIVE:
68 DefaultDialerManager.setDefaultDialerApplication(ChangeDefaultDialerDialog.this,
69 mNewPackage);
70 setResult(RESULT_OK);
71 break;
72 case BUTTON_NEGATIVE:
73 setResult(RESULT_CANCELED);
74 break;
75 }
76 }
77
Yorke Lee7d3416d2015-05-29 15:07:42 -070078 private boolean canChangeToProvidedPackage(String oldPackage, String newPackage) {
Yorke Leeb5c5d442015-04-27 15:21:53 -070079 final TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
80 if (!tm.isVoiceCapable()) {
81 Log.w(TAG, "Dialog launched but device is not voice capable.");
82 return false;
83 }
84
Yorke Lee7d3416d2015-05-29 15:07:42 -070085 if (!DefaultDialerManager.getInstalledDialerApplications(this).contains(newPackage)) {
Yorke Leeb5c5d442015-04-27 15:21:53 -070086 Log.w(TAG, "Provided package name does not correspond to an installed Dialer "
87 + "application.");
88 return false;
89 }
90
Yorke Lee7d3416d2015-05-29 15:07:42 -070091 if (!TextUtils.isEmpty(oldPackage) && TextUtils.equals(oldPackage, newPackage)) {
Yorke Leeb5c5d442015-04-27 15:21:53 -070092 Log.w(TAG, "Provided package name is already the current default Dialer application.");
93 return false;
94 }
Yorke Lee7d3416d2015-05-29 15:07:42 -070095 return true;
96 }
Yorke Leeb5c5d442015-04-27 15:21:53 -070097
Yorke Lee7d3416d2015-05-29 15:07:42 -070098 private boolean buildDialog(String oldPackage, String newPackage) {
Yorke Leeb5c5d442015-04-27 15:21:53 -070099 final PackageManager pm = getPackageManager();
100 final String newPackageLabel =
Yorke Lee7d3416d2015-05-29 15:07:42 -0700101 getApplicationLabelForPackageName(pm, newPackage);
Yorke Leeb5c5d442015-04-27 15:21:53 -0700102 final AlertController.AlertParams p = mAlertParams;
103 p.mTitle = getString(R.string.change_default_dialer_dialog_title);
104 if (!TextUtils.isEmpty(oldPackage)) {
105 String oldPackageLabel =
106 getApplicationLabelForPackageName(pm, oldPackage);
107 p.mMessage = getString(R.string.change_default_dialer_with_previous_app_set_text,
108 newPackageLabel,
109 oldPackageLabel);
110 } else {
111 p.mMessage = getString(R.string.change_default_dialer_no_previous_app_set_text,
112 newPackageLabel);
113 }
114 p.mPositiveButtonText = getString(android.R.string.yes);
115 p.mNegativeButtonText = getString(android.R.string.no);
116 p.mPositiveButtonListener = this;
117 p.mNegativeButtonListener = this;
118 setupAlert();
119
120 return true;
121 }
122
123 /**
124 * Returns the application label that corresponds to the given package name
125 *
126 * @param pm An instance of a {@link PackageManager}.
127 * @param packageName A valid package name.
128 *
129 * @return Application label for the given package name, or null if not found.
130 */
131 private String getApplicationLabelForPackageName(PackageManager pm, String packageName) {
132 ApplicationInfo info = null;
133 try {
134 info = pm.getApplicationInfo(packageName, 0);
135 } catch (NameNotFoundException e) {
136 Log.w(TAG, "Application info not found for packageName " + packageName);
137 }
138 if (info == null) {
139 return packageName;
140 } else {
141 return info.loadLabel(pm).toString();
142 }
143 }
144}