• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *
31  * @see AppOpsManager
32  */
33 // TODO eugenesusla: observe appops
34 // TODO eugenesusla: use for external storage
35 class AppOpLiveData 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      * <p> Key value is a triple of string package name, string appop, and
56      * package uid, value is its corresponding LiveData.
57      */
58     companion object : DataRepository<Triple<String, String, Int>, AppOpLiveData>() {
newValuenull59         override fun newValue(key: Triple<String, String, Int>): AppOpLiveData {
60             return AppOpLiveData(PermissionControllerApplication.get(),
61                 key.first, key.second, key.third)
62         }
63     }
64 }