• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
16 package android.os;
17 
18 import android.annotation.IntDef;
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.util.Log;
22 
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.RetentionPolicy;
25 
26 /**
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  */
30 public class HardwarePropertiesManager {
31 
32     private static final String TAG = HardwarePropertiesManager.class.getSimpleName();
33 
34     private final IHardwarePropertiesManager mService;
35 
36     /**
37      * @hide
38      */
39     @Retention(RetentionPolicy.SOURCE)
40     @IntDef({
41         DEVICE_TEMPERATURE_CPU, DEVICE_TEMPERATURE_GPU, DEVICE_TEMPERATURE_BATTERY,
42                 DEVICE_TEMPERATURE_SKIN
43     })
44     public @interface DeviceTemperatureType {}
45 
46     /**
47      * @hide
48      */
49     @Retention(RetentionPolicy.SOURCE)
50     @IntDef({
51         TEMPERATURE_CURRENT, TEMPERATURE_THROTTLING, TEMPERATURE_SHUTDOWN,
52                 TEMPERATURE_THROTTLING_BELOW_VR_MIN
53     })
54     public @interface TemperatureSource {}
55 
56     /**
57      * 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 
69     /** 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 
81     /**
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 
87     /** Undefined temperature constant. */
88     public static final float UNDEFINED_TEMPERATURE = -Float.MAX_VALUE;
89 
90     /** Calling app context. */
91     private final Context mContext;
92 
93     /** @hide */
HardwarePropertiesManager(Context context, IHardwarePropertiesManager service)94     public HardwarePropertiesManager(Context context, IHardwarePropertiesManager service) {
95         mContext = context;
96         mService = service;
97     }
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},
103      * {@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},
106      * {@link #TEMPERATURE_THROTTLING}, {@link #TEMPERATURE_THROTTLING_BELOW_VR_MIN} or
107      * {@link #TEMPERATURE_SHUTDOWN}.
108      * @return an array of requested float device temperatures. Temperature equals to
109      *         {@link #UNDEFINED_TEMPERATURE} if undefined.
110      *         Empty if platform doesn't provide the queried temperature.
111      *
112      * @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.
114     */
getDeviceTemperatures(@eviceTemperatureType int type, @TemperatureSource int source)115     public @NonNull float[] getDeviceTemperatures(@DeviceTemperatureType int type,
116             @TemperatureSource int source) {
117         switch (type) {
118             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:
126                     case TEMPERATURE_THROTTLING_BELOW_VR_MIN:
127                         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];
140         }
141     }
142 
143     /**
144      * Return an array of CPU usage info for each core.
145      *
146      * @return an array of {@link android.os.CpuUsageInfo} for each core. Return {@code null} for
147      *         each unplugged core.
148      *         Empty if CPU usage is not supported on this system.
149      *
150      * @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.
152      */
getCpuUsages()153     public @NonNull CpuUsageInfo[] getCpuUsages() {
154         try {
155             return mService.getCpuUsages(mContext.getOpPackageName());
156         } catch (RemoteException e) {
157             throw e.rethrowFromSystemServer();
158         }
159     }
160 
161     /**
162      * Return an array of fan speeds in RPM.
163      *
164      * @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      *
167      * @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.
169      */
getFanSpeeds()170     public @NonNull float[] getFanSpeeds() {
171         try {
172             return mService.getFanSpeeds(mContext.getOpPackageName());
173         } catch (RemoteException e) {
174             throw e.rethrowFromSystemServer();
175         }
176     }
177 }
178