Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 81aca11b authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add dumpsys for HardwarePropertiesManagerService"

parents 6d0310a0 c59f0b33
Loading
Loading
Loading
Loading
+77 −12
Original line number Diff line number Diff line
@@ -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.CpuUsageInfo;
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 @@ import java.util.Arrays;
 */
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 @@ public class HardwarePropertiesManagerService extends IHardwarePropertiesManager

    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 @@ public class HardwarePropertiesManagerService extends IHardwarePropertiesManager
        }
    }

    // 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 @@ public class HardwarePropertiesManagerService extends IHardwarePropertiesManager
        }
    }

    // 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 @@ public class HardwarePropertiesManagerService extends IHardwarePropertiesManager
        }
    }

    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 @@ public class HardwarePropertiesManagerService extends IHardwarePropertiesManager
     */
    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 @@ public class HardwarePropertiesManagerService extends IHardwarePropertiesManager
        }
    }
}