blob: 3d072c5c4f5e09f5945e68f35880d3faa2fa56a4 [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;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060020import android.annotation.SystemService;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010021import android.content.Context;
Andreas Huberfb7accf2016-10-24 12:36:23 -070022import android.hardware.thermal.V1_0.Constants;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010023import android.util.Log;
24
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010025import java.lang.annotation.Retention;
26import java.lang.annotation.RetentionPolicy;
27
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010028/**
29 * The HardwarePropertiesManager class provides a mechanism of accessing hardware state of a
30 * device: CPU, GPU and battery temperatures, CPU usage per core, fan speed, etc.
31 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060032@SystemService(Context.HARDWARE_PROPERTIES_SERVICE)
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010033public class HardwarePropertiesManager {
34
35 private static final String TAG = HardwarePropertiesManager.class.getSimpleName();
36
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010037 private final IHardwarePropertiesManager mService;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010038
Polina Bondarenkoeb845522016-03-01 14:39:48 +010039 /**
40 * @hide
41 */
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010042 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -070043 @IntDef(prefix = { "DEVICE_TEMPERATURE_" }, value = {
44 DEVICE_TEMPERATURE_CPU,
45 DEVICE_TEMPERATURE_GPU,
46 DEVICE_TEMPERATURE_BATTERY,
47 DEVICE_TEMPERATURE_SKIN
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010048 })
49 public @interface DeviceTemperatureType {}
50
51 /**
Polina Bondarenkoeb845522016-03-01 14:39:48 +010052 * @hide
53 */
54 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -070055 @IntDef(prefix = { "TEMPERATURE_" }, value = {
56 TEMPERATURE_CURRENT,
57 TEMPERATURE_THROTTLING,
58 TEMPERATURE_SHUTDOWN,
59 TEMPERATURE_THROTTLING_BELOW_VR_MIN
Polina Bondarenkoeb845522016-03-01 14:39:48 +010060 })
61 public @interface TemperatureSource {}
62
63 /**
Polina Bondarenko71cb0c52016-10-11 18:44:12 +020064 * Device temperature types.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010065 */
Tej Singh40298312018-02-16 00:15:09 -080066 // These constants are also defined in android/os/enums.proto.
67 // Any change to the types here or in the thermal hal should be made in the proto as well.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010068 /** Temperature of CPUs in Celsius. */
Andreas Huberfb7accf2016-10-24 12:36:23 -070069 public static final int DEVICE_TEMPERATURE_CPU = Constants.TemperatureType.CPU;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010070
71 /** Temperature of GPUs in Celsius. */
Andreas Huberfb7accf2016-10-24 12:36:23 -070072 public static final int DEVICE_TEMPERATURE_GPU = Constants.TemperatureType.GPU;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010073
74 /** Temperature of battery in Celsius. */
Andreas Huberfb7accf2016-10-24 12:36:23 -070075 public static final int DEVICE_TEMPERATURE_BATTERY = Constants.TemperatureType.BATTERY;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +010076
Polina Bondarenkoeb845522016-03-01 14:39:48 +010077 /** Temperature of device skin in Celsius. */
Andreas Huberfb7accf2016-10-24 12:36:23 -070078 public static final int DEVICE_TEMPERATURE_SKIN = Constants.TemperatureType.SKIN;
Polina Bondarenkoeb845522016-03-01 14:39:48 +010079
80 /** Get current temperature. */
81 public static final int TEMPERATURE_CURRENT = 0;
82
83 /** Get throttling temperature threshold. */
84 public static final int TEMPERATURE_THROTTLING = 1;
85
86 /** Get shutdown temperature threshold. */
87 public static final int TEMPERATURE_SHUTDOWN = 2;
88
Ruben Brunk946ef642016-03-25 13:45:42 -070089 /**
90 * Get throttling temperature threshold above which minimum clockrates for VR mode will not be
91 * met.
92 */
93 public static final int TEMPERATURE_THROTTLING_BELOW_VR_MIN = 3;
94
Polina Bondarenkoeb845522016-03-01 14:39:48 +010095 /** Undefined temperature constant. */
96 public static final float UNDEFINED_TEMPERATURE = -Float.MAX_VALUE;
97
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010098 /** Calling app context. */
99 private final Context mContext;
100
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100101 /** @hide */
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100102 public HardwarePropertiesManager(Context context, IHardwarePropertiesManager service) {
103 mContext = context;
104 mService = service;
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100105 }
106
107 /**
108 * Return an array of device temperatures in Celsius.
109 *
110 * @param type type of requested device temperature, one of {@link #DEVICE_TEMPERATURE_CPU},
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100111 * {@link #DEVICE_TEMPERATURE_GPU}, {@link #DEVICE_TEMPERATURE_BATTERY} or {@link
112 * #DEVICE_TEMPERATURE_SKIN}.
113 * @param source source of requested device temperature, one of {@link #TEMPERATURE_CURRENT},
Ruben Brunk946ef642016-03-25 13:45:42 -0700114 * {@link #TEMPERATURE_THROTTLING}, {@link #TEMPERATURE_THROTTLING_BELOW_VR_MIN} or
115 * {@link #TEMPERATURE_SHUTDOWN}.
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100116 * @return an array of requested float device temperatures. Temperature equals to
117 * {@link #UNDEFINED_TEMPERATURE} if undefined.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100118 * Empty if platform doesn't provide the queried temperature.
119 *
Polina Bondarenko8503fd52016-12-20 12:44:38 +0100120 * @throws SecurityException if something other than the device owner or the current VR service
121 * tries to retrieve information provided by this service.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100122 */
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100123 public @NonNull float[] getDeviceTemperatures(@DeviceTemperatureType int type,
124 @TemperatureSource int source) {
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100125 switch (type) {
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100126 case DEVICE_TEMPERATURE_CPU:
127 case DEVICE_TEMPERATURE_GPU:
128 case DEVICE_TEMPERATURE_BATTERY:
129 case DEVICE_TEMPERATURE_SKIN:
130 switch (source) {
131 case TEMPERATURE_CURRENT:
132 case TEMPERATURE_THROTTLING:
133 case TEMPERATURE_SHUTDOWN:
Ruben Brunk946ef642016-03-25 13:45:42 -0700134 case TEMPERATURE_THROTTLING_BELOW_VR_MIN:
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100135 try {
136 return mService.getDeviceTemperatures(mContext.getOpPackageName(), type,
137 source);
138 } catch (RemoteException e) {
139 throw e.rethrowFromSystemServer();
140 }
141 default:
142 Log.w(TAG, "Unknown device temperature source.");
143 return new float[0];
144 }
145 default:
146 Log.w(TAG, "Unknown device temperature type.");
147 return new float[0];
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100148 }
149 }
150
151 /**
152 * Return an array of CPU usage info for each core.
153 *
Polina Bondarenkoeb845522016-03-01 14:39:48 +0100154 * @return an array of {@link android.os.CpuUsageInfo} for each core. Return {@code null} for
155 * each unplugged core.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100156 * Empty if CPU usage is not supported on this system.
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100157 *
Polina Bondarenko8503fd52016-12-20 12:44:38 +0100158 * @throws SecurityException if something other than the device owner or the current VR service
159 * tries to retrieve information provided by this service.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100160 */
161 public @NonNull CpuUsageInfo[] getCpuUsages() {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100162 try {
163 return mService.getCpuUsages(mContext.getOpPackageName());
164 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700165 throw e.rethrowFromSystemServer();
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100166 }
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100167 }
168
169 /**
170 * Return an array of fan speeds in RPM.
171 *
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100172 * @return an array of float fan speeds in RPM. Empty if there are no fans or fan speed is not
173 * supported on this system.
174 *
Polina Bondarenko8503fd52016-12-20 12:44:38 +0100175 * @throws SecurityException if something other than the device owner or the current VR service
176 * tries to retrieve information provided by this service.
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100177 */
178 public @NonNull float[] getFanSpeeds() {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100179 try {
180 return mService.getFanSpeeds(mContext.getOpPackageName());
181 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700182 throw e.rethrowFromSystemServer();
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100183 }
Polina Bondarenko965ecbb2015-11-13 15:34:28 +0100184 }
185}