Added skin temperature, thresholds to HardwarePropertiesManager

Bug: 27425887
Change-Id: I13f69ae37906b71d93edbf6bea6daf809d97cfd5
diff --git a/services/core/java/com/android/server/HardwarePropertiesManagerService.java b/services/core/java/com/android/server/HardwarePropertiesManagerService.java
index cc21e99..575d99e 100644
--- a/services/core/java/com/android/server/HardwarePropertiesManagerService.java
+++ b/services/core/java/com/android/server/HardwarePropertiesManagerService.java
@@ -22,6 +22,7 @@
 import android.os.Binder;
 import android.os.CpuUsageInfo;
 import android.os.IHardwarePropertiesManager;
+import android.os.Process;
 
 import java.util.Arrays;
 
@@ -33,7 +34,7 @@
     private static native void nativeInit();
 
     private static native float[] nativeGetFanSpeeds();
-    private static native float[] nativeGetDeviceTemperatures(int type);
+    private static native float[] nativeGetDeviceTemperatures(int type, int source);
     private static native CpuUsageInfo[] nativeGetCpuUsages();
 
     private final Context mContext;
@@ -47,10 +48,11 @@
     }
 
     @Override
-    public float[] getDeviceTemperatures(String callingPackage, int type) throws SecurityException {
+    public float[] getDeviceTemperatures(String callingPackage, int type, int source)
+            throws SecurityException {
         enforceHardwarePropertiesRetrievalAllowed(callingPackage);
         synchronized (mLock) {
-            return nativeGetDeviceTemperatures(type);
+            return nativeGetDeviceTemperatures(type, source);
         }
     }
 
@@ -76,8 +78,8 @@
      *
      * @param callingPackage The calling package name.
      *
-     * @throws SecurityException if a non profile or device owner tries to retrieve information
-     * provided by the service.
+     * @throws SecurityException if a non profile or device owner or system tries to retrieve
+     * information provided by the service.
      */
     private void enforceHardwarePropertiesRetrievalAllowed(String callingPackage)
             throws SecurityException {
@@ -92,8 +94,9 @@
         }
 
         final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
-        if (!dpm.isDeviceOwnerApp(callingPackage) && !dpm.isProfileOwnerApp(callingPackage)) {
-            throw new SecurityException("The caller is not a device or profile owner.");
+        if (!dpm.isDeviceOwnerApp(callingPackage) && !dpm.isProfileOwnerApp(callingPackage)
+                && Binder.getCallingUid() != Process.SYSTEM_UID) {
+            throw new SecurityException("The caller is not a device or profile owner or system.");
         }
     }
 }
diff --git a/services/core/jni/com_android_server_HardwarePropertiesManagerService.cpp b/services/core/jni/com_android_server_HardwarePropertiesManagerService.cpp
index ce251c1..ec5e8c9 100644
--- a/services/core/jni/com_android_server_HardwarePropertiesManagerService.cpp
+++ b/services/core/jni/com_android_server_HardwarePropertiesManagerService.cpp
@@ -31,11 +31,21 @@
 
 // ---------------------------------------------------------------------------
 
+// These values must be kept in sync with the temperature source constants in
+// HardwarePropertiesManager.java
+enum {
+    TEMPERATURE_CURRENT = 0,
+    TEMPERATURE_THROTTLING = 1,
+    TEMPERATURE_SHUTDOWN = 2
+};
+
 static struct {
     jclass clazz;
     jmethodID initMethod;
 } gCpuUsageInfoClassInfo;
 
+jfloat gUndefinedTemperature;
+
 static struct thermal_module* gThermalModule;
 
 // ----------------------------------------------------------------------------
@@ -78,7 +88,8 @@
     return env->NewFloatArray(0);
 }
 
-static jfloatArray nativeGetDeviceTemperatures(JNIEnv *env, jclass /* clazz */, int type) {
+static jfloatArray nativeGetDeviceTemperatures(JNIEnv *env, jclass /* clazz */, int type,
+                                               int source) {
     if (gThermalModule && gThermalModule->getTemperatures) {
         ssize_t list_size = gThermalModule->getTemperatures(gThermalModule, nullptr, 0);
         if (list_size >= 0) {
@@ -94,7 +105,29 @@
 
                 for (ssize_t i = 0; i < list_size; ++i) {
                     if (list[i].type == type) {
-                        values[length++] = list[i].current_value;
+                        switch (source) {
+                            case TEMPERATURE_CURRENT:
+                                if (list[i].current_value == UNKNOWN_TEMPERATURE) {
+                                    values[length++] = gUndefinedTemperature;
+                                } else {
+                                    values[length++] = list[i].current_value;
+                                }
+                                break;
+                            case TEMPERATURE_THROTTLING:
+                                if (list[i].throttling_threshold == UNKNOWN_TEMPERATURE) {
+                                    values[length++] = gUndefinedTemperature;
+                                } else {
+                                    values[length++] = list[i].throttling_threshold;
+                                }
+                                break;
+                            case TEMPERATURE_SHUTDOWN:
+                                if (list[i].shutdown_threshold == UNKNOWN_TEMPERATURE) {
+                                    values[length++] = gUndefinedTemperature;
+                                } else {
+                                    values[length++] = list[i].shutdown_threshold;
+                                }
+                                break;
+                        }
                     }
                 }
                 jfloatArray deviceTemps = env->NewFloatArray(length);
@@ -144,7 +177,7 @@
             (void*) nativeInit },
     { "nativeGetFanSpeeds", "()[F",
             (void*) nativeGetFanSpeeds },
-    { "nativeGetDeviceTemperatures", "(I)[F",
+    { "nativeGetDeviceTemperatures", "(II)[F",
             (void*) nativeGetDeviceTemperatures },
     { "nativeGetCpuUsages", "()[Landroid/os/CpuUsageInfo;",
             (void*) nativeGetCpuUsages }
@@ -159,6 +192,12 @@
     gCpuUsageInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
     gCpuUsageInfoClassInfo.initMethod = GetMethodIDOrDie(env, gCpuUsageInfoClassInfo.clazz,
                                                          "<init>", "(JJ)V");
+
+    clazz = env->FindClass("android/os/HardwarePropertiesManager");
+    jfieldID undefined_temperature_field = GetStaticFieldIDOrDie(env, clazz,
+                                                                 "UNDEFINED_TEMPERATURE", "F");
+    gUndefinedTemperature = env->GetStaticFloatField(clazz, undefined_temperature_field);
+
     return res;
 }