1 |
package dk.thoerup.traininfo; |
2 |
|
3 |
|
4 |
import java.util.ArrayList; |
5 |
import java.util.List; |
6 |
|
7 |
import android.app.Activity; |
8 |
import android.app.AlertDialog; |
9 |
import android.app.Dialog; |
10 |
import android.app.ProgressDialog; |
11 |
import android.content.DialogInterface; |
12 |
import android.content.Intent; |
13 |
import android.os.AsyncTask; |
14 |
import android.os.Bundle; |
15 |
import android.widget.ListView; |
16 |
import android.widget.TextView; |
17 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
18 |
import dk.thoerup.traininfo.provider.TimetableProvider; |
19 |
import dk.thoerup.traininfo.util.MessageBox; |
20 |
import static dk.thoerup.traininfo.R.string.*; |
21 |
|
22 |
public class TimetableList extends Activity { |
23 |
|
24 |
private static final int DLG_PROGRESS = 8000; |
25 |
DepartureBean departure; |
26 |
TimetableListAdapter adapter; |
27 |
TimetableFetcher fetcher; |
28 |
List<TimetableBean> timetables; |
29 |
|
30 |
TimetableProvider provider; |
31 |
|
32 |
@SuppressWarnings("unchecked") |
33 |
@Override |
34 |
protected void onCreate(Bundle savedInstanceState) { |
35 |
super.onCreate(savedInstanceState); |
36 |
setContentView(R.layout.timetablelist); |
37 |
|
38 |
provider = ProviderFactory.getTimetableProvider(); |
39 |
|
40 |
adapter = new TimetableListAdapter(this); |
41 |
|
42 |
ListView lv = (ListView) findViewById(R.id.List); |
43 |
lv.setAdapter(adapter); |
44 |
|
45 |
|
46 |
Intent launchedBy = getIntent(); |
47 |
departure = (DepartureBean) launchedBy.getSerializableExtra("departure"); |
48 |
|
49 |
((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber()); |
50 |
((TextView)findViewById(R.id.Status)).setText(departure.getStatus()); |
51 |
((TextView)findViewById(R.id.Location)).setText(departure.getLocation()); |
52 |
((TextView)findViewById(R.id.Note)).setText(departure.getNote()); |
53 |
((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString(this)); |
54 |
|
55 |
|
56 |
if (savedInstanceState == null) { |
57 |
startTimetableFetcher(); |
58 |
} else { |
59 |
timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables"); |
60 |
adapter.setTimetable(timetables); |
61 |
} |
62 |
} |
63 |
|
64 |
@Override |
65 |
public void onSaveInstanceState(Bundle outState) |
66 |
{ |
67 |
dismissDialog(DLG_PROGRESS); |
68 |
outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables); |
69 |
} |
70 |
|
71 |
|
72 |
@Override |
73 |
protected void onPrepareDialog(int id, Dialog dialog) { |
74 |
super.onPrepareDialog(id, dialog); |
75 |
|
76 |
switch (id) { |
77 |
case DLG_PROGRESS: |
78 |
//pgDialog = (ProgressDialog) dialog; |
79 |
break; |
80 |
} |
81 |
} |
82 |
|
83 |
@Override |
84 |
protected Dialog onCreateDialog(int id) { |
85 |
switch (id) { |
86 |
case DLG_PROGRESS: |
87 |
ProgressDialog dlg = new ProgressDialog(this); |
88 |
dlg.setMessage( getString(timetablelist_fetchdata) ); |
89 |
dlg.setCancelable(true); |
90 |
return dlg; |
91 |
default: |
92 |
return super.onCreateDialog(id); |
93 |
} |
94 |
} |
95 |
|
96 |
void startTimetableFetcher() { |
97 |
showDialog(DLG_PROGRESS); |
98 |
fetcher = new TimetableFetcher(); |
99 |
fetcher.execute(departure.getTrainNumber()); |
100 |
} |
101 |
|
102 |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
103 |
|
104 |
boolean success; |
105 |
|
106 |
@Override |
107 |
protected void onPostExecute(Void result) { |
108 |
super.onPostExecute(result); |
109 |
dismissDialog(DLG_PROGRESS); |
110 |
|
111 |
|
112 |
if (success) { |
113 |
adapter.setTimetable(timetables); |
114 |
if (timetables.size() == 0) { |
115 |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata)); |
116 |
} |
117 |
} else { // communication or parse error |
118 |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
119 |
builder.setMessage(getString(timetablelist_fetcherror)); |
120 |
builder.setCancelable(true); |
121 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
122 |
public void onClick(DialogInterface dialog, int id) { |
123 |
dialog.dismiss(); |
124 |
startTimetableFetcher(); |
125 |
|
126 |
} |
127 |
}); |
128 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
129 |
public void onClick(DialogInterface dialog, int id) { |
130 |
dialog.dismiss(); |
131 |
} |
132 |
}); |
133 |
builder.show(); |
134 |
} |
135 |
|
136 |
} |
137 |
|
138 |
@Override |
139 |
protected Void doInBackground(String... arg0) { |
140 |
String trainID = arg0[0]; |
141 |
success = provider.lookupTimetable(trainID); |
142 |
timetables = provider.getTimetable(trainID); |
143 |
|
144 |
return null; |
145 |
} |
146 |
|
147 |
} |
148 |
} |