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