Add dumpsys for HardwarePropertiesManagerService

Sometimes, while debugging it is useful to get the device temperature
and also verify if the API is working right. Add dumpsys to help in all
this debugging.

Bug: 63666982
Test:
$ adb shell dumpsys hardware_properties
****** Dump of HardwarePropertiesManagerService ******
CPU temperatures: [51.100002, 49.8, 51.7, 49.8, 51.100002, 51.100002,
52.3, 52.0]
CPU throttling temperatures: [95.0, 95.0, 95.0, 95.0, 95.0, 95.0, 95.0,
95.0]
CPU shutdown temperatures: [115.0, 115.0, 115.0, 115.0, 115.0, 115.0,
115.0, 115.0]
CPU vr throttling temperatures: [95.0, 95.0, 95.0, 95.0, 95.0, 95.0,
95.0, 95.0]
GPU temperatures: [44.0]
GPU throttling temperatures: [-3.4028235E38]
GPU shutdown temperatures: [-3.4028235E38]
GPU vr throttling temperatures: [-3.4028235E38]
Battery temperatures: [29.400002]
Battery throttling temperatures: [-3.4028235E38]
Battery shutdown temperatures: [60.0]
Battery vr throttling temperatures: [-3.4028235E38]
Skin temperatures: [39.0]
Skin throttling temperatures: [49.0]
Skin shutdown temperatures: [66.0]
Skin vr throttling temperatures: [62.0]
Fan speed: []

Cpu usage of core: 0, active = 2122, total = 3883
Cpu usage of core: 1, active = 2213, total = 4172
Cpu usage of core: 2, active = 1456, total = 4108
Cpu usage of core: 3, active = 1362, total = 4129
Cpu usage of core: 4, active = 1307, total = 4159
Cpu usage of core: 5, active = 1372, total = 4166
Cpu usage of core: 6, active = 2490, total = 4160
Cpu usage of core: 7, active = 2335, total = 4150
****** End of HardwarePropertiesManagerService dump ******

Change-Id: I154e3c4734a324185c88398da12476ce67b96889
Signed-off-by: Karthik Ravi Shankar <karthikrs@google.com>
diff --git a/services/core/java/com/android/server/HardwarePropertiesManagerService.java b/services/core/java/com/android/server/HardwarePropertiesManagerService.java
index e03a478..4016d29 100644
--- a/services/core/java/com/android/server/HardwarePropertiesManagerService.java
+++ b/services/core/java/com/android/server/HardwarePropertiesManagerService.java
@@ -16,8 +16,18 @@
 
 package com.android.server;
 
+import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_BATTERY;
+import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU;
+import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_GPU;
+import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN;
+import static android.os.HardwarePropertiesManager.TEMPERATURE_CURRENT;
+import static android.os.HardwarePropertiesManager.TEMPERATURE_SHUTDOWN;
+import static android.os.HardwarePropertiesManager.TEMPERATURE_THROTTLING;
+import static android.os.HardwarePropertiesManager.TEMPERATURE_THROTTLING_BELOW_VR_MIN;
+
 import android.Manifest;
 import android.app.ActivityManager;
+import android.app.AppOpsManager;
 import android.app.admin.DevicePolicyManager;
 import android.content.Context;
 import android.content.pm.PackageManager;
@@ -26,8 +36,11 @@
 import android.os.IHardwarePropertiesManager;
 import android.os.Process;
 import android.os.UserHandle;
+import com.android.internal.util.DumpUtils;
 import com.android.server.vr.VrManagerInternal;
 
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
 import java.util.Arrays;
 
 /**
@@ -35,6 +48,7 @@
  */
 public class HardwarePropertiesManagerService extends IHardwarePropertiesManager.Stub {
 
+    private static final String TAG = "HardwarePropertiesManagerService";
     private static native void nativeInit();
 
     private static native float[] nativeGetFanSpeeds();
@@ -43,14 +57,18 @@
 
     private final Context mContext;
     private final Object mLock = new Object();
+    private final AppOpsManager mAppOps;
 
     public HardwarePropertiesManagerService(Context context) {
         mContext = context;
+        mAppOps = (AppOpsManager)mContext.getSystemService(Context.APP_OPS_SERVICE);
         synchronized (mLock) {
             nativeInit();
         }
     }
 
+    // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy
+    // cross user permission - b/63697518
     @Override
     public float[] getDeviceTemperatures(String callingPackage, int type, int source)
             throws SecurityException {
@@ -60,6 +78,8 @@
         }
     }
 
