Android Open Source - khandroid Khandroid Async Task






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.kat;
// w ww. j  a v a2s  .  c o m
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.slf4j.LoggerFactory;

import android.annotation.TargetApi;
import android.os.AsyncTask;
import android.os.Build;


abstract public class KhandroidAsyncTask<Params, Progress, Result> {
    private InnerAsyncTask<Params, Progress, Result> mTask;
    private KatListener<Progress, Result> mListener;
    private boolean mIsSerialized = true;

    private final org.slf4j.Logger mLogger = LoggerFactory.getLogger(KhandroidAsyncTask.class
            .getSimpleName());


    public KhandroidAsyncTask() {
        mTask = new InnerAsyncTask<Params, Progress, Result>(this);
    }


    public final AsyncTask<Params, Progress, Result> execute(KatListener<Progress, Result> listener,
                                                             Params... params) {
        mListener = listener;
        return execute(params);
    }


    @TargetApi(11)
    public final AsyncTask<Params, Progress, Result> execute(Params... params) {
        mLogger.trace("Executing KhandroidAsyncTask");

        if (!mIsSerialized && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)) {
            return mTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
        } else {
            return mTask.execute(params);
        }
    }


    public void detach() {
        mLogger.trace("detach " + this);
        mListener = null;
    }


    public void attach(KatListener<Progress, Result> listener) {
        if (listener != null) {
            mLogger.trace("attach " + this);
            this.mListener = listener;
        } else {
            throw new IllegalArgumentException("executor == null");
        }
    }


    public AsyncTask.Status getStatus() {
        if (mTask != null) {
            return mTask.getStatus();
        } else {
            return null;
        }
    }


    public final boolean isCancelled() {
        return mTask.isCancelled();
    }


    public final boolean cancel(boolean mayInterruptIfRunning) {
        return mTask.cancel(mayInterruptIfRunning);
    }


    public final Result get(long timeout, TimeUnit unit) throws InterruptedException,
                                                        ExecutionException,
                                                        TimeoutException {
        return mTask.get(timeout, unit);
    }


    public final Result get() throws InterruptedException, ExecutionException {
        return mTask.get();
    }


    protected void onPostExecuteCaller(Result result) {
        onPostExecute(result);
        if (mListener != null) {
            mListener.onTaskCompleted(result);
        }
    }


    protected void onPostExecute(Result result) {
        // intentionally left empty
    }


    protected void onPreExecute() {
        // intentionally left empty
    }


    protected void onProgressUpdateCaler(Progress... values) {
        onProgressUpdate(values);
        if (mListener != null) {
            mListener.onTaskPublishProgress(values);
        }
    }


    protected void onProgressUpdate(Progress... values) {
        // intentionally left empty
    }


    protected void onCancelledCaller() {
        onCancelled();
        if (mListener != null) {
            mListener.onTaskCancelled();
        }
    }


    protected void onCancelled() {
        // intentionally left empty
    }


    protected final void publishProgress(Progress... values) {
        mTask.publishProgressInner(values);
    }


    abstract protected Result doInBackground(Params... params);

    private static class InnerAsyncTask<T, U, V> extends AsyncTask<T, U, V> {
        private KhandroidAsyncTask<T, U, V> mWrapper;


        public InnerAsyncTask(KhandroidAsyncTask<T, U, V> wrapper) {
            super();
            mWrapper = wrapper;
        }


        @Override
        protected void onPreExecute() {
            mWrapper.onPreExecute();
        }


        @Override
        protected void onPostExecute(V result) {
            mWrapper.onPostExecuteCaller(result);
        }


        @Override
        protected void onProgressUpdate(U... values) {
            mWrapper.onProgressUpdateCaler(values);
        }


        @Override
        protected void onCancelled() {
            mWrapper.onCancelledCaller();
        }


        @Override
        protected V doInBackground(T... params) {
            return (V) mWrapper.doInBackground(params);
        }


        protected void publishProgressInner(U... values) {
            super.publishProgress(values);
        }
    }

    public interface KatListener<Progress, Result> {
        void onTaskPublishProgress(Progress[] progress);


        void onTaskCancelled();


        void onTaskCompleted(Result result);
    }


    public boolean isSerialized() {
        return mIsSerialized;
    }


    public void setIsSerialized(boolean isSerialized) {
        mIsSerialized = isSerialized;
    }

}




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