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 /** 31 * Packages that are the installer of record for some package on the device. 32 */ 33 @RequiresApi(Build.VERSION_CODES.S) 34 class InstallerPackagesLiveData(private val user: UserHandle) 35 : SmartAsyncMediatorLiveData<Set<String>>() { 36 37 init { 38 addSource(AllPackageInfosLiveData) { 39 update() 40 } 41 } 42 43 override suspend fun loadDataAndPostValue(job: Job) { 44 if (job.isCancelled) { 45 return 46 } 47 if (!AllPackageInfosLiveData.isInitialized) { 48 return 49 } 50 val userPackageInfos = AllPackageInfosLiveData.value!![user] 51 val installerPackages = mutableSetOf<String>() 52 val packageManager = PermissionControllerApplication.get().packageManager 53 54 userPackageInfos!!.forEach { pkgInfo -> 55 try { 56 val installerPkg = 57 packageManager.getInstallSourceInfo(pkgInfo.packageName).installingPackageName 58 if (installerPkg != null) { 59 installerPackages.add(installerPkg) 60 } 61 } catch (e: PackageManager.NameNotFoundException) { 62 DumpableLog.w(LOG_TAG, "Unable to find installer source info", e) 63 } 64 } 65 66 postValue(installerPackages) 67 } 68 69 /** 70 * Repository for installer packages 71 * 72 * <p> Key value is user 73 */ 74 companion object : DataRepositoryForPackage<UserHandle, InstallerPackagesLiveData>() { 75 override fun newValue(key: UserHandle): InstallerPackagesLiveData { 76 return InstallerPackagesLiveData(key) 77 } 78 } 79 } 80