Skip to content

Instantly share code, notes, and snippets.

@ftabashir
Created November 20, 2017 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ftabashir/7d5135165261b4df84dd7623fedcac28 to your computer and use it in GitHub Desktop.
Save ftabashir/7d5135165261b4df84dd7623fedcac28 to your computer and use it in GitHub Desktop.
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment