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

Commit ceac8bbb authored by kholoud mohamed's avatar kholoud mohamed
Browse files

Exposed some testAPIs in BatteryStatsManager

Added the following testsAPIs to replace the usage
of adb shell commands in tests:
* setChargerAcOnline
* setBatteryLevel
* unplugBattery
* resetBattery
* suspendBatteryInput

Test: atest MixedDeviceOwnerTest#testInstallUpdate using the adb
commands and the new testAPIs
Bug: 182260585

Change-Id: I48c2ad37f6240f4d7a3c437416fdcc0beeed6bd1
parent 8923c654
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -1495,6 +1495,14 @@ package android.net {

package android.os {

  public final class BatteryStatsManager {
    method @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public void resetBattery(boolean);
    method @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public void setBatteryLevel(int, boolean);
    method @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public void setChargerAcOnline(boolean, boolean);
    method @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public void suspendBatteryInput();
    method @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public void unplugBattery(boolean);
  }

  public class Build {
    method public static boolean is64BitAbi(String);
    field public static final boolean IS_EMULATOR;
+25 −0
Original line number Diff line number Diff line
@@ -83,4 +83,29 @@ public abstract class BatteryManagerInternal {
     * wait on the battery service lock.
     */
    public abstract int getInvalidCharger();

    /**
     * Sets battery AC charger to enabled/disabled, and freezes the battery state.
     */
    public abstract void setChargerAcOnline(boolean online, boolean forceUpdate);

    /**
     * Sets battery level, and freezes the battery state.
     */
    public abstract void setBatteryLevel(int level, boolean forceUpdate);

    /**
     * Unplugs battery, and freezes the battery state.
     */
    public abstract void unplugBattery(boolean forceUpdate);

    /**
     * Unfreezes battery state, returning to current hardware values.
     */
    public abstract void resetBattery(boolean forceUpdate);

    /**
     * Suspend charging even if plugged in.
     */
    public abstract void suspendBatteryInput();
}
+72 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.content.Context;
import android.net.NetworkStack;
import android.os.connectivity.CellularBatteryStats;
@@ -487,4 +488,74 @@ public final class BatteryStatsManager {
        return isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH
                : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
    }

    /**
     * Sets battery AC charger to enabled/disabled, and freezes the battery state.
     * @hide
     */
    @TestApi
    @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
    public void setChargerAcOnline(boolean online, boolean forceUpdate) {
        try {
            mBatteryStats.setChargerAcOnline(online, forceUpdate);
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
        }
    }

    /**
     * Sets battery level, and freezes the battery state.
     * @hide
     */
    @TestApi
    @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
    public void setBatteryLevel(int level, boolean forceUpdate) {
        try {
            mBatteryStats.setBatteryLevel(level, forceUpdate);
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
        }
    }

    /**
     * Unplugs battery, and freezes the battery state.
     * @hide
     */
    @TestApi
    @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
    public void unplugBattery(boolean forceUpdate) {
        try {
            mBatteryStats.unplugBattery(forceUpdate);
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
        }
    }

    /**
     * Unfreezes battery state, returning to current hardware values.
     * @hide
     */
    @TestApi
    @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
    public void resetBattery(boolean forceUpdate) {
        try {
            mBatteryStats.resetBattery(forceUpdate);
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
        }
    }

    /**
     * Suspend charging even if plugged in.
     * @hide
     */
    @TestApi
    @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
    public void suspendBatteryInput() {
        try {
            mBatteryStats.suspendBatteryInput();
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
        }
    }
}
 No newline at end of file
+11 −0
Original line number Diff line number Diff line
@@ -166,4 +166,15 @@ interface IBatteryStats {

    /** {@hide} */
    boolean setChargingStateUpdateDelayMillis(int delay);

    /** Exposed as a test API. */
    void setChargerAcOnline(boolean online, boolean forceUpdate);
    /** Exposed as a test API. */
    void setBatteryLevel(int level, boolean forceUpdate);
    /** Exposed as a test API. */
    void unplugBattery(boolean forceUpdate);
    /** Exposed as a test API. */
    void resetBattery(boolean forceUpdate);
    /** Exposed as a test API. */
    void suspendBatteryInput();
}
+93 −37
Original line number Diff line number Diff line
@@ -918,19 +918,7 @@ public final class BatteryService extends SystemService {
                int opts = parseOptions(shell);
                getContext().enforceCallingOrSelfPermission(
                        android.Manifest.permission.DEVICE_POWER, null);
                if (!mUpdatesStopped) {
                    copy(mLastHealthInfo, mHealthInfo);
                }
                mHealthInfo.chargerAcOnline = false;
                mHealthInfo.chargerUsbOnline = false;
                mHealthInfo.chargerWirelessOnline = false;
                final long ident = Binder.clearCallingIdentity();
                try {
                    mUpdatesStopped = true;
                    processValuesFromShellLocked(pw, opts);
                } finally {
                    Binder.restoreCallingIdentity(ident);
                }
                unplugBattery(/* forceUpdate= */ (opts & OPTION_FORCE_UPDATE) != 0, pw);
            } break;
            case "set": {
                int opts = parseOptions(shell);
@@ -990,7 +978,8 @@ public final class BatteryService extends SystemService {
                        final long ident = Binder.clearCallingIdentity();
                        try {
                            mUpdatesStopped = true;
                            processValuesFromShellLocked(pw, opts);
                            processValuesLocked(
                                    /* forceUpdate= */ (opts & OPTION_FORCE_UPDATE) != 0, pw);
                        } finally {
                            Binder.restoreCallingIdentity(ident);
                        }
@@ -1004,40 +993,72 @@ public final class BatteryService extends SystemService {
                int opts = parseOptions(shell);
                getContext().enforceCallingOrSelfPermission(
                        android.Manifest.permission.DEVICE_POWER, null);
                final long ident = Binder.clearCallingIdentity();
                try {
                resetBattery(/* forceUpdate= */ (opts & OPTION_FORCE_UPDATE) != 0, pw);
            } break;
            case "suspend_input": {
                getContext().enforceCallingOrSelfPermission(
                        android.Manifest.permission.DEVICE_POWER, null);
                suspendBatteryInput();
            } break;
            default:
                return shell.handleDefaultCommands(cmd);
        }
        return 0;
    }

    private void setChargerAcOnline(boolean online, boolean forceUpdate) {
        if (!mUpdatesStopped) {
            copy(mLastHealthInfo, mHealthInfo);
        }
        mHealthInfo.chargerAcOnline = online;
        mUpdatesStopped = true;
        Binder.withCleanCallingIdentity(() -> processValuesLocked(forceUpdate));
    }

    private void setBatteryLevel(int level, boolean forceUpdate) {
        if (!mUpdatesStopped) {
            copy(mLastHealthInfo, mHealthInfo);
        }
        mHealthInfo.batteryLevel = level;
        mUpdatesStopped = true;
        Binder.withCleanCallingIdentity(() -> processValuesLocked(forceUpdate));
    }

    private void unplugBattery(boolean forceUpdate, PrintWriter pw) {
        if (!mUpdatesStopped) {
            copy(mLastHealthInfo, mHealthInfo);
        }
        mHealthInfo.chargerAcOnline = false;
        mHealthInfo.chargerUsbOnline = false;
        mHealthInfo.chargerWirelessOnline = false;
        mUpdatesStopped = true;
        Binder.withCleanCallingIdentity(() -> processValuesLocked(forceUpdate, pw));
    }

    private void resetBattery(boolean forceUpdate, @Nullable PrintWriter pw) {
        if (mUpdatesStopped) {
            mUpdatesStopped = false;
            copy(mHealthInfo, mLastHealthInfo);
                        processValuesFromShellLocked(pw, opts);
                    }
                } finally {
                    Binder.restoreCallingIdentity(ident);
            Binder.withCleanCallingIdentity(() -> processValuesLocked(forceUpdate, pw));
        }
        if (mBatteryInputSuspended) {
            PowerProperties.battery_input_suspended(false);
            mBatteryInputSuspended = false;
        }
            } break;
            case "suspend_input": {
    }

    private void suspendBatteryInput() {
        if (!Build.IS_DEBUGGABLE) {
            throw new SecurityException(
                    "battery suspend_input is only supported on debuggable builds");
        }
                getContext().enforceCallingOrSelfPermission(
                        android.Manifest.permission.DEVICE_POWER, null);
        PowerProperties.battery_input_suspended(true);
        mBatteryInputSuspended = true;
            } break;
            default:
                return shell.handleDefaultCommands(cmd);
        }
        return 0;
    }

    private void processValuesFromShellLocked(PrintWriter pw, int opts) {
        processValuesLocked((opts & OPTION_FORCE_UPDATE) != 0);
        if ((opts & OPTION_FORCE_UPDATE) != 0) {
    private void processValuesLocked(boolean forceUpdate, @Nullable PrintWriter pw) {
        processValuesLocked(forceUpdate);
        if (pw != null && forceUpdate) {
            pw.println(mSequence);
        }
    }
@@ -1363,6 +1384,41 @@ public final class BatteryService extends SystemService {
                return mInvalidCharger;
            }
        }

        @Override
        public void setChargerAcOnline(boolean online, boolean forceUpdate) {
            getContext().enforceCallingOrSelfPermission(
                    android.Manifest.permission.DEVICE_POWER, /* message= */ null);
            BatteryService.this.setChargerAcOnline(online, forceUpdate);
        }

        @Override
        public void setBatteryLevel(int level, boolean forceUpdate) {
            getContext().enforceCallingOrSelfPermission(
                    android.Manifest.permission.DEVICE_POWER, /* message= */ null);
            BatteryService.this.setBatteryLevel(level, forceUpdate);
        }

        @Override
        public void unplugBattery(boolean forceUpdate) {
            getContext().enforceCallingOrSelfPermission(
                    android.Manifest.permission.DEVICE_POWER, /* message= */ null);
            BatteryService.this.unplugBattery(forceUpdate, /* printWriter= */ null);
        }

        @Override
        public void resetBattery(boolean forceUpdate) {
            getContext().enforceCallingOrSelfPermission(
                    android.Manifest.permission.DEVICE_POWER, /* message= */ null);
            BatteryService.this.resetBattery(forceUpdate, /* printWriter= */ null);
        }

        @Override
        public void suspendBatteryInput() {
            getContext().enforceCallingOrSelfPermission(
                    android.Manifest.permission.DEVICE_POWER, /* message= */ null);
            BatteryService.this.suspendBatteryInput();
        }
    }

    /**
Loading