1- package org .neotech .app .retainabletasksdemo ;
1+ package org .neotech .app .retainabletasksdemo . activity ;
22
3- import android .content .Intent ;
4- import android .net .Uri ;
53import android .os .Bundle ;
64import android .support .annotation .NonNull ;
75import android .support .design .widget .Snackbar ;
86import android .support .v4 .app .DialogFragment ;
9- import android .support .v7 .widget .Toolbar ;
10- import android .text .Html ;
117import android .view .View ;
128import android .widget .Button ;
13- import android .widget .TextView ;
149import android .widget .Toast ;
1510
11+ import org .neotech .app .retainabletasksdemo .OnAlertDialogClickListener ;
12+ import org .neotech .app .retainabletasksdemo .ProgressDialog ;
13+ import org .neotech .app .retainabletasksdemo .R ;
1614import org .neotech .app .retainabletasksdemo .tasks .CountDownTask ;
1715import org .neotech .app .retainabletasksdemo .tasks .SimpleTask ;
1816import org .neotech .app .retainabletasksdemo .tasks .TaskWithoutCallback ;
1917import org .neotech .library .retainabletasks .Task ;
20- import org .neotech .library .retainabletasks .providers .TaskActivityCompat ;
2118import org .neotech .library .retainabletasks .TaskExecutor ;
19+ import org .neotech .library .retainabletasks .providers .TaskActivityCompat ;
2220
23- public class Main extends TaskActivityCompat implements View .OnClickListener , Task .AdvancedCallback , OnAlertDialogClickListener {
21+ public class DemoActivityBasic extends TaskActivityCompat implements View .OnClickListener , Task .AdvancedCallback , OnAlertDialogClickListener {
2422
2523 private static final String TASK_RETAIN_UI_STATE = "retain-ui-state" ;
2624 private static final String TASK_PROGRESS = "progress-dialog" ;
27- private static final String TASK_SERIAL = "serial-" ;
28-
29- private static final int [] serialTaskText = new int []{R .string .task_serial_1 , R .string .task_serial_2 , R .string .task_serial_2 };
30- private static final Button [] serialTaskButton = new Button [3 ];
31-
3225
3326 private static final String DIALOG_PROGRESS = "progress-dialog" ;
3427
3528 private ProgressDialog progressDialog ;
36-
3729 private Button retainUserInterfaceButton ;
3830
3931 @ Override
4032 protected void onCreate (Bundle savedInstanceState ) {
4133 super .onCreate (savedInstanceState );
42- setContentView (R .layout .activity_main );
43- setSupportActionBar ((Toolbar ) findViewById (R .id .toolbar ));
34+ setContentView (R .layout .activity_demo_basic );
4435
45- findViewById (R .id .button_open_fragment_activity ).setOnClickListener (this );
46- findViewById (R .id .button_open_v11_activity ).setOnClickListener (this );
47-
48- findViewById (R .id .fab ).setOnClickListener (this );
4936 findViewById (R .id .button_no_ui_task ).setOnClickListener (this );
5037 findViewById (R .id .button_progress_task ).setOnClickListener (this );
5138
52-
53- ((TextView ) findViewById (R .id .text_serial_tasks )).setText (Html .fromHtml (getString (R .string .task_serial )));
54- serialTaskButton [0 ] = (Button ) findViewById (R .id .button_serial_task_1 );
55- serialTaskButton [0 ].setOnClickListener (this );
56- serialTaskButton [1 ] = (Button ) findViewById (R .id .button_serial_task_2 );
57- serialTaskButton [1 ].setOnClickListener (this );
58- serialTaskButton [2 ] = (Button ) findViewById (R .id .button_serial_task_3 );
59- serialTaskButton [2 ].setOnClickListener (this );
60-
61-
6239 retainUserInterfaceButton = (Button ) findViewById (R .id .button_retain_ui_state_task );
6340 retainUserInterfaceButton .setOnClickListener (this );
6441 }
@@ -78,18 +55,14 @@ public Task.Callback onPreAttach(@NonNull Task<?, ?> task) {
7855 }
7956 } else if (task .getTag ().equals (TASK_PROGRESS )){
8057 progressDialog = ProgressDialog .getExistingInstance (getSupportFragmentManager (), DIALOG_PROGRESS );
81- } else if (task .getTag ().startsWith (TASK_SERIAL )){
82- onPreAttachSerialTask (task );
8358 }
8459 return this ;
8560 }
8661
8762 @ Override
8863 public void onClick (View v ) {
8964 final int id = v .getId ();
90- if (id == R .id .fab ){
91- startActivity (new Intent (Intent .ACTION_VIEW , Uri .parse ("https://github.com/NeoTech-Software/Android-Retainable-Tasks" )));
92- } else if (id == R .id .button_progress_task ) {
65+ if (id == R .id .button_progress_task ) {
9366 if (getTaskManager ().isRunning (TASK_PROGRESS )) {
9467 Toast .makeText (this , R .string .toast_task_already_running , Toast .LENGTH_SHORT ).show ();
9568 }
@@ -103,19 +76,6 @@ public void onClick(View v) {
10376 CountDownTask task = new CountDownTask (TASK_RETAIN_UI_STATE , 10 );
10477 getTaskManager ().execute (task , this );
10578 retainUserInterfaceButton .setEnabled (false );
106- } else if (id == R .id .button_open_fragment_activity ){
107- startActivity (new Intent (this , ActivityWithFragments .class ));
108- } else if (id == R .id .button_open_v11_activity ){
109- startActivity (new Intent (this , ActivityV11 .class ));
110- } else if (id == R .id .button_serial_task_1 ){
111- getTaskManager ().execute (new CountDownTask (TASK_SERIAL + 1 , 10 ), this , TaskExecutor .SERIAL_EXECUTOR );
112- v .setEnabled (false );
113- } else if (id == R .id .button_serial_task_2 ){
114- getTaskManager ().execute (new CountDownTask (TASK_SERIAL + 2 , 10 ), this , TaskExecutor .SERIAL_EXECUTOR );
115- v .setEnabled (false );
116- } else if (id == R .id .button_serial_task_3 ){
117- getTaskManager ().execute (new CountDownTask (TASK_SERIAL + 3 , 10 ), this , TaskExecutor .SERIAL_EXECUTOR );
118- v .setEnabled (false );
11979 }
12080 }
12181
@@ -134,8 +94,6 @@ public void onPostExecute(Task<?, ?> task) {
13494 } else if (task .getTag ().equals (TASK_RETAIN_UI_STATE )){
13595 retainUserInterfaceButton .setEnabled (true );
13696 retainUserInterfaceButton .setText (R .string .task_retain_ui_state );
137- } else if (task .getTag ().startsWith (TASK_SERIAL )) {
138- onPostExecuteSerialTask (task );
13997 }
14098 }
14199
@@ -153,40 +111,11 @@ public void onProgressUpdate(Task<?, ?> task, Object progress) {
153111 progressDialog .setProgress ((int ) progress );
154112 } else if (task .getTag ().equals (TASK_RETAIN_UI_STATE )){
155113 retainUserInterfaceButton .setText ("" + (int ) progress );
156- } else if (task .getTag ().startsWith (TASK_SERIAL )){
157- onProgressUpdateSerialTask (task , (Integer ) progress );
158114 }
159115 }
160116
161117 @ Override
162118 public void onDialogFragmentClick (DialogFragment fragment , int which ) {
163119 getTaskManager ().cancel (TASK_PROGRESS );
164120 }
165-
166-
167-
168- private int getSerialTaskIndex (String tag ){
169- return Integer .parseInt (tag .substring (TASK_SERIAL .length ())) - 1 ;
170- }
171-
172- private void onPreAttachSerialTask (Task <?, ?> task ) {
173- final int index = getSerialTaskIndex (task .getTag ());
174- serialTaskButton [index ].setEnabled (false );
175- final Integer progress = (Integer ) task .getLastKnownProgress ();
176- if (progress != null ) {
177- serialTaskButton [index ].setText ("" + progress );
178- }
179- }
180-
181- private void onPostExecuteSerialTask (Task <?, ?> task ) {
182- final int index = getSerialTaskIndex (task .getTag ());
183- serialTaskButton [index ].setEnabled (false );
184- serialTaskButton [index ].setText (serialTaskText [index ]);
185- serialTaskButton [index ].setEnabled (true );
186- }
187-
188- private void onProgressUpdateSerialTask (Task <?, ?> task , int progress ){
189- final int index = getSerialTaskIndex (task .getTag ());
190- serialTaskButton [index ].setText ("" + progress );
191- }
192121}
0 commit comments