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 |
365 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
26 |
|
|
import dk.thoerup.traininfo.provider.TimetableProvider; |
27 |
|
|
import dk.thoerup.traininfo.util.MessageBox; |
28 |
torben |
362 |
|
29 |
torben |
839 |
public class TimetableList extends ListActivity { |
30 |
torben |
364 |
|
31 |
torben |
365 |
private static final int DLG_PROGRESS = 8000; |
32 |
torben |
981 |
DepartureEntry departure; |
33 |
torben |
365 |
TimetableListAdapter adapter; |
34 |
|
|
TimetableFetcher fetcher; |
35 |
|
|
List<TimetableBean> timetables; |
36 |
torben |
1017 |
int commFailCounter = 0; |
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 |
torben |
981 |
departure = (DepartureEntry) launchedBy.getSerializableExtra("departure"); |
55 |
torben |
364 |
|
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 |
1028 |
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
63 |
torben |
365 |
|
64 |
|
|
if (savedInstanceState == null) { |
65 |
|
|
startTimetableFetcher(); |
66 |
|
|
} else { |
67 |
|
|
timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables"); |
68 |
|
|
adapter.setTimetable(timetables); |
69 |
|
|
} |
70 |
torben |
362 |
} |
71 |
torben |
918 |
|
72 |
|
|
@Override |
73 |
|
|
protected void onDestroy() { |
74 |
|
|
super.onDestroy(); |
75 |
|
|
|
76 |
|
|
if (fetcher != null) { |
77 |
|
|
fetcher.cancel(true); |
78 |
|
|
} |
79 |
|
|
} |
80 |
torben |
365 |
|
81 |
torben |
840 |
@Override |
82 |
|
|
protected void onListItemClick(ListView l, View v, int position, long id) { |
83 |
|
|
super.onListItemClick(l, v, position, id); |
84 |
|
|
|
85 |
|
|
TimetableBean tt = timetables.get(position); |
86 |
|
|
|
87 |
|
|
StationBean station = new StationBean(); |
88 |
|
|
station.setName( tt.getStation() ); |
89 |
|
|
station.setId( tt.getStationId() ); |
90 |
|
|
station.setRegional(true); |
91 |
|
|
|
92 |
|
|
Intent intent = new Intent(this, DepartureList.class); |
93 |
|
|
intent.putExtra("stationbean", station); |
94 |
|
|
startActivity(intent); |
95 |
|
|
|
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
@Override |
101 |
torben |
365 |
public void onSaveInstanceState(Bundle outState) |
102 |
|
|
{ |
103 |
|
|
dismissDialog(DLG_PROGRESS); |
104 |
|
|
outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables); |
105 |
|
|
} |
106 |
torben |
364 |
|
107 |
torben |
365 |
|
108 |
|
|
@Override |
109 |
|
|
protected void onPrepareDialog(int id, Dialog dialog) { |
110 |
|
|
super.onPrepareDialog(id, dialog); |
111 |
|
|
|
112 |
|
|
switch (id) { |
113 |
|
|
case DLG_PROGRESS: |
114 |
|
|
//pgDialog = (ProgressDialog) dialog; |
115 |
|
|
break; |
116 |
|
|
} |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
@Override |
120 |
|
|
protected Dialog onCreateDialog(int id) { |
121 |
|
|
switch (id) { |
122 |
|
|
case DLG_PROGRESS: |
123 |
torben |
561 |
ProgressDialog dlg = new ProgressDialog(this); |
124 |
|
|
dlg.setMessage( getString(timetablelist_fetchdata) ); |
125 |
torben |
365 |
dlg.setCancelable(true); |
126 |
|
|
return dlg; |
127 |
|
|
default: |
128 |
|
|
return super.onCreateDialog(id); |
129 |
|
|
} |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
void startTimetableFetcher() { |
133 |
|
|
showDialog(DLG_PROGRESS); |
134 |
|
|
fetcher = new TimetableFetcher(); |
135 |
torben |
699 |
fetcher.execute(departure.getType(), departure.getTrainNumber()); |
136 |
torben |
365 |
} |
137 |
|
|
|
138 |
|
|
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
139 |
|
|
|
140 |
|
|
|
141 |
|
|
@Override |
142 |
|
|
protected void onPostExecute(Void result) { |
143 |
|
|
super.onPostExecute(result); |
144 |
|
|
dismissDialog(DLG_PROGRESS); |
145 |
|
|
|
146 |
|
|
|
147 |
torben |
1007 |
if (timetables != null) { |
148 |
torben |
1017 |
commFailCounter = 0; |
149 |
torben |
917 |
TimetableList.this.getListView().invalidateViews(); |
150 |
torben |
365 |
adapter.setTimetable(timetables); |
151 |
|
|
if (timetables.size() == 0) { |
152 |
torben |
906 |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true); |
153 |
torben |
365 |
} |
154 |
|
|
} else { // communication or parse error |
155 |
torben |
1017 |
commFailCounter++; |
156 |
torben |
365 |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
157 |
torben |
561 |
builder.setMessage(getString(timetablelist_fetcherror)); |
158 |
torben |
365 |
builder.setCancelable(true); |
159 |
torben |
1017 |
if (commFailCounter < 3) { |
160 |
|
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
161 |
|
|
public void onClick(DialogInterface dialog, int id) { |
162 |
|
|
dialog.dismiss(); |
163 |
|
|
startTimetableFetcher(); |
164 |
|
|
|
165 |
|
|
} |
166 |
|
|
}); |
167 |
|
|
} |
168 |
torben |
561 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
169 |
torben |
365 |
public void onClick(DialogInterface dialog, int id) { |
170 |
|
|
dialog.dismiss(); |
171 |
torben |
843 |
TimetableList.this.finish(); |
172 |
torben |
365 |
} |
173 |
torben |
630 |
}); |
174 |
|
|
|
175 |
|
|
try { |
176 |
|
|
builder.show(); |
177 |
|
|
} catch (android.view.WindowManager.BadTokenException e) { |
178 |
|
|
Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
179 |
|
|
} |
180 |
|
|
|
181 |
torben |
365 |
} |
182 |
|
|
|
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
@Override |
186 |
|
|
protected Void doInBackground(String... arg0) { |
187 |
torben |
699 |
String type = arg0[0]; |
188 |
|
|
String trainID = arg0[1]; |
189 |
torben |
1007 |
timetables = provider.lookupTimetable(type, trainID); |
190 |
torben |
365 |
|
191 |
|
|
return null; |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
} |
195 |
torben |
362 |
} |