blob: 6dec0dc1c3cf10c73b97626e0a1561de53c62b4c [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;
Andreas Huberfb7accf2016-10-24 12:36:23 -070021import android.hardware.thermal.V1_0.Constants;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010022import android.util.Log;
23
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010024import java.lang.annotation.Retention;
25import java.lang.annotation.RetentionPolicy;
26
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010027/**
28 * The HardwarePropertiesManager class provides a mechanism of accessing hardware state of a
29 * device: CPU, GPU and battery temperatures, CPU usage per core, fan speed, etc.
30 */
31public class HardwarePropertiesManager {
32
33 private static final String TAG = HardwarePropertiesManager.class.getSimpleName();
34
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010035 private final IHardwarePropertiesManager mService;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010036
Polina Bondarenkoeb845522016-03-01 14:39:48 +010037 /**
38 * @hide
39 */
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010040 @Retention(RetentionPolicy.SOURCE)
41 @IntDef({
Polina Bondarenkoeb845522016-03-01 14:39:48 +010042 DEVICE_TEMPERATURE_CPU, DEVICE_TEMPERATURE_GPU, DEVICE_TEMPERATURE_BATTERY,
43 DEVICE_TEMPERATURE_SKIN
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010044 })
45 public @interface DeviceTemperatureType {}
46
47 /**
Polina Bondarenkoeb845522016-03-01 14:39:48 +010048 * @hide
49 */
50 @Retention(RetentionPolicy.SOURCE)
51 @IntDef({
Ruben Brunk946ef642016-03-25 13:45:42 -070052 TEMPERATURE_CURRENT, TEMPERATURE_THROTTLING, TEMPERATURE_SHUTDOWN,
53 TEMPERATURE_THROTTLING_BELOW_VR_MIN
Polina Bondarenkoeb845522016-03-01 14:39:48 +010054 })
55 public @interface TemperatureSource {}
56
57 /**
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010058 * Device temperature types. These must match the values in
59 * frameworks/native/include/hardwareproperties/HardwarePropertiesManager.h
Andreas Huberfb7accf2016-10-24 12:36:23 -070060 * TODO(b/32022261) Remove this comment.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010061 */
62 /** Temperature of CPUs in Celsius. */
Andreas Huberfb7accf2016-10-24 12:36:23 -070063 public static final int DEVICE_TEMPERATURE_CPU = Constants.TemperatureType.CPU;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010064
65 /** Temperature of GPUs in Celsius. */
Andreas Huberfb7accf2016-10-24 12:36:23 -070066 public static final int DEVICE_TEMPERATURE_GPU = Constants.TemperatureType.GPU;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010067
68 /** Temperature of battery in Celsius. */
Andreas Huberfb7accf2016-10-24 12:36:23 -070069 public static final int DEVICE_TEMPERATURE_BATTERY = Constants.TemperatureType.BATTERY;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010070
Polina Bondarenkoeb845522016-03-01 14:39:48 +010071 /** Temperature of device skin in Celsius. */
Andreas Huberfb7accf2016-10-24 12:36:23 -070072 public static final int DEVICE_TEMPERATURE_SKIN = Constants.TemperatureType.SKIN;
Polina Bondarenkoeb845522016-03-01 14:39:48 +010073
74 /** Get current temperature. */
75 public static final int TEMPERATURE_CURRENT = 0;
76
77 /** Get throttling temperature threshold. */
78 public static final int TEMPERATURE_THROTTLING = 1;
79
80 /** Get shutdown temperature threshold. */
81 public static final int TEMPERATURE_SHUTDOWN = 2;
82
Ruben Brunk946ef642016-03-25 13:45:42 -070083 /**
84 * Get throttling temperature threshold above which minimum clockrates for VR mode will not be
85 * met.
86 */
87 public static final int TEMPERATURE_THROTTLING_BELOW_VR_MIN = 3;
88
Polina Bondarenkoeb845522016-03-01 14:39:48 +010089 /** Undefined temperature constant. */
90 public static final float UNDEFINED_TEMPERATURE = -Float.MAX_VALUE;
91
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010092 /** Calling app context. */
93 private final Context mContext;
94
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010095 /** @hide */
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010096 public HardwarePropertiesManager(Context context, IHardwarePropertiesManager service) {
97 mContext = context;
98 mService = service;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010099 }
100
101 /**
102 * Return an array of device temperatures in Celsius.
103 *
104 * @param type type of requested device temperature, one of {@link #DEVICE_TEMPERATURE_CPU},
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100105 * {@link #DEVICE_TEMPERATURE_GPU}, {@link #DEVICE_TEMPERATURE_BATTERY} or {@link
106 * #DEVICE_TEMPERATURE_SKIN}.
107 * @param source source of requested device temperature, one of {@link #TEMPERATURE_CURRENT},
Ruben Brunk946ef642016-03-25 13:45:42 -0700108 * {@link #TEMPERATURE_THROTTLING}, {@link #TEMPERATURE_THROTTLING_BELOW_VR_MIN} or
109 * {@link #TEMPERATURE_SHUTDOWN}.
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100110 * @return an array of requested float device temperatures. Temperature equals to
111 * {@link #UNDEFINED_TEMPERATURE} if undefined.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100112 * Empty if platform doesn't provide the queried temperature.
113 *
Ruben Brunk8f1d5cb2016-03-22 18:08:41 -0700114 * @throws SecurityException if something other than the profile or device owner, or the
115 * current VR service tries to retrieve information provided by this service.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100116 */
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100117 public @NonNull float[] getDeviceTemperatures(@DeviceTemperatureType int type,
118 @TemperatureSource int source) {
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100119 switch (type) {
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100120 case DEVICE_TEMPERATURE_CPU:
121 case DEVICE_TEMPERATURE_GPU:
122 case DEVICE_TEMPERATURE_BATTERY:
123 case DEVICE_TEMPERATURE_SKIN:
124 switch (source) {
125 case TEMPERATURE_CURRENT:
126 case TEMPERATURE_THROTTLING:
127 case TEMPERATURE_SHUTDOWN:
Ruben Brunk946ef642016-03-25 13:45:42 -0700128 case TEMPERATURE_THROTTLING_BELOW_VR_MIN:
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100129 try {
130 return mService.getDeviceTemperatures(mContext.getOpPackageName(), type,
131 source);
132 } catch (RemoteException e) {
133 throw e.rethrowFromSystemServer();
134 }
135 default:
136 Log.w(TAG, "Unknown device temperature source.");
137 return new float[0];
138 }
139 default:
140 Log.w(TAG, "Unknown device temperature type.");
141 return new float[0];
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100142 }
143 }
144
145 /**
146 * Return an array of CPU usage info for each core.
147 *
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100148 * @return an array of {@link android.os.CpuUsageInfo} for each core. Return {@code null} for
149 * each unplugged core.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100150 * Empty if CPU usage is not supported on this system.
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100151 *
Ruben Brunk8f1d5cb2016-03-22 18:08:41 -0700152 * @throws SecurityException if something other than the profile or device owner, or the
153 * current VR service tries to retrieve information provided by this service.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100154 */
155 public @NonNull CpuUsageInfo[] getCpuUsages() {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100156 try {
157 return mService.getCpuUsages(mContext.getOpPackageName());
158 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700159 throw e.rethrowFromSystemServer();
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100160 }
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100161 }
162
163 /**
164 * Return an array of fan speeds in RPM.
165 *
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100166 * @return an array of float fan speeds in RPM. Empty if there are no fans or fan speed is not
167 * supported on this system.
168 *
Ruben Brunk8f1d5cb2016-03-22 18:08:41 -0700169 * @throws SecurityException if something other than the profile or device owner, or the
170 * current VR service tries to retrieve information provided by this service.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100171 */
172 public @NonNull float[] getFanSpeeds() {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100173 try {
174 return mService.getFanSpeeds(mContext.getOpPackageName());
175 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700176 throw e.rethrowFromSystemServer();
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100177 }
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100178 }
179}