| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -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"); 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 | |
| 15 | package android.telecom; |
| 16 | |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 17 | import android.content.Context; |
| 18 | import android.content.Intent; |
| 19 | import android.content.pm.ActivityInfo; |
| 20 | import android.content.pm.PackageManager; |
| 21 | import android.content.pm.ResolveInfo; |
| 22 | import android.provider.Settings; |
| 23 | import android.text.TextUtils; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import 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 | */ |
| 34 | public 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 |
| Yorke Lee | 8e0207b | 2015-04-28 09:39:20 -0700 | [diff] [blame^] | 54 | List<String> packageNames = getInstalledDialerApplications(context); |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 55 | |
| Yorke Lee | 8e0207b | 2015-04-28 09:39:20 -0700 | [diff] [blame^] | 56 | if (packageNames.contains(packageName)) { |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 57 | // Update the secure setting. |
| 58 | Settings.Secure.putString(context.getContentResolver(), |
| Yorke Lee | 8e0207b | 2015-04-28 09:39:20 -0700 | [diff] [blame^] | 59 | Settings.Secure.DIALER_DEFAULT_APPLICATION, packageName); |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 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 | * */ |
| Yorke Lee | 8e0207b | 2015-04-28 09:39:20 -0700 | [diff] [blame^] | 74 | public static String getDefaultDialerApplication(Context context) { |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 75 | String defaultPackageName = Settings.Secure.getString(context.getContentResolver(), |
| 76 | Settings.Secure.DIALER_DEFAULT_APPLICATION); |
| 77 | |
| Yorke Lee | 8e0207b | 2015-04-28 09:39:20 -0700 | [diff] [blame^] | 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; |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // No user-set dialer found, fallback to system dialer |
| Yorke Lee | 8e0207b | 2015-04-28 09:39:20 -0700 | [diff] [blame^] | 87 | String systemDialerPackageName = getTelecomManager(context).getSystemDialerPackage(); |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 88 | |
| Yorke Lee | 8e0207b | 2015-04-28 09:39:20 -0700 | [diff] [blame^] | 89 | if (TextUtils.isEmpty(systemDialerPackageName)) { |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 90 | // No system dialer configured at build time |
| 91 | return null; |
| 92 | } |
| 93 | |
| Yorke Lee | 8e0207b | 2015-04-28 09:39:20 -0700 | [diff] [blame^] | 94 | if (packageNames.contains(systemDialerPackageName)) { |
| 95 | return systemDialerPackageName; |
| 96 | } else { |
| 97 | return null; |
| 98 | } |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 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 | **/ |
| Yorke Lee | 8e0207b | 2015-04-28 09:39:20 -0700 | [diff] [blame^] | 112 | public static List<String> getInstalledDialerApplications(Context context) { |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 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 | |
| Yorke Lee | 8e0207b | 2015-04-28 09:39:20 -0700 | [diff] [blame^] | 119 | List<String> packageNames = new ArrayList<>(); |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 120 | |
| 121 | for (ResolveInfo resolveInfo : resolveInfoList) { |
| 122 | final ActivityInfo activityInfo = resolveInfo.activityInfo; |
| 123 | if (activityInfo == null) { |
| 124 | continue; |
| 125 | } |
| Yorke Lee | 8e0207b | 2015-04-28 09:39:20 -0700 | [diff] [blame^] | 126 | packageNames.add(activityInfo.packageName); |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // TODO: Filter for apps that don't handle DIAL intent with tel scheme |
| Yorke Lee | 8e0207b | 2015-04-28 09:39:20 -0700 | [diff] [blame^] | 130 | return packageNames; |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| Yorke Lee | 6104382 | 2015-04-27 11:18:38 -0700 | [diff] [blame] | 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 | |
| Yorke Lee | 014de02 | 2015-04-21 17:15:47 -0700 | [diff] [blame] | 154 | private static TelecomManager getTelecomManager(Context context) { |
| 155 | return (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); |
| 156 | } |
| 157 | } |