Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.iranfmcg.dokan.customer.helper;
- import android.os.AsyncTask;
- import android.support.annotation.MainThread;
- import android.support.annotation.WorkerThread;
- /* Copyright (C) Farzad Tabashir <ftabashir@gmail.com> 15 February 2017, 8:38 AM
- * All Rights Reserved
- */
- public abstract class SafeAsyncTask<Params, Progress, Result> {
- private AsyncTask<Params, Progress, Result> asyncTask;
- private boolean safePre, safeBackground, safePost;
- private ExceptionHandler preExceptionHandler, backgroundExceptionHandler, postExceptionHandler;
- public SafeAsyncTask() {
- asyncTask = new Task();
- }
- public SafeAsyncTask(boolean throttleExceptions) {
- this();
- if (throttleExceptions) {
- ExceptionHandler exceptionHandler = exception -> {
- //do nothing, throttle exception
- };
- setPreExceptionHandler(exceptionHandler);
- setBackgroundExceptionHandler(exceptionHandler);
- setPostExceptionHandler(exceptionHandler);
- }
- }
- public SafeAsyncTask<Params, Progress, Result> setPreExceptionHandler(ExceptionHandler exceptionHandler) {
- this.safePre = true;
- this.preExceptionHandler = exceptionHandler;
- return this;
- }
- public SafeAsyncTask<Params, Progress, Result> setBackgroundExceptionHandler(ExceptionHandler exceptionHandler) {
- this.safeBackground = true;
- this.backgroundExceptionHandler = exceptionHandler;
- return this;
- }
- public SafeAsyncTask<Params, Progress, Result> setPostExceptionHandler(ExceptionHandler exceptionHandler) {
- this.safePost = true;
- this.postExceptionHandler = exceptionHandler;
- return this;
- }
- @MainThread
- protected void onPreExecute() {
- }
- @SuppressWarnings("unchecked")
- @WorkerThread
- protected abstract Result doInBackground(Params... params);
- @MainThread
- protected void onPostExecute(Result result) {
- }
- @SafeVarargs
- @MainThread
- protected final void onProgressUpdate(Progress... values) {
- }
- @MainThread
- protected void onCancelled() {
- }
- @MainThread
- protected void onCancelled(Result result) {
- onCancelled();
- }
- public void execute(Params... params) {
- asyncTask.execute(params);
- }
- public interface ExceptionHandler {
- void handle(Exception exception);
- }
- private class Task extends AsyncTask<Params, Progress, Result> {
- @SafeVarargs
- @Override
- protected final Result doInBackground(Params... params) {
- if (safeBackground) {
- try {
- return SafeAsyncTask.this.doInBackground(params);
- } catch (Exception exception) {
- backgroundExceptionHandler.handle(exception);
- return null;
- }
- } else {
- return SafeAsyncTask.this.doInBackground(params);
- }
- }
- @Override
- protected void onPreExecute() {
- if (safePre) {
- try {
- SafeAsyncTask.this.onPreExecute();
- } catch (Exception exception) {
- preExceptionHandler.handle(exception);
- }
- } else {
- SafeAsyncTask.this.onPreExecute();
- }
- }
- @Override
- protected void onPostExecute(Result result) {
- if (safePost) {
- try {
- SafeAsyncTask.this.onPostExecute(result);
- } catch (Exception exception) {
- postExceptionHandler.handle(exception);
- }
- } else {
- SafeAsyncTask.this.onPostExecute(result);
- }
- }
- @SafeVarargs
- @Override
- protected final void onProgressUpdate(Progress... values) {
- SafeAsyncTask.this.onProgressUpdate(values);
- }
- @Override
- protected void onCancelled(Result result) {
- SafeAsyncTask.this.onCancelled(result);
- }
- @Override
- protected void onCancelled() {
- SafeAsyncTask.this.onCancelled();
- }
- }
- }
Add Comment
Please, Sign In to add comment