From 75aeb6849f3b16478c9947849d5ceeaf3f3e6383 Mon Sep 17 00:00:00 2001 From: Felipe Leme Date: Tue, 21 Jan 2020 14:22:14 -0800 Subject: [PATCH] Improved 'am switch' to handle errors. Bug: 146207078 Test: adb shell am switch-user 42 ; echo $? Change-Id: I005c76652be1fd0908f5c2b40e2513362b5ce5ac --- .../server/am/ActivityManagerShellCommand.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java index d7ad1c2524014..a82fbf0804704 100644 --- a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java +++ b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java @@ -91,7 +91,6 @@ import android.view.Display; import com.android.internal.compat.CompatibilityChangeConfig; import com.android.internal.util.HexDump; import com.android.internal.util.MemInfoReader; -import com.android.internal.util.Preconditions; import com.android.server.compat.PlatformCompat; import java.io.BufferedReader; @@ -1734,7 +1733,7 @@ final class ActivityManagerShellCommand extends ShellCommand { return 0; } - private void switchUserAndWaitForComplete(int userId) throws RemoteException { + private boolean switchUserAndWaitForComplete(int userId) throws RemoteException { // Register switch observer. final CountDownLatch switchLatch = new CountDownLatch(1); mInterface.registerUserSwitchObserver( @@ -1748,7 +1747,7 @@ final class ActivityManagerShellCommand extends ShellCommand { }, ActivityManagerShellCommand.class.getName()); // Switch. - mInterface.switchUser(userId); + boolean switched = mInterface.switchUser(userId); // Wait. try { @@ -1756,6 +1755,7 @@ final class ActivityManagerShellCommand extends ShellCommand { } catch (InterruptedException e) { getErrPrintWriter().println("Thread interrupted unexpectedly."); } + return switched; } int runSwitchUser(PrintWriter pw) throws RemoteException { @@ -1777,12 +1777,18 @@ final class ActivityManagerShellCommand extends ShellCommand { } int userId = Integer.parseInt(getNextArgRequired()); + boolean switched; if (wait) { - switchUserAndWaitForComplete(userId); + switched = switchUserAndWaitForComplete(userId); } else { - mInterface.switchUser(userId); + switched = mInterface.switchUser(userId); + } + if (switched) { + return 0; + } else { + pw.printf("Failed to switch to user %d\n", userId); + return 1; } - return 0; } int runGetCurrentUser(PrintWriter pw) throws RemoteException {