44import android .content .Intent ;
55import android .net .Uri ;
66import android .os .Bundle ;
7+ import android .os .SystemClock ;
8+ import android .support .annotation .NonNull ;
79import android .support .v7 .app .AppCompatActivity ;
810import android .support .v7 .widget .Toolbar ;
911import android .text .Spanned ;
1618import android .widget .Button ;
1719import android .widget .ListView ;
1820import android .widget .TextView ;
21+ import android .widget .ViewSwitcher ;
1922
2023import org .neotech .app .retainabletasksdemo .ExtendedHtml ;
2124import org .neotech .app .retainabletasksdemo .R ;
25+ import org .neotech .library .retainabletasks .Task ;
26+ import org .neotech .library .retainabletasks .providers .TaskActivityCompat ;
2227
2328import java .util .ArrayList ;
29+ import java .util .List ;
2430
2531/**
2632 * Created by Rolf on 16-3-2016.
2733 */
28- public class Main extends AppCompatActivity {
34+ public class Main extends TaskActivityCompat implements Task . Callback {
2935
36+ private ViewSwitcher vSwitcher ;
37+ private ListView list ;
3038
3139 @ Override
3240 protected void onCreate (Bundle savedInstanceState ) {
3341 super .onCreate (savedInstanceState );
3442 setContentView (R .layout .activity_main );
35- setSupportActionBar ((Toolbar ) findViewById (R .id .toolbar ));
43+ // setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
3644
37- ArrayList <Demo > demos = new ArrayList <>(1 );
38- demos .add (new Demo (this , R .string .demo_examples_title , R .string .demo_examples_description , "org/neotech/app/retainabletasksdemo/activity/DemoActivityBasic.java" , new Intent (this , DemoActivityBasic .class )));
39- demos .add (new Demo (this , R .string .demo_serial_title , R .string .demo_serial_description , "org/neotech/app/retainabletasksdemo/activity/DemoActivitySerial.java" , new Intent (this , DemoActivitySerial .class )));
40- demos .add (new Demo (this , R .string .demo_fragments_title , R .string .demo_fragments_description , "org/neotech/app/retainabletasksdemo/activity/DemoActivityFragments.java" , new Intent (this , DemoActivityFragments .class )));
41- demos .add (new Demo (this , R .string .demo_no_compat_title , R .string .demo_no_compat_description , "org/neotech/app/retainabletasksdemo/activity/DemoActivityV11.java" , new Intent (this , DemoActivityV11 .class )));
45+ vSwitcher = (ViewSwitcher ) findViewById (R .id .switcher );
46+ list = (ListView ) findViewById (android .R .id .list );
4247
43-
44- ListView list = ( ListView ) findViewById ( android . R . id . list );
45- list . setAdapter ( new DemoAdapter ( demos ));
48+ if (! getTaskManager (). isRunning ( "list-loader" )) {
49+ getTaskManager (). execute ( new UselessLoadingTask ( " list-loader" , this ), this );
50+ }
4651 }
4752
4853 @ Override
@@ -59,6 +64,56 @@ public boolean onOptionsItemSelected(MenuItem item) {
5964 return super .onOptionsItemSelected (item );
6065 }
6166
67+ public void setListShown (boolean shown ){
68+ if (vSwitcher .getCurrentView () == list && !shown ){
69+ vSwitcher .showNext ();
70+ } else if (vSwitcher .getCurrentView () != list && shown ){
71+ vSwitcher .showNext ();
72+ }
73+ }
74+
75+ @ Override
76+ public Task .Callback onPreAttach (@ NonNull Task <?, ?> task ) {
77+ setListShown (false );
78+ return this ;
79+ }
80+
81+ @ Override
82+ public void onPreExecute (Task <?, ?> task ) {
83+
84+ }
85+
86+ @ Override
87+ public void onPostExecute (Task <?, ?> raw ) {
88+ UselessLoadingTask task = (UselessLoadingTask ) raw ;
89+ list .setAdapter (new DemoAdapter (task .getResult ()));
90+ setListShown (true );
91+ }
92+
93+ /**
94+ * Task just to demonstrate the principe of starting a task before the UI is ready.
95+ */
96+ private static class UselessLoadingTask extends Task <Void , ArrayList <Demo >> {
97+
98+ private final Context context ;
99+
100+ public UselessLoadingTask (String tag , Context context ) {
101+ super (tag );
102+ this .context = context .getApplicationContext ();
103+ }
104+
105+ @ Override
106+ protected ArrayList <Demo > doInBackground () {
107+ SystemClock .sleep (1500 );
108+ ArrayList <Demo > demos = new ArrayList <>(4 );
109+ demos .add (new Demo (context , R .string .demo_examples_title , R .string .demo_examples_description , "org/neotech/app/retainabletasksdemo/activity/DemoActivityBasic.java" , new Intent (context , DemoActivityBasic .class )));
110+ demos .add (new Demo (context , R .string .demo_serial_title , R .string .demo_serial_description , "org/neotech/app/retainabletasksdemo/activity/DemoActivitySerial.java" , new Intent (context , DemoActivitySerial .class )));
111+ demos .add (new Demo (context , R .string .demo_fragments_title , R .string .demo_fragments_description , "org/neotech/app/retainabletasksdemo/activity/DemoActivityFragments.java" , new Intent (context , DemoActivityFragments .class )));
112+ demos .add (new Demo (context , R .string .demo_no_compat_title , R .string .demo_no_compat_description , "org/neotech/app/retainabletasksdemo/activity/DemoActivityV11.java" , new Intent (context , DemoActivityV11 .class )));
113+ return demos ;
114+ }
115+ }
116+
62117 private static class DemoAdapter extends BaseAdapter implements View .OnClickListener {
63118
64119 private static class ViewHolder {
@@ -82,6 +137,14 @@ public DemoAdapter(ArrayList<Demo> demos){
82137 this .demos = demos ;
83138 }
84139
140+ public DemoAdapter (){
141+ this .demos = new ArrayList <>();
142+ }
143+
144+ public List <Demo > getItems (){
145+ return demos ;
146+ }
147+
85148 @ Override
86149 public int getCount () {
87150 return demos .size ();
0 commit comments