Skip to content
This repository was archived by the owner on Dec 17, 2020. It is now read-only.

Commit e0a1e8a

Browse files
author
Rolf Smit
committed
Polished demo app a bit.
1 parent b8558ab commit e0a1e8a

File tree

9 files changed

+59
-31
lines changed

9 files changed

+59
-31
lines changed

demo/src/main/java/org/neotech/app/retainabletasksdemo/Main.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,47 @@
1717

1818
public 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
}

demo/src/main/java/org/neotech/app/retainabletasksdemo/tasks/TaskWithoutCallback.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Created by Rolf on 29-2-2016.
1111
*/
12-
public class TaskWithoutCallback extends Task<Void, String> {
12+
public class TaskWithoutCallback extends Task<Void, Void> {
1313

1414
private final Context context;
1515

@@ -19,13 +19,18 @@ public TaskWithoutCallback(Context context) {
1919
}
2020

2121
@Override
22-
protected String doInBackground() {
23-
SystemClock.sleep(5000);
24-
return "Result";
22+
protected Void doInBackground() {
23+
SystemClock.sleep(4000);
24+
return null;
25+
}
26+
27+
@Override
28+
protected void onPreExecute() {
29+
Toast.makeText(context, "'Task without UI callback' started.", Toast.LENGTH_SHORT).show();
2530
}
2631

2732
@Override
2833
protected void onPostExecute() {
29-
Toast.makeText(context, "Task finished, result: " + getResult(), Toast.LENGTH_SHORT).show();
34+
Toast.makeText(context, "'Task without UI callback' finished.", Toast.LENGTH_SHORT).show();
3035
}
3136
}
595 Bytes
Loading
376 Bytes
Loading
796 Bytes
Loading
1.15 KB
Loading
1.6 KB
Loading

demo/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@
2929
android:layout_height="wrap_content"
3030
android:layout_gravity="bottom|end"
3131
android:layout_margin="@dimen/fab_margin"
32-
android:src="@android:drawable/ic_dialog_email" />
32+
android:src="@drawable/ic_info_outline" />
3333

3434
</android.support.design.widget.CoordinatorLayout>
Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
@@ -12,8 +12,23 @@
1212
tools:context="org.neotech.app.retainabletasksdemo.Main"
1313
tools:showIn="@layout/activity_main">
1414

15-
<TextView
16-
android:layout_width="wrap_content"
17-
android:layout_height="wrap_content"
18-
android:text="Hello World!" />
19-
</RelativeLayout>
15+
<LinearLayout
16+
android:orientation="vertical"
17+
android:gravity="center_horizontal"
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content">
20+
21+
<Button
22+
android:id="@+id/button_no_ui_task"
23+
android:layout_height="wrap_content"
24+
android:layout_width="wrap_content"
25+
android:text="Task without UI callback" />
26+
27+
<Button
28+
android:id="@+id/button_progress_task"
29+
android:layout_height="wrap_content"
30+
android:layout_width="wrap_content"
31+
android:text="Progress Task" />
32+
33+
</LinearLayout>
34+
</ScrollView>

0 commit comments

Comments
 (0)