Guest User

Untitled

a guest
Nov 21st, 2017
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. package com.iranfmcg.dokan.customer.helper;
  2.  
  3. import android.os.AsyncTask;
  4. import android.support.annotation.MainThread;
  5. import android.support.annotation.WorkerThread;
  6.  
  7. /* Copyright (C) Farzad Tabashir <ftabashir@gmail.com> 15 February 2017, 8:38 AM
  8. * All Rights Reserved
  9. */
  10. public abstract class SafeAsyncTask<Params, Progress, Result> {
  11.  
  12. private AsyncTask<Params, Progress, Result> asyncTask;
  13.  
  14. private boolean safePre, safeBackground, safePost;
  15. private ExceptionHandler preExceptionHandler, backgroundExceptionHandler, postExceptionHandler;
  16.  
  17. public SafeAsyncTask() {
  18. asyncTask = new Task();
  19. }
  20.  
  21. public SafeAsyncTask(boolean throttleExceptions) {
  22. this();
  23. if (throttleExceptions) {
  24. ExceptionHandler exceptionHandler = exception -> {
  25. //do nothing, throttle exception
  26. };
  27. setPreExceptionHandler(exceptionHandler);
  28. setBackgroundExceptionHandler(exceptionHandler);
  29. setPostExceptionHandler(exceptionHandler);
  30. }
  31. }
  32.  
  33. public SafeAsyncTask<Params, Progress, Result> setPreExceptionHandler(ExceptionHandler exceptionHandler) {
  34. this.safePre = true;
  35. this.preExceptionHandler = exceptionHandler;
  36. return this;
  37. }
  38.  
  39. public SafeAsyncTask<Params, Progress, Result> setBackgroundExceptionHandler(ExceptionHandler exceptionHandler) {
  40. this.safeBackground = true;
  41. this.backgroundExceptionHandler = exceptionHandler;
  42. return this;
  43. }
  44.  
  45. public SafeAsyncTask<Params, Progress, Result> setPostExceptionHandler(ExceptionHandler exceptionHandler) {
  46. this.safePost = true;
  47. this.postExceptionHandler = exceptionHandler;
  48. return this;
  49. }
  50.  
  51. @MainThread
  52. protected void onPreExecute() {
  53. }
  54.  
  55. @SuppressWarnings("unchecked")
  56. @WorkerThread
  57. protected abstract Result doInBackground(Params... params);
  58.  
  59. @MainThread
  60. protected void onPostExecute(Result result) {
  61. }
  62.  
  63. @SafeVarargs
  64. @MainThread
  65. protected final void onProgressUpdate(Progress... values) {
  66. }
  67.  
  68. @MainThread
  69. protected void onCancelled() {
  70. }
  71.  
  72. @MainThread
  73. protected void onCancelled(Result result) {
  74. onCancelled();
  75. }
  76.  
  77. public void execute(Params... params) {
  78. asyncTask.execute(params);
  79. }
  80.  
  81. public interface ExceptionHandler {
  82. void handle(Exception exception);
  83. }
  84.  
  85. private class Task extends AsyncTask<Params, Progress, Result> {
  86. @SafeVarargs
  87. @Override
  88. protected final Result doInBackground(Params... params) {
  89. if (safeBackground) {
  90. try {
  91. return SafeAsyncTask.this.doInBackground(params);
  92. } catch (Exception exception) {
  93. backgroundExceptionHandler.handle(exception);
  94. return null;
  95. }
  96. } else {
  97. return SafeAsyncTask.this.doInBackground(params);
  98. }
  99. }
  100.  
  101. @Override
  102. protected void onPreExecute() {
  103. if (safePre) {
  104. try {
  105. SafeAsyncTask.this.onPreExecute();
  106. } catch (Exception exception) {
  107. preExceptionHandler.handle(exception);
  108. }
  109. } else {
  110. SafeAsyncTask.this.onPreExecute();
  111. }
  112. }
  113.  
  114. @Override
  115. protected void onPostExecute(Result result) {
  116. if (safePost) {
  117. try {
  118. SafeAsyncTask.this.onPostExecute(result);
  119. } catch (Exception exception) {
  120. postExceptionHandler.handle(exception);
  121. }
  122. } else {
  123. SafeAsyncTask.this.onPostExecute(result);
  124. }
  125. }
  126.  
  127. @SafeVarargs
  128. @Override
  129. protected final void onProgressUpdate(Progress... values) {
  130. SafeAsyncTask.this.onProgressUpdate(values);
  131. }
  132.  
  133. @Override
  134. protected void onCancelled(Result result) {
  135. SafeAsyncTask.this.onCancelled(result);
  136. }
  137.  
  138. @Override
  139. protected void onCancelled() {
  140. SafeAsyncTask.this.onCancelled();
  141. }
  142. }
  143. }
Add Comment
Please, Sign In to add comment