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

Commit 77f0a8db authored by Shao-Chuan Lee's avatar Shao-Chuan Lee
Browse files

Improve arg parsing for `cmd power set-wakelock`

- Make `-d` actually optional and reject unknown options
- Make wakelock type required instead of causing NPE

Bug: 435557564
Test: cmd power set-wakelock acquire PARTIAL_WAKE_LOCK
Test: cmd power set-wakelock acquire -d 0 PARTIAL_WAKE_LOCK
Flag: EXEMPT shell command only
Change-Id: I62f8b9db34f7d74e3453eaf8db63c7885c2c84f4
parent 5af1da80
Loading
Loading
Loading
Loading
+23 −9
Original line number Diff line number Diff line
@@ -226,17 +226,31 @@ class PowerManagerShellCommand extends ShellCommand {
        }

        int displayId = Display.INVALID_DISPLAY;
        String displayOption = getNextArg();
        if ("-d".equals(displayOption)) {
            String idStr = getNextArg();
        String opt;
        while ((opt = getNextOption()) != null) {
            switch (opt) {
                case "-d" -> {
                    String idStr = getNextArgRequired();
                    displayId = Integer.parseInt(idStr);
                    if (displayId < 0) {
                pw.println("Error: Specified displayId (" + idStr + ") must a non-negative int.");
                        pw.println(
                                "Error: Specified displayId ("
                                        + idStr
                                        + ") must be a non-negative int.");
                        return -1;
                    }
                }
        String wakelockTypeString = proxForLegacy ? PowerManagerInternal.getLockLevelString(
                PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) : getNextArg().toUpperCase(Locale.US);
                default -> {
                    pw.println("Error: Unknown option: " + opt);
                    return -1;
                }
            }
        }
        String wakelockTypeString =
                proxForLegacy
                        ? PowerManagerInternal.getLockLevelString(
                                PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)
                        : getNextArgRequired().toUpperCase(Locale.US);
        WakeLock wakelock = getWakelock(displayId, wakelockTypeString);

        if (acquire) {