MultiUserUtils { public static boolean hasMultipleUsers(@Nullable IDevice device, long timeout, TimeUnit units, boolean defaultValue) { if (device == null) { return defaultValue; } if (device.getVersion().getApiLevel() < AndroidVersion.SUPPORTS_MULTI_USER.getApiLevel()) { return false; } PmListUserReceiver receiver = new PmListUserReceiver(); try { device.executeShellCommand("pm list users", receiver, timeout, units); } catch (Exception e) { Logger.getInstance(MultiUserUtils.class).warn("Timed out waiting for output from `pm list users`, returning " + defaultValue); return defaultValue; } return receiver.getNumUsers() > 1; }  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 hasMultipleUsers() throws Exception { String[] pmOutput = new String[]{ "Users:", " UserInfo{0:User0:13} running", " UserInfo{11:Sample Managed Profile:30} running" }; IDevice device = createListUsersMockDevice(pmOutput); assertTrue(MultiUserUtils.hasMultipleUsers(device, 200, TimeUnit.MILLISECONDS, false)); pmOutput = new String[]{ "Users:", " UserInfo{0:User0:13} running" }; device = createListUsersMockDevice(pmOutput); assertFalse(MultiUserUtils.hasMultipleUsers(device, 200, TimeUnit.MILLISECONDS, true)); pmOutput = new String[]{ "WARNING: linker: libdvm.so has text relocations. This is wasting memory and is a security risk. Please fix.", "Users:", "\tUserInfo{0:Owner:13}" }; device = createListUsersMockDevice(pmOutput); assertFalse(MultiUserUtils.hasMultipleUsers(device, 200, TimeUnit.MILLISECONDS, true)); }