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 com.android.server.devicestate;
18 
19 import static android.hardware.devicestate.DeviceStateManager.MAXIMUM_DEVICE_STATE;
20 import static android.hardware.devicestate.DeviceStateManager.MINIMUM_DEVICE_STATE;
21 
22 import android.annotation.IntDef;
23 import android.annotation.IntRange;
24 import android.annotation.NonNull;
25 
26 import com.android.internal.util.Preconditions;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 import java.util.Objects;
31 
32 /**
33  * A state of the device defined by the {@link DeviceStateProvider} and managed by the
34  * {@link DeviceStateManagerService}.
35  * <p>
36  * Device state is an abstract concept that allows mapping the current state of the device to the
37  * state of the system. This is useful for variable-state devices, like foldable or rollable
38  * devices, that can be configured by users into differing hardware states, which each may have a
39  * different expected use case.
40  *
41  * @see DeviceStateProvider
42  * @see DeviceStateManagerService
43  */
44 public final class DeviceState {
45     /**
46      * Flag that indicates sticky requests should be cancelled when this device state becomes the
47      * base device state.
48      */
49     public static final int FLAG_CANCEL_STICKY_REQUESTS = 1 << 0;
50 
51     /** @hide */
52     @IntDef(prefix = {"FLAG_"}, flag = true, value = {
53             FLAG_CANCEL_STICKY_REQUESTS,
54     })
55     @Retention(RetentionPolicy.SOURCE)
56     public @interface DeviceStateFlags {}
57 
58     /** Unique identifier for the device state. */
59     @IntRange(from = MINIMUM_DEVICE_STATE, to = MAXIMUM_DEVICE_STATE)
60     private final int mIdentifier;
61 
62     /** String description of the device state. */
63     @NonNull
64     private final String mName;
65 
66     @DeviceStateFlags
67     private final int mFlags;
68 
DeviceState( @ntRangefrom = MINIMUM_DEVICE_STATE, to = MAXIMUM_DEVICE_STATE) int identifier, @NonNull String name, @DeviceStateFlags int flags)69     public DeviceState(
70             @IntRange(from = MINIMUM_DEVICE_STATE, to = MAXIMUM_DEVICE_STATE) int identifier,
71             @NonNull String name,
72             @DeviceStateFlags int flags) {
73         Preconditions.checkArgumentInRange(identifier, MINIMUM_DEVICE_STATE, MAXIMUM_DEVICE_STATE,
74                 "identifier");
75 
76         mIdentifier = identifier;
77         mName = name;
78         mFlags = flags;
79     }
80 
81     /** Returns the unique identifier for the device state. */
82     @IntRange(from = MINIMUM_DEVICE_STATE, to = MAXIMUM_DEVICE_STATE)
getIdentifier()83     public int getIdentifier() {
84         return mIdentifier;
85     }
86 
87     /** Returns a string description of the device state. */
88     @NonNull
getName()89     public String getName() {
90         return mName;
91     }
92 
93     @DeviceStateFlags
getFlags()94     public int getFlags() {
95         return mFlags;
96     }
97 
98     @Override
toString()99     public String toString() {
100         return "DeviceState{" + "identifier=" + mIdentifier + ", name='" + mName + '\'' + '}';
101     }
102 
103     @Override
equals(Object o)104     public boolean equals(Object o) {
105         if (this == o) return true;
106         if (o == null || getClass() != o.getClass()) return false;
107         DeviceState that = (DeviceState) o;
108         return mIdentifier == that.mIdentifier
109                 && Objects.equals(mName, that.mName)
110                 && mFlags == that.mFlags;
111     }
112 
113     @Override
hashCode()114     public int hashCode() {
115         return Objects.hash(mIdentifier, mName, mFlags);
116     }
117 }
118