blob: 45c975b2695647d654698a04f3ba63da573dc42c [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
65 if (result == null) {
66 Collection<SmsApplication.SmsApplicationData> applications =
67 SmsApplication.getApplicationCollectionAsUser(mContext, userId);
68 SmsApplication.SmsApplicationData applicationData;
69 String defaultPackage = mContext.getResources()
70 .getString(com.android.internal.R.string.default_sms_application);
71 applicationData =
72 SmsApplication.getApplicationForPackage(applications, defaultPackage);
73
74 if (applicationData == null) {
75 // Are there any applications?
76 if (applications.size() != 0) {
77 applicationData =
78 (SmsApplication.SmsApplicationData) applications.toArray()[0];
79 }
80 }
81 if (DEBUG) {
82 Log.i(LOG_TAG, "Found default sms app: " + applicationData
83 + " among: " + applications + " from " + Debug.getCallers(4));
84 }
85 SmsApplication.SmsApplicationData app = applicationData;
86 result = app == null ? null : app.mPackageName;
87 }
88
89 return CollectionUtils.singletonOrEmpty(result);
90 }
91 default: {
92 Slog.e(LOG_TAG, "Don't know how to find legacy role holders for " + roleName);
93 return Collections.emptyList();
94 }
95 }
96 }
97}