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.server.power.feature; 18 19 import android.text.TextUtils; 20 import android.util.Slog; 21 22 import com.android.server.power.feature.flags.Flags; 23 24 import java.io.PrintWriter; 25 import java.util.function.Supplier; 26 27 /** 28 * Utility class to read the flags used in the power manager server. 29 */ 30 public class PowerManagerFlags { 31 private static final boolean DEBUG = false; 32 private static final String TAG = "PowerManagerFlags"; 33 34 private final FlagState mEarlyScreenTimeoutDetectorFlagState = new FlagState( 35 Flags.FLAG_ENABLE_EARLY_SCREEN_TIMEOUT_DETECTOR, 36 Flags::enableEarlyScreenTimeoutDetector); 37 38 private final FlagState mImproveWakelockLatency = new FlagState( 39 Flags.FLAG_IMPROVE_WAKELOCK_LATENCY, 40 Flags::improveWakelockLatency 41 ); 42 43 /** Returns whether early-screen-timeout-detector is enabled on not. */ isEarlyScreenTimeoutDetectorEnabled()44 public boolean isEarlyScreenTimeoutDetectorEnabled() { 45 return mEarlyScreenTimeoutDetectorFlagState.isEnabled(); 46 } 47 48 /** 49 * @return Whether to improve the wakelock acquire/release latency or not 50 */ improveWakelockLatency()51 public boolean improveWakelockLatency() { 52 return mImproveWakelockLatency.isEnabled(); 53 } 54 55 /** 56 * dumps all flagstates 57 * @param pw printWriter 58 */ dump(PrintWriter pw)59 public void dump(PrintWriter pw) { 60 pw.println("PowerManagerFlags:"); 61 pw.println(" " + mEarlyScreenTimeoutDetectorFlagState); 62 pw.println(" " + mImproveWakelockLatency); 63 } 64 65 private static class FlagState { 66 67 private final String mName; 68 69 private final Supplier<Boolean> mFlagFunction; 70 private boolean mEnabledSet; 71 private boolean mEnabled; 72 FlagState(String name, Supplier<Boolean> flagFunction)73 private FlagState(String name, Supplier<Boolean> flagFunction) { 74 mName = name; 75 mFlagFunction = flagFunction; 76 } 77 isEnabled()78 private boolean isEnabled() { 79 if (mEnabledSet) { 80 if (DEBUG) { 81 Slog.d(TAG, mName + ": mEnabled. Recall = " + mEnabled); 82 } 83 return mEnabled; 84 } 85 mEnabled = mFlagFunction.get(); 86 if (DEBUG) { 87 Slog.d(TAG, mName + ": mEnabled. Flag value = " + mEnabled); 88 } 89 mEnabledSet = true; 90 return mEnabled; 91 } 92 93 @Override toString()94 public String toString() { 95 // remove com.android.server.power.feature.flags. from the beginning of the name. 96 // align all isEnabled() values. 97 // Adjust lengths if we end up with longer names 98 final int nameLength = mName.length(); 99 return TextUtils.substring(mName, 39, nameLength) + ": " 100 + TextUtils.formatSimple("%" + (91 - nameLength) + "s%s", " " , isEnabled()) 101 + " (def:" + mFlagFunction.get() + ")"; 102 } 103 } 104 } 105