1717
1818public class Main extends AppCompatActivity implements View .OnClickListener , Task .AdvancedCallback , OnAlertDialogClickListener {
1919
20- private static final String TASK_SIMPLE = "Demo-task" ;
20+ private static final String TASK_PROGRESS = "Demo-task" ;
21+ private static final String DIALOG_PROGRESS = "progress-dialog" ;
2122
2223 private ProgressDialog progressDialog ;
2324
2425 @ Override
2526 protected void onCreate (Bundle savedInstanceState ) {
2627 super .onCreate (savedInstanceState );
2728 setContentView (R .layout .activity_main );
28- Toolbar toolbar = (Toolbar ) findViewById (R .id .toolbar );
29- setSupportActionBar (toolbar );
29+ setSupportActionBar ((Toolbar ) findViewById (R .id .toolbar ));
3030
31- FloatingActionButton fab = (FloatingActionButton ) findViewById (R .id .fab );
32- fab .setOnClickListener (this );
31+ findViewById (R .id .fab ).setOnClickListener (this );
32+
33+ findViewById (R .id .button_no_ui_task ).setOnClickListener (this );
34+ findViewById (R .id .button_progress_task ).setOnClickListener (this );
3335 }
3436
3537 @ Override
3638 protected void onStart () {
3739 super .onStart ();
38- progressDialog = ProgressDialog .getExistingInstance (getSupportFragmentManager (), "progress-dialog" );
39- getTaskHandler ().attachListener (TASK_SIMPLE , this );
40+ progressDialog = ProgressDialog .getExistingInstance (getSupportFragmentManager (), DIALOG_PROGRESS );
41+ getTaskHandler ().attachListener (TASK_PROGRESS , this );
4042 }
4143
4244 @ Override
4345 public void onClick (View v ) {
44- if (getTaskHandler ().isRunning (TASK_SIMPLE )){
45- Toast .makeText (this , "Task already running" , Toast .LENGTH_SHORT ).show ();
46- }
46+ final int id = v .getId ();
47+ if (id == R .id .fab ){
48+ //TODO info
49+ } else if (id == R .id .button_progress_task ) {
50+ if (getTaskHandler ().isRunning (TASK_PROGRESS )) {
51+ Toast .makeText (this , "Task already running." , Toast .LENGTH_SHORT ).show ();
52+ }
53+ SimpleTask task = new SimpleTask (TASK_PROGRESS );
54+ getTaskHandler ().execute (task , this );
55+
56+ } else if (id == R .id .button_no_ui_task ){
57+ TaskWithoutCallback task = new TaskWithoutCallback (this );
58+ TaskExecutor .execute (task );
4759
48- SimpleTask task = new SimpleTask (TASK_SIMPLE );
49- getTaskHandler ().execute (task , this );
50-
51- TaskWithoutCallback callback = new TaskWithoutCallback (this );
52- TaskExecutor .execute (callback );
60+ }
5361 }
5462
5563 public TaskHandler getTaskHandler (){
@@ -58,19 +66,19 @@ public TaskHandler getTaskHandler(){
5866
5967 @ Override
6068 public void onPreExecute (Task <?, ?> task ) {
61- progressDialog = ProgressDialog .showIfNotShowing (getSupportFragmentManager (), "progress-dialog" );
69+ progressDialog = ProgressDialog .showIfNotShowing (getSupportFragmentManager (), DIALOG_PROGRESS );
6270 }
6371
6472 @ Override
6573 public void onPostExecute (Task <?, ?> task ) {
6674 progressDialog .dismiss ();
67- Snackbar .make (findViewById (android .R .id .content ), "Task finished." , Snackbar .LENGTH_LONG ).show ();
75+ Snackbar .make (findViewById (android .R .id .content ), "'Progress task' finished." , Snackbar .LENGTH_LONG ).show ();
6876 }
6977
7078 @ Override
7179 public void onCanceled (Task <?, ?> task ) {
7280 progressDialog .dismiss ();
73- Snackbar .make (findViewById (android .R .id .content ), "Task canceled." , Snackbar .LENGTH_LONG ).show ();
81+ Snackbar .make (findViewById (android .R .id .content ), "'Progress task' canceled." , Snackbar .LENGTH_LONG ).show ();
7482 }
7583
7684 @ Override
@@ -80,6 +88,6 @@ public void onProgressUpdate(Task<?, ?> task, Object progress) {
8088
8189 @ Override
8290 public void onDialogFragmentClick (DialogFragment fragment , int which ) {
83- getTaskHandler ().cancel (TASK_SIMPLE );
91+ getTaskHandler ().cancel (TASK_PROGRESS );
8492 }
8593}
0 commit comments