blob: e649637582919771d9c61fa8fa54ea87354022f6 [file] [log] [blame]
Yorke Lee014de022015-04-21 17:15:47 -07001/*
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
Yorke Lee014de022015-04-21 17:15:47 -070017import android.content.Context;
18import android.content.Intent;
19import android.content.pm.ActivityInfo;
20import android.content.pm.PackageManager;
21import android.content.pm.ResolveInfo;
Yorke Lee856a5ac2015-04-28 15:45:42 -070022import android.net.Uri;
Yorke Lee014de022015-04-21 17:15:47 -070023import android.provider.Settings;
24import android.text.TextUtils;
25
26import java.util.ArrayList;
27import java.util.List;
28
29/**
30 * Class for managing the default dialer application that will receive incoming calls, and be
31 * allowed to make emergency outgoing calls.
32 *
33 * @hide
34 */
35public class DefaultDialerManager {
36 private static final String TAG = "DefaultDialerManager";
37
38 /**
39 * Sets the specified package name as the default dialer application. The caller of this method
40 * needs to have permission to write to secure settings.
41 *
42 * @hide
43 * */
Yorke Leea162f022015-04-27 15:17:50 -070044 public static void setDefaultDialerApplication(Context context, String packageName) {
Yorke Lee014de022015-04-21 17:15:47 -070045 // Get old package name
46 String oldPackageName = Settings.Secure.getString(context.getContentResolver(),
47 Settings.Secure.DIALER_DEFAULT_APPLICATION);
48
49 if (packageName != null && oldPackageName != null && packageName.equals(oldPackageName)) {
50 // No change
51 return;
52 }
53
54 // Only make the change if the new package belongs to a valid phone application
Yorke Lee8e0207b2015-04-28 09:39:20 -070055 List<String> packageNames = getInstalledDialerApplications(context);
Yorke Lee014de022015-04-21 17:15:47 -070056
Yorke Lee8e0207b2015-04-28 09:39:20 -070057 if (packageNames.contains(packageName)) {
Yorke Lee014de022015-04-21 17:15:47 -070058 // Update the secure setting.
59 Settings.Secure.putString(context.getContentResolver(),
Yorke Lee8e0207b2015-04-28 09:39:20 -070060 Settings.Secure.DIALER_DEFAULT_APPLICATION, packageName);
Yorke Lee014de022015-04-21 17:15:47 -070061 }
62 }
63
64 /**
65 * Returns the installed dialer application that will be used to receive incoming calls, and is
66 * allowed to make emergency calls.
67 *
68 * The application will be returned in order of preference:
69 * 1) User selected phone application (if still installed)
70 * 2) Pre-installed system dialer (if not disabled)
71 * 3) Null
72 *
73 * @hide
74 * */
Yorke Lee8e0207b2015-04-28 09:39:20 -070075 public static String getDefaultDialerApplication(Context context) {
Yorke Lee014de022015-04-21 17:15:47 -070076 String defaultPackageName = Settings.Secure.getString(context.getContentResolver(),
77 Settings.Secure.DIALER_DEFAULT_APPLICATION);
78
Yorke Lee8e0207b2015-04-28 09:39:20 -070079
80 final List<String> packageNames = getInstalledDialerApplications(context);
81
82 // Verify that the default dialer has not been disabled or uninstalled.
83 if (packageNames.contains(defaultPackageName)) {
84 return defaultPackageName;
Yorke Lee014de022015-04-21 17:15:47 -070085 }
86
87 // No user-set dialer found, fallback to system dialer
Yorke Lee8e0207b2015-04-28 09:39:20 -070088 String systemDialerPackageName = getTelecomManager(context).getSystemDialerPackage();
Yorke Lee014de022015-04-21 17:15:47 -070089
Yorke Lee8e0207b2015-04-28 09:39:20 -070090 if (TextUtils.isEmpty(systemDialerPackageName)) {
Yorke Lee014de022015-04-21 17:15:47 -070091 // No system dialer configured at build time
92 return null;
93 }
94
Yorke Lee8e0207b2015-04-28 09:39:20 -070095 if (packageNames.contains(systemDialerPackageName)) {
96 return systemDialerPackageName;
97 } else {
98 return null;
99 }
Yorke Lee014de022015-04-21 17:15:47 -0700100 }
101
102 /**
103 * Returns a list of installed and available dialer applications.
104 *
105 * In order to appear in the list, a dialer application must implement an intent-filter with
106 * the DIAL intent for the following schemes:
107 *
108 * 1) Empty scheme
109 * 2) tel Uri scheme
110 *
111 * @hide
112 **/
Yorke Lee8e0207b2015-04-28 09:39:20 -0700113 public static List<String> getInstalledDialerApplications(Context context) {
Yorke Lee014de022015-04-21 17:15:47 -0700114 PackageManager packageManager = context.getPackageManager();
115
116 // Get the list of apps registered for the DIAL intent with empty scheme
117 Intent intent = new Intent(Intent.ACTION_DIAL);
118 List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0);
119
Yorke Lee8e0207b2015-04-28 09:39:20 -0700120 List<String> packageNames = new ArrayList<>();
Yorke Lee014de022015-04-21 17:15:47 -0700121
122 for (ResolveInfo resolveInfo : resolveInfoList) {
123 final ActivityInfo activityInfo = resolveInfo.activityInfo;
Yorke Lee856a5ac2015-04-28 15:45:42 -0700124 if (activityInfo != null && !packageNames.contains(activityInfo.packageName)) {
125 packageNames.add(activityInfo.packageName);
Yorke Lee014de022015-04-21 17:15:47 -0700126 }
Yorke Lee014de022015-04-21 17:15:47 -0700127 }
128
Yorke Lee856a5ac2015-04-28 15:45:42 -0700129 final Intent dialIntentWithTelScheme = new Intent(Intent.ACTION_DIAL);
130 dialIntentWithTelScheme.setData(Uri.fromParts(PhoneAccount.SCHEME_TEL, "", null));
131 return filterByIntent(context, packageNames, dialIntentWithTelScheme);
Yorke Lee014de022015-04-21 17:15:47 -0700132 }
133
134 /**
Yorke Lee61043822015-04-27 11:18:38 -0700135 * Determines if the package name belongs to the user-selected default dialer or the preloaded
136 * system dialer, and thus should be allowed to perform certain privileged operations.
137 *
138 * @param context A valid context.
139 * @param packageName of the package to check for.
140 *
141 * @return {@code true} if the provided package name corresponds to the user-selected default
142 * dialer or the preloaded system dialer, {@code false} otherwise.
143 *
144 * @hide
145 */
146 public static boolean isDefaultOrSystemDialer(Context context, String packageName) {
147 if (TextUtils.isEmpty(packageName)) {
148 return false;
149 }
150 final TelecomManager tm = getTelecomManager(context);
151 return packageName.equals(tm.getDefaultDialerPackage())
152 || packageName.equals(tm.getSystemDialerPackage());
153 }
154
Yorke Lee856a5ac2015-04-28 15:45:42 -0700155 /**
156 * Filter a given list of package names for those packages that contain an activity that has
157 * an intent filter for a given intent.
158 *
159 * @param context A valid context
160 * @param packageNames List of package names to filter.
161 * @return The filtered list.
162 */
163 private static List<String> filterByIntent(Context context, List<String> packageNames,
164 Intent intent) {
165 if (packageNames == null || packageNames.isEmpty()) {
166 return new ArrayList<>();
167 }
168
169 final List<String> result = new ArrayList<>();
170 final List<ResolveInfo> resolveInfoList =
171 context.getPackageManager().queryIntentActivities(intent, 0);
172 final int length = resolveInfoList.size();
173 for (int i = 0; i < length; i++) {
174 final ActivityInfo info = resolveInfoList.get(i).activityInfo;
175 if (info != null && packageNames.contains(info.packageName)
176 && !result.contains(info.packageName)) {
177 result.add(info.packageName);
178 }
179 }
180
181 return result;
182 }
183
184
Yorke Lee014de022015-04-21 17:15:47 -0700185 private static TelecomManager getTelecomManager(Context context) {
186 return (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
187 }
188}