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.permission.data; 18 19 import static android.os.UserHandle.getUserHandleForUid; 20 21 import static com.android.packageinstaller.permission.utils.Utils.FLAGS_ALWAYS_USER_SENSITIVE; 22 23 import android.app.Application; 24 import android.content.pm.ApplicationInfo; 25 import android.content.pm.PackageManager; 26 import android.os.AsyncTask; 27 import android.os.UserHandle; 28 import android.util.ArrayMap; 29 30 import androidx.annotation.MainThread; 31 import androidx.annotation.NonNull; 32 import androidx.lifecycle.MediatorLiveData; 33 34 import java.util.ArrayList; 35 36 /** 37 * Live data of packages that are not fully user sensitive by default. 38 * 39 * <p>Data source: {@link UidToSensitivityLiveData} 40 */ 41 public class NonSensitivePackagesLiveData extends MediatorLiveData<ArrayList<ApplicationInfo>> { 42 private static NonSensitivePackagesLiveData sInstance; 43 44 /** 45 * Get a (potentially shared) live data. 46 * 47 * @param application The application context 48 * 49 * @return The live data 50 */ 51 @MainThread get(@onNull Application application)52 public static NonSensitivePackagesLiveData get(@NonNull Application application) { 53 if (sInstance == null) { 54 sInstance = new NonSensitivePackagesLiveData(application); 55 } 56 57 return sInstance; 58 } 59 NonSensitivePackagesLiveData(@onNull Application application)60 private NonSensitivePackagesLiveData(@NonNull Application application) { 61 UidToSensitivityLiveData uidLiveData = UidToSensitivityLiveData.get(application); 62 63 addSource(uidLiveData, uidToSensitivity -> AsyncTask.execute(() -> { 64 PackageManager pm = application.getPackageManager(); 65 66 ArrayList<ApplicationInfo> pkgs = new ArrayList<>(); 67 68 int numUids = uidToSensitivity.size(); 69 for (int uidNum = 0; uidNum < numUids; uidNum++) { 70 int uid = uidToSensitivity.keyAt(uidNum); 71 UserHandle user = getUserHandleForUid(uid); 72 ArrayMap<String, Integer> sensitivity = uidToSensitivity.valueAt(uidNum); 73 74 int numPerms = sensitivity.size(); 75 for (int permNum = 0; permNum < numPerms; permNum++) { 76 if (sensitivity.valueAt(permNum) != FLAGS_ALWAYS_USER_SENSITIVE) { 77 String[] uidPkgs = pm.getPackagesForUid(uid); 78 79 if (uidPkgs != null) { 80 for (String pkg : uidPkgs) { 81 ApplicationInfo appInfo; 82 try { 83 appInfo = pm.getApplicationInfoAsUser(pkg, 0, user); 84 } catch (PackageManager.NameNotFoundException e) { 85 continue; 86 } 87 88 pkgs.add(appInfo); 89 } 90 } 91 break; 92 } 93 } 94 95 } 96 97 postValue(pkgs); 98 })); 99 } 100 } 101