blob: 9ca1dcd702426e6af71e0b7a5f8baa74b186eb06 [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 *
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010072 * @throws SecurityException if a non profile or device owner tries to call this method.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010073 */
74 public @NonNull float[] getDeviceTemperatures(@DeviceTemperatureType int type) {
75 switch (type) {
76 case DEVICE_TEMPERATURE_CPU:
77 case DEVICE_TEMPERATURE_GPU:
78 case DEVICE_TEMPERATURE_BATTERY:
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010079 try {
80 return mService.getDeviceTemperatures(mContext.getOpPackageName(), type);
81 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -070082 throw e.rethrowFromSystemServer();
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010083 }
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010084 default:
Polina Bondarenko725844d2016-02-26 12:07:50 +010085 Log.w(TAG, "Unknown device temperature type.");
86 return new float[0];
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010087 }
88 }
89
90 /**
91 * Return an array of CPU usage info for each core.
92 *
93 * @return an array of {@link android.os.CpuUsageInfo} for each core.
94 * Empty if CPU usage is not supported on this system.
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010095 *
96 * @throws SecurityException if a non profile or device owner tries to call this method.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010097 */
98 public @NonNull CpuUsageInfo[] getCpuUsages() {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010099 try {
100 return mService.getCpuUsages(mContext.getOpPackageName());
101 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700102 throw e.rethrowFromSystemServer();
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100103 }
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100104 }
105
106 /**
107 * Return an array of fan speeds in RPM.
108 *
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100109 * @return an array of float fan speeds in RPM. Empty if there are no fans or fan speed is not
110 * supported on this system.
111 *
112 * @throws SecurityException if a non profile or device owner tries to call this method.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100113 */
114 public @NonNull float[] getFanSpeeds() {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100115 try {
116 return mService.getFanSpeeds(mContext.getOpPackageName());
117 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700118 throw e.rethrowFromSystemServer();
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100119 }
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100120 }
121}