• 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.accessibilityservice.AccessibilityServiceInfo
20 import android.app.Application
21 import android.os.UserHandle
22 import android.view.accessibility.AccessibilityManager
23 import com.android.permissioncontroller.PermissionControllerApplication
24 import com.android.permissioncontroller.permission.utils.componentInfo
25 import com.android.permissioncontroller.permission.utils.Utils
26 import kotlinx.coroutines.Job
27 
28 /**
29  * A LiveData which tracks enabled accessibility service packages
30  *
31  * @param app The current application
32  * @param user The user the services should be determined for
33  */
34 // TODO(eugenesusla): think of a way to observe the data
35 class EnabledAccessibilityServicesLiveData(
36     private val app: Application,
37     private val user: UserHandle
38 ) : SmartAsyncMediatorLiveData<List<String>>() {
39 
40     override suspend fun loadDataAndPostValue(job: Job) {
41         if (job.isCancelled) {
42             return
43         }
44 
45         val packageNames = Utils.getUserContext(app, user)
46                 .getSystemService(AccessibilityManager::class.java)!!
47                 .getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK)
48                 .map { info: AccessibilityServiceInfo ->
49                     info.resolveInfo.componentInfo.packageName
50                 }
51 
52         postValue(packageNames)
53     }
54 
55     override fun onActive() {
56         super.onActive()
57         updateAsync()
58     }
59 
60     /**
61      * Repository for [EnabledAccessibilityServicesLiveData]
62      *
63      * <p> Key value is a user, value is its corresponding LiveData.
64      */
65     companion object : DataRepositoryForPackage<UserHandle,
66             EnabledAccessibilityServicesLiveData>() {
67         override fun newValue(key: UserHandle): EnabledAccessibilityServicesLiveData {
68             return EnabledAccessibilityServicesLiveData(PermissionControllerApplication.get(), key)
69         }
70     }
71 }