Make error message more useful when switch-user fails.

Test: btests com.android.bedstead.nene.user.UserReferenceTest
Fixes: 272380882

Change-Id: I791e2f8065984dd3ae2f3d11b54d04df3d6cfca8
diff --git a/common/device-side/bedstead/nene/src/main/java/com/android/bedstead/nene/logcat/Logcat.java b/common/device-side/bedstead/nene/src/main/java/com/android/bedstead/nene/logcat/Logcat.java
index 48ff45a..2cae108 100644
--- a/common/device-side/bedstead/nene/src/main/java/com/android/bedstead/nene/logcat/Logcat.java
+++ b/common/device-side/bedstead/nene/src/main/java/com/android/bedstead/nene/logcat/Logcat.java
@@ -21,7 +21,10 @@
 import com.android.bedstead.nene.utils.ShellCommand;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
 
 /**
  * TestApis related to logcat.
@@ -46,6 +49,14 @@
     }
 
     /**
+     * Get an instant dump from logcat, filtered by {@code lineFilter}.
+     */
+    public String dump(Predicate<String> lineFilter) {
+        return Arrays.stream(dump().split("\n"))
+                .filter(lineFilter).collect(Collectors.joining("\n"));
+    }
+
+    /**
      * Find a system server exception in logcat matching the passed in {@link Throwable}.
      *
      * <p>If there is any problem finding a matching exception, or if the exception is not found,
diff --git a/common/device-side/bedstead/nene/src/main/java/com/android/bedstead/nene/users/UserReference.java b/common/device-side/bedstead/nene/src/main/java/com/android/bedstead/nene/users/UserReference.java
index 55e0df6..2253de4 100644
--- a/common/device-side/bedstead/nene/src/main/java/com/android/bedstead/nene/users/UserReference.java
+++ b/common/device-side/bedstead/nene/src/main/java/com/android/bedstead/nene/users/UserReference.java
@@ -30,6 +30,7 @@
 import static com.android.bedstead.nene.users.Users.users;
 
 import android.annotation.TargetApi;
+import android.app.ActivityManager;
 import android.app.KeyguardManager;
 import android.app.admin.DevicePolicyManager;
 import android.content.Intent;
@@ -257,20 +258,27 @@
                 }
             }
 
-            // Expects no output on success or failure
-            ShellCommand.builder("am switch-user")
-                    .addOperand(mId)
-                    .allowEmptyOutput(true)
-                    .validate(String::isEmpty)
-                    .execute();
+            ActivityManager am = TestApis.context().instrumentedContext().getSystemService(
+                    ActivityManager.class);
+
+            boolean switched = false;
+            try (PermissionContext p = TestApis.permissions().withPermission(CREATE_USERS)) {
+                switched = am.switchUser(userHandle());
+            }
+
+            if (!switched) {
+                // TODO(273229540): It might take a while to fail - we should stream from the
+                // start of the call
+                throw new NeneException("Error switching user to " + this
+                        + ". Relevant logcat: " + TestApis.logcat().dump(
+                                (line) -> line.contains("ActivityManager")));
+            }
 
             if (Versions.meetsMinimumSdkVersionRequirement(R)) {
                 broadcastReceiver.awaitForBroadcast();
             } else {
                 Thread.sleep(20000);
             }
-        } catch (AdbException e) {
-            throw new NeneException("Could not switch to user", e);
         } catch (InterruptedException e) {
             Log.e(LOG_TAG, "Interrupted while switching user", e);
         } finally {
diff --git a/hostsidetests/devicepolicy/app/IntentSender/src/com/android/cts/intent/sender/IntentSenderActivity.java b/hostsidetests/devicepolicy/app/IntentSender/src/com/android/cts/intent/sender/IntentSenderActivity.java
index 935090f..72f27c5 100644
--- a/hostsidetests/devicepolicy/app/IntentSender/src/com/android/cts/intent/sender/IntentSenderActivity.java
+++ b/hostsidetests/devicepolicy/app/IntentSender/src/com/android/cts/intent/sender/IntentSenderActivity.java
@@ -77,7 +77,8 @@
         if (result != null) {
             Log.d(TAG, "Result intent: " + result.data);
         } else {
-            Log.d(TAG, "null result after " + timeoutSec + "s");
+            Log.d(TAG, "no result after " + timeoutSec
+                    + "s (see log for \"onActivityResult()\" to see actual result");
         }
         return (result != null) ? result.data : null;
     }