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.permissioncontroller.role.ui; 18 19 import android.app.role.OnRoleHoldersChangedListener; 20 import android.app.role.RoleManager; 21 import android.content.Context; 22 import android.content.pm.ApplicationInfo; 23 import android.os.UserHandle; 24 import android.util.Log; 25 import android.util.Pair; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.WorkerThread; 29 import androidx.lifecycle.LiveData; 30 31 import com.android.permissioncontroller.AsyncTaskLiveData; 32 import com.android.permissioncontroller.role.utils.PackageUtils; 33 import com.android.permissioncontroller.role.utils.RoleUiBehaviorUtils; 34 import com.android.role.controller.model.Role; 35 36 import java.util.ArrayList; 37 import java.util.List; 38 39 /** 40 * {@link LiveData} for a role. 41 */ 42 public class RoleLiveData extends AsyncTaskLiveData<List<Pair<ApplicationInfo, Boolean>>> 43 implements OnRoleHoldersChangedListener { 44 45 private static final String LOG_TAG = RoleLiveData.class.getSimpleName(); 46 47 @NonNull 48 private final Role mRole; 49 @NonNull 50 private final UserHandle mUser; 51 @NonNull 52 private final Context mContext; 53 RoleLiveData(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)54 public RoleLiveData(@NonNull Role role, @NonNull UserHandle user, @NonNull Context context) { 55 mRole = role; 56 mUser = user; 57 mContext = context; 58 } 59 60 @Override onActive()61 protected void onActive() { 62 loadValue(); 63 64 RoleManager roleManager = mContext.getSystemService(RoleManager.class); 65 roleManager.addOnRoleHoldersChangedListenerAsUser(mContext.getMainExecutor(), this, mUser); 66 } 67 68 @Override onInactive()69 protected void onInactive() { 70 RoleManager roleManager = mContext.getSystemService(RoleManager.class); 71 roleManager.removeOnRoleHoldersChangedListenerAsUser(this, mUser); 72 } 73 74 @Override onRoleHoldersChanged(@onNull String roleName, @NonNull UserHandle user)75 public void onRoleHoldersChanged(@NonNull String roleName, @NonNull UserHandle user) { 76 loadValue(); 77 } 78 79 @Override 80 @WorkerThread loadValueInBackground()81 protected List<Pair<ApplicationInfo, Boolean>> loadValueInBackground() { 82 RoleManager roleManager = mContext.getSystemService(RoleManager.class); 83 List<String> holderPackageNames = roleManager.getRoleHoldersAsUser(mRole.getName(), mUser); 84 85 List<String> qualifyingPackageNames = mRole.getQualifyingPackagesAsUser(mUser, mContext); 86 List<Pair<ApplicationInfo, Boolean>> qualifyingApplications = new ArrayList<>(); 87 int qualifyingPackageNamesSize = qualifyingPackageNames.size(); 88 for (int i = 0; i < qualifyingPackageNamesSize; i++) { 89 String qualifyingPackageName = qualifyingPackageNames.get(i); 90 91 ApplicationInfo qualifyingApplicationInfo = PackageUtils.getApplicationInfoAsUser( 92 qualifyingPackageName, mUser, mContext); 93 if (qualifyingApplicationInfo == null) { 94 Log.w(LOG_TAG, "Cannot get ApplicationInfo for application, skipping: " 95 + qualifyingPackageName); 96 continue; 97 } 98 if (!RoleUiBehaviorUtils.isApplicationVisibleAsUser(mRole, qualifyingApplicationInfo, 99 mUser, mContext)) { 100 continue; 101 } 102 boolean isHolderApplication = holderPackageNames.contains(qualifyingPackageName); 103 qualifyingApplications.add(new Pair<>(qualifyingApplicationInfo, isHolderApplication)); 104 } 105 106 return qualifyingApplications; 107 } 108 } 109