• 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.content.Intent
20 import android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE
21 import android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE
22 import android.content.pm.PackageManager.FEATURE_LEANBACK
23 import com.android.permissioncontroller.PermissionControllerApplication
24 import kotlinx.coroutines.Job
25 
26 /**
27  * A livedata which stores a list of package names of packages which have launcher icons.
28  */
29 object LauncherPackagesLiveData : SmartAsyncMediatorLiveData<Set<String>>(),
30     PackageBroadcastReceiver.PackageBroadcastListener {
31 
32     private val LAUNCHER_INTENT = Intent(Intent.ACTION_MAIN, null)
33         .addCategory(Intent.CATEGORY_LAUNCHER)
34 
35     // On ATV some apps may have a leanback launcher icon but no regular launcher icon
36     private val LEANBACK_LAUNCHER_INTENT = Intent(Intent.ACTION_MAIN, null)
37         .addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER)
38 
loadDataAndPostValuenull39     override suspend fun loadDataAndPostValue(job: Job) {
40         val launcherPkgs = mutableSetOf<String>()
41 
42         loadPkgsFromIntent(launcherPkgs, LAUNCHER_INTENT)
43         if (PermissionControllerApplication.get().packageManager
44                         .hasSystemFeature(FEATURE_LEANBACK)) {
45             loadPkgsFromIntent(launcherPkgs, LEANBACK_LAUNCHER_INTENT)
46         }
47         postValue(launcherPkgs)
48     }
49 
loadPkgsFromIntentnull50     private fun loadPkgsFromIntent(launcherPkgs: MutableSet<String>, intent: Intent) {
51         for (info in PermissionControllerApplication.get().packageManager.queryIntentActivities(
52             intent, MATCH_DIRECT_BOOT_AWARE or MATCH_DIRECT_BOOT_UNAWARE)) {
53             launcherPkgs.add(info.activityInfo.packageName)
54         }
55     }
56 
onPackageUpdatenull57     override fun onPackageUpdate(packageName: String) {
58         update()
59     }
60 
onActivenull61     override fun onActive() {
62         super.onActive()
63         update()
64         PackageBroadcastReceiver.addAllCallback(this)
65     }
66 
onInactivenull67     override fun onInactive() {
68         super.onInactive()
69         PackageBroadcastReceiver.removeAllCallback(this)
70     }
71 }