1 /** 2 * Copyright (C) 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 package com.android.internal.config.sysui; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.os.Build; 22 import android.os.SystemProperties; 23 import android.util.Log; 24 25 import com.android.internal.annotations.VisibleForTesting; 26 27 /** 28 * Provides a central definition of debug SystemUI's SystemProperties flags, and their defaults. 29 * 30 * The main feature of this class is that it encodes a system-wide default for each flag which can 31 * be updated by engineers with a single-line CL. 32 * 33 * NOTE: Because flag values returned by this class are not cached, it is important that developers 34 * understand the intricacies of changing values and how that applies to their own code. 35 * Generally, the best practice is to set the property, and then restart the device so that any 36 * processes with stale state can be updated. However, if your code has no state derived from the 37 * flag value and queries it any time behavior is relevant, then it may be safe to change the flag 38 * and not immediately reboot. 39 * 40 * To enable flags in debuggable builds, use the following commands: 41 * 42 * $ adb shell setprop persist.sysui.whatever_the_flag true 43 * $ adb reboot 44 * 45 * @hide 46 */ 47 public class SystemUiSystemPropertiesFlags { 48 49 /** The teamfood flag allows multiple features to be opted into at once. */ 50 public static final Flag TEAMFOOD = devFlag("persist.sysui.teamfood"); 51 52 /** 53 * Flags related to notification features 54 */ 55 public static final class NotificationFlags { 56 57 /** 58 * FOR DEVELOPMENT / TESTING ONLY!!! 59 * Forcibly demote *ALL* FSI notifications as if no apps have the app op permission. 60 * NOTE: enabling this implies SHOW_STICKY_HUN_FOR_DENIED_FSI in SystemUI 61 */ 62 public static final Flag FSI_FORCE_DEMOTE = 63 devFlag("persist.sysui.notification.fsi_force_demote"); 64 65 /** Gating the feature which shows FSI-denied notifications as Sticky HUNs */ 66 public static final Flag SHOW_STICKY_HUN_FOR_DENIED_FSI = 67 devFlag("persist.sysui.notification.show_sticky_hun_for_denied_fsi"); 68 69 /** Gating the ability for users to dismiss ongoing event notifications */ 70 public static final Flag ALLOW_DISMISS_ONGOING = 71 devFlag("persist.sysui.notification.ongoing_dismissal"); 72 73 /** Gating the redaction of OTP notifications on the lockscreen */ 74 public static final Flag OTP_REDACTION = 75 devFlag("persist.sysui.notification.otp_redaction"); 76 77 } 78 79 //// == End of flags. Everything below this line is the implementation. == //// 80 81 /** The interface used for resolving SystemUI SystemProperties Flags to booleans. */ 82 public interface FlagResolver { 83 /** Is the flag enabled? */ isEnabled(Flag flag)84 boolean isEnabled(Flag flag); 85 } 86 87 /** The primary, immutable resolver returned by getResolver() */ 88 private static final FlagResolver 89 MAIN_RESOLVER = 90 Build.IS_DEBUGGABLE ? new DebugResolver() : new ProdResolver(); 91 92 /** 93 * On debuggable builds, this can be set to override the resolver returned by getResolver(). 94 * This can be useful to override flags when testing components that do not allow injecting the 95 * SystemUiPropertiesFlags resolver they use. 96 * Always set this to null when tests tear down. 97 */ 98 @VisibleForTesting 99 public static FlagResolver TEST_RESOLVER = null; 100 101 /** Get the resolver for this device configuration. */ getResolver()102 public static FlagResolver getResolver() { 103 if (Build.IS_DEBUGGABLE && TEST_RESOLVER != null) { 104 Log.i("SystemUiSystemPropertiesFlags", "Returning debug resolver " + TEST_RESOLVER); 105 return TEST_RESOLVER; 106 } 107 return MAIN_RESOLVER; 108 } 109 110 /** 111 * Creates a flag that is enabled by default in debuggable builds. 112 * It can be enabled by setting this flag's SystemProperty to 1. 113 * 114 * This flag is ALWAYS disabled in release builds. 115 */ 116 @VisibleForTesting devFlag(String name)117 public static Flag devFlag(String name) { 118 return new Flag(name, false, null); 119 } 120 121 /** 122 * Creates a flag that is disabled by default in debuggable builds. 123 * It can be enabled or force-disabled by setting this flag's SystemProperty to 1 or 0. 124 * If this flag's SystemProperty is not set, the flag can be enabled by setting the 125 * TEAMFOOD flag's SystemProperty to 1. 126 * 127 * This flag is ALWAYS disabled in release builds. 128 */ 129 @VisibleForTesting teamfoodFlag(String name)130 public static Flag teamfoodFlag(String name) { 131 return new Flag(name, false, TEAMFOOD); 132 } 133 134 /** 135 * Creates a flag that is enabled by default in debuggable builds. 136 * It can be enabled by setting this flag's SystemProperty to 0. 137 * 138 * This flag is ALWAYS enabled in release builds. 139 */ 140 @VisibleForTesting releasedFlag(String name)141 public static Flag releasedFlag(String name) { 142 return new Flag(name, true, null); 143 } 144 145 /** Represents a developer-switchable gate for a feature. */ 146 public static final class Flag { 147 public final String mSysPropKey; 148 public final boolean mDefaultValue; 149 @Nullable 150 public final Flag mDebugDefault; 151 152 /** constructs a new flag. only visible for testing the class */ 153 @VisibleForTesting Flag(@onNull String sysPropKey, boolean defaultValue, @Nullable Flag debugDefault)154 public Flag(@NonNull String sysPropKey, boolean defaultValue, @Nullable Flag debugDefault) { 155 mSysPropKey = sysPropKey; 156 mDefaultValue = defaultValue; 157 mDebugDefault = debugDefault; 158 } 159 } 160 161 /** Implementation of the interface used in release builds. */ 162 @VisibleForTesting 163 public static final class ProdResolver implements 164 FlagResolver { 165 @Override isEnabled(Flag flag)166 public boolean isEnabled(Flag flag) { 167 return flag.mDefaultValue; 168 } 169 } 170 171 /** Implementation of the interface used in debuggable builds. */ 172 @VisibleForTesting 173 public static class DebugResolver implements FlagResolver { 174 @Override isEnabled(Flag flag)175 public final boolean isEnabled(Flag flag) { 176 if (flag.mDebugDefault == null) { 177 return getBoolean(flag.mSysPropKey, flag.mDefaultValue); 178 } 179 return getBoolean(flag.mSysPropKey, isEnabled(flag.mDebugDefault)); 180 } 181 182 /** Look up the value; overridable for tests to avoid needing to set SystemProperties */ 183 @VisibleForTesting getBoolean(String key, boolean defaultValue)184 public boolean getBoolean(String key, boolean defaultValue) { 185 return SystemProperties.getBoolean(key, defaultValue); 186 } 187 } 188 } 189