• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2020 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 @file:Suppress("DEPRECATION")
17 
18 package com.android.permissioncontroller.permission.data
19 
20 import android.app.Application
21 import android.content.pm.PackageManager.GET_PERMISSIONS
22 import android.content.pm.PackageManager.MATCH_FACTORY_ONLY
23 import android.content.pm.PackageManager.MATCH_UNINSTALLED_PACKAGES
24 import android.os.UserHandle
25 import com.android.permissioncontroller.PermissionControllerApplication
26 import com.android.permissioncontroller.permission.model.livedatatypes.LightPackageInfo
27 import kotlinx.coroutines.Job
28 
29 /**
30  * A LiveData which returns all of the preinstalled packageinfos. For packages that are preinstalled
31  * and then updated, the preinstalled (i.e. old) version is returned.
32  *
33  * @param app The current application
34  * @param user The user whose packages are desired
35  */
36 class PreinstalledUserPackageInfosLiveData private constructor(
37     private val app: Application,
38     private val user: UserHandle
39 ) : SmartAsyncMediatorLiveData<@kotlin.jvm.JvmSuppressWildcards List<LightPackageInfo>>(
40     isStaticVal = true, alwaysUpdateOnActive = false
41 ) {
42 
43     /**
44      * Get all of the preinstalled packages in the system for this user
45      */
46     override suspend fun loadDataAndPostValue(job: Job) {
47         if (job.isCancelled) {
48             return
49         }
50         val packageInfos = app.applicationContext.packageManager
51                 .getInstalledPackagesAsUser(GET_PERMISSIONS or MATCH_UNINSTALLED_PACKAGES
52                         or MATCH_FACTORY_ONLY, user.identifier)
53         postValue(packageInfos.map { packageInfo -> LightPackageInfo(packageInfo) })
54     }
55 
56     override fun onActive() {
57         super.onActive()
58 
59         // Data never changes, hence no need to reload
60         if (value == null) {
61             updateAsync()
62         }
63     }
64 
65     /**
66      * Repository for PreinstalledUserPackageInfosLiveData.
67      *
68      * <p>Key value is a UserHandle, value is its corresponding LiveData.
69      */
70     companion object : DataRepository<UserHandle, PreinstalledUserPackageInfosLiveData>() {
71         override fun newValue(key: UserHandle): PreinstalledUserPackageInfosLiveData {
72             return PreinstalledUserPackageInfosLiveData(PermissionControllerApplication.get(), key)
73         }
74     }
75 }
76