MultiUserUtils { public static boolean isCurrentUserThePrimaryUser(@NotNull IDevice device, long timeout, TimeUnit units, boolean defaultValue) { if (device.getVersion().getApiLevel() < AndroidVersion.SUPPORTS_MULTI_USER.getApiLevel()) { return false; } CountDownLatch latch = new CountDownLatch(1); CollectingOutputReceiver receiver = new CollectingOutputReceiver(latch); try { device.executeShellCommand("am get-current-user", receiver); } catch (Exception e) { return defaultValue; } try { latch.await(timeout, units); } catch (InterruptedException e) { Logger.getInstance(MultiUserUtils.class).warn("Timed out waiting for output from `am get-current-user`, returning " + defaultValue); return defaultValue; } String output = receiver.getOutput(); try { return Integer.parseInt(output.trim()) == PRIMARY_USERID; } catch (NumberFormatException e) { if (output.length() > 40) { output = output.substring(0, 40) + "..."; } Logger.getInstance(MultiUserUtils.class).warn("Error parsing output of `am get-current-user`: " + output); return defaultValue; } }  static boolean isCurrentUserThePrimaryUser(@NotNull IDevice device, long timeout, TimeUnit units, boolean defaultValue); static boolean hasMultipleUsers(@Nullable IDevice device, long timeout, TimeUnit units, boolean defaultValue); static int getUserIdFromAmParameters(@NotNull String amFlags); static final int PRIMARY_USERID; }
@Test public void getCurrentUser() throws Exception { IDevice device = createCurrentUserMockDevice("10"); assertFalse(MultiUserUtils.isCurrentUserThePrimaryUser(device, 200, TimeUnit.MILLISECONDS, true)); device = createCurrentUserMockDevice("0"); assertTrue(MultiUserUtils.isCurrentUserThePrimaryUser(device, 200, TimeUnit.MILLISECONDS, false)); }