blob: e7de8ddf87d180dfa72336a3f99d34b77ad5c002 [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;
20import android.app.role.RoleManager;
Philip P. Moltmann5d894502019-01-17 10:31:00 -080021import android.content.ComponentName;
Eugene Susla4ab95112018-12-17 14:45:11 -080022import android.content.Context;
Hai Zhanga22cd832019-01-30 13:38:43 -080023import android.content.pm.PackageManagerInternal;
Eugene Susla4ab95112018-12-17 14:45:11 -080024import android.os.Debug;
25import android.provider.Settings;
Eugene Susla92b88c72019-01-11 13:31:20 -080026import android.telecom.TelecomManager;
27import android.text.TextUtils;
Eugene Susla4ab95112018-12-17 14:45:11 -080028import android.util.Log;
29import android.util.Slog;
30
31import com.android.internal.telephony.SmsApplication;
32import com.android.internal.util.CollectionUtils;
Hai Zhanga22cd832019-01-30 13:38:43 -080033import com.android.server.LocalServices;
Eugene Susla4ab95112018-12-17 14:45:11 -080034import com.android.server.role.RoleManagerService;
35
36import java.util.Collection;
37import java.util.Collections;
38import java.util.List;
39
40/**
41 * Logic to retrieve the various legacy(pre-Q) equivalents of role holders.
42 *
43 * Unlike {@link RoleManagerService} this is meant to be pretty high-level to allow for depending
44 * on all kinds of various systems that are historically involved in legacy role resolution,
45 * e.g. {@link SmsApplication}
46 *
47 * @see RoleManagerService#migrateRoleIfNecessary
48 */
49public class LegacyRoleResolutionPolicy implements RoleManagerService.RoleHoldersResolver {
50
51 private static final boolean DEBUG = false;
52 private static final String LOG_TAG = "LegacyRoleResolutionPol";
53
54 @NonNull
55 private final Context mContext;
56
57 public LegacyRoleResolutionPolicy(Context context) {
58 mContext = context;
59 }
60
61 @Override
62 public List<String> getRoleHolders(String roleName, int userId) {
63 switch (roleName) {
64 case RoleManager.ROLE_SMS: {
65 // Moved over from SmsApplication#getApplication
66 String result = Settings.Secure.getStringForUser(
67 mContext.getContentResolver(),
68 Settings.Secure.SMS_DEFAULT_APPLICATION, userId);
69
Hai Zhang7876adb2019-01-15 21:18:41 -080070 // TODO: STOPSHIP: Remove the following code once we remove default_sms_application
71 // and use the new config_defaultRoleHolders.
Eugene Susla4ab95112018-12-17 14:45:11 -080072 if (result == null) {
73 Collection<SmsApplication.SmsApplicationData> applications =
74 SmsApplication.getApplicationCollectionAsUser(mContext, userId);
75 SmsApplication.SmsApplicationData applicationData;
76 String defaultPackage = mContext.getResources()
77 .getString(com.android.internal.R.string.default_sms_application);
78 applicationData =
79 SmsApplication.getApplicationForPackage(applications, defaultPackage);
80
81 if (applicationData == null) {
82 // Are there any applications?
83 if (applications.size() != 0) {
84 applicationData =
85 (SmsApplication.SmsApplicationData) applications.toArray()[0];
86 }
87 }
88 if (DEBUG) {
89 Log.i(LOG_TAG, "Found default sms app: " + applicationData
90 + " among: " + applications + " from " + Debug.getCallers(4));
91 }
92 SmsApplication.SmsApplicationData app = applicationData;
93 result = app == null ? null : app.mPackageName;
94 }
95
96 return CollectionUtils.singletonOrEmpty(result);
97 }
Philip P. Moltmann5d894502019-01-17 10:31:00 -080098 case RoleManager.ROLE_ASSISTANT: {
99 String legacyAssistant = Settings.Secure.getStringForUser(
100 mContext.getContentResolver(), Settings.Secure.ASSISTANT, userId);
101
102 if (legacyAssistant == null || legacyAssistant.isEmpty()) {
103 return Collections.emptyList();
104 } else {
105 return Collections.singletonList(
106 ComponentName.unflattenFromString(legacyAssistant).getPackageName());
107 }
108 }
Eugene Susla92b88c72019-01-11 13:31:20 -0800109 case RoleManager.ROLE_DIALER: {
110 String setting = Settings.Secure.getStringForUser(
111 mContext.getContentResolver(),
112 Settings.Secure.DIALER_DEFAULT_APPLICATION, userId);
113
114 return CollectionUtils.singletonOrEmpty(!TextUtils.isEmpty(setting)
115 ? setting
116 : mContext.getSystemService(TelecomManager.class).getSystemDialerPackage());
117 }
Hai Zhanga22cd832019-01-30 13:38:43 -0800118 case RoleManager.ROLE_BROWSER: {
119 PackageManagerInternal packageManagerInternal = LocalServices.getService(
120 PackageManagerInternal.class);
121 String packageName = packageManagerInternal.removeLegacyDefaultBrowserPackageName(
122 userId);
123 return CollectionUtils.singletonOrEmpty(packageName);
124 }
Eugene Susla4ab95112018-12-17 14:45:11 -0800125 default: {
126 Slog.e(LOG_TAG, "Don't know how to find legacy role holders for " + roleName);
127 return Collections.emptyList();
128 }
129 }
130 }
131}