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