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.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             TYPE_TPU,
56             TYPE_POWER_AMPLIFIER,
57             TYPE_DISPLAY,
58             TYPE_SPEAKER
59     })
60     @Retention(RetentionPolicy.SOURCE)
61     public @interface Type {}
62 
63     /** Keep in sync with hardware/interfaces/thermal/aidl/android/hardware/thermal
64      * /ThrottlingSeverity.aidl */
65     /** Fan for active cooling */
66     public static final int TYPE_FAN = CoolingType.FAN;
67     /** Battery charging cooling deivice */
68     public static final int TYPE_BATTERY = CoolingType.BATTERY;
69     /** CPU cooling deivice */
70     public static final int TYPE_CPU = CoolingType.CPU;
71     /** GPU cooling deivice */
72     public static final int TYPE_GPU = CoolingType.GPU;
73     /** Modem cooling deivice */
74     public static final int TYPE_MODEM = CoolingType.MODEM;
75     /** NPU cooling deivice */
76     public static final int TYPE_NPU = CoolingType.NPU;
77     /** Generic passive cooling deivice */
78     public static final int TYPE_COMPONENT = CoolingType.COMPONENT;
79     /** TPU cooling deivice */
80     public static final int TYPE_TPU = CoolingType.TPU;
81     /** Power amplifier cooling device */
82     public static final int TYPE_POWER_AMPLIFIER = CoolingType.POWER_AMPLIFIER;
83     /** Display cooling device */
84     public static final int TYPE_DISPLAY = CoolingType.DISPLAY;
85     /** Speaker cooling device */
86     public static final int TYPE_SPEAKER = CoolingType.SPEAKER;
87 
88     /**
89      * Verify a valid cooling device type.
90      *
91      * @return true if a cooling device type is valid otherwise false.
92      */
isValidType(@ype int type)93     public static boolean isValidType(@Type int type) {
94         return type >= TYPE_FAN && type <= TYPE_SPEAKER;
95     }
96 
CoolingDevice(long value, @Type int type, @NonNull String name)97     public CoolingDevice(long value, @Type int type, @NonNull String name) {
98         Preconditions.checkArgument(isValidType(type), "Invalid Type");
99         mValue = value;
100         mType = type;
101         mName = Preconditions.checkStringNotEmpty(name);
102     }
103 
104     /**
105      * Return the cooling device value.
106      *
107      * @return a cooling device value in int.
108      */
getValue()109     public long getValue() {
110         return mValue;
111     }
112 
113     /**
114      * Return the cooling device type.
115      *
116      * @return a cooling device type: TYPE_*
117      */
getType()118     public @Type int getType() {
119         return mType;
120     }
121 
122     /**
123      * Return the cooling device name.
124      *
125      * @return a cooling device name as String.
126      */
getName()127     public String getName() {
128         return mName;
129     }
130 
131     @Override
toString()132     public String toString() {
133         return "CoolingDevice{mValue=" + mValue + ", mType=" + mType + ", mName=" + mName + "}";
134     }
135 
136     @Override
hashCode()137     public int hashCode() {
138         int hash = mName.hashCode();
139         hash = 31 * hash + Long.hashCode(mValue);
140         hash = 31 * hash + mType;
141         return hash;
142     }
143 
144     @Override
equals(@ullable Object o)145     public boolean equals(@Nullable Object o) {
146         if (!(o instanceof CoolingDevice)) {
147             return false;
148         }
149         CoolingDevice other = (CoolingDevice) o;
150         return other.mValue == mValue && other.mType == mType && other.mName.equals(mName);
151     }
152 
153     @Override
writeToParcel(Parcel p, int flags)154     public void writeToParcel(Parcel p, int flags) {
155         p.writeLong(mValue);
156         p.writeInt(mType);
157         p.writeString(mName);
158     }
159 
160     public static final @android.annotation.NonNull Parcelable.Creator<CoolingDevice> CREATOR =
161             new Parcelable.Creator<CoolingDevice>() {
162                 @Override
163                 public CoolingDevice createFromParcel(Parcel p) {
164                     long value = p.readLong();
165                     int type = p.readInt();
166                     String name = p.readString();
167                     return new CoolingDevice(value, type, name);
168                 }
169 
170                 @Override
171                 public CoolingDevice[] newArray(int size) {
172                     return new CoolingDevice[size];
173                 }
174             };
175 
176     @Override
describeContents()177     public int describeContents() {
178         return 0;
179     }
180 }
181