DefaultDialerManager.java revision 8e0207ba5b7ca3bca9d87847eef4d00aa89d7a7a
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15package android.telecom; 16 17import android.content.Context; 18import android.content.Intent; 19import android.content.pm.ActivityInfo; 20import android.content.pm.PackageManager; 21import android.content.pm.ResolveInfo; 22import android.provider.Settings; 23import android.text.TextUtils; 24 25import java.util.ArrayList; 26import java.util.List; 27 28/** 29 * Class for managing the default dialer application that will receive incoming calls, and be 30 * allowed to make emergency outgoing calls. 31 * 32 * @hide 33 */ 34public class DefaultDialerManager { 35 private static final String TAG = "DefaultDialerManager"; 36 37 /** 38 * Sets the specified package name as the default dialer application. The caller of this method 39 * needs to have permission to write to secure settings. 40 * 41 * @hide 42 * */ 43 public static void setDefaultPhoneApplication(Context context, String packageName) { 44 // Get old package name 45 String oldPackageName = Settings.Secure.getString(context.getContentResolver(), 46 Settings.Secure.DIALER_DEFAULT_APPLICATION); 47 48 if (packageName != null && oldPackageName != null && packageName.equals(oldPackageName)) { 49 // No change 50 return; 51 } 52 53 // Only make the change if the new package belongs to a valid phone application 54 List<String> packageNames = getInstalledDialerApplications(context); 55 56 if (packageNames.contains(packageName)) { 57 // Update the secure setting. 58 Settings.Secure.putString(context.getContentResolver(), 59 Settings.Secure.DIALER_DEFAULT_APPLICATION, packageName); 60 } 61 } 62 63 /** 64 * Returns the installed dialer application that will be used to receive incoming calls, and is 65 * allowed to make emergency calls. 66 * 67 * The application will be returned in order of preference: 68 * 1) User selected phone application (if still installed) 69 * 2) Pre-installed system dialer (if not disabled) 70 * 3) Null 71 * 72 * @hide 73 * */ 74 public static String getDefaultDialerApplication(Context context) { 75 String defaultPackageName = Settings.Secure.getString(context.getContentResolver(), 76 Settings.Secure.DIALER_DEFAULT_APPLICATION); 77 78 79 final List<String> packageNames = getInstalledDialerApplications(context); 80 81 // Verify that the default dialer has not been disabled or uninstalled. 82 if (packageNames.contains(defaultPackageName)) { 83 return defaultPackageName; 84 } 85 86 // No user-set dialer found, fallback to system dialer 87 String systemDialerPackageName = getTelecomManager(context).getSystemDialerPackage(); 88 89 if (TextUtils.isEmpty(systemDialerPackageName)) { 90 // No system dialer configured at build time 91 return null; 92 } 93 94 if (packageNames.contains(systemDialerPackageName)) { 95 return systemDialerPackageName; 96 } else { 97 return null; 98 } 99 } 100 101 /** 102 * Returns a list of installed and available dialer applications. 103 * 104 * In order to appear in the list, a dialer application must implement an intent-filter with 105 * the DIAL intent for the following schemes: 106 * 107 * 1) Empty scheme 108 * 2) tel Uri scheme 109 * 110 * @hide 111 **/ 112 public static List<String> getInstalledDialerApplications(Context context) { 113 PackageManager packageManager = context.getPackageManager(); 114 115 // Get the list of apps registered for the DIAL intent with empty scheme 116 Intent intent = new Intent(Intent.ACTION_DIAL); 117 List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0); 118 119 List<String> packageNames = new ArrayList<>(); 120 121 for (ResolveInfo resolveInfo : resolveInfoList) { 122 final ActivityInfo activityInfo = resolveInfo.activityInfo; 123 if (activityInfo == null) { 124 continue; 125 } 126 packageNames.add(activityInfo.packageName); 127 } 128 129 // TODO: Filter for apps that don't handle DIAL intent with tel scheme 130 return packageNames; 131 } 132 133 /** 134 * Determines if the package name belongs to the user-selected default dialer or the preloaded 135 * system dialer, and thus should be allowed to perform certain privileged operations. 136 * 137 * @param context A valid context. 138 * @param packageName of the package to check for. 139 * 140 * @return {@code true} if the provided package name corresponds to the user-selected default 141 * dialer or the preloaded system dialer, {@code false} otherwise. 142 * 143 * @hide 144 */ 145 public static boolean isDefaultOrSystemDialer(Context context, String packageName) { 146 if (TextUtils.isEmpty(packageName)) { 147 return false; 148 } 149 final TelecomManager tm = getTelecomManager(context); 150 return packageName.equals(tm.getDefaultDialerPackage()) 151 || packageName.equals(tm.getSystemDialerPackage()); 152 } 153 154 private static TelecomManager getTelecomManager(Context context) { 155 return (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); 156 } 157} 158