package fr.utc.assos.uvweb.io.base;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;

/**
 * A base UI-less fragment to easily manage async queries while being tight
 * to the corresponding Activity or Fragment lifecycle.
 * If the attached Activity or Fragment gets destroyed by a configuration
 * change (like a rotation), the {@link Callbacks} reference needs to be updated
 * using the {@code setCallCallbacks()} method.
 */
public abstract class BaseTaskFragment extends SherlockFragment {
	public static final int THREAD_DEFAULT_POLICY = 0;
	public static final int THREAD_POOL_EXECUTOR_POLICY = 1;
	protected FragmentTask mTask;
	private Callbacks mCallbacks;
	private boolean mIsRunning;

	public BaseTaskFragment() {
	}

	// Public API
	public static <T extends BaseTaskFragment> T get(FragmentManager fm, Class<T> clazz) {
		final String tag = clazz.getSimpleName();
		T instance = clazz.cast(fm.findFragmentByTag(tag));
		if (instance == null) {
			try {
				instance = clazz.newInstance();
				fm.beginTransaction().add(instance, tag).commit();
			} catch (java.lang.InstantiationException e) {
				throw new IllegalArgumentException("Class must be instantiable");
			} catch (IllegalAccessException e) {
				throw new IllegalArgumentException("Class must be accessible");
			}
		}
		return instance;
	}

	public void startNewTask() {
		startNewTask(THREAD_DEFAULT_POLICY);
	}

	public void startNewTask(final int threadMode) {
		if (mTask != null) {
			mTask.cancel(true);
		}
		if (threadMode == THREAD_DEFAULT_POLICY) {
			start();
		} else if (threadMode == THREAD_POOL_EXECUTOR_POLICY) {
			startOnThreadPoolExecutor();
		} else {
			throw new IllegalArgumentException("threadMode must be either THREAD_DEFAULT_POLICY" +
					"or THREAD_POOL_EXECUTOR_POLICY");
		}
	}

	public boolean isRunning() {
		return mIsRunning;
	}

	// Lifecycle
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		if (activity instanceof Callbacks) {
			mCallbacks = (Callbacks) activity;
		}
	}

	/**
	 * This method will only be called once when the retained
	 * Fragment is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Retain this fragment across configuration changes.
		setRetainInstance(true);
		setUserVisibleHint(false);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return null;
	}

	@Override
	public void onStart() {
		super.onStart();

		final Fragment f = getTargetFragment();
		if (!(f instanceof Callbacks)) {
			throw new IllegalStateException("Target fragment must implement Callbacks");
		}
		setCallbacks((Callbacks) f);
	}

	/**
	 * Set the callback to null so we don't accidentally leak the
	 * Fragment instance.
	 */
	@Override
	public void onDetach() {
		mCallbacks = null;

		super.onDetach();
	}

	protected abstract void start();

	protected abstract void startOnThreadPoolExecutor();

	protected void setCallbacks(Callbacks callbacks) {
		if (callbacks == null) {
			if (mTask != null) {
				mTask.cancel(true);
			}
		}
		mCallbacks = callbacks;
	}

	public interface Callbacks<Result> {
		void onPreExecute();

		void onPostExecute(Result result);

		void onError();
	}

	public abstract class FragmentTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
		protected static final String API_ENDPOINT = "http://192.168.0.15/Uvweb/web/app_dev.php/uv/app/";

		@Override
		protected void onPreExecute() {
			if (mCallbacks != null) {
				mCallbacks.onPreExecute();
			}
			mIsRunning = true;
		}

		@Override
		@SuppressWarnings("unchecked")
		protected void onPostExecute(Result result) {
			mIsRunning = false;
			if (mCallbacks != null) {
				if (result == null) {
					mCallbacks.onError();
				} else {
					mCallbacks.onPostExecute(result);
				}
			}
		}

		@Override
		protected void onCancelled() {
			mCallbacks = null;
			mIsRunning = false;
		}
	}
}

@@ -1749,17 +1749,241 @@
 rue);%0A%09%09
-%7D
+%09mTask = null;%0A%09%09%7D%0A%09%09final Fragment target = getTargetFragment();%0A%09%09if (!(target instanceof Callbacks)) %7B%0A%09%09%09throw new IllegalStateException(%22Target fragment must implement Callbacks%22);%0A%09%09%7D%0A%09%09setCallbacks((Callbacks) target);
 %0A%09%09if (t
@@ -2986,34 +2986,97 @@
 d on
-Start() %7B%0A%09%09super.onStart(
+ActivityCreated(Bundle savedInstanceState) %7B%0A%09%09super.onActivityCreated(savedInstanceState
 );%0A%0A
@@ -3092,17 +3092,22 @@
 ragment 
-f
+target
  = getTa
@@ -3132,11 +3132,14 @@
 if (
-!(f
+target
  ins
@@ -3160,96 +3160,12 @@
 cks)
-)
  %7B%0A%09
-%09%09throw new IllegalStateException(%22Target fragment must implement Callbacks%22);%0A%09%09%7D%0A
 %09%09se
@@ -3191,11 +3191,20 @@
 ks) 
-f);
+target);%0A%09%09%7D
 %0A%09%7D%0A
@@ -3535,43 +3535,36 @@
 if (
-callbacks == null) %7B%0A%09%09%09if (mTask !
+mTask != null && callbacks =
 = nu
@@ -3565,25 +3565,24 @@
  == null) %7B%0A
-%09
 %09%09%09mTask.can
@@ -3591,25 +3591,37 @@
 l(true);%0A%09%09%09
-%7D
+mTask = null;
 %0A%09%09%7D%0A%09%09mCall

Fix a few bugs introduced in the previous dev iteration