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 com.android.permissioncontroller.PermissionControllerApplication 23 import kotlinx.coroutines.Job 24 25 /** 26 * A livedata which stores a list of package names of packages which have launcher icons. 27 */ 28 object LauncherPackagesLiveData : SmartAsyncMediatorLiveData<Set<String>>(), 29 PackageBroadcastReceiver.PackageBroadcastListener { 30 31 private val LAUNCHER_INTENT = Intent(Intent.ACTION_MAIN, null) 32 .addCategory(Intent.CATEGORY_LAUNCHER) 33 loadDataAndPostValuenull34 override suspend fun loadDataAndPostValue(job: Job) { 35 val launcherPkgs = mutableSetOf<String>() 36 for (info in PermissionControllerApplication.get().packageManager.queryIntentActivities( 37 LAUNCHER_INTENT, MATCH_DIRECT_BOOT_AWARE or MATCH_DIRECT_BOOT_UNAWARE)) { 38 launcherPkgs.add(info.activityInfo.packageName) 39 } 40 41 postValue(launcherPkgs) 42 } 43 onPackageUpdatenull44 override fun onPackageUpdate(packageName: String) { 45 update() 46 } 47 onActivenull48 override fun onActive() { 49 super.onActive() 50 update() 51 PackageBroadcastReceiver.addAllCallback(this) 52 } 53 onInactivenull54 override fun onInactive() { 55 super.onInactive() 56 PackageBroadcastReceiver.removeAllCallback(this) 57 } 58 }