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.NonNull; 20 import android.annotation.SystemApi; 21 22 import com.android.internal.util.Preconditions; 23 24 import java.util.ArrayList; 25 import java.util.HashMap; 26 import java.util.List; 27 import java.util.Map; 28 /** 29 * Encapsulates a request to modify the state of multiple lights. 30 * 31 */ 32 public final class LightsRequest { 33 34 /** Visible to {@link LightsManager.Session}. */ 35 final Map<Light, LightState> mRequests = new HashMap<>(); 36 final List<Integer> mLightIds = new ArrayList<>(); 37 final List<LightState> mLightStates = new ArrayList<>(); 38 39 /** 40 * Can only be constructed via {@link LightsRequest.Builder#build()}. 41 */ LightsRequest(Map<Light, LightState> requests)42 private LightsRequest(Map<Light, LightState> requests) { 43 mRequests.putAll(requests); 44 List<Light> lights = new ArrayList<Light>(mRequests.keySet()); 45 for (int i = 0; i < lights.size(); i++) { 46 final Light light = lights.get(i); 47 mLightIds.add(i, light.getId()); 48 mLightStates.add(i, mRequests.get(light)); 49 } 50 } 51 52 /** 53 * Get a list of Light as ids. 54 * 55 * @return List of light ids in the request. 56 */ getLights()57 public @NonNull List<Integer> getLights() { 58 return mLightIds; 59 } 60 61 /** 62 * Get a list of LightState. The states will be returned in same order as the light ids 63 * returned by {@link #getLights()}. 64 * 65 * @return List of light states 66 */ getLightStates()67 public @NonNull List<LightState> getLightStates() { 68 return mLightStates; 69 } 70 71 /** 72 * Get a map of lights and states. The map will contain all the lights as keys and 73 * the corresponding LightState requested as values. 74 */ getLightsAndStates()75 public @NonNull Map<Light, LightState> getLightsAndStates() { 76 return mRequests; 77 } 78 79 /** 80 * Builder for creating device light change requests. 81 */ 82 public static final class Builder { 83 84 final Map<Light, LightState> mChanges = new HashMap<>(); 85 /** 86 * Overrides the color and intensity of a given light. 87 * 88 * @param light the light to modify 89 * @param state the desired color and intensity of the light 90 */ addLight(@onNull Light light, @NonNull LightState state)91 public @NonNull Builder addLight(@NonNull Light light, @NonNull LightState state) { 92 Preconditions.checkNotNull(light); 93 Preconditions.checkNotNull(state); 94 mChanges.put(light, state); 95 return this; 96 } 97 98 /** 99 * Overrides the color and intensity of a given light. 100 * 101 * @param light the light to modify 102 * @param state the desired color and intensity of the light * 103 * @deprecated Use {@link #addLight(Light, LightState)} instead. 104 * @hide 105 */ 106 @SystemApi 107 @Deprecated setLight(@onNull Light light, @NonNull LightState state)108 public @NonNull Builder setLight(@NonNull Light light, @NonNull LightState state) { 109 return addLight(light, state); 110 } 111 112 /** 113 * Removes the override for the color and intensity of a given light. 114 * 115 * @param light the light to modify 116 */ clearLight(@onNull Light light)117 public @NonNull Builder clearLight(@NonNull Light light) { 118 Preconditions.checkNotNull(light); 119 mChanges.put(light, null); 120 return this; 121 } 122 123 /** 124 * Create a LightsRequest object used to override lights on the device. 125 * 126 * <p>The generated {@link LightsRequest} should be used in 127 * {@link LightsManager.Session#requestLights(LightsLightsRequest). 128 */ build()129 public @NonNull LightsRequest build() { 130 return new LightsRequest(mChanges); 131 } 132 } 133 } 134