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 | |
| 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 | |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 55 | /** Calling app context. */ |
| 56 | private final Context mContext; |
| 57 | |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 58 | /** @hide */ |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 59 | public HardwarePropertiesManager(Context context, IHardwarePropertiesManager service) { |
| 60 | mContext = context; |
| 61 | mService = service; |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 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. |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 73 | * @throws SecurityException if a non profile or device owner tries to call this method. |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 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: |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 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 | } |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 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. |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 96 | * |
| 97 | * @throws SecurityException if a non profile or device owner tries to call this method. |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 98 | */ |
| 99 | public @NonNull CpuUsageInfo[] getCpuUsages() { |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 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 | } |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Return an array of fan speeds in RPM. |
| 110 | * |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 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. |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 115 | */ |
| 116 | public @NonNull float[] getFanSpeeds() { |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 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 | } |
Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame] | 123 | } |
| 124 | } |