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; |
Philip P. Moltmann | 5d89450 | 2019-01-17 10:31:00 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.os.Debug; |
| 24 | import android.provider.Settings; |
Eugene Susla | 92b88c7 | 2019-01-11 13:31:20 -0800 | [diff] [blame] | 25 | import android.telecom.TelecomManager; |
| 26 | import android.text.TextUtils; |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | import android.util.Slog; |
| 29 | |
| 30 | import com.android.internal.telephony.SmsApplication; |
| 31 | import com.android.internal.util.CollectionUtils; |
| 32 | import com.android.server.role.RoleManagerService; |
| 33 | |
| 34 | import java.util.Collection; |
| 35 | import java.util.Collections; |
| 36 | import 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 | */ |
| 47 | public 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 Zhang | 7876adb | 2019-01-15 21:18:41 -0800 | [diff] [blame] | 68 | // TODO: STOPSHIP: Remove the following code once we remove default_sms_application |
| 69 | // and use the new config_defaultRoleHolders. |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 70 | 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. Moltmann | 5d89450 | 2019-01-17 10:31:00 -0800 | [diff] [blame] | 96 | 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 Susla | 92b88c7 | 2019-01-11 13:31:20 -0800 | [diff] [blame] | 107 | 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 Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 116 | 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 | } |