1 |
torben |
362 |
package dk.thoerup.traininfo; |
2 |
|
|
|
3 |
torben |
365 |
|
4 |
torben |
630 |
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 |
torben |
365 |
import java.util.ArrayList; |
11 |
|
|
import java.util.List; |
12 |
|
|
|
13 |
|
|
import android.app.AlertDialog; |
14 |
|
|
import android.app.Dialog; |
15 |
torben |
839 |
import android.app.ListActivity; |
16 |
torben |
365 |
import android.app.ProgressDialog; |
17 |
|
|
import android.content.DialogInterface; |
18 |
torben |
364 |
import android.content.Intent; |
19 |
torben |
365 |
import android.os.AsyncTask; |
20 |
torben |
362 |
import android.os.Bundle; |
21 |
torben |
630 |
import android.util.Log; |
22 |
torben |
840 |
import android.view.View; |
23 |
|
|
import android.widget.ListView; |
24 |
torben |
362 |
import android.widget.TextView; |
25 |
torben |
840 |
import android.widget.Toast; |
26 |
torben |
365 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
27 |
|
|
import dk.thoerup.traininfo.provider.TimetableProvider; |
28 |
|
|
import dk.thoerup.traininfo.util.MessageBox; |
29 |
torben |
362 |
|
30 |
torben |
839 |
public class TimetableList extends ListActivity { |
31 |
torben |
364 |
|
32 |
torben |
365 |
private static final int DLG_PROGRESS = 8000; |
33 |
torben |
364 |
DepartureBean departure; |
34 |
torben |
365 |
TimetableListAdapter adapter; |
35 |
|
|
TimetableFetcher fetcher; |
36 |
|
|
List<TimetableBean> timetables; |
37 |
torben |
364 |
|
38 |
torben |
365 |
TimetableProvider provider; |
39 |
|
|
|
40 |
|
|
@SuppressWarnings("unchecked") |
41 |
torben |
362 |
@Override |
42 |
|
|
protected void onCreate(Bundle savedInstanceState) { |
43 |
|
|
super.onCreate(savedInstanceState); |
44 |
|
|
setContentView(R.layout.timetablelist); |
45 |
torben |
364 |
|
46 |
torben |
365 |
provider = ProviderFactory.getTimetableProvider(); |
47 |
|
|
|
48 |
torben |
839 |
adapter = new TimetableListAdapter(this); |
49 |
|
|
setListAdapter(adapter); |
50 |
torben |
366 |
|
51 |
torben |
839 |
|
52 |
torben |
366 |
|
53 |
torben |
364 |
Intent launchedBy = getIntent(); |
54 |
|
|
departure = (DepartureBean) launchedBy.getSerializableExtra("departure"); |
55 |
|
|
|
56 |
|
|
((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber()); |
57 |
|
|
((TextView)findViewById(R.id.Status)).setText(departure.getStatus()); |
58 |
torben |
398 |
((TextView)findViewById(R.id.Location)).setText(departure.getLocation()); |
59 |
torben |
364 |
((TextView)findViewById(R.id.Note)).setText(departure.getNote()); |
60 |
torben |
563 |
((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString(this)); |
61 |
torben |
366 |
|
62 |
torben |
365 |
|
63 |
|
|
if (savedInstanceState == null) { |
64 |
|
|
startTimetableFetcher(); |
65 |
|
|
} else { |
66 |
|
|
timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables"); |
67 |
|
|
adapter.setTimetable(timetables); |
68 |
|
|
} |
69 |
torben |
362 |
} |
70 |
torben |
918 |
|
71 |
|
|
@Override |
72 |
|
|
protected void onDestroy() { |
73 |
|
|
super.onDestroy(); |
74 |
|
|
|
75 |
|
|
if (fetcher != null) { |
76 |
|
|
fetcher.cancel(true); |
77 |
|
|
} |
78 |
|
|
} |
79 |
torben |
365 |
|
80 |
torben |
840 |
@Override |
81 |
|
|
protected void onListItemClick(ListView l, View v, int position, long id) { |
82 |
|
|
super.onListItemClick(l, v, position, id); |
83 |
|
|
|
84 |
|
|
TimetableBean tt = timetables.get(position); |
85 |
|
|
|
86 |
|
|
StationBean station = new StationBean(); |
87 |
|
|
station.setName( tt.getStation() ); |
88 |
|
|
station.setId( tt.getStationId() ); |
89 |
|
|
station.setRegional(true); |
90 |
|
|
|
91 |
|
|
Intent intent = new Intent(this, DepartureList.class); |
92 |
|
|
intent.putExtra("stationbean", station); |
93 |
|
|
startActivity(intent); |
94 |
|
|
|
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
@Override |
100 |
torben |
365 |
public void onSaveInstanceState(Bundle outState) |
101 |
|
|
{ |
102 |
|
|
dismissDialog(DLG_PROGRESS); |
103 |
|
|
outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables); |
104 |
|
|
} |
105 |
torben |
364 |
|
106 |
torben |
365 |
|
107 |
|
|
@Override |
108 |
|
|
protected void onPrepareDialog(int id, Dialog dialog) { |
109 |
|
|
super.onPrepareDialog(id, dialog); |
110 |
|
|
|
111 |
|
|
switch (id) { |
112 |
|
|
case DLG_PROGRESS: |
113 |
|
|
//pgDialog = (ProgressDialog) dialog; |
114 |
|
|
break; |
115 |
|
|
} |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
@Override |
119 |
|
|
protected Dialog onCreateDialog(int id) { |
120 |
|
|
switch (id) { |
121 |
|
|
case DLG_PROGRESS: |
122 |
torben |
561 |
ProgressDialog dlg = new ProgressDialog(this); |
123 |
|
|
dlg.setMessage( getString(timetablelist_fetchdata) ); |
124 |
torben |
365 |
dlg.setCancelable(true); |
125 |
|
|
return dlg; |
126 |
|
|
default: |
127 |
|
|
return super.onCreateDialog(id); |
128 |
|
|
} |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
void startTimetableFetcher() { |
132 |
|
|
showDialog(DLG_PROGRESS); |
133 |
|
|
fetcher = new TimetableFetcher(); |
134 |
torben |
699 |
fetcher.execute(departure.getType(), departure.getTrainNumber()); |
135 |
torben |
365 |
} |
136 |
|
|
|
137 |
|
|
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
138 |
|
|
|
139 |
|
|
boolean success; |
140 |
|
|
|
141 |
|
|
@Override |
142 |
|
|
protected void onPostExecute(Void result) { |
143 |
|
|
super.onPostExecute(result); |
144 |
|
|
dismissDialog(DLG_PROGRESS); |
145 |
|
|
|
146 |
|
|
|
147 |
|
|
if (success) { |
148 |
torben |
917 |
TimetableList.this.getListView().invalidateViews(); |
149 |
torben |
365 |
adapter.setTimetable(timetables); |
150 |
|
|
if (timetables.size() == 0) { |
151 |
torben |
906 |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true); |
152 |
torben |
365 |
} |
153 |
|
|
} else { // communication or parse error |
154 |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
155 |
torben |
561 |
builder.setMessage(getString(timetablelist_fetcherror)); |
156 |
torben |
365 |
builder.setCancelable(true); |
157 |
torben |
561 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
158 |
torben |
365 |
public void onClick(DialogInterface dialog, int id) { |
159 |
|
|
dialog.dismiss(); |
160 |
|
|
startTimetableFetcher(); |
161 |
|
|
|
162 |
|
|
} |
163 |
|
|
}); |
164 |
torben |
561 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
165 |
torben |
365 |
public void onClick(DialogInterface dialog, int id) { |
166 |
|
|
dialog.dismiss(); |
167 |
torben |
843 |
TimetableList.this.finish(); |
168 |
torben |
365 |
} |
169 |
torben |
630 |
}); |
170 |
|
|
|
171 |
|
|
try { |
172 |
|
|
builder.show(); |
173 |
|
|
} catch (android.view.WindowManager.BadTokenException e) { |
174 |
|
|
Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
175 |
|
|
} |
176 |
|
|
|
177 |
torben |
365 |
} |
178 |
|
|
|
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
@Override |
182 |
|
|
protected Void doInBackground(String... arg0) { |
183 |
torben |
699 |
String type = arg0[0]; |
184 |
|
|
String trainID = arg0[1]; |
185 |
|
|
success = provider.lookupTimetable(type, trainID); |
186 |
|
|
timetables = provider.getTimetable(type, trainID); |
187 |
torben |
365 |
|
188 |
|
|
return null; |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
} |
192 |
torben |
362 |
} |