HardwarePropertiesManager.java revision 946ef6417b0b9ba18d509843146e5a1a28ed1049
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 */ 16package android.os; 17 18import android.annotation.IntDef; 19import android.annotation.NonNull; 20import android.content.Context; 21import android.util.Log; 22 23import java.lang.annotation.Retention; 24import java.lang.annotation.RetentionPolicy; 25 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 */ 30public class HardwarePropertiesManager { 31 32 private static final String TAG = HardwarePropertiesManager.class.getSimpleName(); 33 34 private final IHardwarePropertiesManager mService; 35 36 /** 37 * @hide 38 */ 39 @Retention(RetentionPolicy.SOURCE) 40 @IntDef({ 41 DEVICE_TEMPERATURE_CPU, DEVICE_TEMPERATURE_GPU, DEVICE_TEMPERATURE_BATTERY, 42 DEVICE_TEMPERATURE_SKIN 43 }) 44 public @interface DeviceTemperatureType {} 45 46 /** 47 * @hide 48 */ 49 @Retention(RetentionPolicy.SOURCE) 50 @IntDef({ 51 TEMPERATURE_CURRENT, TEMPERATURE_THROTTLING, TEMPERATURE_SHUTDOWN, 52 TEMPERATURE_THROTTLING_BELOW_VR_MIN 53 }) 54 public @interface TemperatureSource {} 55 56 /** 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 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 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 87 /** Undefined temperature constant. */ 88 public static final float UNDEFINED_TEMPERATURE = -Float.MAX_VALUE; 89 90 /** Calling app context. */ 91 private final Context mContext; 92 93 /** @hide */ 94 public HardwarePropertiesManager(Context context, IHardwarePropertiesManager service) { 95 mContext = context; 96 mService = service; 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}, 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}, 106 * {@link #TEMPERATURE_THROTTLING}, {@link #TEMPERATURE_THROTTLING_BELOW_VR_MIN} or 107 * {@link #TEMPERATURE_SHUTDOWN}. 108 * @return an array of requested float device temperatures. Temperature equals to 109 * {@link #UNDEFINED_TEMPERATURE} if undefined. 110 * Empty if platform doesn't provide the queried temperature. 111 * 112 * @throws SecurityException if a non profile or device owner tries to call this method. 113 */ 114 public @NonNull float[] getDeviceTemperatures(@DeviceTemperatureType int type, 115 @TemperatureSource int source) { 116 switch (type) { 117 case DEVICE_TEMPERATURE_CPU: 118 case DEVICE_TEMPERATURE_GPU: 119 case DEVICE_TEMPERATURE_BATTERY: 120 case DEVICE_TEMPERATURE_SKIN: 121 switch (source) { 122 case TEMPERATURE_CURRENT: 123 case TEMPERATURE_THROTTLING: 124 case TEMPERATURE_SHUTDOWN: 125 case TEMPERATURE_THROTTLING_BELOW_VR_MIN: 126 try { 127 return mService.getDeviceTemperatures(mContext.getOpPackageName(), type, 128 source); 129 } catch (RemoteException e) { 130 throw e.rethrowFromSystemServer(); 131 } 132 default: 133 Log.w(TAG, "Unknown device temperature source."); 134 return new float[0]; 135 } 136 default: 137 Log.w(TAG, "Unknown device temperature type."); 138 return new float[0]; 139 } 140 } 141 142 /** 143 * Return an array of CPU usage info for each core. 144 * 145 * @return an array of {@link android.os.CpuUsageInfo} for each core. Return {@code null} for 146 * each unplugged core. 147 * Empty if CPU usage is not supported on this system. 148 * 149 * @throws SecurityException if a non profile or device owner tries to call this method. 150 */ 151 public @NonNull CpuUsageInfo[] getCpuUsages() { 152 try { 153 return mService.getCpuUsages(mContext.getOpPackageName()); 154 } catch (RemoteException e) { 155 throw e.rethrowFromSystemServer(); 156 } 157 } 158 159 /** 160 * Return an array of fan speeds in RPM. 161 * 162 * @return an array of float fan speeds in RPM. Empty if there are no fans or fan speed is not 163 * supported on this system. 164 * 165 * @throws SecurityException if a non profile or device owner tries to call this method. 166 */ 167 public @NonNull float[] getFanSpeeds() { 168 try { 169 return mService.getFanSpeeds(mContext.getOpPackageName()); 170 } catch (RemoteException e) { 171 throw e.rethrowFromSystemServer(); 172 } 173 } 174} 175