HardwarePropertiesManager.java revision 965ecbbb3eb19007690c333ab3aa05e380deb633
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 java.lang.annotation.Retention; 21import java.lang.annotation.RetentionPolicy; 22 23 24/** 25 * The HardwarePropertiesManager class provides a mechanism of accessing hardware state of a 26 * device: CPU, GPU and battery temperatures, CPU usage per core, fan speed, etc. 27 */ 28public class HardwarePropertiesManager { 29 30 private static final String TAG = HardwarePropertiesManager.class.getSimpleName(); 31 32 private static native void nativeInit(); 33 34 private static native float[] nativeGetFanSpeeds(); 35 private static native float[] nativeGetDeviceTemperatures(int type); 36 private static native CpuUsageInfo[] nativeGetCpuUsages(); 37 38 @Retention(RetentionPolicy.SOURCE) 39 @IntDef({ 40 DEVICE_TEMPERATURE_CPU, DEVICE_TEMPERATURE_GPU, DEVICE_TEMPERATURE_BATTERY 41 }) 42 public @interface DeviceTemperatureType {} 43 44 /** 45 * Device temperature types. These must match the values in 46 * frameworks/native/include/hardwareproperties/HardwarePropertiesManager.h 47 */ 48 /** Temperature of CPUs in Celsius. */ 49 public static final int DEVICE_TEMPERATURE_CPU = 0; 50 51 /** Temperature of GPUs in Celsius. */ 52 public static final int DEVICE_TEMPERATURE_GPU = 1; 53 54 /** Temperature of battery in Celsius. */ 55 public static final int DEVICE_TEMPERATURE_BATTERY = 2; 56 57 /** @hide */ 58 public HardwarePropertiesManager() { 59 nativeInit(); 60 } 61 62 /** 63 * Return an array of device temperatures in Celsius. 64 * 65 * @param type type of requested device temperature, one of {@link #DEVICE_TEMPERATURE_CPU}, 66 * {@link #DEVICE_TEMPERATURE_GPU} or {@link #DEVICE_TEMPERATURE_BATTERY}. 67 * @return an array of requested float device temperatures. 68 * Empty if platform doesn't provide the queried temperature. 69 * 70 * @throws IllegalArgumentException if an incorrect temperature type is queried. 71 */ 72 public @NonNull float[] getDeviceTemperatures(@DeviceTemperatureType int type) { 73 switch (type) { 74 case DEVICE_TEMPERATURE_CPU: 75 case DEVICE_TEMPERATURE_GPU: 76 case DEVICE_TEMPERATURE_BATTERY: 77 return nativeGetDeviceTemperatures(type); 78 default: 79 throw new IllegalArgumentException(); 80 } 81 } 82 83 /** 84 * Return an array of CPU usage info for each core. 85 * 86 * @return an array of {@link android.os.CpuUsageInfo} for each core. 87 * Empty if CPU usage is not supported on this system. 88 */ 89 public @NonNull CpuUsageInfo[] getCpuUsages() { 90 return nativeGetCpuUsages(); 91 } 92 93 /** 94 * Return an array of fan speeds in RPM. 95 * 96 * @return an arrat of float fan speeds. Empty if there is no fans or fan speed 97 * not supported on this system. 98 */ 99 public @NonNull float[] getFanSpeeds() { 100 return nativeGetFanSpeeds(); 101 } 102} 103