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; |
Hai Zhang | 8be463f | 2019-02-11 16:44:45 -0800 | [diff] [blame] | 20 | import android.annotation.UserIdInt; |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 21 | import android.app.role.RoleManager; |
Philip P. Moltmann | 5d89450 | 2019-01-17 10:31:00 -0800 | [diff] [blame] | 22 | import android.content.ComponentName; |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Hai Zhang | 8be463f | 2019-02-11 16:44:45 -0800 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Hai Zhang | a22cd83 | 2019-01-30 13:38:43 -0800 | [diff] [blame] | 25 | import android.content.pm.PackageManagerInternal; |
Hai Zhang | 8be463f | 2019-02-11 16:44:45 -0800 | [diff] [blame] | 26 | import android.content.pm.ResolveInfo; |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 27 | import android.os.Debug; |
| 28 | import android.provider.Settings; |
Eugene Susla | 92b88c7 | 2019-01-11 13:31:20 -0800 | [diff] [blame] | 29 | import android.telecom.TelecomManager; |
| 30 | import android.text.TextUtils; |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 31 | import android.util.Log; |
| 32 | import android.util.Slog; |
| 33 | |
| 34 | import com.android.internal.telephony.SmsApplication; |
| 35 | import com.android.internal.util.CollectionUtils; |
Hai Zhang | a22cd83 | 2019-01-30 13:38:43 -0800 | [diff] [blame] | 36 | import com.android.server.LocalServices; |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 37 | import com.android.server.role.RoleManagerService; |
| 38 | |
Hai Zhang | 8be463f | 2019-02-11 16:44:45 -0800 | [diff] [blame] | 39 | import java.util.ArrayList; |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 40 | import java.util.Collection; |
| 41 | import java.util.Collections; |
| 42 | import java.util.List; |
| 43 | |
| 44 | /** |
| 45 | * Logic to retrieve the various legacy(pre-Q) equivalents of role holders. |
| 46 | * |
| 47 | * Unlike {@link RoleManagerService} this is meant to be pretty high-level to allow for depending |
| 48 | * on all kinds of various systems that are historically involved in legacy role resolution, |
| 49 | * e.g. {@link SmsApplication} |
| 50 | * |
| 51 | * @see RoleManagerService#migrateRoleIfNecessary |
| 52 | */ |
| 53 | public class LegacyRoleResolutionPolicy implements RoleManagerService.RoleHoldersResolver { |
| 54 | |
| 55 | private static final boolean DEBUG = false; |
| 56 | private static final String LOG_TAG = "LegacyRoleResolutionPol"; |
| 57 | |
| 58 | @NonNull |
| 59 | private final Context mContext; |
| 60 | |
Hai Zhang | 8be463f | 2019-02-11 16:44:45 -0800 | [diff] [blame] | 61 | public LegacyRoleResolutionPolicy(@NonNull Context context) { |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 62 | mContext = context; |
| 63 | } |
| 64 | |
Hai Zhang | 8be463f | 2019-02-11 16:44:45 -0800 | [diff] [blame] | 65 | @NonNull |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 66 | @Override |
Hai Zhang | 8be463f | 2019-02-11 16:44:45 -0800 | [diff] [blame] | 67 | public List<String> getRoleHolders(@NonNull String roleName, @UserIdInt int userId) { |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 68 | switch (roleName) { |
Hai Zhang | 8be463f | 2019-02-11 16:44:45 -0800 | [diff] [blame] | 69 | case RoleManager.ROLE_ASSISTANT: { |
| 70 | String legacyAssistant = Settings.Secure.getStringForUser( |
| 71 | mContext.getContentResolver(), Settings.Secure.ASSISTANT, userId); |
| 72 | if (legacyAssistant == null || legacyAssistant.isEmpty()) { |
| 73 | return Collections.emptyList(); |
| 74 | } else { |
| 75 | return Collections.singletonList( |
| 76 | ComponentName.unflattenFromString(legacyAssistant).getPackageName()); |
| 77 | } |
| 78 | } |
| 79 | case RoleManager.ROLE_BROWSER: { |
| 80 | PackageManagerInternal packageManagerInternal = LocalServices.getService( |
| 81 | PackageManagerInternal.class); |
| 82 | String packageName = packageManagerInternal.removeLegacyDefaultBrowserPackageName( |
| 83 | userId); |
| 84 | return CollectionUtils.singletonOrEmpty(packageName); |
| 85 | } |
| 86 | case RoleManager.ROLE_DIALER: { |
| 87 | String setting = Settings.Secure.getStringForUser( |
| 88 | mContext.getContentResolver(), |
| 89 | Settings.Secure.DIALER_DEFAULT_APPLICATION, userId); |
| 90 | return CollectionUtils.singletonOrEmpty(!TextUtils.isEmpty(setting) |
| 91 | ? setting |
| 92 | : mContext.getSystemService(TelecomManager.class).getSystemDialerPackage()); |
| 93 | } |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 94 | case RoleManager.ROLE_SMS: { |
| 95 | // Moved over from SmsApplication#getApplication |
| 96 | String result = Settings.Secure.getStringForUser( |
| 97 | mContext.getContentResolver(), |
| 98 | Settings.Secure.SMS_DEFAULT_APPLICATION, userId); |
Hai Zhang | 9eb69e9 | 2019-02-07 20:03:40 -0800 | [diff] [blame] | 99 | // TODO: STOPSHIP: Remove the following code once we read the value of |
| 100 | // config_defaultSms in RoleControllerService. |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 101 | if (result == null) { |
| 102 | Collection<SmsApplication.SmsApplicationData> applications = |
| 103 | SmsApplication.getApplicationCollectionAsUser(mContext, userId); |
| 104 | SmsApplication.SmsApplicationData applicationData; |
| 105 | String defaultPackage = mContext.getResources() |
| 106 | .getString(com.android.internal.R.string.default_sms_application); |
| 107 | applicationData = |
| 108 | SmsApplication.getApplicationForPackage(applications, defaultPackage); |
| 109 | |
| 110 | if (applicationData == null) { |
| 111 | // Are there any applications? |
| 112 | if (applications.size() != 0) { |
| 113 | applicationData = |
| 114 | (SmsApplication.SmsApplicationData) applications.toArray()[0]; |
| 115 | } |
| 116 | } |
| 117 | if (DEBUG) { |
| 118 | Log.i(LOG_TAG, "Found default sms app: " + applicationData |
| 119 | + " among: " + applications + " from " + Debug.getCallers(4)); |
| 120 | } |
| 121 | SmsApplication.SmsApplicationData app = applicationData; |
| 122 | result = app == null ? null : app.mPackageName; |
| 123 | } |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 124 | return CollectionUtils.singletonOrEmpty(result); |
| 125 | } |
Hai Zhang | 8be463f | 2019-02-11 16:44:45 -0800 | [diff] [blame] | 126 | case RoleManager.ROLE_HOME: { |
| 127 | PackageManager packageManager = mContext.getPackageManager(); |
| 128 | List<ResolveInfo> resolveInfos = new ArrayList<>(); |
| 129 | ComponentName componentName = packageManager.getHomeActivities(resolveInfos); |
| 130 | String packageName = componentName != null ? componentName.getPackageName() : null; |
Hai Zhang | a22cd83 | 2019-01-30 13:38:43 -0800 | [diff] [blame] | 131 | return CollectionUtils.singletonOrEmpty(packageName); |
| 132 | } |
Hongming Jin | 910cd7b | 2019-01-23 11:19:07 -0800 | [diff] [blame] | 133 | case RoleManager.ROLE_EMERGENCY: { |
| 134 | String defaultEmergencyApp = Settings.Secure.getStringForUser( |
| 135 | mContext.getContentResolver(), |
| 136 | Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION, userId); |
| 137 | return CollectionUtils.singletonOrEmpty(defaultEmergencyApp); |
| 138 | } |
Eugene Susla | 4ab9511 | 2018-12-17 14:45:11 -0800 | [diff] [blame] | 139 | default: { |
| 140 | Slog.e(LOG_TAG, "Don't know how to find legacy role holders for " + roleName); |
| 141 | return Collections.emptyList(); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |