1 /*
2  * Copyright (C) 2021 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.content.res.Resources;
20 import android.util.SparseBooleanArray;
21 
22 import androidx.annotation.BoolRes;
23 import androidx.annotation.NonNull;
24 
25 import com.android.systemui.Dumpable;
26 import com.android.systemui.dagger.SysUISingleton;
27 import com.android.systemui.dagger.qualifiers.Main;
28 import com.android.systemui.dump.DumpManager;
29 
30 import java.io.FileDescriptor;
31 import java.io.PrintWriter;
32 
33 import javax.inject.Inject;
34 
35 /**
36  * Default implementation of the a Flag manager that returns default values for release builds
37  *
38  * There's a version of this file in src-debug which allows overriding, and has documentation about
39  * how to set flags.
40  */
41 @SysUISingleton
42 public class FeatureFlagManager implements FlagReader, FlagWriter, Dumpable {
43     SparseBooleanArray mAccessedFlags = new SparseBooleanArray();
44     private Resources mResources;
45 
46     @Inject
FeatureFlagManager(DumpManager dumpManager, @Main Resources resources)47     public FeatureFlagManager(DumpManager dumpManager, @Main Resources resources) {
48         mResources = resources;
49         dumpManager.registerDumpable("SysUIFlags", this);
50     }
51 
52     @Override
addListener(Listener run)53     public void addListener(Listener run) {}
54 
55     @Override
removeListener(Listener run)56     public void removeListener(Listener run) {}
57 
58     @Override
isEnabled(BooleanFlag flag)59     public boolean isEnabled(BooleanFlag flag) {
60         boolean def = flag.getDefault();
61         if (flag.hasResourceOverride()) {
62             try {
63                 def = isEnabledInOverlay(flag.getResourceOverride());
64             } catch (Resources.NotFoundException e) {
65                 // no-op
66             }
67         }
68 
69         return isEnabled(flag.getId(), def);
70     }
71 
isEnabledInOverlay(@oolRes int resId)72     private boolean isEnabledInOverlay(@BoolRes int resId) {
73         return mResources.getBoolean(resId);
74     }
75 
76     @Override
isEnabled(int key, boolean defaultValue)77     public boolean isEnabled(int key, boolean defaultValue) {
78         mAccessedFlags.append(key, defaultValue);
79         return defaultValue;
80     }
81     @Override
setEnabled(int key, boolean value)82     public void setEnabled(int key, boolean value) {}
83 
84     @Override
dump(@onNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args)85     public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
86         pw.println("can override: false");
87         int size = mAccessedFlags.size();
88         for (int i = 0; i < size; i++) {
89             pw.println("  sysui_flag_" + mAccessedFlags.keyAt(i)
90                     + ": " + mAccessedFlags.valueAt(i));
91         }
92     }
93 }
94