// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

package com.google.appinventor.components.runtime.util;

import android.os.Handler;

/**
 * Utilities for handling asynchronous calls.
 *
 * @author markf@google.com (Mark Friedman)
 */

public class AsynchUtil {

  /**
   * Make an asynchronous call in a separate thread.
   * @param call a {@link Runnable} to run in the thread.
   */
  public static void runAsynchronously(final Runnable call) {
    Thread thread = new Thread(call);
    thread.start();
  }
  /**
   * Make an asynchronous call in a separate thread, with a callback that's run on the current
   * Android UI thread.
   * @param androidUIHandler  the Handler from the current Android context
   * @param call a {@link Runnable} to run in the thread.
   * @param callback a {@link Runnable} to run in the Android UI thread when the call above returns
   */
  public static void runAsynchronously(final Handler androidUIHandler,
                                       final Runnable call,
                                       final Runnable callback) {
    Runnable runnable = new Runnable() {
      public void run() {
        call.run();
        if (callback != null) {
          androidUIHandler.post(new Runnable() {
            public void run() {
              callback.run();
            }
          });
        }
      }
    };
    Thread thread = new Thread(runnable);
    thread.start();
  }
}

@@ -1601,11 +1601,581 @@
 t();%0A  %7D
+%0A%0A  public static %3CT%3E T runAsynchronously(final Callable%3CT%3E call) throws InterruptedException %7B%0A    final AtomicReference%3CT%3E result = new AtomicReference%3C%3E();%0A    synchronized (result) %7B%0A      AsynchUtil.runAsynchronously(new Runnable() %7B%0A        @Override%0A        public void run() %7B%0A          try %7B%0A            result.set(call.call());%0A          %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A          %7D%0A          synchronized (result) %7B%0A            result.notifyAll();%0A          %7D%0A        %7D%0A      %7D);%0A      result.wait();%0A    %7D%0A    return result.get();%0A  %7D
 %0A%7D%0A

Add missing AsynchUtil functionality