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