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     // These enum values start from 10001 to avoid collision with expanding of HAL light types.
42     /**
43      * Type for lights that indicate a monochrome color LED light.
44      */
45     public static final int LIGHT_TYPE_INPUT = 10001;
46 
47     /**
48      * Type for lights that indicate a group of LED lights representing player id.
49      * Player id lights normally present on game controllers are lights that consist of a row of
50      * LEDs.
51      * During multi-player game, the player id for the current game controller is represented by
52      * one of the LED that is lit according to its position in the row.
53      */
54     public static final int LIGHT_TYPE_PLAYER_ID = 10002;
55 
56     /**
57      * Capability for lights that could adjust its LED brightness. If the capability is not present
58      * the led can only be turned either on or off.
59      */
60     public static final int LIGHT_CAPABILITY_BRIGHTNESS = 1 << 0;
61 
62     /**
63      * Capability for lights that has red, green and blue LEDs to control the light's color.
64      */
65     public static final int LIGHT_CAPABILITY_RGB = 0 << 1;
66 
67     /** @hide */
68     @Retention(RetentionPolicy.SOURCE)
69     @IntDef(prefix = {"LIGHT_TYPE_"},
70         value = {
71             LIGHT_TYPE_MICROPHONE,
72             LIGHT_TYPE_INPUT,
73             LIGHT_TYPE_PLAYER_ID,
74         })
75     public @interface LightType {}
76 
77     /** @hide */
78     @Retention(RetentionPolicy.SOURCE)
79     @IntDef(flag = true, prefix = {"LIGHT_CAPABILITY_"},
80         value = {
81             LIGHT_CAPABILITY_BRIGHTNESS,
82             LIGHT_CAPABILITY_RGB,
83         })
84     public @interface LightCapability {}
85 
86     private final int mId;
87     private final String mName;
88     private final int mOrdinal;
89     private final int mType;
90     private final int mCapabilities;
91 
92     /**
93      * Creates a new light with the given data.
94      *
95      * @hide
96      */
Light(int id, int ordinal, int type)97     public Light(int id, int ordinal, int type) {
98         this(id, "Light", ordinal, type, 0);
99     }
100 
101     /**
102      * Creates a new light with the given data.
103      *
104      * @hide
105      */
Light(int id, String name, int ordinal, int type, int capabilities)106     public Light(int id, String name, int ordinal, int type, int capabilities) {
107         mId = id;
108         mName = name;
109         mOrdinal = ordinal;
110         mType = type;
111         mCapabilities = capabilities;
112     }
113 
Light(@onNull Parcel in)114     private Light(@NonNull Parcel in) {
115         mId = in.readInt();
116         mName = in.readString();
117         mOrdinal = in.readInt();
118         mType = in.readInt();
119         mCapabilities = in.readInt();
120     }
121 
122     /** Implement the Parcelable interface */
123     @Override
writeToParcel(@onNull Parcel dest, int flags)124     public void writeToParcel(@NonNull Parcel dest, int flags) {
125         dest.writeInt(mId);
126         dest.writeString(mName);
127         dest.writeInt(mOrdinal);
128         dest.writeInt(mType);
129         dest.writeInt(mCapabilities);
130     }
131 
132     /** Implement the Parcelable interface */
133     @Override
describeContents()134     public int describeContents() {
135         return 0;
136     }
137 
138     /** Implement the Parcelable interface */
139     public static final @android.annotation.NonNull Parcelable.Creator<Light> CREATOR =
140             new Parcelable.Creator<Light>() {
141                 public Light createFromParcel(Parcel in) {
142                     return new Light(in);
143                 }
144 
145                 public Light[] newArray(int size) {
146                     return new Light[size];
147                 }
148             };
149 
150     @Override
equals(@ullable Object obj)151     public boolean equals(@Nullable Object obj) {
152         if (obj instanceof Light) {
153             Light light = (Light) obj;
154             return mId == light.mId && mOrdinal == light.mOrdinal && mType == light.mType
155                     && mCapabilities == light.mCapabilities;
156         }
157         return false;
158     }
159 
160     @Override
hashCode()161     public int hashCode() {
162         return mId;
163     }
164 
165     @Override
toString()166     public String toString() {
167         return "[Name=" + mName + " Id=" + mId + " Type=" + mType + " Capabilities="
168                 + mCapabilities + " Ordinal=" + mOrdinal + "]";
169     }
170 
171     /**
172      * Returns the id of the light.
173      *
174      * <p>This is an opaque value used as a unique identifier for the light.
175      */
getId()176     public int getId() {
177         return mId;
178     }
179 
180     /**
181      * Returns the name of the light.
182      */
183     @NonNull
getName()184     public String getName() {
185         return mName;
186     }
187 
188     /**
189      * Returns the ordinal of the light.
190      *
191      * <p>This is a sort key that represents the physical order of lights on the device with the
192      * same type. In the case of multiple lights arranged in a line, for example, the ordinals
193      * could be [1, 2, 3, 4], or [0, 10, 20, 30], or any other values that have the same sort order.
194      */
getOrdinal()195     public int getOrdinal() {
196         return mOrdinal;
197     }
198 
199     /**
200      * Returns the logical type of the light.
201      */
getType()202     public @LightType int getType() {
203         return mType;
204     }
205 
206     /**
207      * Returns the capabilities of the light.
208      * @hide
209      */
210     @TestApi
getCapabilities()211     public @LightCapability int getCapabilities() {
212         return mCapabilities;
213     }
214 
215     /**
216      * Check whether the light has led brightness control.
217      *
218      * @return True if the hardware can control the led brightness, otherwise false.
219      */
hasBrightnessControl()220     public boolean hasBrightnessControl() {
221         return (mCapabilities & LIGHT_CAPABILITY_BRIGHTNESS) == LIGHT_CAPABILITY_BRIGHTNESS;
222     }
223 
224     /**
225      * Check whether the light has RGB led control.
226      *
227      * @return True if the hardware can control the RGB led, otherwise false.
228      */
hasRgbControl()229     public boolean hasRgbControl() {
230         return (mCapabilities & LIGHT_CAPABILITY_RGB) == LIGHT_CAPABILITY_RGB;
231     }
232 
233 }
234