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