blob: 657e6994a5115447c7c7ed7c4aed52bb8d1594cb [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;
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
Yorke Lee8e0207b2015-04-28 09:39:20 -070054 List<String> packageNames = getInstalledDialerApplications(context);
Yorke Lee014de022015-04-21 17:15:47 -070055
Yorke Lee8e0207b2015-04-28 09:39:20 -070056 if (packageNames.contains(packageName)) {
Yorke Lee014de022015-04-21 17:15:47 -070057 // Update the secure setting.
58 Settings.Secure.putString(context.getContentResolver(),
Yorke Lee8e0207b2015-04-28 09:39:20 -070059 Settings.Secure.DIALER_DEFAULT_APPLICATION, packageName);
Yorke Lee014de022015-04-21 17:15:47 -070060 }
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 Lee8e0207b2015-04-28 09:39:20 -070074 public static String getDefaultDialerApplication(Context context) {
Yorke Lee014de022015-04-21 17:15:47 -070075 String defaultPackageName = Settings.Secure.getString(context.getContentResolver(),
76 Settings.Secure.DIALER_DEFAULT_APPLICATION);
77
Yorke Lee8e0207b2015-04-28 09:39:20 -070078
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 Lee014de022015-04-21 17:15:47 -070084 }
85
86 // No user-set dialer found, fallback to system dialer
Yorke Lee8e0207b2015-04-28 09:39:20 -070087 String systemDialerPackageName = getTelecomManager(context).getSystemDialerPackage();
Yorke Lee014de022015-04-21 17:15:47 -070088
Yorke Lee8e0207b2015-04-28 09:39:20 -070089 if (TextUtils.isEmpty(systemDialerPackageName)) {
Yorke Lee014de022015-04-21 17:15:47 -070090 // No system dialer configured at build time
91 return null;
92 }
93
Yorke Lee8e0207b2015-04-28 09:39:20 -070094 if (packageNames.contains(systemDialerPackageName)) {
95 return systemDialerPackageName;
96 } else {
97 return null;
98 }
Yorke Lee014de022015-04-21 17:15:47 -070099 }
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 Lee8e0207b2015-04-28 09:39:20 -0700112 public static List<String> getInstalledDialerApplications(Context context) {
Yorke Lee014de022015-04-21 17:15:47 -0700113 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 Lee8e0207b2015-04-28 09:39:20 -0700119 List<String> packageNames = new ArrayList<>();
Yorke Lee014de022015-04-21 17:15:47 -0700120
121 for (ResolveInfo resolveInfo : resolveInfoList) {
122 final ActivityInfo activityInfo = resolveInfo.activityInfo;
123 if (activityInfo == null) {
124 continue;
125 }
Yorke Lee8e0207b2015-04-28 09:39:20 -0700126 packageNames.add(activityInfo.packageName);
Yorke Lee014de022015-04-21 17:15:47 -0700127 }
128
129 // TODO: Filter for apps that don't handle DIAL intent with tel scheme
Yorke Lee8e0207b2015-04-28 09:39:20 -0700130 return packageNames;
Yorke Lee014de022015-04-21 17:15:47 -0700131 }
132
133 /**
Yorke Lee61043822015-04-27 11:18:38 -0700134 * 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 Lee014de022015-04-21 17:15:47 -0700154 private static TelecomManager getTelecomManager(Context context) {
155 return (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
156 }
157}