1 /* 2 * Copyright (c) 2017 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.TemperatureType; 23 import android.hardware.thermal.ThrottlingSeverity; 24 25 import com.android.internal.util.Preconditions; 26 27 import java.lang.annotation.Retention; 28 import java.lang.annotation.RetentionPolicy; 29 30 /** 31 * Temperature values used by IThermalService. 32 * 33 * @hide 34 */ 35 public final class Temperature implements Parcelable { 36 /** Temperature value */ 37 private final float mValue; 38 /** A Temperature type from ThermalHAL */ 39 private final int mType; 40 /** Name of this Temperature */ 41 private final String mName; 42 /** The level of the sensor is currently in throttling */ 43 private final int mStatus; 44 45 @IntDef(prefix = { "THROTTLING_" }, value = { 46 THROTTLING_NONE, 47 THROTTLING_LIGHT, 48 THROTTLING_MODERATE, 49 THROTTLING_SEVERE, 50 THROTTLING_CRITICAL, 51 THROTTLING_EMERGENCY, 52 THROTTLING_SHUTDOWN, 53 }) 54 @Retention(RetentionPolicy.SOURCE) 55 public @interface ThrottlingStatus {} 56 57 /** Keep in sync with hardware/interfaces/thermal/aidl/android/hardware/thermal 58 * /ThrottlingSeverity.aidl */ 59 public static final int THROTTLING_NONE = ThrottlingSeverity.NONE; 60 public static final int THROTTLING_LIGHT = ThrottlingSeverity.LIGHT; 61 public static final int THROTTLING_MODERATE = ThrottlingSeverity.MODERATE; 62 public static final int THROTTLING_SEVERE = ThrottlingSeverity.SEVERE; 63 public static final int THROTTLING_CRITICAL = ThrottlingSeverity.CRITICAL; 64 public static final int THROTTLING_EMERGENCY = ThrottlingSeverity.EMERGENCY; 65 public static final int THROTTLING_SHUTDOWN = ThrottlingSeverity.SHUTDOWN; 66 67 @IntDef(prefix = { "TYPE_" }, value = { 68 TYPE_UNKNOWN, 69 TYPE_CPU, 70 TYPE_GPU, 71 TYPE_BATTERY, 72 TYPE_SKIN, 73 TYPE_USB_PORT, 74 TYPE_POWER_AMPLIFIER, 75 TYPE_BCL_VOLTAGE, 76 TYPE_BCL_CURRENT, 77 TYPE_BCL_PERCENTAGE, 78 TYPE_NPU, 79 TYPE_TPU, 80 TYPE_DISPLAY, 81 TYPE_MODEM, 82 TYPE_SOC, 83 TYPE_WIFI, 84 TYPE_CAMERA, 85 TYPE_FLASHLIGHT, 86 TYPE_SPEAKER, 87 TYPE_AMBIENT, 88 TYPE_POGO 89 }) 90 @Retention(RetentionPolicy.SOURCE) 91 public @interface Type {} 92 93 /** Keep in sync with hardware/interfaces/thermal/aidl/android/hardware/thermal 94 * /TemperatureType.aidl */ 95 public static final int TYPE_UNKNOWN = TemperatureType.UNKNOWN; 96 public static final int TYPE_CPU = TemperatureType.CPU; 97 public static final int TYPE_GPU = TemperatureType.GPU; 98 public static final int TYPE_BATTERY = TemperatureType.BATTERY; 99 public static final int TYPE_SKIN = TemperatureType.SKIN; 100 public static final int TYPE_USB_PORT = TemperatureType.USB_PORT; 101 public static final int TYPE_POWER_AMPLIFIER = TemperatureType.POWER_AMPLIFIER; 102 public static final int TYPE_BCL_VOLTAGE = TemperatureType.BCL_VOLTAGE; 103 public static final int TYPE_BCL_CURRENT = TemperatureType.BCL_CURRENT; 104 public static final int TYPE_BCL_PERCENTAGE = TemperatureType.BCL_PERCENTAGE; 105 public static final int TYPE_NPU = TemperatureType.NPU; 106 public static final int TYPE_TPU = TemperatureType.TPU; 107 public static final int TYPE_DISPLAY = TemperatureType.DISPLAY; 108 public static final int TYPE_MODEM = TemperatureType.MODEM; 109 public static final int TYPE_SOC = TemperatureType.SOC; 110 public static final int TYPE_WIFI = TemperatureType.WIFI; 111 public static final int TYPE_CAMERA = TemperatureType.CAMERA; 112 public static final int TYPE_FLASHLIGHT = TemperatureType.FLASHLIGHT; 113 public static final int TYPE_SPEAKER = TemperatureType.SPEAKER; 114 public static final int TYPE_AMBIENT = TemperatureType.AMBIENT; 115 public static final int TYPE_POGO = TemperatureType.POGO; 116 117 /** 118 * Verify a valid Temperature type. 119 * 120 * @return true if a Temperature type is valid otherwise false. 121 */ isValidType(@ype int type)122 public static boolean isValidType(@Type int type) { 123 return type >= TYPE_UNKNOWN && type <= TYPE_POGO; 124 } 125 126 /** 127 * Verify a valid throttling status. 128 * 129 * @return true if a status is valid otherwise false. 130 */ isValidStatus(@hrottlingStatus int status)131 public static boolean isValidStatus(@ThrottlingStatus int status) { 132 return status >= THROTTLING_NONE && status <= THROTTLING_SHUTDOWN; 133 } 134 Temperature(float value, @Type int type, @NonNull String name, @ThrottlingStatus int status)135 public Temperature(float value, @Type int type, 136 @NonNull String name, @ThrottlingStatus int status) { 137 Preconditions.checkArgument(isValidType(type), "Invalid Type"); 138 Preconditions.checkArgument(isValidStatus(status) , "Invalid Status"); 139 mValue = value; 140 mType = type; 141 mName = Preconditions.checkStringNotEmpty(name); 142 mStatus = status; 143 } 144 145 /** 146 * Return the Temperature value. 147 * 148 * @return a Temperature value in floating point could be NaN. 149 */ getValue()150 public float getValue() { 151 return mValue; 152 } 153 154 /** 155 * Return the Temperature type. 156 * 157 * @return a Temperature type: TYPE_* 158 */ getType()159 public @Type int getType() { 160 return mType; 161 } 162 163 /** 164 * Return the Temperature name. 165 * 166 * @return a Temperature name as String. 167 */ getName()168 public String getName() { 169 return mName; 170 } 171 172 /** 173 * Return the Temperature throttling status. 174 * 175 * @return a Temperature throttling status: THROTTLING_* 176 */ getStatus()177 public @ThrottlingStatus int getStatus() { 178 return mStatus; 179 } 180 181 @Override toString()182 public String toString() { 183 return "Temperature{mValue=" + mValue + ", mType=" + mType 184 + ", mName=" + mName + ", mStatus=" + mStatus + "}"; 185 } 186 187 @Override hashCode()188 public int hashCode() { 189 int hash = mName.hashCode(); 190 hash = 31 * hash + Float.hashCode(mValue); 191 hash = 31 * hash + mType; 192 hash = 31 * hash + mStatus; 193 return hash; 194 } 195 196 @Override equals(@ullable Object o)197 public boolean equals(@Nullable Object o) { 198 if (!(o instanceof Temperature)) { 199 return false; 200 } 201 Temperature other = (Temperature) o; 202 return other.mValue == mValue && other.mType == mType 203 && other.mName.equals(mName) && other.mStatus == mStatus; 204 } 205 206 @Override writeToParcel(Parcel p, int flags)207 public void writeToParcel(Parcel p, int flags) { 208 p.writeFloat(mValue); 209 p.writeInt(mType); 210 p.writeString(mName); 211 p.writeInt(mStatus); 212 } 213 214 public static final @android.annotation.NonNull Parcelable.Creator<Temperature> CREATOR = 215 new Parcelable.Creator<Temperature>() { 216 @Override 217 public Temperature createFromParcel(Parcel p) { 218 float value = p.readFloat(); 219 int type = p.readInt(); 220 String name = p.readString(); 221 int status = p.readInt(); 222 return new Temperature(value, type, name, status); 223 } 224 225 @Override 226 public Temperature[] newArray(int size) { 227 return new Temperature[size]; 228 } 229 230 }; 231 232 @Override describeContents()233 public int describeContents() { 234 return 0; 235 } 236 } 237