blob: 9d362d61d9d4b890533b002069c73d420203201c [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
Polina Bondarenkoeb845522016-03-01 14:39:48 +010036 /**
37 * @hide
38 */
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010039 @Retention(RetentionPolicy.SOURCE)
40 @IntDef({
Polina Bondarenkoeb845522016-03-01 14:39:48 +010041 DEVICE_TEMPERATURE_CPU, DEVICE_TEMPERATURE_GPU, DEVICE_TEMPERATURE_BATTERY,
42 DEVICE_TEMPERATURE_SKIN
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010043 })
44 public @interface DeviceTemperatureType {}
45
46 /**
Polina Bondarenkoeb845522016-03-01 14:39:48 +010047 * @hide
48 */
49 @Retention(RetentionPolicy.SOURCE)
50 @IntDef({
Ruben Brunk946ef642016-03-25 13:45:42 -070051 TEMPERATURE_CURRENT, TEMPERATURE_THROTTLING, TEMPERATURE_SHUTDOWN,
52 TEMPERATURE_THROTTLING_BELOW_VR_MIN
Polina Bondarenkoeb845522016-03-01 14:39:48 +010053 })
54 public @interface TemperatureSource {}
55
56 /**
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010057 * Device temperature types. These must match the values in
58 * frameworks/native/include/hardwareproperties/HardwarePropertiesManager.h
59 */
60 /** Temperature of CPUs in Celsius. */
61 public static final int DEVICE_TEMPERATURE_CPU = 0;
62
63 /** Temperature of GPUs in Celsius. */
64 public static final int DEVICE_TEMPERATURE_GPU = 1;
65
66 /** Temperature of battery in Celsius. */
67 public static final int DEVICE_TEMPERATURE_BATTERY = 2;
68
Polina Bondarenkoeb845522016-03-01 14:39:48 +010069 /** Temperature of device skin in Celsius. */
70 public static final int DEVICE_TEMPERATURE_SKIN = 3;
71
72 /** Get current temperature. */
73 public static final int TEMPERATURE_CURRENT = 0;
74
75 /** Get throttling temperature threshold. */
76 public static final int TEMPERATURE_THROTTLING = 1;
77
78 /** Get shutdown temperature threshold. */
79 public static final int TEMPERATURE_SHUTDOWN = 2;
80
Ruben Brunk946ef642016-03-25 13:45:42 -070081 /**
82 * Get throttling temperature threshold above which minimum clockrates for VR mode will not be
83 * met.
84 */
85 public static final int TEMPERATURE_THROTTLING_BELOW_VR_MIN = 3;
86
Polina Bondarenkoeb845522016-03-01 14:39:48 +010087 /** Undefined temperature constant. */
88 public static final float UNDEFINED_TEMPERATURE = -Float.MAX_VALUE;
89
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010090 /** Calling app context. */
91 private final Context mContext;
92
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010093 /** @hide */
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010094 public HardwarePropertiesManager(Context context, IHardwarePropertiesManager service) {
95 mContext = context;
96 mService = service;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010097 }
98
99 /**
100 * Return an array of device temperatures in Celsius.
101 *
102 * @param type type of requested device temperature, one of {@link #DEVICE_TEMPERATURE_CPU},
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100103 * {@link #DEVICE_TEMPERATURE_GPU}, {@link #DEVICE_TEMPERATURE_BATTERY} or {@link
104 * #DEVICE_TEMPERATURE_SKIN}.
105 * @param source source of requested device temperature, one of {@link #TEMPERATURE_CURRENT},
Ruben Brunk946ef642016-03-25 13:45:42 -0700106 * {@link #TEMPERATURE_THROTTLING}, {@link #TEMPERATURE_THROTTLING_BELOW_VR_MIN} or
107 * {@link #TEMPERATURE_SHUTDOWN}.
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100108 * @return an array of requested float device temperatures. Temperature equals to
109 * {@link #UNDEFINED_TEMPERATURE} if undefined.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100110 * Empty if platform doesn't provide the queried temperature.
111 *
Ruben Brunk8f1d5cb2016-03-22 18:08:41 -0700112 * @throws SecurityException if something other than the profile or device owner, or the
113 * current VR service tries to retrieve information provided by this service.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100114 */
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100115 public @NonNull float[] getDeviceTemperatures(@DeviceTemperatureType int type,
116 @TemperatureSource int source) {
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100117 switch (type) {
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100118 case DEVICE_TEMPERATURE_CPU:
119 case DEVICE_TEMPERATURE_GPU:
120 case DEVICE_TEMPERATURE_BATTERY:
121 case DEVICE_TEMPERATURE_SKIN:
122 switch (source) {
123 case TEMPERATURE_CURRENT:
124 case TEMPERATURE_THROTTLING:
125 case TEMPERATURE_SHUTDOWN:
Ruben Brunk946ef642016-03-25 13:45:42 -0700126 case TEMPERATURE_THROTTLING_BELOW_VR_MIN:
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100127 try {
128 return mService.getDeviceTemperatures(mContext.getOpPackageName(), type,
129 source);
130 } catch (RemoteException e) {
131 throw e.rethrowFromSystemServer();
132 }
133 default:
134 Log.w(TAG, "Unknown device temperature source.");
135 return new float[0];
136 }
137 default:
138 Log.w(TAG, "Unknown device temperature type.");
139 return new float[0];
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100140 }
141 }
142
143 /**
144 * Return an array of CPU usage info for each core.
145 *
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100146 * @return an array of {@link android.os.CpuUsageInfo} for each core. Return {@code null} for
147 * each unplugged core.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100148 * Empty if CPU usage is not supported on this system.
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100149 *
Ruben Brunk8f1d5cb2016-03-22 18:08:41 -0700150 * @throws SecurityException if something other than the profile or device owner, or the
151 * current VR service tries to retrieve information provided by this service.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100152 */
153 public @NonNull CpuUsageInfo[] getCpuUsages() {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100154 try {
155 return mService.getCpuUsages(mContext.getOpPackageName());
156 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700157 throw e.rethrowFromSystemServer();
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100158 }
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100159 }
160
161 /**
162 * Return an array of fan speeds in RPM.
163 *
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100164 * @return an array of float fan speeds in RPM. Empty if there are no fans or fan speed is not
165 * supported on this system.
166 *
Ruben Brunk8f1d5cb2016-03-22 18:08:41 -0700167 * @throws SecurityException if something other than the profile or device owner, or the
168 * current VR service tries to retrieve information provided by this service.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100169 */
170 public @NonNull float[] getFanSpeeds() {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100171 try {
172 return mService.getFanSpeeds(mContext.getOpPackageName());
173 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700174 throw e.rethrowFromSystemServer();
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100175 }
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100176 }
177}