blob: 2becabcc7ac463f63b9be8b174e262a0a4054b3b [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
Hall Liu941cb232019-05-29 16:09:49 -070019import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
20
Yorke Leeb5c5d442015-04-27 15:21:53 -070021import android.content.Context;
22import android.content.DialogInterface;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageManager;
25import android.content.pm.PackageManager.NameNotFoundException;
Brad Ebinger8103efb2016-07-11 12:36:37 -070026import android.graphics.Color;
27import android.graphics.Typeface;
Yorke Leeb5c5d442015-04-27 15:21:53 -070028import android.os.Bundle;
29import android.telecom.DefaultDialerManager;
30import android.telecom.TelecomManager;
31import android.telephony.TelephonyManager;
Brad Ebinger8103efb2016-07-11 12:36:37 -070032import android.text.Spannable;
33import android.text.SpannableString;
34import android.text.SpannableStringBuilder;
Yorke Leeb5c5d442015-04-27 15:21:53 -070035import android.text.TextUtils;
Brad Ebinger8103efb2016-07-11 12:36:37 -070036import android.text.style.ForegroundColorSpan;
37import android.text.style.StyleSpan;
Yorke Leeb5c5d442015-04-27 15:21:53 -070038import android.util.Log;
Hall Liu941cb232019-05-29 16:09:49 -070039import android.view.WindowManager;
40import android.view.Window;
Yorke Leeb5c5d442015-04-27 15:21:53 -070041
42import com.android.internal.app.AlertActivity;
43import com.android.internal.app.AlertController;
44import com.android.server.telecom.R;
45
Yorke Lee7d3416d2015-05-29 15:07:42 -070046/**
47 * Activity that shows a dialog for the user to confirm whether or not the default dialer should
48 * be changed.
49 *
50 * This dialog can be skipped directly for CTS tests using the adb command:
51 * adb shell am start -a android.telecom.action.CHANGE_DEFAULT_DIALER_PRIVILEGED -e android.telecom.extra.CHANGE_DEFAULT_DIALER_PACKAGE_NAME <packageName>
52 */
Yorke Leeb5c5d442015-04-27 15:21:53 -070053public class ChangeDefaultDialerDialog extends AlertActivity implements
54 DialogInterface.OnClickListener{
55 private static final String TAG = ChangeDefaultDialerDialog.class.getSimpleName();
56 private String mNewPackage;
57
58 @Override
59 protected void onCreate(Bundle savedInstanceState) {
60 super.onCreate(savedInstanceState);
61
Yorke Lee7d3416d2015-05-29 15:07:42 -070062 final String oldPackage = DefaultDialerManager.getDefaultDialerApplication(this);
63 mNewPackage = getIntent().getStringExtra(
Yorke Leeb5c5d442015-04-27 15:21:53 -070064 TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME);
Yorke Lee7d3416d2015-05-29 15:07:42 -070065 if (!canChangeToProvidedPackage(oldPackage, mNewPackage)) {
Yorke Leeb5c5d442015-04-27 15:21:53 -070066 setResult(RESULT_CANCELED);
67 finish();
68 }
Yorke Lee7d3416d2015-05-29 15:07:42 -070069
Yorke Lee71734c22015-06-02 14:22:56 -070070 // Show dialog to require user confirmation.
Brad Ebinger8103efb2016-07-11 12:36:37 -070071 buildDialog(mNewPackage);
Yorke Leeb5c5d442015-04-27 15:21:53 -070072 }
73
74 @Override
75 public void onClick(DialogInterface dialog, int which) {
76 switch (which) {
77 case BUTTON_POSITIVE:
Yorke Leeb3984302015-06-15 12:06:35 -070078 TelecomManager.from(this).setDefaultDialer(mNewPackage);
Yorke Leeb5c5d442015-04-27 15:21:53 -070079 setResult(RESULT_OK);
80 break;
81 case BUTTON_NEGATIVE:
82 setResult(RESULT_CANCELED);
83 break;
84 }
85 }
86
Hall Liu941cb232019-05-29 16:09:49 -070087 @Override
88 public void onStart() {
89 super.onStart();
90 getWindow().addPrivateFlags(PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
91 }
92
93 @Override
94 public void onStop() {
95 final Window window = getWindow();
96 final WindowManager.LayoutParams attrs = window.getAttributes();
97 attrs.privateFlags &= ~PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
98 window.setAttributes(attrs);
99 super.onStop();
100 }
101
Yorke Lee7d3416d2015-05-29 15:07:42 -0700102 private boolean canChangeToProvidedPackage(String oldPackage, String newPackage) {
Yorke Leeb5c5d442015-04-27 15:21:53 -0700103 final TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
104 if (!tm.isVoiceCapable()) {
105 Log.w(TAG, "Dialog launched but device is not voice capable.");
106 return false;
107 }
108
Yorke Lee7d3416d2015-05-29 15:07:42 -0700109 if (!DefaultDialerManager.getInstalledDialerApplications(this).contains(newPackage)) {
Brad Ebinger8103efb2016-07-11 12:36:37 -0700110 Log.w(TAG, "Provided package name does not correspond to an installed Phone "
Yorke Leeb5c5d442015-04-27 15:21:53 -0700111 + "application.");
112 return false;
113 }
114
Yorke Lee7d3416d2015-05-29 15:07:42 -0700115 if (!TextUtils.isEmpty(oldPackage) && TextUtils.equals(oldPackage, newPackage)) {
Brad Ebinger8103efb2016-07-11 12:36:37 -0700116 Log.w(TAG, "Provided package name is already the current default Phone application.");
Yorke Leeb5c5d442015-04-27 15:21:53 -0700117 return false;
118 }
Yorke Lee7d3416d2015-05-29 15:07:42 -0700119 return true;
120 }
Yorke Leeb5c5d442015-04-27 15:21:53 -0700121
Brad Ebinger8103efb2016-07-11 12:36:37 -0700122 private boolean buildDialog(String newPackage) {
Yorke Leeb5c5d442015-04-27 15:21:53 -0700123 final PackageManager pm = getPackageManager();
Brad Ebinger8103efb2016-07-11 12:36:37 -0700124 final String newPackageLabel = getApplicationLabelForPackageName(pm, newPackage);
Yorke Leeb5c5d442015-04-27 15:21:53 -0700125 final AlertController.AlertParams p = mAlertParams;
Brad Ebinger8103efb2016-07-11 12:36:37 -0700126 p.mTitle = getString(R.string.change_default_dialer_dialog_title, newPackageLabel);
127 p.mMessage = getString(R.string.change_default_dialer_warning_message, newPackageLabel);
128 p.mPositiveButtonText = getString(R.string.change_default_dialer_dialog_affirmative);
129 p.mNegativeButtonText = getString(R.string.change_default_dialer_dialog_negative);
Yorke Leeb5c5d442015-04-27 15:21:53 -0700130 p.mPositiveButtonListener = this;
131 p.mNegativeButtonListener = this;
132 setupAlert();
133
134 return true;
135 }
136
137 /**
138 * Returns the application label that corresponds to the given package name
139 *
140 * @param pm An instance of a {@link PackageManager}.
141 * @param packageName A valid package name.
142 *
143 * @return Application label for the given package name, or null if not found.
144 */
145 private String getApplicationLabelForPackageName(PackageManager pm, String packageName) {
146 ApplicationInfo info = null;
147 try {
148 info = pm.getApplicationInfo(packageName, 0);
149 } catch (NameNotFoundException e) {
150 Log.w(TAG, "Application info not found for packageName " + packageName);
151 }
152 if (info == null) {
153 return packageName;
154 } else {
155 return info.loadLabel(pm).toString();
156 }
157 }
158}