+    // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy
+    // cross user permission - b/63697518
     @Override
     public CpuUsageInfo[] getCpuUsages(String callingPackage) throws SecurityException {
         enforceHardwarePropertiesRetrievalAllowed(callingPackage);
@@ -68,6 +88,8 @@
         }
     }
 
+    // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy
+    // cross user permission - b/63697518
     @Override
     public float[] getFanSpeeds(String callingPackage) throws SecurityException {
         enforceHardwarePropertiesRetrievalAllowed(callingPackage);
@@ -76,6 +98,57 @@
         }
     }
 
+    private String getCallingPackageName() {
+        final String[] packages = mContext.getPackageManager().getPackagesForUid(
+                Binder.getCallingUid());
+        if (packages != null && packages.length > 0) {
+           return packages[0];
+        }
+        return "unknown";
+    }
+
+    private void dumpTempValues(String pkg, PrintWriter pw, int type,
+            String typeLabel) {
+        dumpTempValues(pkg, pw, type, typeLabel, "temperatures: ",
+                TEMPERATURE_CURRENT);
+        dumpTempValues(pkg, pw, type, typeLabel, "throttling temperatures: ",
+                TEMPERATURE_THROTTLING);
+        dumpTempValues(pkg, pw, type, typeLabel, "shutdown temperatures: ",
+                TEMPERATURE_SHUTDOWN);
+        dumpTempValues(pkg, pw, type, typeLabel, "vr throttling temperatures: ",
+                TEMPERATURE_THROTTLING_BELOW_VR_MIN);
+    }
+
+    private void dumpTempValues(String pkg, PrintWriter pw, int type,
+            String typeLabel, String subLabel, int valueType) {
+        pw.println(typeLabel + subLabel + Arrays.toString(getDeviceTemperatures(
+                pkg, type, valueType)));
+    }
+
+    @Override
+    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+        if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
+        pw.println("****** Dump of HardwarePropertiesManagerService ******");
+
+        final String PKG = getCallingPackageName();
+        dumpTempValues(PKG, pw, DEVICE_TEMPERATURE_CPU, "CPU ");
+        dumpTempValues(PKG, pw, DEVICE_TEMPERATURE_GPU, "GPU ");
+        dumpTempValues(PKG, pw, DEVICE_TEMPERATURE_BATTERY, "Battery ");
+        dumpTempValues(PKG, pw, DEVICE_TEMPERATURE_SKIN, "Skin ");
+
+        float[] fanSpeeds = getFanSpeeds(PKG);
+        pw.println("Fan speed: " + Arrays.toString(fanSpeeds) + "\n");
+
+        CpuUsageInfo[] cpuUsageInfos = getCpuUsages(PKG);
+        int core = 0;
+        for (int i = 0; i < cpuUsageInfos.length; i++) {
+            pw.println("Cpu usage of core: " + i +
+                    ", active = " + cpuUsageInfos[i].getActive() +
+                    ", total = " + cpuUsageInfos[i].getTotal());
+        }
+        pw.println("****** End of HardwarePropertiesManagerService dump ******");
+    }
+
     /**
      * Throws SecurityException if the calling package is not allowed to retrieve information
      * provided by the service.
@@ -88,18 +161,8 @@
      */
     private void enforceHardwarePropertiesRetrievalAllowed(String callingPackage)
             throws SecurityException {
-        final PackageManager pm = mContext.getPackageManager();
-        int uid = 0;
-        try {
-            uid = pm.getPackageUid(callingPackage, 0);
-            if (Binder.getCallingUid() != uid) {
-                throw new SecurityException("The caller has faked the package name.");
-            }
-        } catch (PackageManager.NameNotFoundException e) {
-            throw new SecurityException("The caller has faked the package name.");
-        }
-
-        final int userId = UserHandle.getUserId(uid);
+        mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
+        final int userId = UserHandle.getUserId(Binder.getCallingUid());
         final VrManagerInternal vrService = LocalServices.getService(VrManagerInternal.class);
         final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
         if (!dpm.isDeviceOwnerApp(callingPackage)
@@ -111,3 +174,5 @@
         }
     }
 }
+
+