Search in sources :

Example 1 with CancelledException

use of org.andengine.util.exception.CancelledException in project AndEngine by nicolasgramlich.

the class ActivityUtils method doAsync.

public static <T> void doAsync(final Context pContext, final CharSequence pTitle, final CharSequence pMessage, final Callable<T> pCallable, final Callback<T> pCallback, final Callback<Exception> pExceptionCallback, final boolean pCancelable) {
    new AsyncTask<Void, Void, T>() {

        private ProgressDialog mPD;

        private Exception mException = null;

        @Override
        public void onPreExecute() {
            this.mPD = ProgressDialog.show(pContext, pTitle, pMessage, true, pCancelable);
            if (pCancelable) {
                this.mPD.setOnCancelListener(new OnCancelListener() {

                    @Override
                    public void onCancel(final DialogInterface pDialogInterface) {
                        pExceptionCallback.onCallback(new CancelledException());
                        pDialogInterface.dismiss();
                    }
                });
            }
            super.onPreExecute();
        }

        @Override
        public T doInBackground(final Void... params) {
            try {
                return pCallable.call();
            } catch (final Exception e) {
                this.mException = e;
            }
            return null;
        }

        @Override
        public void onPostExecute(final T result) {
            try {
                this.mPD.dismiss();
            } catch (final Exception e) {
                Debug.e("Error", e);
            }
            if (this.isCancelled()) {
                this.mException = new CancelledException();
            }
            if (this.mException == null) {
                pCallback.onCallback(result);
            } else {
                if (pExceptionCallback == null) {
                    Debug.e("Error", this.mException);
                } else {
                    pExceptionCallback.onCallback(this.mException);
                }
            }
            super.onPostExecute(result);
        }
    }.execute((Void[]) null);
}
Also used : DialogInterface(android.content.DialogInterface) CancelledException(org.andengine.util.exception.CancelledException) ProgressDialog(android.app.ProgressDialog) CancelledException(org.andengine.util.exception.CancelledException) OnCancelListener(android.content.DialogInterface.OnCancelListener)

Aggregations

ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1 OnCancelListener (android.content.DialogInterface.OnCancelListener)1 CancelledException (org.andengine.util.exception.CancelledException)1