1 /* <lambda>null2 * Copyright (C) 2022 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.hibernation.v31 18 19 import android.content.pm.PackageManager 20 import android.os.Build 21 import android.os.UserHandle 22 import androidx.annotation.RequiresApi 23 import com.android.permissioncontroller.DumpableLog 24 import com.android.permissioncontroller.PermissionControllerApplication 25 import com.android.permissioncontroller.permission.data.AllPackageInfosLiveData 26 import com.android.permissioncontroller.permission.data.DataRepositoryForPackage 27 import com.android.permissioncontroller.permission.data.SmartAsyncMediatorLiveData 28 import kotlinx.coroutines.Job 29 30 /** Packages that are the installer of record for some package on the device. */ 31 @RequiresApi(Build.VERSION_CODES.S) 32 class InstallerPackagesLiveData(private val user: UserHandle) : 33 SmartAsyncMediatorLiveData<Set<String>>() { 34 35 init { 36 addSource(AllPackageInfosLiveData) { update() } 37 } 38 39 override suspend fun loadDataAndPostValue(job: Job) { 40 if (job.isCancelled) { 41 return 42 } 43 if (!AllPackageInfosLiveData.isInitialized) { 44 return 45 } 46 val userPackageInfos = AllPackageInfosLiveData.value!![user] 47 val installerPackages = mutableSetOf<String>() 48 val packageManager = PermissionControllerApplication.get().packageManager 49 50 userPackageInfos!!.forEach { pkgInfo -> 51 try { 52 val installerPkg = 53 packageManager.getInstallSourceInfo(pkgInfo.packageName).installingPackageName 54 if (installerPkg != null) { 55 installerPackages.add(installerPkg) 56 } 57 } catch (e: PackageManager.NameNotFoundException) { 58 DumpableLog.w(LOG_TAG, "Unable to find installer source info", e) 59 } 60 } 61 62 postValue(installerPackages) 63 } 64 65 /** 66 * Repository for installer packages 67 * 68 * <p> Key value is user 69 */ 70 companion object : DataRepositoryForPackage<UserHandle, InstallerPackagesLiveData>() { 71 override fun newValue(key: UserHandle): InstallerPackagesLiveData { 72 return InstallerPackagesLiveData(key) 73 } 74 } 75 } 76