/*
 * Copyright 2017 Maia Everett <maia@lucidfox.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.lucidfox.jpromises.android;

import android.os.Handler;
import android.os.Looper;

import org.lucidfox.jpromises.PromiseFactory;
import org.lucidfox.jpromises.annotation.GwtIncompatible;
import org.lucidfox.jpromises.core.DeferredInvoker;

/**
 * Promise factory that eases Android integration by providing standard deferred invokers for the main thread
 * and handler threads.
 */
@GwtIncompatible("android")
public class AndroidPromiseFactory extends PromiseFactory {
	/**
	 * Instantiates a new {@code AndroidPromiseFactory} that runs tasks deferred on the main thread.
	 */
	public AndroidPromiseFactory() {
		this(new Handler(Looper.getMainLooper()));
	}

	/**
	 * Instantiates a new {@code AndroidPromiseFactory} that uses the given handler's {@code post} method
	 * as the deferred invoker.
	 *
	 * @param handler the handler to use
	 */
	public AndroidPromiseFactory(final Handler handler) {
		super(new DeferredInvoker() {
			@Override
			public void invokeDeferred(final Runnable task) {
				handler.post(task);
			}
		});
	}

	/**
	 * Returns the string {@code "AndroidPromiseFactory"}.
	 */
	@Override
	public String toString() {
		return "AndroidPromiseFactory";
	}
}

@@ -1608,16 +1608,48 @@
 ctory %7B%0A
+%09private final Thread thread;%0A%09%0A
 %09/**%0A%09 *
@@ -2202,16 +2202,61 @@
 %09%7D%0A%09%09%7D);
+%0A%0A%09%09thread = handler.getLooper().getThread();
 %0A%09%7D%0A%0A%09/*
@@ -2313,10 +2313,274 @@
 tory
-%22%7D
+ (thread = %25THREAD%25)%22%7D, where %7B@code %25THREAD%25%7D is the result%0A%09 * of calling %7B@link Thread#toString()%7D on the thread to which this promise factory is bound%0A%09 * (usually the main thread, but possibly also a message loop thread associated with a custom %7B@link Handler%7D)
 .%0A%09 
@@ -2652,16 +2652,42 @@
 eFactory
+ (thread = %22 + thread + %22)
 %22;%0A%09%7D%0A%7D%0A

Add some diagnostic information to AndroidPromiseFactory.toString