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

Commit 922ed181 authored by Kim Low's avatar Kim Low Committed by Chris Ye
Browse files

New API methods to access external battery info

Created new API methods that allow apps to access external battery info
of devices such as bluetooth connected gamepads.

Bug: 161633432
Test: atest android.hardware.input.cts.tests.SonyDualshock4BluetoothTest
      atest android.hardware.input.cts.tests.SonyDualshock3UsbTest

Change-Id: Iba59fc9a259818b03c24ee8c2c49601952ed65a3
Merged-In: Iba59fc9a259818b03c24ee8c2c49601952ed65a3
parent 4aea1b05
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -16803,6 +16803,18 @@ package android.graphics.text {
package android.hardware {
  public abstract class Battery {
    ctor public Battery();
    method @FloatRange(from=-1.0F, to=1.0f) public abstract float getCapacity();
    method public abstract int getStatus();
    method public abstract boolean hasBattery();
    field public static final int STATUS_CHARGING = 2; // 0x2
    field public static final int STATUS_DISCHARGING = 3; // 0x3
    field public static final int STATUS_FULL = 5; // 0x5
    field public static final int STATUS_NOT_CHARGING = 4; // 0x4
    field public static final int STATUS_UNKNOWN = 1; // 0x1
  }
  @Deprecated public class Camera {
    method @Deprecated public final void addCallbackBuffer(byte[]);
    method @Deprecated public final void autoFocus(android.hardware.Camera.AutoFocusCallback);
@@ -46363,6 +46375,7 @@ package android.view {
  public final class InputDevice implements android.os.Parcelable {
    method public int describeContents();
    method @NonNull public android.hardware.Battery getBattery();
    method public int getControllerNumber();
    method public String getDescriptor();
    method public static android.view.InputDevice getDevice(int);
+74 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.hardware;

import android.annotation.FloatRange;
import android.annotation.IntDef;
import android.os.BatteryManager;

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

/**
 * The Battery class is a representation of a single battery on a device.
 */
public abstract class Battery {
    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(prefix = { "STATUS_" }, value = {
            STATUS_UNKNOWN,
            STATUS_CHARGING,
            STATUS_DISCHARGING,
            STATUS_NOT_CHARGING,
            STATUS_FULL
    })
    public @interface BatteryStatus {
    }

    /** Battery status is unknown. */
    public static final int STATUS_UNKNOWN = BatteryManager.BATTERY_STATUS_UNKNOWN;
    /** Battery is charging. */
    public static final int STATUS_CHARGING = BatteryManager.BATTERY_STATUS_CHARGING;
    /** Battery is discharging. */
    public static final int STATUS_DISCHARGING = BatteryManager.BATTERY_STATUS_DISCHARGING;
    /** Battery is connected to power but not charging. */
    public static final int STATUS_NOT_CHARGING = BatteryManager.BATTERY_STATUS_NOT_CHARGING;
    /** Battery is full. */
    public static final int STATUS_FULL = BatteryManager.BATTERY_STATUS_FULL;

    /**
     * Check whether the hardware has a battery.
     *
     * @return True if the hardware has a battery, else false.
     */
    public abstract boolean hasBattery();

    /**
     * Get the battery status.
     *
     * @return the battery status.
     */
    public abstract @BatteryStatus int getStatus();

    /**
     * Get remaining battery capacity as float percentage [0.0f, 1.0f] of total capacity
     * Returns -1 when battery capacity can't be read.
     *
     * @return the battery capacity.
     */
    public abstract @FloatRange(from = -1.0f, to = 1.0f) float getCapacity();
}
+4 −0
Original line number Diff line number Diff line
@@ -93,6 +93,10 @@ interface IInputManager {
    int[] getVibratorIds(int deviceId);
    boolean isVibrating(int deviceId);

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

    void setPointerIconType(int typeId);
    void setCustomPointerIcon(in PointerIcon icon);

+65 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.hardware.input;

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

import android.hardware.Battery;

/**
 * Battery implementation for input devices.
 *
 * @hide
 */
public final class InputDeviceBattery extends Battery {
    private static final float NULL_BATTERY_CAPACITY = -1.0f;

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

    InputDeviceBattery(InputManager inputManager, int deviceId, boolean hasBattery) {
        mInputManager = inputManager;
        mDeviceId = deviceId;
        mHasBattery = hasBattery;
    }

    @Override
    public boolean hasBattery() {
        return mHasBattery;
    }

    @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;
    }
}
+35 −0
Original line number Diff line number Diff line
@@ -1244,6 +1244,32 @@ 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.
@@ -1470,6 +1496,15 @@ public final class InputManager {
        return mInputDeviceSensorManager.getSensorManager(deviceId);
    }

    /**
     * Gets a battery object associated with an input device, assuming it has one.
     * @return The battery, never null.
     * @hide
     */
    public InputDeviceBattery getInputDeviceBattery(int deviceId, boolean hasBattery) {
        return new InputDeviceBattery(this, deviceId, hasBattery);
    }

    /**
     * Listens for changes in input devices.
     */
Loading