1 /*
2  * Copyright (C) 2022 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.flags
18 
19 import android.content.Intent
20 import com.android.systemui.CoreStartable
21 import com.android.systemui.broadcast.BroadcastSender
22 import com.android.systemui.dump.DumpManager
23 import com.android.systemui.statusbar.commandline.CommandRegistry
24 import com.android.systemui.util.InitializationChecker
25 import dagger.Binds
26 import dagger.Module
27 import dagger.multibindings.ClassKey
28 import dagger.multibindings.IntoMap
29 import javax.inject.Inject
30 
31 class FeatureFlagsDebugStartable
32 @Inject
33 constructor(
34     dumpManager: DumpManager,
35     private val commandRegistry: CommandRegistry,
36     private val flagCommand: FlagCommand,
37     private val featureFlags: FeatureFlagsDebug,
38     private val broadcastSender: BroadcastSender,
39     private val initializationChecker: InitializationChecker,
40 ) : CoreStartable {
41 
42     init {
43         dumpManager.registerCriticalDumpable(FeatureFlagsDebug.TAG) { pw, args ->
44             featureFlags.dump(pw, args)
45         }
46     }
47 
48     override fun start() {
49         featureFlags.init()
50         commandRegistry.registerCommand(FlagCommand.FLAG_COMMAND) { flagCommand }
51         if (initializationChecker.initializeComponents()) {
52             // protected broadcast should only be sent for the main process
53             val intent = Intent(FlagManager.ACTION_SYSUI_STARTED)
54             broadcastSender.sendBroadcast(intent)
55         }
56     }
57 }
58 
59 @Module
60 abstract class FeatureFlagsDebugStartableModule {
61     @Binds
62     @IntoMap
63     @ClassKey(FeatureFlagsDebugStartable::class)
64     abstract fun bind(impl: FeatureFlagsDebugStartable): CoreStartable
65 }
66