• 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 @file:Suppress("DEPRECATION")
17 
18 package com.android.permissioncontroller.permission.data
19 
20 import android.content.Intent
21 import android.content.pm.PackageManager.FEATURE_LEANBACK
22 import android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE
23 import android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE
24 import com.android.permissioncontroller.PermissionControllerApplication
25 import kotlinx.coroutines.Job
26 
27 /** A livedata which stores a list of package names of packages which have launcher icons. */
28 object LauncherPackagesLiveData :
29     SmartAsyncMediatorLiveData<Set<String>>(), PackageBroadcastReceiver.PackageBroadcastListener {
30 
31     private val LAUNCHER_INTENT =
32         Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER)
33 
34     // On ATV some apps may have a leanback launcher icon but no regular launcher icon
35     private val LEANBACK_LAUNCHER_INTENT =
36         Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER)
37 
loadDataAndPostValuenull38     override suspend fun loadDataAndPostValue(job: Job) {
39         val launcherPkgs = mutableSetOf<String>()
40 
41         loadPkgsFromIntent(launcherPkgs, LAUNCHER_INTENT)
42         if (
43             PermissionControllerApplication.get().packageManager.hasSystemFeature(FEATURE_LEANBACK)
44         ) {
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
52             PermissionControllerApplication.get()
53                 .packageManager
54                 .queryIntentActivities(
55                     intent,
56                     MATCH_DIRECT_BOOT_AWARE or MATCH_DIRECT_BOOT_UNAWARE
57                 )) {
58             launcherPkgs.add(info.activityInfo.packageName)
59         }
60     }
61 
onPackageUpdatenull62     override fun onPackageUpdate(packageName: String) {
63         update()
64     }
65 
onActivenull66     override fun onActive() {
67         super.onActive()
68         update()
69         PackageBroadcastReceiver.addAllCallback(this)
70     }
71 
onInactivenull72     override fun onInactive() {
73         super.onInactive()
74         PackageBroadcastReceiver.removeAllCallback(this)
75     }
76 }
77