1 /* 2 * Copyright (C) 2019 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.packageinstaller.role.model; 18 19 import android.content.Context; 20 import android.content.pm.ApplicationInfo; 21 import android.content.res.Resources; 22 import android.text.TextUtils; 23 import android.util.Log; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 28 import com.android.packageinstaller.permission.utils.CollectionUtils; 29 import com.android.packageinstaller.role.utils.PackageUtils; 30 31 import java.util.List; 32 33 /** 34 * Mixin for {@link RoleBehavior#getDefaultHolders(Role, Context)} that returns a single default 35 * role holder from the corresponding string resource. 36 */ 37 public class ExclusiveDefaultHolderMixin { 38 39 private static final String LOG_TAG = ExclusiveDefaultHolderMixin.class.getSimpleName(); 40 ExclusiveDefaultHolderMixin()41 private ExclusiveDefaultHolderMixin() {} 42 43 /** 44 * @see Role#getDefaultHolders(Context) 45 */ 46 @NonNull getDefaultHolders(@onNull Role role, @NonNull String resourceName, @NonNull Context context)47 public static List<String> getDefaultHolders(@NonNull Role role, @NonNull String resourceName, 48 @NonNull Context context) { 49 return CollectionUtils.singletonOrEmpty(getDefaultHolder(role, resourceName, context)); 50 } 51 52 /** 53 * @see Role#getDefaultHolders(Context) 54 */ 55 @Nullable getDefaultHolder(@onNull Role role, @NonNull String resourceName, @NonNull Context context)56 public static String getDefaultHolder(@NonNull Role role, @NonNull String resourceName, 57 @NonNull Context context) { 58 Resources resources = context.getResources(); 59 int resourceId = resources.getIdentifier(resourceName, "string", "android"); 60 if (resourceId == 0) { 61 Log.w(LOG_TAG, "Cannot find resource for default holder: " + resourceName); 62 return null; 63 } 64 String packageName; 65 try { 66 packageName = resources.getString(resourceId); 67 } catch (Resources.NotFoundException e) { 68 Log.w(LOG_TAG, "Cannot get resource for default holder: " + resourceName, e); 69 return null; 70 } 71 if (TextUtils.isEmpty(packageName)) { 72 return null; 73 } 74 75 ApplicationInfo applicationInfo = PackageUtils.getApplicationInfo(packageName, context); 76 if (applicationInfo == null) { 77 Log.w(LOG_TAG, "Cannot get ApplicationInfo for default holder, config: " + resourceName 78 + ", package: " + packageName); 79 return null; 80 } 81 if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { 82 Log.w(LOG_TAG, "Default holder is not a system app, config: " + resourceName 83 + ", package: " + packageName); 84 return null; 85 } 86 87 if (!role.isPackageQualified(packageName, context)) { 88 Log.w(LOG_TAG, "Default holder does not qualify for the role, config: " + resourceName 89 + ", package: " + packageName); 90 return null; 91 } 92 return packageName; 93 } 94 } 95