Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | package android.os; |
| 17 | |
| 18 | import android.annotation.IntDef; |
| 19 | import android.annotation.NonNull; |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 20 | import android.content.Context; |
Andreas Huber | fb7accf | 2016-10-24 12:36:23 -0700 | [diff] [blame^] | 21 | import android.hardware.thermal.V1_0.Constants; |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 22 | import android.util.Log; |
| 23 | |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 24 | import java.lang.annotation.Retention; |
| 25 | import java.lang.annotation.RetentionPolicy; |
| 26 | |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 27 | /** |
| 28 | * The HardwarePropertiesManager class provides a mechanism of accessing hardware state of a |
| 29 | * device: CPU, GPU and battery temperatures, CPU usage per core, fan speed, etc. |
| 30 | */ |
| 31 | public class HardwarePropertiesManager { |
| 32 | |
| 33 | private static final String TAG = HardwarePropertiesManager.class.getSimpleName(); |
| 34 | |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 35 | private final IHardwarePropertiesManager mService; |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 36 | |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 37 | /** |
| 38 | * @hide |
| 39 | */ |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 40 | @Retention(RetentionPolicy.SOURCE) |
| 41 | @IntDef({ |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 42 | DEVICE_TEMPERATURE_CPU, DEVICE_TEMPERATURE_GPU, DEVICE_TEMPERATURE_BATTERY, |
| 43 | DEVICE_TEMPERATURE_SKIN |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 44 | }) |
| 45 | public @interface DeviceTemperatureType {} |
| 46 | |
| 47 | /** |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 48 | * @hide |
| 49 | */ |
| 50 | @Retention(RetentionPolicy.SOURCE) |
| 51 | @IntDef({ |
Ruben Brunk | 946ef64 | 2016-03-25 13:45:42 -0700 | [diff] [blame] | 52 | TEMPERATURE_CURRENT, TEMPERATURE_THROTTLING, TEMPERATURE_SHUTDOWN, |
| 53 | TEMPERATURE_THROTTLING_BELOW_VR_MIN |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 54 | }) |
| 55 | public @interface TemperatureSource {} |
| 56 | |
| 57 | /** |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 58 | * Device temperature types. These must match the values in |
| 59 | * frameworks/native/include/hardwareproperties/HardwarePropertiesManager.h |
Andreas Huber | fb7accf | 2016-10-24 12:36:23 -0700 | [diff] [blame^] | 60 | * TODO(b/32022261) Remove this comment. |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 61 | */ |
| 62 | /** Temperature of CPUs in Celsius. */ |
Andreas Huber | fb7accf | 2016-10-24 12:36:23 -0700 | [diff] [blame^] | 63 | public static final int DEVICE_TEMPERATURE_CPU = Constants.TemperatureType.CPU; |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 64 | |
| 65 | /** Temperature of GPUs in Celsius. */ |
Andreas Huber | fb7accf | 2016-10-24 12:36:23 -0700 | [diff] [blame^] | 66 | public static final int DEVICE_TEMPERATURE_GPU = Constants.TemperatureType.GPU; |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 67 | |
| 68 | /** Temperature of battery in Celsius. */ |
Andreas Huber | fb7accf | 2016-10-24 12:36:23 -0700 | [diff] [blame^] | 69 | public static final int DEVICE_TEMPERATURE_BATTERY = Constants.TemperatureType.BATTERY; |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 70 | |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 71 | /** Temperature of device skin in Celsius. */ |
Andreas Huber | fb7accf | 2016-10-24 12:36:23 -0700 | [diff] [blame^] | 72 | public static final int DEVICE_TEMPERATURE_SKIN = Constants.TemperatureType.SKIN; |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 73 | |
| 74 | /** Get current temperature. */ |
| 75 | public static final int TEMPERATURE_CURRENT = 0; |
| 76 | |
| 77 | /** Get throttling temperature threshold. */ |
| 78 | public static final int TEMPERATURE_THROTTLING = 1; |
| 79 | |
| 80 | /** Get shutdown temperature threshold. */ |
| 81 | public static final int TEMPERATURE_SHUTDOWN = 2; |
| 82 | |
Ruben Brunk | 946ef64 | 2016-03-25 13:45:42 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Get throttling temperature threshold above which minimum clockrates for VR mode will not be |
| 85 | * met. |
| 86 | */ |
| 87 | public static final int TEMPERATURE_THROTTLING_BELOW_VR_MIN = 3; |
| 88 | |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 89 | /** Undefined temperature constant. */ |
| 90 | public static final float UNDEFINED_TEMPERATURE = -Float.MAX_VALUE; |
| 91 | |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 92 | /** Calling app context. */ |
| 93 | private final Context mContext; |
| 94 | |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 95 | /** @hide */ |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 96 | public HardwarePropertiesManager(Context context, IHardwarePropertiesManager service) { |
| 97 | mContext = context; |
| 98 | mService = service; |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return an array of device temperatures in Celsius. |
| 103 | * |
| 104 | * @param type type of requested device temperature, one of {@link #DEVICE_TEMPERATURE_CPU}, |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 105 | * {@link #DEVICE_TEMPERATURE_GPU}, {@link #DEVICE_TEMPERATURE_BATTERY} or {@link |
| 106 | * #DEVICE_TEMPERATURE_SKIN}. |
| 107 | * @param source source of requested device temperature, one of {@link #TEMPERATURE_CURRENT}, |
Ruben Brunk | 946ef64 | 2016-03-25 13:45:42 -0700 | [diff] [blame] | 108 | * {@link #TEMPERATURE_THROTTLING}, {@link #TEMPERATURE_THROTTLING_BELOW_VR_MIN} or |
| 109 | * {@link #TEMPERATURE_SHUTDOWN}. |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 110 | * @return an array of requested float device temperatures. Temperature equals to |
| 111 | * {@link #UNDEFINED_TEMPERATURE} if undefined. |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 112 | * Empty if platform doesn't provide the queried temperature. |
| 113 | * |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 114 | * @throws SecurityException if something other than the profile or device owner, or the |
| 115 | * current VR service tries to retrieve information provided by this service. |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 116 | */ |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 117 | public @NonNull float[] getDeviceTemperatures(@DeviceTemperatureType int type, |
| 118 | @TemperatureSource int source) { |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 119 | switch (type) { |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 120 | case DEVICE_TEMPERATURE_CPU: |
| 121 | case DEVICE_TEMPERATURE_GPU: |
| 122 | case DEVICE_TEMPERATURE_BATTERY: |
| 123 | case DEVICE_TEMPERATURE_SKIN: |
| 124 | switch (source) { |
| 125 | case TEMPERATURE_CURRENT: |
| 126 | case TEMPERATURE_THROTTLING: |
| 127 | case TEMPERATURE_SHUTDOWN: |
Ruben Brunk | 946ef64 | 2016-03-25 13:45:42 -0700 | [diff] [blame] | 128 | case TEMPERATURE_THROTTLING_BELOW_VR_MIN: |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 129 | try { |
| 130 | return mService.getDeviceTemperatures(mContext.getOpPackageName(), type, |
| 131 | source); |
| 132 | } catch (RemoteException e) { |
| 133 | throw e.rethrowFromSystemServer(); |
| 134 | } |
| 135 | default: |
| 136 | Log.w(TAG, "Unknown device temperature source."); |
| 137 | return new float[0]; |
| 138 | } |
| 139 | default: |
| 140 | Log.w(TAG, "Unknown device temperature type."); |
| 141 | return new float[0]; |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Return an array of CPU usage info for each core. |
| 147 | * |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 148 | * @return an array of {@link android.os.CpuUsageInfo} for each core. Return {@code null} for |
| 149 | * each unplugged core. |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 150 | * Empty if CPU usage is not supported on this system. |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 151 | * |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 152 | * @throws SecurityException if something other than the profile or device owner, or the |
| 153 | * current VR service tries to retrieve information provided by this service. |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 154 | */ |
| 155 | public @NonNull CpuUsageInfo[] getCpuUsages() { |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 156 | try { |
| 157 | return mService.getCpuUsages(mContext.getOpPackageName()); |
| 158 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 159 | throw e.rethrowFromSystemServer(); |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 160 | } |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Return an array of fan speeds in RPM. |
| 165 | * |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 166 | * @return an array of float fan speeds in RPM. Empty if there are no fans or fan speed is not |
| 167 | * supported on this system. |
| 168 | * |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 169 | * @throws SecurityException if something other than the profile or device owner, or the |
| 170 | * current VR service tries to retrieve information provided by this service. |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 171 | */ |
| 172 | public @NonNull float[] getFanSpeeds() { |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 173 | try { |
| 174 | return mService.getFanSpeeds(mContext.getOpPackageName()); |
| 175 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 176 | throw e.rethrowFromSystemServer(); |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 177 | } |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 178 | } |
| 179 | } |