1 /*
2  * Copyright (c) 2019 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.os;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.hardware.thermal.V2_0.CoolingType;
22 
23 import com.android.internal.util.Preconditions;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * Cooling device values used by IThermalService.
30  *
31  * @hide
32  */
33 public final class CoolingDevice implements Parcelable {
34     /**
35      * Current throttle state of the cooling device. The value can any unsigned integer
36      * numbers between 0 and max_state defined in its driver, usually representing the
37      * associated device's power state. 0 means device is not in throttling, higher value
38      * means deeper throttling.
39      */
40     private final long mValue;
41     /** A cooling device type from ThermalHAL */
42     private final int mType;
43     /** Name of this cooling device */
44     private final String mName;
45 
46     @IntDef(prefix = { "TYPE_" }, value = {
47             TYPE_FAN,
48             TYPE_BATTERY,
49             TYPE_CPU,
50             TYPE_GPU,
51             TYPE_MODEM,
52             TYPE_NPU,
53             TYPE_COMPONENT,
54     })
55     @Retention(RetentionPolicy.SOURCE)
56     public @interface Type {}
57 
58     /** Keep in sync with hardware/interfaces/thermal/2.0/types.hal */
59     /** Fan for active cooling */
60     public static final int TYPE_FAN = CoolingType.FAN;
61     /** Battery charging cooling deivice */
62     public static final int TYPE_BATTERY = CoolingType.BATTERY;
63     /** CPU cooling deivice */
64     public static final int TYPE_CPU = CoolingType.CPU;
65     /** GPU cooling deivice */
66     public static final int TYPE_GPU = CoolingType.GPU;
67     /** Modem cooling deivice */
68     public static final int TYPE_MODEM = CoolingType.MODEM;
69     /** NPU/TPU cooling deivice */
70     public static final int TYPE_NPU = CoolingType.NPU;
71     /** Generic passive cooling deivice */
72     public static final int TYPE_COMPONENT = CoolingType.COMPONENT;
73 
74     /**
75      * Verify a valid cooling device type.
76      *
77      * @return true if a cooling device type is valid otherwise false.
78      */
isValidType(@ype int type)79     public static boolean isValidType(@Type int type) {
80         return type >= TYPE_FAN && type <= TYPE_COMPONENT;
81     }
82 
CoolingDevice(long value, @Type int type, @NonNull String name)83     public CoolingDevice(long value, @Type int type, @NonNull String name) {
84         Preconditions.checkArgument(isValidType(type), "Invalid Type");
85         mValue = value;
86         mType = type;
87         mName = Preconditions.checkStringNotEmpty(name);
88     }
89 
90     /**
91      * Return the cooling device value.
92      *
93      * @return a cooling device value in int.
94      */
getValue()95     public long getValue() {
96         return mValue;
97     }
98 
99     /**
100      * Return the cooling device type.
101      *
102      * @return a cooling device type: TYPE_*
103      */
getType()104     public @Type int getType() {
105         return mType;
106     }
107 
108     /**
109      * Return the cooling device name.
110      *
111      * @return a cooling device name as String.
112      */
getName()113     public String getName() {
114         return mName;
115     }
116 
117     @Override
toString()118     public String toString() {
119         return "CoolingDevice{mValue=" + mValue + ", mType=" + mType + ", mName=" + mName + "}";
120     }
121 
122     @Override
hashCode()123     public int hashCode() {
124         int hash = mName.hashCode();
125         hash = 31 * hash + Long.hashCode(mValue);
126         hash = 31 * hash + mType;
127         return hash;
128     }
129 
130     @Override
equals(Object o)131     public boolean equals(Object o) {
132         if (!(o instanceof CoolingDevice)) {
133             return false;
134         }
135         CoolingDevice other = (CoolingDevice) o;
136         return other.mValue == mValue && other.mType == mType && other.mName.equals(mName);
137     }
138 
139     @Override
writeToParcel(Parcel p, int flags)140     public void writeToParcel(Parcel p, int flags) {
141         p.writeLong(mValue);
142         p.writeInt(mType);
143         p.writeString(mName);
144     }
145 
146     public static final @android.annotation.NonNull Parcelable.Creator<CoolingDevice> CREATOR =
147             new Parcelable.Creator<CoolingDevice>() {
148                 @Override
149                 public CoolingDevice createFromParcel(Parcel p) {
150                     long value = p.readLong();
151                     int type = p.readInt();
152                     String name = p.readString();
153                     return new CoolingDevice(value, type, name);
154                 }
155 
156                 @Override
157                 public CoolingDevice[] newArray(int size) {
158                     return new CoolingDevice[size];
159                 }
160             };
161 
162     @Override
describeContents()163     public int describeContents() {
164         return 0;
165     }
166 }
167