xref: /aosp_15_r20/frameworks/base/packages/SystemUI/src/com/android/systemui/scene/shared/flag/SceneContainerFlag.kt (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
<lambda>null2  * Copyright 2023 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 @file:Suppress("NOTHING_TO_INLINE")
18 
19 package com.android.systemui.scene.shared.flag
20 
21 import com.android.systemui.Flags.FLAG_SCENE_CONTAINER
22 import com.android.systemui.Flags.sceneContainer
23 import com.android.systemui.flags.FlagToken
24 import com.android.systemui.flags.RefactorFlagUtils
25 import com.android.systemui.keyguard.KeyguardBottomAreaRefactor
26 import com.android.systemui.keyguard.KeyguardWmStateRefactor
27 import com.android.systemui.keyguard.MigrateClocksToBlueprint
28 import com.android.systemui.statusbar.notification.shared.NotificationThrottleHun
29 import com.android.systemui.statusbar.phone.PredictiveBackSysUiFlag
30 
31 /** Helper for reading or using the scene container flag state. */
32 object SceneContainerFlag {
33     /** The flag description -- not an aconfig flag name */
34     const val DESCRIPTION = "SceneContainerFlag"
35 
36     @JvmStatic
37     inline val isEnabled
38         get() =
39             sceneContainer() && // mainAconfigFlag
40                 KeyguardBottomAreaRefactor.isEnabled &&
41                 KeyguardWmStateRefactor.isEnabled &&
42                 MigrateClocksToBlueprint.isEnabled &&
43                 NotificationThrottleHun.isEnabled &&
44                 PredictiveBackSysUiFlag.isEnabled
45 
46     // NOTE: Changes should also be made in getSecondaryFlags and @EnableSceneContainer
47 
48     /** The main aconfig flag. */
49     inline fun getMainAconfigFlag() = FlagToken(FLAG_SCENE_CONTAINER, sceneContainer())
50 
51     /** The set of secondary flags which must be enabled for scene container to work properly */
52     inline fun getSecondaryFlags(): Sequence<FlagToken> =
53         sequenceOf(
54             KeyguardBottomAreaRefactor.token,
55             KeyguardWmStateRefactor.token,
56             MigrateClocksToBlueprint.token,
57             NotificationThrottleHun.token,
58             PredictiveBackSysUiFlag.token,
59             // NOTE: Changes should also be made in isEnabled and @EnableSceneContainer
60         )
61 
62     /** The full set of requirements for SceneContainer */
63     inline fun getAllRequirements(): Sequence<FlagToken> {
64         return sequenceOf(getMainAconfigFlag()) + getSecondaryFlags()
65     }
66 
67     /** Return all dependencies of this flag in pairs where [Pair.first] depends on [Pair.second] */
68     inline fun getFlagDependencies(): Sequence<Pair<FlagToken, FlagToken>> {
69         val mainAconfigFlag = getMainAconfigFlag()
70         return getSecondaryFlags().map { mainAconfigFlag to it }
71     }
72 
73     /**
74      * Called to ensure code is only run when the flag is enabled. This protects users from the
75      * unintended behaviors caused by accidentally running new logic, while also crashing on an eng
76      * build to ensure that the refactor author catches issues in testing.
77      */
78     @JvmStatic
79     inline fun isUnexpectedlyInLegacyMode() =
80         RefactorFlagUtils.isUnexpectedlyInLegacyMode(isEnabled, DESCRIPTION)
81 
82     /**
83      * Called to ensure code is only run when the flag is disabled. This will throw an exception if
84      * the flag is enabled to ensure that the refactor author catches issues in testing.
85      */
86     @JvmStatic
87     inline fun assertInLegacyMode() = RefactorFlagUtils.assertInLegacyMode(isEnabled, DESCRIPTION)
88 
89     /**
90      * Called to ensure the new code is only run when the flag is enabled. This will throw an
91      * exception if the flag is disabled to ensure that the refactor author catches issues in
92      * testing.
93      */
94     @JvmStatic
95     inline fun assertInNewMode() = RefactorFlagUtils.assertInNewMode(isEnabled, DESCRIPTION)
96 
97     /** Returns a developer-readable string that describes the current requirement list. */
98     @JvmStatic
99     fun requirementDescription(): String {
100         return buildString {
101             getAllRequirements().forEach { requirement ->
102                 append('\n')
103                 append(if (requirement.isEnabled) "    [MET]" else "[NOT MET]")
104                 append(" ${requirement.name}")
105             }
106         }
107     }
108 }
109