1 /*
2  * Copyright (C) 2021 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.devicestate;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 import java.util.Arrays;
28 import java.util.Objects;
29 import java.util.concurrent.Executor;
30 
31 
32 /**
33  * Information about the state of the device.
34  *
35  * @hide
36  */
37 public final class DeviceStateInfo implements Parcelable {
38     /** Bit that indicates the {@link #supportedStates} field has changed. */
39     public static final int CHANGED_SUPPORTED_STATES = 1 << 0;
40 
41     /** Bit that indicates the {@link #baseState} field has changed. */
42     public static final int CHANGED_BASE_STATE = 1 << 1;
43 
44     /** Bit that indicates the {@link #currentState} field has changed. */
45     public static final int CHANGED_CURRENT_STATE = 1 << 2;
46 
47     @IntDef(prefix = {"CHANGED_"}, flag = true, value = {
48             CHANGED_SUPPORTED_STATES,
49             CHANGED_BASE_STATE,
50             CHANGED_CURRENT_STATE,
51     })
52     @Retention(RetentionPolicy.SOURCE)
53     public @interface ChangeFlags {}
54 
55     /**
56      * The list of states supported by the device.
57      */
58     @NonNull
59     public final int[] supportedStates;
60 
61     /**
62      * The base (non-override) state of the device. The base state is the state of the device
63      * ignoring any override requests made through a call to {@link DeviceStateManager#requestState(
64      * DeviceStateRequest, Executor, DeviceStateRequest.Callback)}.
65      */
66     public final int baseState;
67 
68     /**
69      * The state of the device.
70      */
71     public final int currentState;
72 
73     /**
74      * Creates a new instance of {@link DeviceStateInfo}.
75      * <p>
76      * NOTE: Unlike {@link #DeviceStateInfo(DeviceStateInfo)}, this constructor does not copy the
77      * supplied parameters.
78      */
DeviceStateInfo(@onNull int[] supportedStates, int baseState, int state)79     public DeviceStateInfo(@NonNull int[] supportedStates, int baseState, int state) {
80         this.supportedStates = supportedStates;
81         this.baseState = baseState;
82         this.currentState = state;
83     }
84 
85     /**
86      * Creates a new instance of {@link DeviceStateInfo} copying the fields of {@code info} into
87      * the fields of the returned instance.
88      */
DeviceStateInfo(@onNull DeviceStateInfo info)89     public DeviceStateInfo(@NonNull DeviceStateInfo info) {
90         this(Arrays.copyOf(info.supportedStates, info.supportedStates.length),
91                 info.baseState, info.currentState);
92     }
93 
94     @Override
equals(@ullable Object other)95     public boolean equals(@Nullable Object other) {
96         if (this == other) return true;
97         if (other == null || getClass() != other.getClass()) return false;
98         DeviceStateInfo that = (DeviceStateInfo) other;
99         return baseState == that.baseState
100                 &&  currentState == that.currentState
101                 && Arrays.equals(supportedStates, that.supportedStates);
102     }
103 
104     @Override
hashCode()105     public int hashCode() {
106         int result = Objects.hash(baseState, currentState);
107         result = 31 * result + Arrays.hashCode(supportedStates);
108         return result;
109     }
110 
111     /** Returns a bitmask of the differences between this instance and {@code other}. */
112     @ChangeFlags
diff(@onNull DeviceStateInfo other)113     public int diff(@NonNull DeviceStateInfo other) {
114         int diff = 0;
115         if (!Arrays.equals(supportedStates, other.supportedStates)) {
116             diff |= CHANGED_SUPPORTED_STATES;
117         }
118         if (baseState != other.baseState) {
119             diff |= CHANGED_BASE_STATE;
120         }
121         if (currentState != other.currentState) {
122             diff |= CHANGED_CURRENT_STATE;
123         }
124         return diff;
125     }
126 
127     @Override
writeToParcel(Parcel dest, int flags)128     public void writeToParcel(Parcel dest, int flags) {
129         dest.writeInt(supportedStates.length);
130         for (int i = 0; i < supportedStates.length; i++) {
131             dest.writeInt(supportedStates[i]);
132         }
133 
134         dest.writeInt(baseState);
135         dest.writeInt(currentState);
136     }
137 
138     @Override
describeContents()139     public int describeContents() {
140         return 0;
141     }
142 
143     public static final @NonNull Creator<DeviceStateInfo> CREATOR = new Creator<DeviceStateInfo>() {
144         @Override
145         public DeviceStateInfo createFromParcel(Parcel source) {
146             final int numberOfSupportedStates = source.readInt();
147             final int[] supportedStates = new int[numberOfSupportedStates];
148             for (int i = 0; i < numberOfSupportedStates; i++) {
149                 supportedStates[i] = source.readInt();
150             }
151             final int baseState = source.readInt();
152             final int currentState = source.readInt();
153 
154             return new DeviceStateInfo(supportedStates, baseState, currentState);
155         }
156 
157         @Override
158         public DeviceStateInfo[] newArray(int size) {
159             return new DeviceStateInfo[size];
160         }
161     };
162 }
163