|
| 1 | +package org.neotech.app.retainabletasksdemo; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.design.widget.FloatingActionButton; |
| 5 | +import android.support.design.widget.Snackbar; |
| 6 | +import android.support.v4.app.DialogFragment; |
| 7 | +import android.support.v7.app.AppCompatActivity; |
| 8 | +import android.support.v7.widget.Toolbar; |
| 9 | +import android.view.View; |
| 10 | +import android.widget.Toast; |
| 11 | + |
| 12 | +import org.neotech.app.retainabletasksdemo.tasks.SimpleTask; |
| 13 | +import org.neotech.app.retainabletasksdemo.tasks.TaskWithoutCallback; |
| 14 | +import org.neotech.library.retainabletasks.Task; |
| 15 | +import org.neotech.library.retainabletasks.TaskExecutor; |
| 16 | +import org.neotech.library.retainabletasks.TaskHandler; |
| 17 | + |
| 18 | +public class Main extends AppCompatActivity implements View.OnClickListener, Task.AdvancedCallback, OnAlertDialogClickListener { |
| 19 | + |
| 20 | + private static final String TASK_SIMPLE = "Demo-task"; |
| 21 | + |
| 22 | + private ProgressDialog progressDialog; |
| 23 | + |
| 24 | + @Override |
| 25 | + protected void onCreate(Bundle savedInstanceState) { |
| 26 | + super.onCreate(savedInstanceState); |
| 27 | + setContentView(R.layout.activity_main); |
| 28 | + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 29 | + setSupportActionBar(toolbar); |
| 30 | + |
| 31 | + FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); |
| 32 | + fab.setOnClickListener(this); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + protected void onStart() { |
| 37 | + super.onStart(); |
| 38 | + progressDialog = ProgressDialog.getExistingInstance(getSupportFragmentManager(), "progress-dialog"); |
| 39 | + getTaskHandler().attachListener(TASK_SIMPLE, this); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onClick(View v) { |
| 44 | + if(getTaskHandler().isRunning(TASK_SIMPLE)){ |
| 45 | + Toast.makeText(this, "Task already running", Toast.LENGTH_SHORT).show(); |
| 46 | + } |
| 47 | + |
| 48 | + SimpleTask task = new SimpleTask(TASK_SIMPLE); |
| 49 | + getTaskHandler().execute(task, this); |
| 50 | + |
| 51 | + TaskWithoutCallback callback = new TaskWithoutCallback(this); |
| 52 | + TaskExecutor.execute(callback); |
| 53 | + } |
| 54 | + |
| 55 | + public TaskHandler getTaskHandler(){ |
| 56 | + return TaskHandler.getActivityTaskHandler(getSupportFragmentManager()); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onPreExecute(Task<?, ?> task) { |
| 61 | + progressDialog = ProgressDialog.showIfNotShowing(getSupportFragmentManager(), "progress-dialog"); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void onPostExecute(Task<?, ?> task) { |
| 66 | + progressDialog.dismiss(); |
| 67 | + Snackbar.make(findViewById(android.R.id.content), "Task finished.", Snackbar.LENGTH_LONG).show(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onCanceled(Task<?, ?> task) { |
| 72 | + progressDialog.dismiss(); |
| 73 | + Snackbar.make(findViewById(android.R.id.content), "Task canceled.", Snackbar.LENGTH_LONG).show(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onProgressUpdate(Task<?, ?> task, Object progress) { |
| 78 | + progressDialog.setProgress((int) progress); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void onDialogFragmentClick(DialogFragment fragment, int which) { |
| 83 | + getTaskHandler().cancel(TASK_SIMPLE); |
| 84 | + } |
| 85 | +} |
0 commit comments