1 /*
2  * 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.statusbar.notification.shared
18 
19 import android.os.SystemProperties
20 import com.android.systemui.Flags
21 import com.android.systemui.flags.FlagToken
22 import com.android.systemui.flags.RefactorFlagUtils
23 
24 /** Helper for reading or using the minimalism prototype flag state. */
25 @Suppress("NOTHING_TO_INLINE")
26 object NotificationMinimalismPrototype {
27 
<lambda>null28     val version: Int by lazy {
29         SystemProperties.getInt("persist.notification_minimalism_prototype.version", 2)
30     }
31 
32     object V1 {
33         /** The aconfig flag name */
34         const val FLAG_NAME = Flags.FLAG_NOTIFICATION_MINIMALISM_PROTOTYPE
35 
36         /** A token used for dependency declaration */
37         val token: FlagToken
38             get() = FlagToken(FLAG_NAME, isEnabled)
39 
40         /** Is the heads-up cycling animation enabled */
41         @JvmStatic
42         inline val isEnabled
43             get() = Flags.notificationMinimalismPrototype() && version == 1
44 
45         /**
46          * the prototype will now show seen notifications on the locked shade by default, but this
47          * property read allows that to be quickly disabled for testing
48          */
49         val showOnLockedShade: Boolean
50             get() =
51                 if (isUnexpectedlyInLegacyMode()) false
52                 else
53                     SystemProperties.getBoolean(
54                         "persist.notification_minimalism_prototype.show_on_locked_shade",
55                         true
56                     )
57 
58         /** gets the configurable max number of notifications */
59         val maxNotifs: Int
60             get() =
61                 if (isUnexpectedlyInLegacyMode()) -1
62                 else
63                     SystemProperties.getInt(
64                         "persist.notification_minimalism_prototype.lock_screen_max_notifs",
65                         1
66                     )
67 
68         /**
69          * Called to ensure code is only run when the flag is enabled. This protects users from the
70          * unintended behaviors caused by accidentally running new logic, while also crashing on an
71          * eng build to ensure that the refactor author catches issues in testing.
72          */
73         @JvmStatic
isUnexpectedlyInLegacyModenull74         inline fun isUnexpectedlyInLegacyMode() =
75             RefactorFlagUtils.isUnexpectedlyInLegacyMode(isEnabled, FLAG_NAME)
76 
77         /**
78          * Called to ensure code is only run when the flag is disabled. This will throw an exception
79          * if the flag is enabled to ensure that the refactor author catches issues in testing.
80          */
81         @JvmStatic
82         inline fun assertInLegacyMode() = RefactorFlagUtils.assertInLegacyMode(isEnabled, FLAG_NAME)
83     }
84     object V2 {
85         const val FLAG_NAME = Flags.FLAG_NOTIFICATION_MINIMALISM_PROTOTYPE
86 
87         /** A token used for dependency declaration */
88         val token: FlagToken
89             get() = FlagToken(FLAG_NAME, isEnabled)
90 
91         /** Is the heads-up cycling animation enabled */
92         @JvmStatic
93         inline val isEnabled
94             get() = Flags.notificationMinimalismPrototype() && version == 2
95 
96         /**
97          * The prototype will (by default) use a promoter to ensure that the top unseen notification
98          * is not grouped, but this property read allows that behavior to be disabled.
99          */
100         val ungroupTopUnseen: Boolean
101             get() =
102                 if (isUnexpectedlyInLegacyMode()) false
103                 else
104                     SystemProperties.getBoolean(
105                         "persist.notification_minimalism_prototype.ungroup_top_unseen",
106                         true
107                     )
108 
109         /**
110          * Called to ensure code is only run when the flag is enabled. This protects users from the
111          * unintended behaviors caused by accidentally running new logic, while also crashing on an
112          * eng build to ensure that the refactor author catches issues in testing.
113          */
114         @JvmStatic
115         inline fun isUnexpectedlyInLegacyMode() =
116             RefactorFlagUtils.isUnexpectedlyInLegacyMode(isEnabled, FLAG_NAME)
117 
118         /**
119          * Called to ensure code is only run when the flag is disabled. This will throw an exception
120          * if the flag is enabled to ensure that the refactor author catches issues in testing.
121          */
122         @JvmStatic
123         inline fun assertInLegacyMode() = RefactorFlagUtils.assertInLegacyMode(isEnabled, FLAG_NAME)
124     }
125 }
126