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