class 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;
    }
  }

}