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.util.Log 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 loadDataAndPostValuenull35 override suspend fun loadDataAndPostValue(job: Job) { 36 // TODO ntmyren: remove once b/154796729 is fixed 37 Log.i("LancherPackagesLiveData", "updating LauncherPackageLiveData") 38 val launcherPkgs = mutableSetOf<String>() 39 for (info in PermissionControllerApplication.get().packageManager.queryIntentActivities( 40 LAUNCHER_INTENT, MATCH_DIRECT_BOOT_AWARE or MATCH_DIRECT_BOOT_UNAWARE)) { 41 launcherPkgs.add(info.activityInfo.packageName) 42 } 43 44 postValue(launcherPkgs) 45 } 46 onPackageUpdatenull47 override fun onPackageUpdate(packageName: String) { 48 updateIfActive() 49 } 50 onActivenull51 override fun onActive() { 52 super.onActive() 53 updateIfActive() 54 PackageBroadcastReceiver.addAllCallback(this) 55 } 56 onInactivenull57 override fun onInactive() { 58 super.onInactive() 59 PackageBroadcastReceiver.removeAllCallback(this) 60 } 61 }