• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.wm.shell.desktopmode.common
18 
19 import android.content.BroadcastReceiver
20 import android.content.Context
21 import android.content.Intent
22 import android.content.IntentFilter
23 import android.content.pm.PackageManager
24 import android.os.Handler
25 import com.android.wm.shell.shared.annotations.ShellMainThread
26 import com.android.wm.shell.sysui.ShellInit
27 import java.util.function.Supplier
28 
29 /**
30  * This supplies the package name of default home in an efficient way. The query to package manager
31  * only executes on initialization and when the preferred activity (e.g. default home) is changed.
32  * Note that this returns null package name if the default home is setup wizard.
33  */
34 class DefaultHomePackageSupplier(
35     private val context: Context,
36     shellInit: ShellInit,
37     @ShellMainThread private val mainHandler: Handler,
38 ) : BroadcastReceiver(), Supplier<String?> {
39 
40     private var defaultHomePackage: String? = null
41     private var isSetupWizard: Boolean = false
42 
43     init {
<lambda>null44         shellInit.addInitCallback({ onInit() }, this)
45     }
46 
onInitnull47     private fun onInit() {
48         context.registerReceiver(
49             this,
50             IntentFilter(Intent.ACTION_PREFERRED_ACTIVITY_CHANGED),
51             null /* broadcastPermission */,
52             mainHandler,
53         )
54     }
55 
updateDefaultHomePackagenull56     private fun updateDefaultHomePackage(): String? {
57         defaultHomePackage = context.packageManager.getHomeActivities(ArrayList())?.packageName
58         isSetupWizard =
59             defaultHomePackage != null &&
60                 context.packageManager.resolveActivity(
61                     Intent()
62                         .setPackage(defaultHomePackage)
63                         .addCategory(Intent.CATEGORY_SETUP_WIZARD),
64                     PackageManager.MATCH_SYSTEM_ONLY,
65                 ) != null
66         return defaultHomePackage
67     }
68 
onReceivenull69     override fun onReceive(contxt: Context?, intent: Intent?) {
70         updateDefaultHomePackage()
71     }
72 
getnull73     override fun get(): String? {
74         if (isSetupWizard) return null
75         return defaultHomePackage ?: updateDefaultHomePackage()
76     }
77 }
78