Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.policy.role; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.app.role.RoleManager; |
| 21 | import android.content.Context; |
| 22 | import android.os.Debug; |
| 23 | import android.provider.Settings; |
| 24 | import android.util.Log; |
| 25 | import android.util.Slog; |
| 26 | |
| 27 | import com.android.internal.telephony.SmsApplication; |
| 28 | import com.android.internal.util.CollectionUtils; |
| 29 | import com.android.server.role.RoleManagerService; |
| 30 | |
| 31 | import java.util.Collection; |
| 32 | import java.util.Collections; |
| 33 | import 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 | */ |
| 44 | public 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 Zhang | 7876adb | 2019-01-15 21:18:41 -0800 | [diff] [blame^] | 65 | // TODO: STOPSHIP: Remove the following code once we remove default_sms_application |
| 66 | // and use the new config_defaultRoleHolders. |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 67 | 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 | } |