blob: 712012d9e621c3dc72fd1fbee7271011b5de5e20 [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.provider.Settings;
Eugene Susla92b88c72019-01-11 13:31:20 -080028import android.text.TextUtils;
Eugene Susla4ab95112018-12-17 14:45:11 -080029import android.util.Slog;
30
Hai Zhangdbffb692019-08-21 16:35:18 -070031import com.android.internal.R;
Eugene Susla4ab95112018-12-17 14:45:11 -080032import com.android.internal.telephony.SmsApplication;
33import com.android.internal.util.CollectionUtils;
Hai Zhanga22cd832019-01-30 13:38:43 -080034import com.android.server.LocalServices;
Eugene Susla4ab95112018-12-17 14:45:11 -080035import com.android.server.role.RoleManagerService;
36
Hai Zhang8be463f2019-02-11 16:44:45 -080037import java.util.ArrayList;
Eugene Susla4ab95112018-12-17 14:45:11 -080038import java.util.Collections;
39import java.util.List;
40
41/**
42 * Logic to retrieve the various legacy(pre-Q) equivalents of role holders.
43 *
44 * Unlike {@link RoleManagerService} this is meant to be pretty high-level to allow for depending
45 * on all kinds of various systems that are historically involved in legacy role resolution,
46 * e.g. {@link SmsApplication}
47 *
48 * @see RoleManagerService#migrateRoleIfNecessary
49 */
50public class LegacyRoleResolutionPolicy implements RoleManagerService.RoleHoldersResolver {
51
52 private static final boolean DEBUG = false;
53 private static final String LOG_TAG = "LegacyRoleResolutionPol";
54
55 @NonNull
56 private final Context mContext;
57
Hai Zhang8be463f2019-02-11 16:44:45 -080058 public LegacyRoleResolutionPolicy(@NonNull Context context) {
Eugene Susla4ab95112018-12-17 14:45:11 -080059 mContext = context;
60 }
61
Hai Zhang8be463f2019-02-11 16:44:45 -080062 @NonNull
Eugene Susla4ab95112018-12-17 14:45:11 -080063 @Override
Hai Zhang8be463f2019-02-11 16:44:45 -080064 public List<String> getRoleHolders(@NonNull String roleName, @UserIdInt int userId) {
Eugene Susla4ab95112018-12-17 14:45:11 -080065 switch (roleName) {
Hai Zhang8be463f2019-02-11 16:44:45 -080066 case RoleManager.ROLE_ASSISTANT: {
Hai Zhangdbffb692019-08-21 16:35:18 -070067 String packageName;
68 String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(),
69 Settings.Secure.ASSISTANT, userId);
70 // AssistUtils was using the default assistant app if Settings.Secure.ASSISTANT is
71 // null, while only an empty string means user selected "None".
72 if (setting != null) {
73 if (!setting.isEmpty()) {
74 ComponentName componentName = ComponentName.unflattenFromString(setting);
75 packageName = componentName != null ? componentName.getPackageName() : null;
76 } else {
77 packageName = null;
78 }
79 } else if (mContext.getPackageManager().isDeviceUpgrading()) {
80 String defaultAssistant = mContext.getString(R.string.config_defaultAssistant);
81 packageName = !TextUtils.isEmpty(defaultAssistant) ? defaultAssistant : null;
Hai Zhang8be463f2019-02-11 16:44:45 -080082 } else {
Hai Zhangdbffb692019-08-21 16:35:18 -070083 packageName = null;
Hai Zhang8be463f2019-02-11 16:44:45 -080084 }
Hai Zhangdbffb692019-08-21 16:35:18 -070085 return CollectionUtils.singletonOrEmpty(packageName);
Hai Zhang8be463f2019-02-11 16:44:45 -080086 }
87 case RoleManager.ROLE_BROWSER: {
88 PackageManagerInternal packageManagerInternal = LocalServices.getService(
89 PackageManagerInternal.class);
90 String packageName = packageManagerInternal.removeLegacyDefaultBrowserPackageName(
91 userId);
92 return CollectionUtils.singletonOrEmpty(packageName);
93 }
94 case RoleManager.ROLE_DIALER: {
Hai Zhangdbffb692019-08-21 16:35:18 -070095 String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(),
Hai Zhang8be463f2019-02-11 16:44:45 -080096 Settings.Secure.DIALER_DEFAULT_APPLICATION, userId);
Hai Zhangdbffb692019-08-21 16:35:18 -070097 String packageName;
98 if (!TextUtils.isEmpty(setting)) {
99 packageName = setting;
100 } else if (mContext.getPackageManager().isDeviceUpgrading()) {
101 // DefaultDialerManager was using the default dialer app if
102 // Settings.Secure.DIALER_DEFAULT_APPLICATION is invalid.
103 // TelecomManager.getSystemDialerPackage() won't work because it might not
104 // be ready.
105 packageName = mContext.getString(R.string.config_defaultDialer);
106 } else {
107 packageName = null;
108 }
109 return CollectionUtils.singletonOrEmpty(packageName);
Hai Zhang8be463f2019-02-11 16:44:45 -0800110 }
Eugene Susla4ab95112018-12-17 14:45:11 -0800111 case RoleManager.ROLE_SMS: {
Hai Zhangdbffb692019-08-21 16:35:18 -0700112 String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(),
Eugene Susla4ab95112018-12-17 14:45:11 -0800113 Settings.Secure.SMS_DEFAULT_APPLICATION, userId);
Hai Zhangdbffb692019-08-21 16:35:18 -0700114 String packageName;
115 if (!TextUtils.isEmpty(setting)) {
116 packageName = setting;
117 } else if (mContext.getPackageManager().isDeviceUpgrading()) {
118 // SmsApplication was using the default SMS app if
119 // Settings.Secure.DIALER_DEFAULT_APPLICATION is invalid.
120 packageName = mContext.getString(R.string.config_defaultSms);
121 } else {
122 packageName = null;
Eugene Susla4ab95112018-12-17 14:45:11 -0800123 }
Hai Zhangdbffb692019-08-21 16:35:18 -0700124 return CollectionUtils.singletonOrEmpty(packageName);
Eugene Susla4ab95112018-12-17 14:45:11 -0800125 }
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}