blob: 77bf930fb4d76442bbe4069cdd7fd7dee3ecac18 [file] [log] [blame]
Eugene Susla4ab95112018-12-17 14:45:11 -08001/*
2 * Copyright (C) 2018 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
17package com.android.server.policy.role;
18
19import android.annotation.NonNull;
Hai Zhang8be463f2019-02-11 16:44:45 -080020import android.annotation.UserIdInt;
Eugene Susla4ab95112018-12-17 14:45:11 -080021import android.app.role.RoleManager;
Philip P. Moltmann5d894502019-01-17 10:31:00 -080022import android.content.ComponentName;
Eugene Susla4ab95112018-12-17 14:45:11 -080023import android.content.Context;
Hai Zhang8be463f2019-02-11 16:44:45 -080024import android.content.pm.PackageManager;
Hai Zhanga22cd832019-01-30 13:38:43 -080025import android.content.pm.PackageManagerInternal;
Hai Zhang8be463f2019-02-11 16:44:45 -080026import android.content.pm.ResolveInfo;
Eugene Susla4ab95112018-12-17 14:45:11 -080027import android.os.Debug;
28import android.provider.Settings;
Eugene Susla92b88c72019-01-11 13:31:20 -080029import android.telecom.TelecomManager;
30import android.text.TextUtils;
Eugene Susla4ab95112018-12-17 14:45:11 -080031import android.util.Log;
32import android.util.Slog;
33
34import com.android.internal.telephony.SmsApplication;
35import com.android.internal.util.CollectionUtils;
Hai Zhanga22cd832019-01-30 13:38:43 -080036import com.android.server.LocalServices;
Eugene Susla4ab95112018-12-17 14:45:11 -080037import com.android.server.role.RoleManagerService;
38
Hai Zhang8be463f2019-02-11 16:44:45 -080039import java.util.ArrayList;
Eugene Susla4ab95112018-12-17 14:45:11 -080040import java.util.Collection;
41import java.util.Collections;
42import java.util.List;
43
44/**
45 * Logic to retrieve the various legacy(pre-Q) equivalents of role holders.
46 *
47 * Unlike {@link RoleManagerService} this is meant to be pretty high-level to allow for depending
48 * on all kinds of various systems that are historically involved in legacy role resolution,
49 * e.g. {@link SmsApplication}
50 *
51 * @see RoleManagerService#migrateRoleIfNecessary
52 */
53public class LegacyRoleResolutionPolicy implements RoleManagerService.RoleHoldersResolver {
54
55 private static final boolean DEBUG = false;
56 private static final String LOG_TAG = "LegacyRoleResolutionPol";
57
58 @NonNull
59 private final Context mContext;
60
Hai Zhang8be463f2019-02-11 16:44:45 -080061 public LegacyRoleResolutionPolicy(@NonNull Context context) {
Eugene Susla4ab95112018-12-17 14:45:11 -080062 mContext = context;
63 }
64
Hai Zhang8be463f2019-02-11 16:44:45 -080065 @NonNull
Eugene Susla4ab95112018-12-17 14:45:11 -080066 @Override
Hai Zhang8be463f2019-02-11 16:44:45 -080067 public List<String> getRoleHolders(@NonNull String roleName, @UserIdInt int userId) {
Eugene Susla4ab95112018-12-17 14:45:11 -080068 switch (roleName) {
Hai Zhang8be463f2019-02-11 16:44:45 -080069 case RoleManager.ROLE_ASSISTANT: {
70 String legacyAssistant = Settings.Secure.getStringForUser(
71 mContext.getContentResolver(), Settings.Secure.ASSISTANT, userId);
72 if (legacyAssistant == null || legacyAssistant.isEmpty()) {
73 return Collections.emptyList();
74 } else {
75 return Collections.singletonList(
76 ComponentName.unflattenFromString(legacyAssistant).getPackageName());
77 }
78 }
79 case RoleManager.ROLE_BROWSER: {
80 PackageManagerInternal packageManagerInternal = LocalServices.getService(
81 PackageManagerInternal.class);
82 String packageName = packageManagerInternal.removeLegacyDefaultBrowserPackageName(
83 userId);
84 return CollectionUtils.singletonOrEmpty(packageName);
85 }
86 case RoleManager.ROLE_DIALER: {
87 String setting = Settings.Secure.getStringForUser(
88 mContext.getContentResolver(),
89 Settings.Secure.DIALER_DEFAULT_APPLICATION, userId);
90 return CollectionUtils.singletonOrEmpty(!TextUtils.isEmpty(setting)
91 ? setting
92 : mContext.getSystemService(TelecomManager.class).getSystemDialerPackage());
93 }
Eugene Susla4ab95112018-12-17 14:45:11 -080094 case RoleManager.ROLE_SMS: {
95 // Moved over from SmsApplication#getApplication
96 String result = Settings.Secure.getStringForUser(
97 mContext.getContentResolver(),
98 Settings.Secure.SMS_DEFAULT_APPLICATION, userId);
Hai Zhang9eb69e92019-02-07 20:03:40 -080099 // TODO: STOPSHIP: Remove the following code once we read the value of
100 // config_defaultSms in RoleControllerService.
Eugene Susla4ab95112018-12-17 14:45:11 -0800101 if (result == null) {
102 Collection<SmsApplication.SmsApplicationData> applications =
103 SmsApplication.getApplicationCollectionAsUser(mContext, userId);
104 SmsApplication.SmsApplicationData applicationData;
105 String defaultPackage = mContext.getResources()
106 .getString(com.android.internal.R.string.default_sms_application);
107 applicationData =
108 SmsApplication.getApplicationForPackage(applications, defaultPackage);
109
110 if (applicationData == null) {
111 // Are there any applications?
112 if (applications.size() != 0) {
113 applicationData =
114 (SmsApplication.SmsApplicationData) applications.toArray()[0];
115 }
116 }
117 if (DEBUG) {
118 Log.i(LOG_TAG, "Found default sms app: " + applicationData
119 + " among: " + applications + " from " + Debug.getCallers(4));
120 }
121 SmsApplication.SmsApplicationData app = applicationData;
122 result = app == null ? null : app.mPackageName;
123 }
Eugene Susla4ab95112018-12-17 14:45:11 -0800124 return CollectionUtils.singletonOrEmpty(result);
125 }
Hai Zhang8be463f2019-02-11 16:44:45 -0800126 case RoleManager.ROLE_HOME: {
127 PackageManager packageManager = mContext.getPackageManager();
128 List<ResolveInfo> resolveInfos = new ArrayList<>();
129 ComponentName componentName = packageManager.getHomeActivities(resolveInfos);
130 String packageName = componentName != null ? componentName.getPackageName() : null;
Hai Zhanga22cd832019-01-30 13:38:43 -0800131 return CollectionUtils.singletonOrEmpty(packageName);
132 }
Hongming Jin910cd7b2019-01-23 11:19:07 -0800133 case RoleManager.ROLE_EMERGENCY: {
134 String defaultEmergencyApp = Settings.Secure.getStringForUser(
135 mContext.getContentResolver(),
136 Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION, userId);
137 return CollectionUtils.singletonOrEmpty(defaultEmergencyApp);
138 }
Eugene Susla4ab95112018-12-17 14:45:11 -0800139 default: {
140 Slog.e(LOG_TAG, "Don't know how to find legacy role holders for " + roleName);
141 return Collections.emptyList();
142 }
143 }
144 }
145}