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