Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.telecom.components; |
| 18 | |
Hall Liu | 941cb23 | 2019-05-29 16:09:49 -0700 | [diff] [blame] | 19 | import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS; |
| 20 | |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.DialogInterface; |
| 23 | import android.content.pm.ApplicationInfo; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.PackageManager.NameNotFoundException; |
Brad Ebinger | 8103efb | 2016-07-11 12:36:37 -0700 | [diff] [blame] | 26 | import android.graphics.Color; |
| 27 | import android.graphics.Typeface; |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 28 | import android.os.Bundle; |
| 29 | import android.telecom.DefaultDialerManager; |
| 30 | import android.telecom.TelecomManager; |
| 31 | import android.telephony.TelephonyManager; |
Brad Ebinger | 8103efb | 2016-07-11 12:36:37 -0700 | [diff] [blame] | 32 | import android.text.Spannable; |
| 33 | import android.text.SpannableString; |
| 34 | import android.text.SpannableStringBuilder; |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 35 | import android.text.TextUtils; |
Brad Ebinger | 8103efb | 2016-07-11 12:36:37 -0700 | [diff] [blame] | 36 | import android.text.style.ForegroundColorSpan; |
| 37 | import android.text.style.StyleSpan; |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 38 | import android.util.Log; |
Hall Liu | 941cb23 | 2019-05-29 16:09:49 -0700 | [diff] [blame] | 39 | import android.view.WindowManager; |
| 40 | import android.view.Window; |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 41 | |
| 42 | import com.android.internal.app.AlertActivity; |
| 43 | import com.android.internal.app.AlertController; |
| 44 | import com.android.server.telecom.R; |
| 45 | |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 46 | /** |
| 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 Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 53 | public 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 Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 62 | final String oldPackage = DefaultDialerManager.getDefaultDialerApplication(this); |
| 63 | mNewPackage = getIntent().getStringExtra( |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 64 | TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME); |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 65 | if (!canChangeToProvidedPackage(oldPackage, mNewPackage)) { |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 66 | setResult(RESULT_CANCELED); |
| 67 | finish(); |
| 68 | } |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 69 | |
Yorke Lee | 71734c2 | 2015-06-02 14:22:56 -0700 | [diff] [blame] | 70 | // Show dialog to require user confirmation. |
Brad Ebinger | 8103efb | 2016-07-11 12:36:37 -0700 | [diff] [blame] | 71 | buildDialog(mNewPackage); |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void onClick(DialogInterface dialog, int which) { |
| 76 | switch (which) { |
| 77 | case BUTTON_POSITIVE: |
Yorke Lee | b398430 | 2015-06-15 12:06:35 -0700 | [diff] [blame] | 78 | TelecomManager.from(this).setDefaultDialer(mNewPackage); |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 79 | setResult(RESULT_OK); |
| 80 | break; |
| 81 | case BUTTON_NEGATIVE: |
| 82 | setResult(RESULT_CANCELED); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
Hall Liu | 941cb23 | 2019-05-29 16:09:49 -0700 | [diff] [blame] | 87 | @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 Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 102 | private boolean canChangeToProvidedPackage(String oldPackage, String newPackage) { |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 103 | 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 Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 109 | if (!DefaultDialerManager.getInstalledDialerApplications(this).contains(newPackage)) { |
Brad Ebinger | 8103efb | 2016-07-11 12:36:37 -0700 | [diff] [blame] | 110 | Log.w(TAG, "Provided package name does not correspond to an installed Phone " |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 111 | + "application."); |
| 112 | return false; |
| 113 | } |
| 114 | |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 115 | if (!TextUtils.isEmpty(oldPackage) && TextUtils.equals(oldPackage, newPackage)) { |
Brad Ebinger | 8103efb | 2016-07-11 12:36:37 -0700 | [diff] [blame] | 116 | Log.w(TAG, "Provided package name is already the current default Phone application."); |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 117 | return false; |
| 118 | } |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 119 | return true; |
| 120 | } |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 121 | |
Brad Ebinger | 8103efb | 2016-07-11 12:36:37 -0700 | [diff] [blame] | 122 | private boolean buildDialog(String newPackage) { |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 123 | final PackageManager pm = getPackageManager(); |
Brad Ebinger | 8103efb | 2016-07-11 12:36:37 -0700 | [diff] [blame] | 124 | final String newPackageLabel = getApplicationLabelForPackageName(pm, newPackage); |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 125 | final AlertController.AlertParams p = mAlertParams; |
Brad Ebinger | 8103efb | 2016-07-11 12:36:37 -0700 | [diff] [blame] | 126 | 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 Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 130 | 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 | } |