blob: c72a6481ebedbbdb33fdcde16f75576d9b5c7733 [file] [log] [blame]
Polina Bondarenko965ecbb2015-11-13 15:34:28 +01001/*
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;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010020import android.content.Context;
21import android.util.Log;
22
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010023import java.lang.annotation.Retention;
24import java.lang.annotation.RetentionPolicy;
25
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010026/**
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
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010034 private final IHardwarePropertiesManager mService;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010035
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 Bondarenkof8754ac2016-02-12 20:38:23 +010055 /** Calling app context. */
56 private final Context mContext;
57
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010058 /** @hide */
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010059 public HardwarePropertiesManager(Context context, IHardwarePropertiesManager service) {
60 mContext = context;
61 mService = service;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010062 }
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 Bondarenkof8754ac2016-02-12 20:38:23 +010073 * @throws SecurityException if a non profile or device owner tries to call this method.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010074 */
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 Bondarenkof8754ac2016-02-12 20:38:23 +010080 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 Bondarenko965ecbb2015-11-13 15:34:28 +010086 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 Bondarenkof8754ac2016-02-12 20:38:23 +010096 *
97 * @throws SecurityException if a non profile or device owner tries to call this method.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010098 */
99 public @NonNull CpuUsageInfo[] getCpuUsages() {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100100 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 Bondarenko965ecbb2015-11-13 15:34:28 +0100106 }
107
108 /**
109 * Return an array of fan speeds in RPM.
110 *
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100111 * @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 Bondarenko965ecbb2015-11-13 15:34:28 +0100115 */
116 public @NonNull float[] getFanSpeeds() {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100117 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 Bondarenko965ecbb2015-11-13 15:34:28 +0100123 }
124}