private static <V> V runOnMainSyncWithTimeoutAndWithCheckedExceptionsExpected(
      Callable<V> callable) throws Exception {
    Looper mainLooper = Looper.getMainLooper();
    if (mainLooper.getThread() == Thread.currentThread()) {
      // This method is being invoked on the main thread -- invoke the Callable inline to avoid
      // a deadlock.
      return callable.call();
    } else {
      FutureTask<V> task = new FutureTask<V>(callable);
      new Handler(mainLooper).post(task);
      try {
        return task.get(UI_ACTION_EFFECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
      } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof Exception) {
          throw (Exception) cause;
        } else {
          throw new RuntimeException("Execution on main thread failed", e);
        }
      }
    }
  }
Invokes the provided {@link Callable} on the main thread and blocks until the operation
completes or times out. If this method is invoked on the main thread, the {@code Callable} is
invoked immediately and no timeout is enforced.

<p>Exceptions thrown by the {@code Callable} are rethrown by this method.

@return result returned by the {@code Callable}.
javatests/com/google/android/apps/authenticator/testing/TestUtilities.java