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