• 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 
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 android.util.Log
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 
41     /**
42      * Get all of the preinstalled packages in the system for this user
43      */
44     override suspend fun loadDataAndPostValue(job: Job) {
45         if (job.isCancelled) {
46             return
47         }
48         // TODO ntmyren: remove once b/154796729 is fixed
49         Log.i("PreinstalledUserPackageInfos", "updating PreinstalledUserPackageInfosLiveData for " +
50             "user ${user.identifier}")
51         val packageInfos = app.applicationContext.packageManager
52                 .getInstalledPackagesAsUser(GET_PERMISSIONS or MATCH_UNINSTALLED_PACKAGES
53                         or MATCH_FACTORY_ONLY, user.identifier)
54         postValue(packageInfos.map { packageInfo -> LightPackageInfo(packageInfo) })
55     }
56 
57     override fun onActive() {
58         super.onActive()
59 
60         // Data never changes, hence no need to reload
61         if (value == null) {
62             updateAsync()
63         }
64     }
65 
66     /**
67      * Repository for PreinstalledUserPackageInfosLiveData.
68      *
69      * <p>Key value is a UserHandle, value is its corresponding LiveData.
70      */
71     companion object : DataRepository<UserHandle, PreinstalledUserPackageInfosLiveData>() {
72         override fun newValue(key: UserHandle): PreinstalledUserPackageInfosLiveData {
73             return PreinstalledUserPackageInfosLiveData(PermissionControllerApplication.get(), key)
74         }
75     }
76 }
77