blob: 055c941f8b0a236a766a47e61208b8cac8ca4eb9 [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;
21import android.content.Context;
22import android.os.Debug;
23import android.provider.Settings;
24import android.util.Log;
25import android.util.Slog;
26
27import com.android.internal.telephony.SmsApplication;
28import com.android.internal.util.CollectionUtils;
29import com.android.server.role.RoleManagerService;
30
31import java.util.Collection;
32import java.util.Collections;
33import java.util.List;
34
35/**
36 * Logic to retrieve the various legacy(pre-Q) equivalents of role holders.
37 *
38 * Unlike {@link RoleManagerService} this is meant to be pretty high-level to allow for depending
39 * on all kinds of various systems that are historically involved in legacy role resolution,
40 * e.g. {@link SmsApplication}
41 *
42 * @see RoleManagerService#migrateRoleIfNecessary
43 */
44public class LegacyRoleResolutionPolicy implements RoleManagerService.RoleHoldersResolver {
45
46 private static final boolean DEBUG = false;
47 private static final String LOG_TAG = "LegacyRoleResolutionPol";
48
49 @NonNull
50 private final Context mContext;
51
52 public LegacyRoleResolutionPolicy(Context context) {
53 mContext = context;
54 }
55
56 @Override
57 public List<String> getRoleHolders(String roleName, int userId) {
58 switch (roleName) {
59 case RoleManager.ROLE_SMS: {
60 // Moved over from SmsApplication#getApplication
61 String result = Settings.Secure.getStringForUser(
62 mContext.getContentResolver(),
63 Settings.Secure.SMS_DEFAULT_APPLICATION, userId);
64
Hai Zhang7876adb2019-01-15 21:18:41 -080065 // TODO: STOPSHIP: Remove the following code once we remove default_sms_application
66 // and use the new config_defaultRoleHolders.
Eugene Susla4ab95112018-12-17 14:45:11 -080067 if (result == null) {
68 Collection<SmsApplication.SmsApplicationData> applications =
69 SmsApplication.getApplicationCollectionAsUser(mContext, userId);
70 SmsApplication.SmsApplicationData applicationData;
71 String defaultPackage = mContext.getResources()
72 .getString(com.android.internal.R.string.default_sms_application);
73 applicationData =
74 SmsApplication.getApplicationForPackage(applications, defaultPackage);
75
76 if (applicationData == null) {
77 // Are there any applications?
78 if (applications.size() != 0) {
79 applicationData =
80 (SmsApplication.SmsApplicationData) applications.toArray()[0];
81 }
82 }
83 if (DEBUG) {
84 Log.i(LOG_TAG, "Found default sms app: " + applicationData
85 + " among: " + applications + " from " + Debug.getCallers(4));
86 }
87 SmsApplication.SmsApplicationData app = applicationData;
88 result = app == null ? null : app.mPackageName;
89 }
90
91 return CollectionUtils.singletonOrEmpty(result);
92 }
93 default: {
94 Slog.e(LOG_TAG, "Don't know how to find legacy role holders for " + roleName);
95 return Collections.emptyList();
96 }
97 }
98 }
99}