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

Commit 1fd86f4c authored by Yifan Hong's avatar Yifan Hong
Browse files

BatteryService serves IBatteryPropertiesRegistrar.

This binder service is originally served by healthd.
To allow BatteryManager to continue to work, BatteryService
implements this binder service.

Test: BatteryManagerTest
Bug: 62229583

Change-Id: I9c97b3936546740c6d63060899fe50c5c4c957bd
parent 89d55c1d
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -43,6 +43,13 @@ public class BatteryProperty implements Parcelable {
        return mValueLong;
    }

    /**
     * @hide
     */
    public void setLong(long val) {
        mValueLong = val;
    }

    /*
     * Parcel read/write code must be kept in sync with
     * frameworks/native/services/batteryservice/BatteryProperty.cpp
+67 −0
Original line number Diff line number Diff line
@@ -41,8 +41,10 @@ import android.hidl.manager.V1_0.IServiceNotification;
import android.hardware.health.V2_0.HealthInfo;
import android.hardware.health.V2_0.IHealthInfoCallback;
import android.hardware.health.V2_0.IHealth;
import android.hardware.health.V2_0.Result;
import android.os.BatteryManager;
import android.os.BatteryManagerInternal;
import android.os.BatteryProperty;
import android.os.Binder;
import android.os.FileUtils;
import android.os.Handler;
@@ -58,6 +60,7 @@ import android.os.UserHandle;
import android.provider.Settings;
import android.service.battery.BatteryServiceDumpProto;
import android.util.EventLog;
import android.util.MutableInt;
import android.util.Slog;
import android.util.proto.ProtoOutputStream;

@@ -170,6 +173,7 @@ public final class BatteryService extends SystemService {

    private HealthServiceWrapper mHealthServiceWrapper;
    private HealthHalCallback mHealthHalCallback;
    private BatteryPropertiesRegistrar mBatteryPropertiesRegistrar;

    public BatteryService(Context context) {
        super(context);
@@ -213,6 +217,8 @@ public final class BatteryService extends SystemService {

        mBinderService = new BinderService();
        publishBinderService("battery", mBinderService);
        mBatteryPropertiesRegistrar = new BatteryPropertiesRegistrar();
        publishBinderService("batteryproperties", mBatteryPropertiesRegistrar);
        publishLocalService(BatteryManagerInternal.class, new LocalService());
    }

@@ -1043,6 +1049,63 @@ public final class BatteryService extends SystemService {
        }
    }

    // Reduced IBatteryPropertiesRegistrar that only implements getProperty for usage
    // in BatteryManager.
    private final class BatteryPropertiesRegistrar extends IBatteryPropertiesRegistrar.Stub {
        public void registerListener(IBatteryPropertiesListener listener) {
            Slog.e(TAG, "health: must not call registerListener on battery properties");
        }
        public void unregisterListener(IBatteryPropertiesListener listener) {
            Slog.e(TAG, "health: must not call unregisterListener on battery properties");
        }
        public int getProperty(int id, final BatteryProperty prop) throws RemoteException {
            IHealth service = mHealthServiceWrapper.getLastService();
            final MutableInt outResult = new MutableInt(Result.NOT_SUPPORTED);
            switch(id) {
                case BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER:
                    service.getChargeCounter((int result, int value) -> {
                        outResult.value = result;
                        if (result == Result.SUCCESS) prop.setLong(value);
                    });
                    break;
                case BatteryManager.BATTERY_PROPERTY_CURRENT_NOW:
                    service.getCurrentNow((int result, int value) -> {
                        outResult.value = result;
                        if (result == Result.SUCCESS) prop.setLong(value);
                    });
                    break;
                case BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE:
                    service.getCurrentAverage((int result, int value) -> {
                        outResult.value = result;
                        if (result == Result.SUCCESS) prop.setLong(value);
                    });
                    break;
                case BatteryManager.BATTERY_PROPERTY_CAPACITY:
                    service.getCapacity((int result, int value) -> {
                        outResult.value = result;
                        if (result == Result.SUCCESS) prop.setLong(value);
                    });
                    break;
                case BatteryManager.BATTERY_PROPERTY_STATUS:
                    service.getChargeStatus((int result, int value) -> {
                        outResult.value = result;
                        if (result == Result.SUCCESS) prop.setLong(value);
                    });
                    break;
                case BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER:
                    service.getEnergyCounter((int result, long value) -> {
                        outResult.value = result;
                        if (result == Result.SUCCESS) prop.setLong(value);
                    });
                    break;
            }
            return outResult.value;
        }
        public void scheduleUpdate() {
            Slog.e(TAG, "health: must not call scheduleUpdate on battery properties");
        }
    }

    private final class LocalService extends BatteryManagerInternal {
        @Override
        public boolean isPowered(int plugTypeSet) {
@@ -1117,6 +1180,10 @@ public final class BatteryService extends SystemService {
        HealthServiceWrapper() {
        }

        IHealth getLastService() {
            return mLastService.get();
        }

        /**
         * Start monitoring registration of new IHealth services. Only instances that are in
         * {@code sAllInstances} and in device / framework manifest are used. This function should