HardwarePropertiesManager.java revision f8754ac2127f8adf40bd4731f39b16340acdb2f7
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 @Retention(RetentionPolicy.SOURCE) 37 @IntDef({ 38 DEVICE_TEMPERATURE_CPU, DEVICE_TEMPERATURE_GPU, DEVICE_TEMPERATURE_BATTERY 39 }) 40 public @interface DeviceTemperatureType {} 41 42 /** 43 * Device temperature types. These must match the values in 44 * frameworks/native/include/hardwareproperties/HardwarePropertiesManager.h 45 */ 46 /** Temperature of CPUs in Celsius. */ 47 public static final int DEVICE_TEMPERATURE_CPU = 0; 48 49 /** Temperature of GPUs in Celsius. */ 50 public static final int DEVICE_TEMPERATURE_GPU = 1; 51 52 /** Temperature of battery in Celsius. */ 53 public static final int DEVICE_TEMPERATURE_BATTERY = 2; 54 55 /** Calling app context. */ 56 private final Context mContext; 57 58 /** @hide */ 59 public HardwarePropertiesManager(Context context, IHardwarePropertiesManager service) { 60 mContext = context; 61 mService = service; 62 } 63 64 /** 65 * Return an array of device temperatures in Celsius. 66 * 67 * @param type type of requested device temperature, one of {@link #DEVICE_TEMPERATURE_CPU}, 68 * {@link #DEVICE_TEMPERATURE_GPU} or {@link #DEVICE_TEMPERATURE_BATTERY}. 69 * @return an array of requested float device temperatures. 70 * Empty if platform doesn't provide the queried temperature. 71 * 72 * @throws IllegalArgumentException if an incorrect temperature type is queried. 73 * @throws SecurityException if a non profile or device owner tries to call this method. 74 */ 75 public @NonNull float[] getDeviceTemperatures(@DeviceTemperatureType int type) { 76 switch (type) { 77 case DEVICE_TEMPERATURE_CPU: 78 case DEVICE_TEMPERATURE_GPU: 79 case DEVICE_TEMPERATURE_BATTERY: 80 try { 81 return mService.getDeviceTemperatures(mContext.getOpPackageName(), type); 82 } catch (RemoteException e) { 83 Log.w(TAG, "Could not get device temperatures", e); 84 return new float[0]; 85 } 86 default: 87 throw new IllegalArgumentException(); 88 } 89 } 90 91 /** 92 * Return an array of CPU usage info for each core. 93 * 94 * @return an array of {@link android.os.CpuUsageInfo} for each core. 95 * Empty if CPU usage is not supported on this system. 96 * 97 * @throws SecurityException if a non profile or device owner tries to call this method. 98 */ 99 public @NonNull CpuUsageInfo[] getCpuUsages() { 100 try { 101 return mService.getCpuUsages(mContext.getOpPackageName()); 102 } catch (RemoteException e) { 103 Log.w(TAG, "Could not get CPU usages", e); 104 return new CpuUsageInfo[0]; 105 } 106 } 107 108 /** 109 * Return an array of fan speeds in RPM. 110 * 111 * @return an array of float fan speeds in RPM. Empty if there are no fans or fan speed is not 112 * supported on this system. 113 * 114 * @throws SecurityException if a non profile or device owner tries to call this method. 115 */ 116 public @NonNull float[] getFanSpeeds() { 117 try { 118 return mService.getFanSpeeds(mContext.getOpPackageName()); 119 } catch (RemoteException e) { 120 Log.w(TAG, "Could not get fan speeds", e); 121 return new float[0]; 122 } 123 } 124} 125