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.annotation.BoolRes 20 21 object FlagsFactory { 22 private val flagMap = mutableMapOf<String, Flag<*>>() 23 24 val knownFlags: Map<String, Flag<*>> 25 get() { 26 // We need to access Flags in order to initialize our map. 27 assert(flagMap.contains(Flags.TEAMFOOD.name)) { "Where is teamfood?" } 28 return flagMap 29 } 30 31 fun unreleasedFlag( 32 name: String, 33 namespace: String = "systemui", 34 teamfood: Boolean = false 35 ): UnreleasedFlag { 36 val flag = UnreleasedFlag(name = name, namespace = namespace, teamfood = teamfood) 37 checkForDupesAndAdd(flag) 38 return flag 39 } 40 41 fun releasedFlag( 42 name: String, 43 namespace: String = "systemui", 44 ): ReleasedFlag { 45 val flag = ReleasedFlag(name = name, namespace = namespace, teamfood = false) 46 checkForDupesAndAdd(flag) 47 return flag 48 } 49 50 fun resourceBooleanFlag( 51 @BoolRes resourceId: Int, 52 name: String, 53 namespace: String = "systemui", 54 ): ResourceBooleanFlag { 55 val flag = 56 ResourceBooleanFlag( 57 name = name, 58 namespace = namespace, 59 resourceId = resourceId, 60 teamfood = false, 61 ) 62 checkForDupesAndAdd(flag) 63 return flag 64 } 65 66 fun sysPropBooleanFlag( 67 name: String, 68 namespace: String = "systemui", 69 default: Boolean = false 70 ): SysPropBooleanFlag { 71 val flag = SysPropBooleanFlag(name = name, namespace = "systemui", default = default) 72 checkForDupesAndAdd(flag) 73 return flag 74 } 75 76 private fun checkForDupesAndAdd(flag: Flag<*>) { 77 if (flagMap.containsKey(flag.name)) { 78 throw IllegalArgumentException("Name {$flag.name} is already registered") 79 } 80 flagMap[flag.name] = flag 81 } 82 } 83