Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. public class AsynkTaskJob<T> {
  2.  
  3. private CustomAsyncTask mCustomAsyncTask;
  4. private Context context;
  5.  
  6.  
  7. public AsynkTaskJob(Job job) {
  8. mCustomAsyncTask = new CustomAsyncTask(job);
  9.  
  10. }
  11.  
  12. public AsynkTaskJob( Context context, Job job) {
  13. mCustomAsyncTask = new CustomAsyncTask(job, context);
  14.  
  15. }
  16.  
  17. public AsynkTaskJob( Context context, String loadingMessage, Job job) {
  18. mCustomAsyncTask = new CustomAsyncTask(job, context, loadingMessage);
  19. }
  20.  
  21. public void start() {
  22. mCustomAsyncTask.execute();
  23. }
  24.  
  25. // CustomAsyncTask
  26. private class CustomAsyncTask extends AsyncTask<Void, Void, T> {
  27.  
  28. private Job<T> mJob;
  29. private Exception mException;
  30. private String loadingMessage = null;
  31. private Context context;
  32. private DialogUtils.DialogLoader dialogLoader;
  33.  
  34.  
  35. private CustomAsyncTask(Job job) {
  36. mJob = job;
  37. }
  38.  
  39. public CustomAsyncTask(Job job, Context context) {
  40. this.context = context;
  41. mJob = job;
  42. }
  43.  
  44. public CustomAsyncTask(Job job, Context context, String loadingMessage) {
  45. this.context = context;
  46. this.loadingMessage = loadingMessage;
  47. mJob = job;
  48. }
  49.  
  50. @Override
  51. protected void onPreExecute() {
  52. super.onPreExecute();
  53.  
  54. if(context != null){
  55. dialogLoader = DialogUtils.showLoader(context, loadingMessage);
  56. dialogLoader.setCancelable(false);
  57. }
  58.  
  59. }
  60.  
  61. @Override
  62. protected T doInBackground(Void... voids) {
  63.  
  64. try {
  65.  
  66. return mJob.onStart();
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. mException = e;
  70. return null;
  71. }
  72. }
  73.  
  74. @Override
  75. protected void onPostExecute(T type) {
  76. super.onPostExecute(type);
  77.  
  78. if (mException == null)
  79. mJob.onComplete(type);
  80. else
  81. mJob.onError(mException);
  82.  
  83.  
  84. if(dialogLoader != null)
  85. dialogLoader.dismiss();
  86. }
  87. }
  88.  
  89. // The Job interface
  90. public interface Job<T> {
  91. public T onStart() throws Exception;
  92. // public void onStart() throws Exception;
  93. public void onComplete(T object);
  94. public void onError(Exception e);
  95. }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement