Home | History | Annotate | Download | only in os
      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.annotation.SystemService;
     21 import android.content.Context;
     22 import android.hardware.thermal.V1_0.Constants;
     23 import android.util.Log;
     24 
     25 import java.lang.annotation.Retention;
     26 import java.lang.annotation.RetentionPolicy;
     27 
     28 /**
     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  */
     32 @SystemService(Context.HARDWARE_PROPERTIES_SERVICE)
     33 public class HardwarePropertiesManager {
     34 
     35     private static final String TAG = HardwarePropertiesManager.class.getSimpleName();
     36 
     37     private final IHardwarePropertiesManager mService;
     38 
     39     /**
     40      * @hide
     41      */
     42     @Retention(RetentionPolicy.SOURCE)
     43     @IntDef({
     44         DEVICE_TEMPERATURE_CPU, DEVICE_TEMPERATURE_GPU, DEVICE_TEMPERATURE_BATTERY,
     45                 DEVICE_TEMPERATURE_SKIN
     46     })
     47     public @interface DeviceTemperatureType {}
     48 
     49     /**
     50      * @hide
     51      */
     52     @Retention(RetentionPolicy.SOURCE)
     53     @IntDef({
     54         TEMPERATURE_CURRENT, TEMPERATURE_THROTTLING, TEMPERATURE_SHUTDOWN,
     55                 TEMPERATURE_THROTTLING_BELOW_VR_MIN
     56     })
     57     public @interface TemperatureSource {}
     58 
     59     /**
     60      * Device temperature types.
     61      */
     62     /** Temperature of CPUs in Celsius. */
     63     public static final int DEVICE_TEMPERATURE_CPU = Constants.TemperatureType.CPU;
     64 
     65     /** Temperature of GPUs in Celsius. */
     66     public static final int DEVICE_TEMPERATURE_GPU = Constants.TemperatureType.GPU;
     67 
     68     /** Temperature of battery in Celsius. */
     69     public static final int DEVICE_TEMPERATURE_BATTERY = Constants.TemperatureType.BATTERY;
     70 
     71     /** Temperature of device skin in Celsius. */
     72     public static final int DEVICE_TEMPERATURE_SKIN = Constants.TemperatureType.SKIN;
     73 
     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 
     83     /**
     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 
     89     /** Undefined temperature constant. */
     90     public static final float UNDEFINED_TEMPERATURE = -Float.MAX_VALUE;
     91 
     92     /** Calling app context. */
     93     private final Context mContext;
     94 
     95     /** @hide */
     96     public HardwarePropertiesManager(Context context, IHardwarePropertiesManager service) {
     97         mContext = context;
     98         mService = service;
     99     }
    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},
    105      * {@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},
    108      * {@link #TEMPERATURE_THROTTLING}, {@link #TEMPERATURE_THROTTLING_BELOW_VR_MIN} or
    109      * {@link #TEMPERATURE_SHUTDOWN}.
    110      * @return an array of requested float device temperatures. Temperature equals to
    111      *         {@link #UNDEFINED_TEMPERATURE} if undefined.
    112      *         Empty if platform doesn't provide the queried temperature.
    113      *
    114      * @throws SecurityException if something other than the device owner or the current VR service
    115      *         tries to retrieve information provided by this service.
    116     */
    117     public @NonNull float[] getDeviceTemperatures(@DeviceTemperatureType int type,
    118             @TemperatureSource int source) {
    119         switch (type) {
    120             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:
    128                     case TEMPERATURE_THROTTLING_BELOW_VR_MIN:
    129                         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];
    142         }
    143     }
    144 
    145     /**
    146      * Return an array of CPU usage info for each core.
    147      *
    148      * @return an array of {@link android.os.CpuUsageInfo} for each core. Return {@code null} for
    149      *         each unplugged core.
    150      *         Empty if CPU usage is not supported on this system.
    151      *
    152      * @throws SecurityException if something other than the device owner or the current VR service
    153      *         tries to retrieve information provided by this service.
    154      */
    155     public @NonNull CpuUsageInfo[] getCpuUsages() {
    156         try {
    157             return mService.getCpuUsages(mContext.getOpPackageName());
    158         } catch (RemoteException e) {
    159             throw e.rethrowFromSystemServer();
    160         }
    161     }
    162 
    163     /**
    164      * Return an array of fan speeds in RPM.
    165      *
    166      * @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      *
    169      * @throws SecurityException if something other than the device owner or the current VR service
    170      *         tries to retrieve information provided by this service.
    171      */
    172     public @NonNull float[] getFanSpeeds() {
    173         try {
    174             return mService.getFanSpeeds(mContext.getOpPackageName());
    175         } catch (RemoteException e) {
    176             throw e.rethrowFromSystemServer();
    177         }
    178     }
    179 }
    180