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.Intent 21 import android.content.pm.PackageManager 22 import android.os.UserHandle 23 import com.android.permissioncontroller.DumpableLog 24 import com.android.permissioncontroller.PermissionControllerApplication 25 import com.android.permissioncontroller.permission.utils.Utils.getUserContext 26 import kotlinx.coroutines.Job 27 28 /** 29 * A LiveData which tracks services for a certain type 30 * 31 * @param app The current application 32 * @param intentAction The name of interface the service implements 33 * @param permission The permission required for the service 34 * @param user The user the services should be determined for 35 */ 36 class ServiceLiveData( 37 private val app: Application, 38 override val intentAction: String, 39 private val permission: String, 40 private val user: UserHandle 41 ) : SmartAsyncMediatorLiveData<Set<String>>(), 42 PackageBroadcastReceiver.PackageBroadcastListener, 43 HasIntentAction { 44 private val DEBUG = false 45 46 override fun onPackageUpdate(packageName: String) { 47 updateAsync() 48 } 49 50 override suspend fun loadDataAndPostValue(job: Job) { 51 if (job.isCancelled) { 52 return 53 } 54 55 val packageNames = getUserContext(app, user).packageManager 56 .queryIntentServices( 57 Intent(intentAction), 58 PackageManager.GET_SERVICES or PackageManager.GET_META_DATA) 59 .mapNotNull { resolveInfo -> 60 if (resolveInfo?.serviceInfo?.permission != permission) { 61 return@mapNotNull null 62 } 63 resolveInfo?.serviceInfo?.packageName 64 }.toSet() 65 if (DEBUG) { 66 DumpableLog.i(LOG_TAG, 67 "Detected ${intentAction.substringAfterLast(".")}s: $packageNames") 68 } 69 70 postValue(packageNames) 71 } 72 73 override fun onActive() { 74 super.onActive() 75 76 PackageBroadcastReceiver.addAllCallback(this) 77 78 updateAsync() 79 } 80 81 override fun onInactive() { 82 super.onInactive() 83 84 PackageBroadcastReceiver.removeAllCallback(this) 85 } 86 87 /** 88 * Repository for [ServiceLiveData] 89 * 90 * <p> Key value is a (string service name, required permission, user) triple, value is its 91 * corresponding LiveData. 92 */ 93 companion object : DataRepositoryForPackage<Triple<String, String, UserHandle>, 94 ServiceLiveData>() { 95 override fun newValue(key: Triple<String, String, UserHandle>): ServiceLiveData { 96 return ServiceLiveData(PermissionControllerApplication.get(), 97 key.first, key.second, key.third) 98 } 99 } 100 }