• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2024 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.systemui.dreams.homecontrols.system
18 
19 import android.content.ComponentName
20 import android.content.Context
21 import android.content.pm.PackageManager
22 import android.service.controls.flags.Flags.homePanelDream
23 import com.android.app.tracing.coroutines.launchTraced as launch
24 import com.android.systemui.CoreStartable
25 import com.android.systemui.Flags.homeControlsDreamHsum
26 import com.android.systemui.dagger.qualifiers.Background
27 import com.android.systemui.dreams.homecontrols.HomeControlsDreamService
28 import com.android.systemui.dreams.homecontrols.system.domain.interactor.HomeControlsComponentInteractor
29 import com.android.systemui.settings.UserContextProvider
30 import javax.inject.Inject
31 import kotlinx.coroutines.CoroutineScope
32 
33 class HomeControlsDreamStartable
34 @Inject
35 constructor(
36     context: Context,
37     private val systemPackageManager: PackageManager,
38     private val userContextProvider: UserContextProvider,
39     private val homeControlsComponentInteractor: HomeControlsComponentInteractor,
40     @Background private val bgScope: CoroutineScope,
41 ) : CoreStartable {
42 
43     private val componentName = ComponentName(context, HomeControlsDreamService::class.java)
44 
45     override fun start() {
46         bgScope.launch {
47             if (homePanelDream()) {
48                 homeControlsComponentInteractor.panelComponent.collect { selectedPanelComponent ->
49                     setEnableHomeControlPanel(selectedPanelComponent != null)
50                 }
51             } else {
52                 setEnableHomeControlPanel(false)
53             }
54         }
55     }
56 
57     private fun setEnableHomeControlPanel(enabled: Boolean) {
58         val packageState =
59             if (enabled) {
60                 PackageManager.COMPONENT_ENABLED_STATE_ENABLED
61             } else {
62                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED
63             }
64         val packageManager =
65             if (homeControlsDreamHsum()) {
66                 userContextProvider.userContext.packageManager
67             } else {
68                 systemPackageManager
69             }
70         packageManager.setComponentEnabledSetting(
71             componentName,
72             packageState,
73             PackageManager.DONT_KILL_APP,
74         )
75     }
76 }
77