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

Commit f8754ac2 authored by Polina Bondarenko's avatar Polina Bondarenko
Browse files

Added hardwareproperties SystemService

Add HardwarePropertiesManagerService which call native methods to
get CPU, GPU, battery temperatures, CPU usage info, fan speeds.
Restrict hardware properties retrieval only for device and profile
owners.

Bug: 26945055
Change-Id: I4d6b30b78e575532d5e9cfa59ef6cd81355439d4
parent 5393a660
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -215,6 +215,7 @@ LOCAL_SRC_FILES += \
	core/java/android/os/IBatteryPropertiesRegistrar.aidl \
	core/java/android/os/ICancellationSignal.aidl \
	core/java/android/os/IDeviceIdleController.aidl \
	core/java/android/os/IHardwarePropertiesManager.aidl \
	core/java/android/os/IMaintenanceActivityListener.aidl \
	core/java/android/os/IMessenger.aidl \
	core/java/android/os/INetworkActivityListener.aidl \
+9 −1
Original line number Diff line number Diff line
@@ -89,6 +89,7 @@ import android.os.BatteryManager;
import android.os.DropBoxManager;
import android.os.HardwarePropertiesManager;
import android.os.IBinder;
import android.os.IHardwarePropertiesManager;
import android.os.IPowerManager;
import android.os.IUserManager;
import android.os.PowerManager;
@@ -715,7 +716,14 @@ final class SystemServiceRegistry {
                new CachedServiceFetcher<HardwarePropertiesManager>() {
            @Override
            public HardwarePropertiesManager createService(ContextImpl ctx) {
                return new HardwarePropertiesManager();
                    IBinder b = ServiceManager.getService(Context.HARDWARE_PROPERTIES_SERVICE);
                    IHardwarePropertiesManager service =
                            IHardwarePropertiesManager.Stub.asInterface(b);
                    if (service == null) {
                        Log.wtf(TAG, "Failed to get hardwareproperties service.");
                        return null;
                    }
                    return new HardwarePropertiesManager(ctx, service);
            }});

        registerService(Context.SOUND_TRIGGER_SERVICE, SoundTriggerManager.class,
+18 −0
Original line number Diff line number Diff line
/* Copyright 2016, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

package android.os;

parcelable CpuUsageInfo;
+35 −13
Original line number Diff line number Diff line
@@ -17,10 +17,12 @@ package android.os;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.content.Context;
import android.util.Log;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;


/**
 * The HardwarePropertiesManager class provides a mechanism of accessing hardware state of a
 * device: CPU, GPU and battery temperatures, CPU usage per core, fan speed, etc.
@@ -29,11 +31,7 @@ public class HardwarePropertiesManager {

    private static final String TAG = HardwarePropertiesManager.class.getSimpleName();

    private static native void nativeInit();

    private static native float[] nativeGetFanSpeeds();
    private static native float[] nativeGetDeviceTemperatures(int type);
    private static native CpuUsageInfo[] nativeGetCpuUsages();
    private final IHardwarePropertiesManager mService;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({
@@ -54,9 +52,13 @@ public class HardwarePropertiesManager {
    /** Temperature of battery in Celsius. */
    public static final int DEVICE_TEMPERATURE_BATTERY = 2;

    /** Calling app context. */
    private final Context mContext;

    /** @hide */
    public HardwarePropertiesManager() {
        nativeInit();
    public HardwarePropertiesManager(Context context, IHardwarePropertiesManager service) {
        mContext = context;
        mService = service;
    }

    /**
@@ -68,13 +70,19 @@ public class HardwarePropertiesManager {
     *         Empty if platform doesn't provide the queried temperature.
     *
     * @throws IllegalArgumentException if an incorrect temperature type is queried.
     * @throws SecurityException if a non profile or device owner tries to call this method.
    */
    public @NonNull float[] getDeviceTemperatures(@DeviceTemperatureType int type) {
        switch (type) {
        case DEVICE_TEMPERATURE_CPU:
        case DEVICE_TEMPERATURE_GPU:
        case DEVICE_TEMPERATURE_BATTERY:
            return nativeGetDeviceTemperatures(type);
            try {
                return mService.getDeviceTemperatures(mContext.getOpPackageName(), type);
            } catch (RemoteException e) {
                Log.w(TAG, "Could not get device temperatures", e);
                return new float[0];
            }
        default:
            throw new IllegalArgumentException();
        }
@@ -85,18 +93,32 @@ public class HardwarePropertiesManager {
     *
     * @return an array of {@link android.os.CpuUsageInfo} for each core.
     *         Empty if CPU usage is not supported on this system.
     *
     * @throws SecurityException if a non profile or device owner tries to call this method.
     */
    public @NonNull CpuUsageInfo[] getCpuUsages() {
        return nativeGetCpuUsages();
        try {
            return mService.getCpuUsages(mContext.getOpPackageName());
        } catch (RemoteException e) {
            Log.w(TAG, "Could not get CPU usages", e);
            return new CpuUsageInfo[0];
        }
    }

    /**
     * Return an array of fan speeds in RPM.
     *
     * @return an arrat of float fan speeds. Empty if there is no fans or fan speed
     *         not supported on this system.
     * @return an array of float fan speeds in RPM. Empty if there are no fans or fan speed is not
     * supported on this system.
     *
     * @throws SecurityException if a non profile or device owner tries to call this method.
     */
    public @NonNull float[] getFanSpeeds() {
        return nativeGetFanSpeeds();
        try {
            return mService.getFanSpeeds(mContext.getOpPackageName());
        } catch (RemoteException e) {
            Log.w(TAG, "Could not get fan speeds", e);
            return new float[0];
        }
    }
}
+28 −0
Original line number Diff line number Diff line
/*
**
** Copyright 2016, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

package android.os;

import android.os.CpuUsageInfo;

/** @hide */

interface IHardwarePropertiesManager {
    float[] getDeviceTemperatures(String callingPackage, int type);
    CpuUsageInfo[] getCpuUsages(String callingPackage);
    float[] getFanSpeeds(String callingPackage);
}
Loading