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

Commit 8605f83e authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

Add AIDL definiton for IInputDeviceBatteryState

Create an AIDL parcelable class to represent the battery state of an
input device.

Instead of separately querying the battery status and capacity from an
app-local implmentation of "InputDeviceBatteryState" that acts as a
"manager", we combine them into one method that queries the battery
state from BatteryController.

Bug: 243005009
Test: atest BatteryControllerTests
Test: atest InputDeviceBatteryListenerTests
Change-Id: I2b77aeca6a2d062c8b04a16e38d3acf6c996c134
parent fdfde64c
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -16,14 +16,14 @@

package android.hardware.input;

import android.hardware.input.IInputDeviceBatteryState;

/** @hide */
oneway interface IInputDeviceBatteryListener {

    /**
     * Called when there is a change in battery state for a monitored device. This will be called
     * immediately after the listener is successfully registered for a new device via IInputManager.
     * The parameters are values exposed through {@link android.hardware.BatteryState}.
     */
    void onBatteryStateChanged(int deviceId, boolean isBatteryPresent, int status, float capacity,
            long eventTime);
    void onBatteryStateChanged(in IInputDeviceBatteryState batteryState);
}
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 * Copyright (C) 2022 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.
@@ -16,50 +16,25 @@

package android.hardware.input;

import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
import static android.os.IInputConstants.INVALID_BATTERY_CAPACITY;

