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 | |
| 19 | import android.content.Context; |
| 20 | import android.content.DialogInterface; |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 21 | import android.content.Intent; |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 22 | import android.content.pm.ApplicationInfo; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.PackageManager.NameNotFoundException; |
| 25 | import android.os.Bundle; |
| 26 | import android.telecom.DefaultDialerManager; |
| 27 | import android.telecom.TelecomManager; |
| 28 | import android.telephony.TelephonyManager; |
| 29 | import android.text.TextUtils; |
| 30 | import android.util.Log; |
| 31 | |
| 32 | import com.android.internal.app.AlertActivity; |
| 33 | import com.android.internal.app.AlertController; |
| 34 | import com.android.server.telecom.R; |
| 35 | |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 36 | /** |
| 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 Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 43 | public 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 Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 52 | final String oldPackage = DefaultDialerManager.getDefaultDialerApplication(this); |
| 53 | mNewPackage = getIntent().getStringExtra( |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 54 | TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME); |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 55 | if (!canChangeToProvidedPackage(oldPackage, mNewPackage)) { |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 56 | setResult(RESULT_CANCELED); |
| 57 | finish(); |
| 58 | } |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 59 | |
Yorke Lee | 71734c2 | 2015-06-02 14:22:56 -0700 | [diff] [blame^] | 60 | // Show dialog to require user confirmation. |
| 61 | buildDialog(oldPackage, mNewPackage); |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 62 | } |
| 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 Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 78 | private boolean canChangeToProvidedPackage(String oldPackage, String newPackage) { |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 79 | 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 Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 85 | if (!DefaultDialerManager.getInstalledDialerApplications(this).contains(newPackage)) { |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 86 | Log.w(TAG, "Provided package name does not correspond to an installed Dialer " |
| 87 | + "application."); |
| 88 | return false; |
| 89 | } |
| 90 | |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 91 | if (!TextUtils.isEmpty(oldPackage) && TextUtils.equals(oldPackage, newPackage)) { |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 92 | Log.w(TAG, "Provided package name is already the current default Dialer application."); |
| 93 | return false; |
| 94 | } |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 95 | return true; |
| 96 | } |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 97 | |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 98 | private boolean buildDialog(String oldPackage, String newPackage) { |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 99 | final PackageManager pm = getPackageManager(); |
| 100 | final String newPackageLabel = |
Yorke Lee | 7d3416d | 2015-05-29 15:07:42 -0700 | [diff] [blame] | 101 | getApplicationLabelForPackageName(pm, newPackage); |
Yorke Lee | b5c5d44 | 2015-04-27 15:21:53 -0700 | [diff] [blame] | 102 | 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 | } |