Android Open Source - khandroid Rest Persist Fragment






From Project

Back to project page khandroid.

License

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.

Java Source Code

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;
    }
}




Java Source Code List

com.github.khandroid.JsonFunctionality.java
com.github.khandroid.http.functionality.HttpFunctionalityImpl.java
com.github.khandroid.http.functionality.HttpFunctionalityWCookiesImpl.java
com.github.khandroid.http.functionality.HttpFunctionalityWCookies.java
com.github.khandroid.http.functionality.HttpFunctionality.java
com.github.khandroid.http.misc.FileDownloader.java
com.github.khandroid.http.misc.KhandroidBasicResponseHandler.java
com.github.khandroid.http.misc.SynchronizedCookieStore.java
com.github.khandroid.http.request.GetRequestBuilder.java
com.github.khandroid.http.request.PostRequestBuilder.java
com.github.khandroid.http.request.RequestBuilder.java
com.github.khandroid.http.ssl.DefaultSslHttpClient.java
com.github.khandroid.http.ssl.KhandroidX509TrustManager.java
com.github.khandroid.http.ssl.SslSocketFactory.java
com.github.khandroid.kat.ActivityKatExecutorFunctionality.java
com.github.khandroid.kat.FragmentKatExecutorFunctionality.java
com.github.khandroid.kat.KatExecutor.java
com.github.khandroid.kat.KhandroidAsyncTask.java
com.github.khandroid.misc.ActivityUtils.java
com.github.khandroid.misc.DateTimeHelper.java
com.github.khandroid.misc.ImageUtils.java
com.github.khandroid.misc.LocationUtils.java
com.github.khandroid.misc.NetUtils.java
com.github.khandroid.misc.StringUtils.java
com.github.khandroid.misc.ViewUtils.java
com.github.khandroid.rest.InvalidJsonString.java
com.github.khandroid.rest.InvalidResponseException.java
com.github.khandroid.rest.KhRestExchangeBuilder.java
com.github.khandroid.rest.KhRestFunctionalityImpl.java
com.github.khandroid.rest.KhRestFunctionality.java
com.github.khandroid.rest.KhRestResult.java
com.github.khandroid.rest.MalformedResponseException.java
com.github.khandroid.rest.RawRestResult.java
com.github.khandroid.rest.RestAsyncTask.java
com.github.khandroid.rest.RestExchangeBuilder.java
com.github.khandroid.rest.RestExchangeFailedException.java
com.github.khandroid.rest.RestExchangeOutcome.java
com.github.khandroid.rest.RestExchange.java
com.github.khandroid.rest.RestFragmentFunctionality.java
com.github.khandroid.rest.RestFunctionalityImpl.java
com.github.khandroid.rest.RestFunctionality.java
com.github.khandroid.rest.RestPersistFragment.java
com.github.khandroid.rest.RestResponse.java
com.github.khandroid.rest.RestResult.java
com.github.khandroid.rest.UnexpectedResponseException.java
com.github.khandroid.state.StateEvent.java
com.github.khandroid.state.StateMachineImpl.java
com.github.khandroid.state.StateMachine.java
com.github.khandroid.state.StateSkeleton.java
com.github.khandroid.state.State.java
com.github.khandroid.state_app.AppStateContextImpl.java
com.github.khandroid.state_app.AppStateContext.java
com.github.khandroid.state_app.AppState.java
com.github.khandroid.state_app.InvalidAppStateException.java
com.github.khandroid.state_app.SkeletonAppState.java
com.github.khandroid.state_app.StateEvent.java
com.github.khandroid.ui_functionality.ActivityAttachable.java
com.github.khandroid.ui_functionality.ActivityAttachedFunctionality.java
com.github.khandroid.ui_functionality.ActivityUniqueAttachedFunctionality.java
com.github.khandroid.ui_functionality.FragmentAttachable.java
com.github.khandroid.ui_functionality.FragmentAttachedFunctionality.java
com.github.khandroid.ui_functionality.FragmentUniqueAttachedFunctionality.java
com.github.khandroid.ui_functionality.HostActivity.java
com.github.khandroid.ui_functionality.HostFragment.java
com.github.khandroid.ui_functionality.SuperNotCalledException.java
com.github.khandroid.views.ImageViewWithContextMenuInfo.java
com.github.khandroid.volley.GetJsonObjectRequestBuilder.java
com.github.khandroid.volley.GetStringRequestBuilder.java
com.github.khandroid.volley.GetVRestRequestBuilder.java
com.github.khandroid.volley.PostJsonObjectRequestBuilder.java
com.github.khandroid.volley.PostRequestBuilderImpl.java
com.github.khandroid.volley.PostRequestBuilder.java
com.github.khandroid.volley.PostStringRequestBuilder.java
com.github.khandroid.volley.PostVRestRequestBuilder.java
com.github.khandroid.volley.RequestBuilder.java
com.github.khandroid.volley.VRestRequestBuilder.java
com.github.khandroid.volley.VRestRequest.java