| 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.ColorInt; |
| 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.SuppressLint; |
| 22 | import android.annotation.SystemApi; |
| 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
| 25 | |
| 26 | /** |
| 27 | * Represents the state of a device light. |
| 28 | * |
| 29 | * <p>Controlling the color and brightness of a light is done on a best-effort basis. Each of the R, |
| 30 | * G and B channels represent the intensities of the respective part of an RGB LED, if that is |
| 31 | * supported. For devices that only support on or off lights, everything that's not off will turn |
| 32 | * the light on. If the light is monochrome and only the brightness can be controlled, the RGB color |
| 33 | * will be converted to only a brightness value and that will be used for the light's single |
| 34 | * channel. |
| 35 | * |
| 36 | */ |
| 37 | public final class LightState implements Parcelable { |
| 38 | private final int mColor; |
| 39 | private final int mPlayerId; |
| 40 | |
| 41 | /** |
| 42 | * Creates a new LightState with the desired color and intensity, for a light type |
| 43 | * of RBG color or monochrome color. |
| 44 | * |
| 45 | * @param color the desired color and intensity in ARGB format. |
| 46 | * @deprecated this has been replaced with {@link android.hardware.lights.LightState.Builder } |
| 47 | * @hide |
| 48 | */ |
| 49 | @Deprecated |
| 50 | @SystemApi |
| 51 | public LightState(@ColorInt int color) { |
| 52 | this(color, 0); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Creates a new LightState with the desired color and intensity, and the player Id. |
| 57 | * Player Id will only be applied on Light with type |
| 58 | * {@link android.hardware.lights.Light#LIGHT_TYPE_PLAYER_ID} |
| 59 | * |
| 60 | * @param color the desired color and intensity in ARGB format. |
| 61 | * @hide |
| 62 | */ |
| 63 | public LightState(@ColorInt int color, int playerId) { |
| 64 | mColor = color; |
| 65 | mPlayerId = playerId; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Builder for creating device light change requests. |
| 70 | */ |
| 71 | public static final class Builder { |
| 72 | private int mValue; |
| 73 | private boolean mIsForPlayerId; |
| 74 | |
| 75 | /** Creates a new {@link LightState.Builder}. */ |
| 76 | public Builder() { |
| 77 | mValue = 0; |
| 78 | mIsForPlayerId = false; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Set the desired color and intensity of the LightState Builder, for a light type |
| 83 | * of RBG color or single monochrome color. |
| 84 | * |
| 85 | * @param color the desired color and intensity in ARGB format. |
| 86 | * @return The {@link LightState.Builder} object contains the light color and intensity. |
| 87 | */ |
| 88 | @SuppressLint("MissingGetterMatchingBuilder") |
| 89 | @NonNull |
| 90 | public Builder setColor(@ColorInt int color) { |
| 91 | mIsForPlayerId = false; |
| 92 | mValue = color; |
| 93 | return this; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Set the desired player id of the LightState Builder, for a light with type |
| 98 | * {@link android.hardware.lights.Light#LIGHT_TYPE_PLAYER_ID}. |
| 99 | * |
| 100 | * @param playerId the desired player id. |
| 101 | * @return The {@link LightState.Builder} object contains the player id. |
| 102 | */ |
| 103 | @SuppressLint("MissingGetterMatchingBuilder") |
| 104 | @NonNull |
| 105 | public Builder setPlayerId(int playerId) { |
| 106 | mIsForPlayerId = true; |
| 107 | mValue = playerId; |
| 108 | return this; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Create a LightState object used to control lights on the device. |
| 113 | * |
| 114 | * <p>The generated {@link LightState} should be used in |
| 115 | * {@link LightsRequest.Builder#addLight(Light, LightState)}. |
| 116 | */ |
| 117 | public @NonNull LightState build() { |
| 118 | if (!mIsForPlayerId) { |
| 119 | return new LightState(mValue, 0); |
| 120 | } else { |
| 121 | return new LightState(0, mValue); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Creates a new LightState from a parcel object. |
| 128 | */ |
| 129 | private LightState(@NonNull Parcel in) { |
| 130 | mColor = in.readInt(); |
| 131 | mPlayerId = in.readInt(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns the color and intensity associated with this LightState. |
| 136 | * @return the color and intensity in ARGB format. The A channel is ignored. return 0 when |
| 137 | * calling LightsManager.getLightState with |
| 138 | * {@link android.hardware.lights.Light#LIGHT_TYPE_PLAYER_ID}. |
| 139 | */ |
| 140 | public @ColorInt int getColor() { |
| 141 | return mColor; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns the player ID associated with this LightState for Light with type |
| 146 | * {@link android.hardware.lights.Light#LIGHT_TYPE_PLAYER_ID}, |
| 147 | * or 0 for other types. |
| 148 | * @return the player ID. |
| 149 | */ |
| 150 | public int getPlayerId() { |
| 151 | return mPlayerId; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 156 | dest.writeInt(mColor); |
| 157 | dest.writeInt(mPlayerId); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public int describeContents() { |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public String toString() { |
| 167 | return "LightState{Color=0x" + Integer.toHexString(mColor) + ", PlayerId=" |
| 168 | + mPlayerId + "}"; |
| 169 | } |
| 170 | |
| 171 | public static final @NonNull Parcelable.Creator<LightState> CREATOR = |
| 172 | new Parcelable.Creator<LightState>() { |
| 173 | public LightState createFromParcel(Parcel in) { |
| 174 | return new LightState(in); |
| 175 | } |
| 176 | |
| 177 | public LightState[] newArray(int size) { |
| 178 | return new LightState[size]; |
| 179 | } |
| 180 | }; |
| 181 | } |