1 /** 2 * Copyright (C) 2020 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 android.hardware.lights; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.TestApi; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 29 /** 30 * Represents a logical light on the device. 31 * 32 */ 33 public final class Light implements Parcelable { 34 // These enum values copy the values from {@link com.android.server.lights.LightsManager} 35 // and the light HAL. Since 0-7 are lights reserved for system use, 8 for microphone light is 36 // defined in {@link android.hardware.lights.LightsManager}, following types are available 37 // through this API. 38 /** Type for lights that indicate microphone usage */ 39 public static final int LIGHT_TYPE_MICROPHONE = 8; 40 41 /** Type for lights that indicate camera usage 42 * 43 * @hide 44 */ 45 public static final int LIGHT_TYPE_CAMERA = 9; 46 47 // These enum values start from 10001 to avoid collision with expanding of HAL light types. 48 /** 49 * Type for lights that indicate a monochrome color LED light. 50 */ 51 public static final int LIGHT_TYPE_INPUT = 10001; 52 53 /** 54 * Type for lights that indicate a group of LED lights representing player id. 55 * Player id lights normally present on game controllers are lights that consist of a row of 56 * LEDs. 57 * During multi-player game, the player id for the current game controller is represented by 58 * one of the LED that is lit according to its position in the row. 59 */ 60 public static final int LIGHT_TYPE_PLAYER_ID = 10002; 61 62 /** 63 * Type for lights that illuminate keyboard keys. 64 */ 65 public static final int LIGHT_TYPE_KEYBOARD_BACKLIGHT = 10003; 66 67 /** 68 * Type for keyboard microphone mute light. 69 * @hide 70 */ 71 public static final int LIGHT_TYPE_KEYBOARD_MIC_MUTE = 10004; 72 73 /** 74 * Type for keyboard volume mute light. 75 * @hide 76 */ 77 public static final int LIGHT_TYPE_KEYBOARD_VOLUME_MUTE = 10005; 78 79 /** 80 * Capability for lights that could adjust its LED brightness. If the capability is not present 81 * the LED can only be turned either on or off. 82 */ 83 public static final int LIGHT_CAPABILITY_BRIGHTNESS = 1 << 0; 84 85 /** 86 * Capability for lights that have red, green and blue LEDs to control the light's color. 87 */ 88 public static final int LIGHT_CAPABILITY_COLOR_RGB = 1 << 1; 89 90 /** 91 * Capability for lights that have red, green and blue LEDs to control the light's color. 92 * 93 * @deprecated Wrong int based flag with value 0. Use capability flag {@code 94 * LIGHT_CAPABILITY_COLOR_RGB} instead. 95 */ 96 @Deprecated 97 public static final int LIGHT_CAPABILITY_RGB = 0; 98 99 /** @hide */ 100 @Retention(RetentionPolicy.SOURCE) 101 @IntDef(prefix = {"LIGHT_TYPE_"}, 102 value = { 103 LIGHT_TYPE_MICROPHONE, 104 LIGHT_TYPE_INPUT, 105 LIGHT_TYPE_PLAYER_ID, 106 LIGHT_TYPE_KEYBOARD_BACKLIGHT, 107 LIGHT_TYPE_KEYBOARD_MIC_MUTE, 108 LIGHT_TYPE_KEYBOARD_VOLUME_MUTE, 109 }) 110 public @interface LightType {} 111 112 /** @hide */ 113 @Retention(RetentionPolicy.SOURCE) 114 @IntDef(flag = true, prefix = {"LIGHT_CAPABILITY_"}, 115 value = { 116 LIGHT_CAPABILITY_BRIGHTNESS, 117 LIGHT_CAPABILITY_COLOR_RGB, 118 LIGHT_CAPABILITY_RGB, 119 }) 120 public @interface LightCapability {} 121 122 private final int mId; 123 private final String mName; 124 private final int mOrdinal; 125 private final int mType; 126 private final int mCapabilities; 127 @Nullable 128 private final int[] mPreferredBrightnessLevels; 129 130 /** 131 * Creates a new light with the given data. 132 * 133 * @hide 134 */ Light(int id, int ordinal, int type)135 public Light(int id, int ordinal, int type) { 136 this(id, "Light", ordinal, type, 0, null); 137 } 138 139 /** 140 * Creates a new light with the given data. 141 * 142 * @hide 143 */ Light(int id, String name, int ordinal, int type, int capabilities)144 public Light(int id, String name, int ordinal, int type, int capabilities) { 145 this(id, name, ordinal, type, capabilities, null); 146 } 147 148 /** 149 * Creates a new light with the given data. 150 * 151 * @hide 152 */ Light(int id, String name, int ordinal, int type, int capabilities, @Nullable int[] preferredBrightnessLevels)153 public Light(int id, String name, int ordinal, int type, int capabilities, 154 @Nullable int[] preferredBrightnessLevels) { 155 mId = id; 156 mName = name; 157 mOrdinal = ordinal; 158 mType = type; 159 mCapabilities = capabilities; 160 mPreferredBrightnessLevels = preferredBrightnessLevels; 161 } 162 Light(@onNull Parcel in)163 private Light(@NonNull Parcel in) { 164 mId = in.readInt(); 165 mName = in.readString(); 166 mOrdinal = in.readInt(); 167 mType = in.readInt(); 168 mCapabilities = in.readInt(); 169 mPreferredBrightnessLevels = in.createIntArray(); 170 } 171 172 /** Implement the Parcelable interface */ 173 @Override writeToParcel(@onNull Parcel dest, int flags)174 public void writeToParcel(@NonNull Parcel dest, int flags) { 175 dest.writeInt(mId); 176 dest.writeString(mName); 177 dest.writeInt(mOrdinal); 178 dest.writeInt(mType); 179 dest.writeInt(mCapabilities); 180 dest.writeIntArray(mPreferredBrightnessLevels); 181 } 182 183 /** Implement the Parcelable interface */ 184 @Override describeContents()185 public int describeContents() { 186 return 0; 187 } 188 189 /** Implement the Parcelable interface */ 190 public static final @android.annotation.NonNull Parcelable.Creator<Light> CREATOR = 191 new Parcelable.Creator<Light>() { 192 public Light createFromParcel(Parcel in) { 193 return new Light(in); 194 } 195 196 public Light[] newArray(int size) { 197 return new Light[size]; 198 } 199 }; 200 201 @Override equals(@ullable Object obj)202 public boolean equals(@Nullable Object obj) { 203 if (obj instanceof Light) { 204 Light light = (Light) obj; 205 return mId == light.mId && mOrdinal == light.mOrdinal && mType == light.mType 206 && mCapabilities == light.mCapabilities; 207 } 208 return false; 209 } 210 211 @Override hashCode()212 public int hashCode() { 213 return mId; 214 } 215 216 @Override toString()217 public String toString() { 218 return "[Name=" + mName + " Id=" + mId + " Type=" + mType + " Capabilities=" 219 + mCapabilities + " Ordinal=" + mOrdinal + "]"; 220 } 221 222 /** 223 * Returns the id of the light. 224 * 225 * <p>This is an opaque value used as a unique identifier for the light. 226 */ getId()227 public int getId() { 228 return mId; 229 } 230 231 /** 232 * Returns the name of the light. 233 */ 234 @NonNull getName()235 public String getName() { 236 return mName; 237 } 238 239 /** 240 * Returns the ordinal of the light. 241 * 242 * <p>This is a sort key that represents the physical order of lights on the device with the 243 * same type. In the case of multiple lights arranged in a line, for example, the ordinals 244 * could be [1, 2, 3, 4], or [0, 10, 20, 30], or any other values that have the same sort order. 245 */ getOrdinal()246 public int getOrdinal() { 247 return mOrdinal; 248 } 249 250 /** 251 * Returns the logical type of the light. 252 */ getType()253 public @LightType int getType() { 254 return mType; 255 } 256 257 /** 258 * Returns the capabilities of the light. 259 * @hide 260 */ 261 @TestApi getCapabilities()262 public @LightCapability int getCapabilities() { 263 return mCapabilities; 264 } 265 266 /** 267 * Check whether the light has led brightness control. 268 * 269 * @return True if the hardware can control the led brightness, otherwise false. 270 */ hasBrightnessControl()271 public boolean hasBrightnessControl() { 272 return (mCapabilities & LIGHT_CAPABILITY_BRIGHTNESS) == LIGHT_CAPABILITY_BRIGHTNESS; 273 } 274 275 /** 276 * Check whether the light has RGB led control. 277 * 278 * @return True if the hardware can control the RGB led, otherwise false. 279 */ hasRgbControl()280 public boolean hasRgbControl() { 281 return (mCapabilities & LIGHT_CAPABILITY_COLOR_RGB) == LIGHT_CAPABILITY_COLOR_RGB; 282 } 283 284 /** 285 * Returns preferred brightness levels for the light which will be used when user 286 * increase/decrease brightness levels for the light (currently only used for Keyboard 287 * backlight control using backlight up/down keys). 288 * 289 * The values in the preferred brightness level array are in the range [0, 255]. 290 * 291 * @hide 292 */ 293 @Nullable getPreferredBrightnessLevels()294 public int[] getPreferredBrightnessLevels() { 295 return mPreferredBrightnessLevels; 296 } 297 } 298