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.server.display.brightness; 18 19 import android.util.Slog; 20 21 import java.util.Objects; 22 23 /** 24 * Stores data about why the brightness was changed. Made up of one main 25 * {@code BrightnessReason.REASON_*} reason and various {@code BrightnessReason.MODIFIER_*} 26 * modifiers. 27 */ 28 public final class BrightnessReason { 29 private static final String TAG = "BrightnessReason"; 30 31 public static final int REASON_UNKNOWN = 0; 32 public static final int REASON_MANUAL = 1; 33 public static final int REASON_DOZE = 2; 34 public static final int REASON_DOZE_DEFAULT = 3; 35 public static final int REASON_AUTOMATIC = 4; 36 public static final int REASON_SCREEN_OFF = 5; 37 public static final int REASON_OVERRIDE = 6; 38 public static final int REASON_TEMPORARY = 7; 39 public static final int REASON_BOOST = 8; 40 public static final int REASON_SCREEN_OFF_BRIGHTNESS_SENSOR = 9; 41 public static final int REASON_FOLLOWER = 10; 42 public static final int REASON_OFFLOAD = 11; 43 public static final int REASON_DOZE_MANUAL = 12; 44 public static final int REASON_MAX = REASON_DOZE_MANUAL; 45 46 public static final int MODIFIER_DIMMED = 0x1; 47 public static final int MODIFIER_LOW_POWER = 0x2; 48 public static final int MODIFIER_HDR = 0x4; 49 public static final int MODIFIER_THROTTLED = 0x8; 50 public static final int MODIFIER_MIN_LUX = 0x10; 51 public static final int MODIFIER_MIN_USER_SET_LOWER_BOUND = 0x20; 52 public static final int MODIFIER_MASK = MODIFIER_DIMMED | MODIFIER_LOW_POWER | MODIFIER_HDR 53 | MODIFIER_THROTTLED | MODIFIER_MIN_LUX | MODIFIER_MIN_USER_SET_LOWER_BOUND; 54 55 // ADJUSTMENT_* 56 // These things can happen at any point, even if the main brightness reason doesn't 57 // fundamentally change, so they're not stored. 58 59 // Auto-brightness adjustment factor changed 60 public static final int ADJUSTMENT_AUTO_TEMP = 0x1; 61 // Temporary adjustment to the auto-brightness adjustment factor. 62 public static final int ADJUSTMENT_AUTO = 0x2; 63 64 // One of REASON_* 65 private int mReason; 66 // Any number of MODIFIER_* 67 private int mModifier; 68 69 /** 70 * A utility to clone a BrightnessReason from another BrightnessReason event 71 * 72 * @param other The BrightnessReason object which is to be cloned 73 */ set(BrightnessReason other)74 public void set(BrightnessReason other) { 75 setReason(other == null ? REASON_UNKNOWN : other.mReason); 76 setModifier(other == null ? 0 : other.mModifier); 77 } 78 79 /** 80 * A utility to add a modifier to the BrightnessReason object 81 * 82 * @param modifier The modifier which is to be added 83 */ addModifier(int modifier)84 public void addModifier(int modifier) { 85 setModifier(modifier | this.mModifier); 86 } 87 88 89 @Override equals(Object obj)90 public boolean equals(Object obj) { 91 if (!(obj instanceof BrightnessReason)) { 92 return false; 93 } 94 BrightnessReason other = (BrightnessReason) obj; 95 return other.mReason == mReason && other.mModifier == mModifier; 96 } 97 98 @Override hashCode()99 public int hashCode() { 100 return Objects.hash(mReason, mModifier); 101 } 102 103 @Override toString()104 public String toString() { 105 return toString(0); 106 } 107 108 /** 109 * A utility to stringify a BrightnessReason 110 * 111 * @param adjustments Indicates if the adjustments field is to be added in the stringify version 112 * of the BrightnessReason 113 * @return A stringified BrightnessReason 114 */ toString(int adjustments)115 public String toString(int adjustments) { 116 final StringBuilder sb = new StringBuilder(); 117 sb.append(reasonToString(mReason)); 118 sb.append(" ["); 119 if ((adjustments & ADJUSTMENT_AUTO_TEMP) != 0) { 120 sb.append(" temp_adj"); 121 } 122 if ((adjustments & ADJUSTMENT_AUTO) != 0) { 123 sb.append(" auto_adj"); 124 } 125 if ((mModifier & MODIFIER_LOW_POWER) != 0) { 126 sb.append(" low_pwr"); 127 } 128 if ((mModifier & MODIFIER_DIMMED) != 0) { 129 sb.append(" dim"); 130 } 131 if ((mModifier & MODIFIER_HDR) != 0) { 132 sb.append(" hdr"); 133 } 134 if ((mModifier & MODIFIER_THROTTLED) != 0) { 135 sb.append(" throttled"); 136 } 137 if ((mModifier & MODIFIER_MIN_LUX) != 0) { 138 sb.append(" lux_lower_bound"); 139 } 140 if ((mModifier & MODIFIER_MIN_USER_SET_LOWER_BOUND) != 0) { 141 sb.append(" user_min_pref"); 142 } 143 int strlen = sb.length(); 144 if (sb.charAt(strlen - 1) == '[') { 145 sb.setLength(strlen - 2); 146 } else { 147 sb.append(" ]"); 148 } 149 return sb.toString(); 150 } 151 152 /** 153 * A utility to set the reason of the BrightnessReason object 154 * 155 * @param reason The value to which the reason is to be updated. 156 */ setReason(int reason)157 public void setReason(int reason) { 158 if (reason < REASON_UNKNOWN || reason > REASON_MAX) { 159 Slog.w(TAG, "brightness reason out of bounds: " + reason); 160 } else { 161 this.mReason = reason; 162 } 163 } 164 getReason()165 public int getReason() { 166 return mReason; 167 } 168 getModifier()169 public int getModifier() { 170 return mModifier; 171 } 172 173 /** 174 * A utility to set the modified of the current BrightnessReason object 175 * 176 * @param modifier The value to which the modifier is to be updated 177 */ setModifier(int modifier)178 public void setModifier(int modifier) { 179 if ((modifier & ~MODIFIER_MASK) != 0) { 180 Slog.w(TAG, "brightness modifier out of bounds: 0x" 181 + Integer.toHexString(modifier)); 182 } else { 183 this.mModifier = modifier; 184 } 185 } 186 reasonToString(int reason)187 private String reasonToString(int reason) { 188 switch (reason) { 189 case REASON_MANUAL: 190 return "manual"; 191 case REASON_DOZE: 192 return "doze"; 193 case REASON_DOZE_DEFAULT: 194 return "doze_default"; 195 case REASON_AUTOMATIC: 196 return "automatic"; 197 case REASON_SCREEN_OFF: 198 return "screen_off"; 199 case REASON_OVERRIDE: 200 return "override"; 201 case REASON_TEMPORARY: 202 return "temporary"; 203 case REASON_BOOST: 204 return "boost"; 205 case REASON_SCREEN_OFF_BRIGHTNESS_SENSOR: 206 return "screen_off_brightness_sensor"; 207 case REASON_FOLLOWER: 208 return "follower"; 209 case REASON_OFFLOAD: 210 return "offload"; 211 case REASON_DOZE_MANUAL: 212 return "doze_manual"; 213 default: 214 return Integer.toString(reason); 215 } 216 } 217 } 218