From 75eddd350e0d7b0d2b41374c2c12c7284b953f68 Mon Sep 17 00:00:00 2001 From: Andras Kloczl Date: Mon, 19 Jul 2021 05:30:57 +0100 Subject: [PATCH] Add new shell commands for DeviceStateChanged CTS test For the new CTS test for DeviceStateChanged metric we need commands with simple outputs to determine the current device state and the supported states. Bug: 181542365 Test: flash device with framework changes then $ atest DeviceStateStatsTests Change-Id: Icb2e755e258f8c3dba3aed34b37cdb7295df5434 --- .../DeviceStateManagerShellCommand.java | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/devicestate/DeviceStateManagerShellCommand.java b/services/core/java/com/android/server/devicestate/DeviceStateManagerShellCommand.java index 56b68b73cb57d..eed68f8b1300f 100644 --- a/services/core/java/com/android/server/devicestate/DeviceStateManagerShellCommand.java +++ b/services/core/java/com/android/server/devicestate/DeviceStateManagerShellCommand.java @@ -27,7 +27,9 @@ import android.os.Binder; import android.os.ShellCommand; import java.io.PrintWriter; +import java.util.Arrays; import java.util.Optional; +import java.util.stream.Collectors; /** * ShellCommands for {@link DeviceStateManagerService}. @@ -56,14 +58,18 @@ public class DeviceStateManagerShellCommand extends ShellCommand { switch (cmd) { case "state": return runState(pw); + case "print-state": + return runPrintState(pw); case "print-states": return runPrintStates(pw); + case "print-states-simple": + return runPrintStatesSimple(pw); default: return handleDefaultCommands(cmd); } } - private void printState(PrintWriter pw) { + private void printAllStates(PrintWriter pw) { Optional committedState = mService.getCommittedState(); Optional baseState = mService.getBaseState(); Optional overrideState = mService.getOverrideState(); @@ -79,7 +85,8 @@ public class DeviceStateManagerShellCommand extends ShellCommand { private int runState(PrintWriter pw) { final String nextArg = getNextArg(); if (nextArg == null) { - printState(pw); + printAllStates(pw); + return 0; } final Context context = mService.getContext(); @@ -123,6 +130,16 @@ public class DeviceStateManagerShellCommand extends ShellCommand { return 0; } + private int runPrintState(PrintWriter pw) { + Optional deviceState = mService.getCommittedState(); + if (deviceState.isPresent()) { + pw.println(deviceState.get().getIdentifier()); + return 0; + } + getErrPrintWriter().println("Error: device state not available."); + return 1; + } + private int runPrintStates(PrintWriter pw) { DeviceState[] states = mService.getSupportedStates(); pw.print("Supported states: [\n"); @@ -133,6 +150,14 @@ public class DeviceStateManagerShellCommand extends ShellCommand { return 0; } + private int runPrintStatesSimple(PrintWriter pw) { + pw.print(Arrays.stream(mService.getSupportedStates()) + .map(DeviceState::getIdentifier) + .map(Object::toString) + .collect(Collectors.joining(","))); + return 0; + } + @Override public void onHelp() { PrintWriter pw = getOutPrintWriter(); @@ -141,8 +166,12 @@ public class DeviceStateManagerShellCommand extends ShellCommand { pw.println(" Print this help text."); pw.println(" state [reset|OVERRIDE_DEVICE_STATE]"); pw.println(" Return or override device state."); + pw.println(" print-state"); + pw.println(" Return the current device state."); pw.println(" print-states"); pw.println(" Return list of currently supported device states."); + pw.println(" print-states-simple"); + pw.println(" Return the currently supported device states in comma separated format."); } private static String toString(@NonNull Optional state) {