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