Back to project page khandroid.
The source code is released under:
Apache License
If you think the Android project khandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.github.khandroid.rest; //from w w w . j a va 2 s. c om import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import javax.inject.Inject; import org.slf4j.LoggerFactory; import android.app.Activity; import android.app.Fragment; import android.os.Looper; import com.github.khandroid.rest.RestExchangeFailedException; public class RestPersistFragment extends Fragment { private final org.slf4j.Logger mLogger = LoggerFactory.getLogger(RestPersistFragment.class .getSimpleName()); @Inject RestFunctionality mRestFunc; private volatile int executing = 0; private Queue<RestExchangeOutcome<?>> mPendingOutcomes = new ConcurrentLinkedQueue<RestExchangeOutcome<?>>(); @Inject public RestPersistFragment() { super(); setRetainInstance(true); mLogger.debug("Creating PersistFragment"); } public <T> void executeRestExchange(final RestExchange<T> x) { if (Looper.getMainLooper().getThread() == Thread.currentThread()) { // On UI thread. Thread t = new Thread(new Runnable() { @Override public void run() { try { executing++; T result = mRestFunc.execute(x); executing--; onExchangeResult(x, result); } catch (RestExchangeFailedException e) { executing--; onExchangeError(x); } } }); t.start(); } else { // Not on UI thread. throw new IllegalStateException("This method must be invoked on UI thread"); } } private synchronized <T> void onExchangeResult(RestExchange<T> x, T result) { RestExchangeOutcome<T> outcome = new RestExchangeOutcome<T>(x, result, false); deliverOutcome(outcome); } private synchronized <T> void onExchangeError(RestExchange<T> x) { RestExchangeOutcome<T> outcome = new RestExchangeOutcome<T>(x, null, true); deliverOutcome(outcome); } private <T> void deliverOutcome(final RestExchangeOutcome<T> outcome) { final Listener parentFragment = (Listener) getTargetFragment(); if (parentFragment != null) { Activity act = getActivity(); if (act != null) { act.runOnUiThread(new Runnable() { @Override public void run() { parentFragment.onExchangeOutcome(outcome); } }); } } else { mPendingOutcomes.add(outcome); } } public static interface Listener { void onExchangeOutcome(RestExchangeOutcome<?> outcome); } public boolean hasPendingOutcomes() { return mPendingOutcomes.size() > 0 ? true : false; } public RestExchangeOutcome<?> nextPendingOutcome() { return mPendingOutcomes.poll(); } public void clearPendingOutcomes() { mPendingOutcomes.clear(); } @Override public void setTargetFragment(Fragment fragment, int requestCode) { if (fragment != null) { if (fragment instanceof RestPersistFragment.Listener) { super.setTargetFragment(fragment, requestCode); } else { throw new IllegalArgumentException("Parent fragment must implement RestPersistFragment.Listener"); } } } public boolean isExecuting() { return executing > 0; } }