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.V2_0.TemperatureType; 23 import android.hardware.thermal.V2_0.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/2.0/types.hal */ 58 public static final int THROTTLING_NONE = ThrottlingSeverity.NONE; 59 public static final int THROTTLING_LIGHT = ThrottlingSeverity.LIGHT; 60 public static final int THROTTLING_MODERATE = ThrottlingSeverity.MODERATE; 61 public static final int THROTTLING_SEVERE = ThrottlingSeverity.SEVERE; 62 public static final int THROTTLING_CRITICAL = ThrottlingSeverity.CRITICAL; 63 public static final int THROTTLING_EMERGENCY = ThrottlingSeverity.EMERGENCY; 64 public static final int THROTTLING_SHUTDOWN = ThrottlingSeverity.SHUTDOWN; 65 66 @IntDef(prefix = { "TYPE_" }, value = { 67 TYPE_UNKNOWN, 68 TYPE_CPU, 69 TYPE_GPU, 70 TYPE_BATTERY, 71 TYPE_SKIN, 72 TYPE_USB_PORT, 73 TYPE_POWER_AMPLIFIER, 74 TYPE_BCL_VOLTAGE, 75 TYPE_BCL_CURRENT, 76 TYPE_BCL_PERCENTAGE, 77 TYPE_NPU, 78 }) 79 @Retention(RetentionPolicy.SOURCE) 80 public @interface Type {} 81 82 /** Keep in sync with hardware/interfaces/thermal/2.0/types.hal */ 83 public static final int TYPE_UNKNOWN = TemperatureType.UNKNOWN; 84 public static final int TYPE_CPU = TemperatureType.CPU; 85 public static final int TYPE_GPU = TemperatureType.GPU; 86 public static final int TYPE_BATTERY = TemperatureType.BATTERY; 87 public static final int TYPE_SKIN = TemperatureType.SKIN; 88 public static final int TYPE_USB_PORT = TemperatureType.USB_PORT; 89 public static final int TYPE_POWER_AMPLIFIER = TemperatureType.POWER_AMPLIFIER; 90 public static final int TYPE_BCL_VOLTAGE = TemperatureType.BCL_VOLTAGE; 91 public static final int TYPE_BCL_CURRENT = TemperatureType.BCL_CURRENT; 92 public static final int TYPE_BCL_PERCENTAGE = TemperatureType.BCL_PERCENTAGE; 93 public static final int TYPE_NPU = TemperatureType.NPU; 94 95 /** 96 * Verify a valid Temperature type. 97 * 98 * @return true if a Temperature type is valid otherwise false. 99 */ isValidType(@ype int type)100 public static boolean isValidType(@Type int type) { 101 return type >= TYPE_UNKNOWN && type <= TYPE_NPU; 102 } 103 104 /** 105 * Verify a valid throttling status. 106 * 107 * @return true if a status is valid otherwise false. 108 */ isValidStatus(@hrottlingStatus int status)109 public static boolean isValidStatus(@ThrottlingStatus int status) { 110 return status >= THROTTLING_NONE && status <= THROTTLING_SHUTDOWN; 111 } 112 Temperature(float value, @Type int type, @NonNull String name, @ThrottlingStatus int status)113 public Temperature(float value, @Type int type, 114 @NonNull String name, @ThrottlingStatus int status) { 115 Preconditions.checkArgument(isValidType(type), "Invalid Type"); 116 Preconditions.checkArgument(isValidStatus(status) , "Invalid Status"); 117 mValue = value; 118 mType = type; 119 mName = Preconditions.checkStringNotEmpty(name); 120 mStatus = status; 121 } 122 123 /** 124 * Return the Temperature value. 125 * 126 * @return a Temperature value in floating point could be NaN. 127 */ getValue()128 public float getValue() { 129 return mValue; 130 } 131 132 /** 133 * Return the Temperature type. 134 * 135 * @return a Temperature type: TYPE_* 136 */ getType()137 public @Type int getType() { 138 return mType; 139 } 140 141 /** 142 * Return the Temperature name. 143 * 144 * @return a Temperature name as String. 145 */ getName()146 public String getName() { 147 return mName; 148 } 149 150 /** 151 * Return the Temperature throttling status. 152 * 153 * @return a Temperature throttling status: THROTTLING_* 154 */ getStatus()155 public @ThrottlingStatus int getStatus() { 156 return mStatus; 157 } 158 159 @Override toString()160 public String toString() { 161 return "Temperature{mValue=" + mValue + ", mType=" + mType 162 + ", mName=" + mName + ", mStatus=" + mStatus + "}"; 163 } 164 165 @Override hashCode()166 public int hashCode() { 167 int hash = mName.hashCode(); 168 hash = 31 * hash + Float.hashCode(mValue); 169 hash = 31 * hash + mType; 170 hash = 31 * hash + mStatus; 171 return hash; 172 } 173 174 @Override equals(@ullable Object o)175 public boolean equals(@Nullable Object o) { 176 if (!(o instanceof Temperature)) { 177 return false; 178 } 179 Temperature other = (Temperature) o; 180 return other.mValue == mValue && other.mType == mType 181 && other.mName.equals(mName) && other.mStatus == mStatus; 182 } 183 184 @Override writeToParcel(Parcel p, int flags)185 public void writeToParcel(Parcel p, int flags) { 186 p.writeFloat(mValue); 187 p.writeInt(mType); 188 p.writeString(mName); 189 p.writeInt(mStatus); 190 } 191 192 public static final @android.annotation.NonNull Parcelable.Creator<Temperature> CREATOR = 193 new Parcelable.Creator<Temperature>() { 194 @Override 195 public Temperature createFromParcel(Parcel p) { 196 float value = p.readFloat(); 197 int type = p.readInt(); 198 String name = p.readString(); 199 int status = p.readInt(); 200 return new Temperature(value, type, name, status); 201 } 202 203 @Override 204 public Temperature[] newArray(int size) { 205 return new Temperature[size]; 206 } 207 208 }; 209 210 @Override describeContents()211 public int describeContents() { 212 return 0; 213 } 214 } 215