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; 21 import android.content.pm.ApplicationInfo; 22 import android.content.pm.PackageManager; 23 import android.content.pm.PackageManager.NameNotFoundException; 24 import android.os.Bundle; 25 import android.telecom.DefaultDialerManager; 26 import android.telecom.TelecomManager; 27 import android.telephony.TelephonyManager; 28 import android.text.TextUtils; 29 import android.util.Log; 30 31 import com.android.internal.app.AlertActivity; 32 import com.android.internal.app.AlertController; 33 import com.android.server.telecom.R; 34 35 /** 36 * Activity that shows a dialog for the user to confirm whether or not the default dialer should 37 * be changed. 38 * 39 * This dialog can be skipped directly for CTS tests using the adb command: 40 * adb shell am start -a android.telecom.action.CHANGE_DEFAULT_DIALER_PRIVILEGED -e android.telecom.extra.CHANGE_DEFAULT_DIALER_PACKAGE_NAME <packageName> 41 */ 42 public class ChangeDefaultDialerDialog extends AlertActivity implements 43 DialogInterface.OnClickListener{ 44 private static final String TAG = ChangeDefaultDialerDialog.class.getSimpleName(); 45 private String mNewPackage; 46 47 @Override onCreate(Bundle savedInstanceState)48 protected void onCreate(Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 51 final String oldPackage = DefaultDialerManager.getDefaultDialerApplication(this); 52 mNewPackage = getIntent().getStringExtra( 53 TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME); 54 if (!canChangeToProvidedPackage(oldPackage, mNewPackage)) { 55 setResult(RESULT_CANCELED); 56 finish(); 57 } 58 59 // Show dialog to require user confirmation. 60 buildDialog(oldPackage, mNewPackage); 61 } 62 63 @Override onClick(DialogInterface dialog, int which)64 public void onClick(DialogInterface dialog, int which) { 65 switch (which) { 66 case BUTTON_POSITIVE: 67 TelecomManager.from(this).setDefaultDialer(mNewPackage); 68 setResult(RESULT_OK); 69 break; 70 case BUTTON_NEGATIVE: 71 setResult(RESULT_CANCELED); 72 break; 73 } 74 } 75 canChangeToProvidedPackage(String oldPackage, String newPackage)76 private boolean canChangeToProvidedPackage(String oldPackage, String newPackage) { 77 final TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 78 if (!tm.isVoiceCapable()) { 79 Log.w(TAG, "Dialog launched but device is not voice capable."); 80 return false; 81 } 82 83 if (!DefaultDialerManager.getInstalledDialerApplications(this).contains(newPackage)) { 84 Log.w(TAG, "Provided package name does not correspond to an installed Dialer " 85 + "application."); 86 return false; 87 } 88 89 if (!TextUtils.isEmpty(oldPackage) && TextUtils.equals(oldPackage, newPackage)) { 90 Log.w(TAG, "Provided package name is already the current default Dialer application."); 91 return false; 92 } 93 return true; 94 } 95 buildDialog(String oldPackage, String newPackage)96 private boolean buildDialog(String oldPackage, String newPackage) { 97 final PackageManager pm = getPackageManager(); 98 final String newPackageLabel = 99 getApplicationLabelForPackageName(pm, newPackage); 100 final AlertController.AlertParams p = mAlertParams; 101 p.mTitle = getString(R.string.change_default_dialer_dialog_title); 102 if (!TextUtils.isEmpty(oldPackage)) { 103 String oldPackageLabel = 104 getApplicationLabelForPackageName(pm, oldPackage); 105 p.mMessage = getString(R.string.change_default_dialer_with_previous_app_set_text, 106 newPackageLabel, 107 oldPackageLabel); 108 } else { 109 p.mMessage = getString(R.string.change_default_dialer_no_previous_app_set_text, 110 newPackageLabel); 111 } 112 p.mPositiveButtonText = getString(android.R.string.yes); 113 p.mNegativeButtonText = getString(android.R.string.no); 114 p.mPositiveButtonListener = this; 115 p.mNegativeButtonListener = this; 116 setupAlert(); 117 118 return true; 119 } 120 121 /** 122 * Returns the application label that corresponds to the given package name 123 * 124 * @param pm An instance of a {@link PackageManager}. 125 * @param packageName A valid package name. 126 * 127 * @return Application label for the given package name, or null if not found. 128 */ getApplicationLabelForPackageName(PackageManager pm, String packageName)129 private String getApplicationLabelForPackageName(PackageManager pm, String packageName) { 130 ApplicationInfo info = null; 131 try { 132 info = pm.getApplicationInfo(packageName, 0); 133 } catch (NameNotFoundException e) { 134 Log.w(TAG, "Application info not found for packageName " + packageName); 135 } 136 if (info == null) { 137 return packageName; 138 } else { 139 return info.loadLabel(pm).toString(); 140 } 141 } 142 } 143