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