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.AppOpsManager
20 import android.app.Application
21 import com.android.permissioncontroller.PermissionControllerApplication
22 
23 /**
24  * A LiveData which represents the appop state
25  *
26  * @param app The current application
27  * @param packageName The name of the package
28  * @param op The name of the appop
29  * @param uid The uid of the package
30  * @see AppOpsManager
31  */
32 // TODO eugenesusla: observe appops
33 // TODO eugenesusla: use for external storage
34 class AppOpLiveData
35 private constructor(
36     private val app: Application,
37     private val packageName: String,
38     private val op: String,
39     private val uid: Int
40 ) : SmartUpdateMediatorLiveData<Int>() {
41 
42     private val appOpsManager = app.getSystemService(AppOpsManager::class.java)!!
43 
onUpdatenull44     override fun onUpdate() {
45         value = appOpsManager.unsafeCheckOpNoThrow(op, uid, packageName)
46     }
47 
onActivenull48     override fun onActive() {
49         super.onActive()
50         update()
51     }
52 
53     /**
54      * Repository for AppOpLiveData.
55      *
56      * <p> Key value is a triple of string package name, string appop, and package uid, value is its
57      * corresponding LiveData.
58      */
59     companion object : DataRepository<Triple<String, String, Int>, AppOpLiveData>() {
newValuenull60         override fun newValue(key: Triple<String, String, Int>): AppOpLiveData {
61             return AppOpLiveData(
62                 PermissionControllerApplication.get(),
63                 key.first,
64                 key.second,
65                 key.third
66             )
67         }
68     }
69 }
70