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