Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 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 | |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 21 | import android.hardware.thermal.V2_0.TemperatureType; |
| 22 | import android.hardware.thermal.V2_0.ThrottlingSeverity; |
| 23 | |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 24 | import com.android.internal.util.Preconditions; |
| 25 | |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 26 | import java.lang.annotation.Retention; |
| 27 | import java.lang.annotation.RetentionPolicy; |
| 28 | |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Temperature values used by IThermalService. |
Wei Wang | 37b1754 | 2018-11-12 11:02:09 -0800 | [diff] [blame] | 31 | * |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 32 | * @hide |
| 33 | */ |
Wei Wang | cc91361 | 2018-11-28 23:34:43 -0800 | [diff] [blame] | 34 | public final class Temperature implements Parcelable { |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 35 | /** Temperature value */ |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 36 | private final float mValue; |
| 37 | /** A Temperature type from ThermalHAL */ |
| 38 | private final int mType; |
| 39 | /** Name of this Temperature */ |
| 40 | private final String mName; |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 41 | /** The level of the sensor is currently in throttling */ |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 42 | private final int mStatus; |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 43 | |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 44 | @IntDef(prefix = { "THROTTLING_" }, value = { |
| 45 | THROTTLING_NONE, |
| 46 | THROTTLING_LIGHT, |
| 47 | THROTTLING_MODERATE, |
| 48 | THROTTLING_SEVERE, |
| 49 | THROTTLING_CRITICAL, |
Wei Wang | ed2c902 | 2018-12-04 14:44:36 -0800 | [diff] [blame] | 50 | THROTTLING_EMERGENCY, |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 51 | THROTTLING_SHUTDOWN, |
| 52 | }) |
| 53 | @Retention(RetentionPolicy.SOURCE) |
| 54 | public @interface ThrottlingStatus {} |
| 55 | |
| 56 | /** Keep in sync with hardware/interfaces/thermal/2.0/types.hal */ |
| 57 | public static final int THROTTLING_NONE = ThrottlingSeverity.NONE; |
| 58 | public static final int THROTTLING_LIGHT = ThrottlingSeverity.LIGHT; |
| 59 | public static final int THROTTLING_MODERATE = ThrottlingSeverity.MODERATE; |
| 60 | public static final int THROTTLING_SEVERE = ThrottlingSeverity.SEVERE; |
| 61 | public static final int THROTTLING_CRITICAL = ThrottlingSeverity.CRITICAL; |
Wei Wang | ed2c902 | 2018-12-04 14:44:36 -0800 | [diff] [blame] | 62 | public static final int THROTTLING_EMERGENCY = ThrottlingSeverity.EMERGENCY; |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 63 | public static final int THROTTLING_SHUTDOWN = ThrottlingSeverity.SHUTDOWN; |
| 64 | |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 65 | @IntDef(prefix = { "TYPE_" }, value = { |
| 66 | TYPE_UNKNOWN, |
| 67 | TYPE_CPU, |
| 68 | TYPE_GPU, |
| 69 | TYPE_BATTERY, |
| 70 | TYPE_SKIN, |
| 71 | TYPE_USB_PORT, |
| 72 | TYPE_POWER_AMPLIFIER, |
| 73 | TYPE_BCL_VOLTAGE, |
| 74 | TYPE_BCL_CURRENT, |
| 75 | TYPE_BCL_PERCENTAGE, |
Wei Wang | bc79c4f | 2018-12-10 11:40:40 -0800 | [diff] [blame] | 76 | TYPE_NPU, |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 77 | }) |
| 78 | @Retention(RetentionPolicy.SOURCE) |
| 79 | public @interface Type {} |
| 80 | |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 81 | /** Keep in sync with hardware/interfaces/thermal/2.0/types.hal */ |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 82 | public static final int TYPE_UNKNOWN = TemperatureType.UNKNOWN; |
| 83 | public static final int TYPE_CPU = TemperatureType.CPU; |
| 84 | public static final int TYPE_GPU = TemperatureType.GPU; |
| 85 | public static final int TYPE_BATTERY = TemperatureType.BATTERY; |
| 86 | public static final int TYPE_SKIN = TemperatureType.SKIN; |
| 87 | public static final int TYPE_USB_PORT = TemperatureType.USB_PORT; |
| 88 | public static final int TYPE_POWER_AMPLIFIER = TemperatureType.POWER_AMPLIFIER; |
| 89 | public static final int TYPE_BCL_VOLTAGE = TemperatureType.BCL_VOLTAGE; |
| 90 | public static final int TYPE_BCL_CURRENT = TemperatureType.BCL_CURRENT; |
| 91 | public static final int TYPE_BCL_PERCENTAGE = TemperatureType.BCL_PERCENTAGE; |
Wei Wang | bc79c4f | 2018-12-10 11:40:40 -0800 | [diff] [blame] | 92 | public static final int TYPE_NPU = TemperatureType.NPU; |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 95 | * Verify a valid Temperature type. |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 96 | * |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 97 | * @return true if a Temperature type is valid otherwise false. |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 98 | */ |
Wei Wang | 37b1754 | 2018-11-12 11:02:09 -0800 | [diff] [blame] | 99 | public static boolean isValidType(@Type int type) { |
Wei Wang | bc79c4f | 2018-12-10 11:40:40 -0800 | [diff] [blame] | 100 | return type >= TYPE_UNKNOWN && type <= TYPE_NPU; |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Wei Wang | 37b1754 | 2018-11-12 11:02:09 -0800 | [diff] [blame] | 103 | /** |
| 104 | * Verify a valid throttling status. |
| 105 | * |
| 106 | * @return true if a status is valid otherwise false. |
| 107 | */ |
| 108 | public static boolean isValidStatus(@ThrottlingStatus int status) { |
| 109 | return status >= THROTTLING_NONE && status <= THROTTLING_SHUTDOWN; |
| 110 | } |
| 111 | |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 112 | public Temperature(float value, @Type int type, |
| 113 | @NonNull String name, @ThrottlingStatus int status) { |
| 114 | Preconditions.checkArgument(isValidType(type), "Invalid Type"); |
| 115 | Preconditions.checkArgument(isValidStatus(status) , "Invalid Status"); |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 116 | mValue = value; |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 117 | mType = type; |
| 118 | mName = Preconditions.checkStringNotEmpty(name); |
| 119 | mStatus = status; |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 123 | * Return the Temperature value. |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 124 | * |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 125 | * @return a Temperature value in floating point could be NaN. |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 126 | */ |
| 127 | public float getValue() { |
| 128 | return mValue; |
| 129 | } |
| 130 | |
| 131 | /** |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 132 | * Return the Temperature type. |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 133 | * |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 134 | * @return a Temperature type: TYPE_* |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 135 | */ |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 136 | public @Type int getType() { |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 137 | return mType; |
| 138 | } |
| 139 | |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 140 | /** |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 141 | * Return the Temperature name. |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 142 | * |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 143 | * @return a Temperature name as String. |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 144 | */ |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 145 | public String getName() { |
| 146 | return mName; |
| 147 | } |
| 148 | |
| 149 | /** |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 150 | * Return the Temperature throttling status. |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 151 | * |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 152 | * @return a Temperature throttling status: THROTTLING_* |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 153 | */ |
| 154 | public @ThrottlingStatus int getStatus() { |
| 155 | return mStatus; |
| 156 | } |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 157 | |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 158 | @Override |
| 159 | public String toString() { |
| 160 | return "Temperature{mValue=" + mValue + ", mType=" + mType |
| 161 | + ", mName=" + mName + ", mStatus=" + mStatus + "}"; |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 164 | @Override |
| 165 | public int hashCode() { |
| 166 | int hash = mName.hashCode(); |
| 167 | hash = 31 * hash + Float.hashCode(mValue); |
| 168 | hash = 31 * hash + mType; |
| 169 | hash = 31 * hash + mStatus; |
| 170 | return hash; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public boolean equals(Object o) { |
| 175 | if (!(o instanceof Temperature)) { |
| 176 | return false; |
| 177 | } |
| 178 | Temperature other = (Temperature) o; |
| 179 | return other.mValue == mValue && other.mType == mType |
| 180 | && other.mName.equals(mName) && other.mStatus == mStatus; |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void writeToParcel(Parcel p, int flags) { |
| 185 | p.writeFloat(mValue); |
| 186 | p.writeInt(mType); |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 187 | p.writeString(mName); |
| 188 | p.writeInt(mStatus); |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 191 | public static final @android.annotation.NonNull Parcelable.Creator<Temperature> CREATOR = |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 192 | new Parcelable.Creator<Temperature>() { |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 193 | @Override |
| 194 | public Temperature createFromParcel(Parcel p) { |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 195 | float value = p.readFloat(); |
| 196 | int type = p.readInt(); |
| 197 | String name = p.readString(); |
| 198 | int status = p.readInt(); |
| 199 | return new Temperature(value, type, name, status); |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 200 | } |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 201 | |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 202 | @Override |
| 203 | public Temperature[] newArray(int size) { |
| 204 | return new Temperature[size]; |
| 205 | } |
Wei Wang | 38e5bd7 | 2019-04-03 14:58:42 -0700 | [diff] [blame] | 206 | |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 207 | }; |
Todd Poynor | dd05582 | 2017-05-25 17:53:21 -0700 | [diff] [blame] | 208 | |
| 209 | @Override |
| 210 | public int describeContents() { |
| 211 | return 0; |
| 212 | } |
| 213 | } |