import android.hardware.BatteryState;
/** @hide */
@JavaDerive(equals=true)
parcelable IInputDeviceBatteryState {
    /** The deviceId of the input device that this battery state is associated with. */
    int deviceId;

    /**
 * Battery implementation for input devices.
 *
 * @hide
     * The timestamp of the last time the battery state was updated, in the
     * {@link SystemClock.uptimeMillis()} time base.
     */
public final class InputDeviceBatteryState extends BatteryState {
    private static final float NULL_BATTERY_CAPACITY = Float.NaN;

    private final InputManager mInputManager;
    private final int mDeviceId;
    private final boolean mHasBattery;
    long updateTime;

    InputDeviceBatteryState(InputManager inputManager, int deviceId, boolean hasBattery) {
        mInputManager = inputManager;
        mDeviceId = deviceId;
        mHasBattery = hasBattery;
    }
    /** Whether the input device has a battery. */
    boolean isPresent;

    @Override
    public boolean isPresent() {
        return mHasBattery;
    }
    /** The battery status for this input device. */
     @JavaPassthrough(annotation="@android.hardware.BatteryState.BatteryStatus")
    int status;

    @Override
    public int getStatus() {
        if (!mHasBattery) {
            return BATTERY_STATUS_UNKNOWN;
        }
        return mInputManager.getBatteryStatus(mDeviceId);
    }

    @Override
    public float getCapacity() {
        if (mHasBattery) {
            int capacity = mInputManager.getBatteryCapacity(mDeviceId);
            if (capacity != INVALID_BATTERY_CAPACITY) {
                return (float) capacity / 100.0f;
            }
        }
        return NULL_BATTERY_CAPACITY;
    }
    /** The battery capacity for this input device, in a range between 0 and 1. */
    float capacity;
}
 No newline at end of file
+2 −3
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.hardware.input.InputDeviceIdentifier;
import android.hardware.input.KeyboardLayout;
import android.hardware.input.IInputDevicesChangedListener;
import android.hardware.input.IInputDeviceBatteryListener;
import android.hardware.input.IInputDeviceBatteryState;
import android.hardware.input.ITabletModeChangedListener;
import android.hardware.input.TouchCalibration;
import android.os.CombinedVibration;
@@ -110,9 +111,7 @@ interface IInputManager {
    boolean registerVibratorStateListener(int deviceId, in IVibratorStateListener listener);
    boolean unregisterVibratorStateListener(int deviceId, in IVibratorStateListener listener);

    // Input device battery query.
    int getBatteryStatus(int deviceId);
    int getBatteryCapacity(int deviceId);
    IInputDeviceBatteryState getBatteryState(int deviceId);

    void setPointerIconType(int typeId);
    void setCustomPointerIcon(in PointerIcon icon);
+32 −51
Original line number Diff line number Diff line
@@ -1307,32 +1307,6 @@ public final class InputManager {
        }
    }

    /**
     * Get the battery status of the input device
     * @param deviceId The input device ID
     * @hide
     */
    public int getBatteryStatus(int deviceId) {
        try {
            return mIm.getBatteryStatus(deviceId);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
    }

    /**
     * Get the remaining battery capacity of the input device
     * @param deviceId The input device ID
     * @hide
     */
    public int getBatteryCapacity(int deviceId) {
        try {
            return mIm.getBatteryCapacity(deviceId);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
    }

    /**
     * Add a runtime association between the input port and the display port. This overrides any
     * static associations.
@@ -1622,8 +1596,17 @@ public final class InputManager {
     * @return The battery, never null.
     * @hide
     */
    public InputDeviceBatteryState getInputDeviceBatteryState(int deviceId, boolean hasBattery) {
        return new InputDeviceBatteryState(this, deviceId, hasBattery);
    @NonNull
    public BatteryState getInputDeviceBatteryState(int deviceId, boolean hasBattery) {
        if (!hasBattery) {
            return new LocalBatteryState();
        }
        try {
            final IInputDeviceBatteryState state = mIm.getBatteryState(deviceId);
            return new LocalBatteryState(state.isPresent, state.status, state.capacity);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
    }

    /**
@@ -1767,8 +1750,8 @@ public final class InputManager {
            listenersForDevice.mDelegates.add(delegate);

            // Notify the listener immediately if we already have the latest battery state.
            if (listenersForDevice.mLatestBatteryState != null) {
                delegate.notifyBatteryStateChanged(listenersForDevice.mLatestBatteryState);
            if (listenersForDevice.mInputDeviceBatteryState != null) {
                delegate.notifyBatteryStateChanged(listenersForDevice.mInputDeviceBatteryState);
            }
        }
    }
@@ -1952,20 +1935,21 @@ public final class InputManager {
        }
    }

    // Implementation of the android.hardware.BatteryState interface used to report the battery
    // state via the InputDevice#getBatteryState() and InputDeviceBatteryListener interfaces.
    private static final class LocalBatteryState extends BatteryState {
        final int mDeviceId;
        final boolean mIsPresent;
        final int mStatus;
        final float mCapacity;
        final long mEventTime;

        LocalBatteryState(int deviceId, boolean isPresent, int status, float capacity,
                long eventTime) {
            mDeviceId = deviceId;
        private final boolean mIsPresent;
        private final int mStatus;
        private final float mCapacity;

        LocalBatteryState() {
            this(false /*isPresent*/, BatteryState.STATUS_UNKNOWN, Float.NaN /*capacity*/);
        }

        LocalBatteryState(boolean isPresent, int status, float capacity) {
            mIsPresent = isPresent;
            mStatus = status;
            mCapacity = capacity;
            mEventTime = eventTime;
        }

        @Override
@@ -1986,7 +1970,7 @@ public final class InputManager {

    private static final class RegisteredBatteryListeners {
        final List<InputDeviceBatteryListenerDelegate> mDelegates = new ArrayList<>();
        LocalBatteryState mLatestBatteryState;
        IInputDeviceBatteryState mInputDeviceBatteryState;
    }

    private static final class InputDeviceBatteryListenerDelegate {
@@ -1998,27 +1982,24 @@ public final class InputManager {
            mExecutor = executor;
        }

        void notifyBatteryStateChanged(LocalBatteryState batteryState) {
        void notifyBatteryStateChanged(IInputDeviceBatteryState state) {
            mExecutor.execute(() ->
                    mListener.onBatteryStateChanged(batteryState.mDeviceId, batteryState.mEventTime,
                            batteryState));
                    mListener.onBatteryStateChanged(state.deviceId, state.updateTime,
                            new LocalBatteryState(state.isPresent, state.status, state.capacity)));
        }
    }

    private class LocalInputDeviceBatteryListener extends IInputDeviceBatteryListener.Stub {
        @Override
        public void onBatteryStateChanged(int deviceId, boolean isBatteryPresent, int status,
                float capacity, long eventTime) {
        public void onBatteryStateChanged(IInputDeviceBatteryState state) {
            synchronized (mBatteryListenersLock) {
                if (mBatteryListeners == null) return;
                final RegisteredBatteryListeners entry = mBatteryListeners.get(deviceId);
                final RegisteredBatteryListeners entry = mBatteryListeners.get(state.deviceId);
                if (entry == null) return;

                entry.mLatestBatteryState =
                        new LocalBatteryState(
                                deviceId, isBatteryPresent, status, capacity, eventTime);
                entry.mInputDeviceBatteryState = state;
                for (InputDeviceBatteryListenerDelegate delegate : entry.mDelegates) {
                    delegate.notifyBatteryStateChanged(entry.mLatestBatteryState);
                    delegate.notifyBatteryStateChanged(entry.mInputDeviceBatteryState);
                }
            }
        }
+1 −7
Original line number Diff line number Diff line
@@ -91,9 +91,6 @@ public final class InputDevice implements Parcelable {
    @GuardedBy("mMotionRanges")
    private SensorManager mSensorManager;

    @GuardedBy("mMotionRanges")
    private BatteryState mBatteryState;

    @GuardedBy("mMotionRanges")
    private LightsManager mLightsManager;

@@ -1058,10 +1055,7 @@ public final class InputDevice implements Parcelable {
     */
    @NonNull
    public BatteryState getBatteryState() {
        if (mBatteryState == null) {
            mBatteryState = InputManager.getInstance().getInputDeviceBatteryState(mId, mHasBattery);
        }
        return mBatteryState;
        return InputManager.getInstance().getInputDeviceBatteryState(mId, mHasBattery);
    }

    /**
Loading