1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
|
4 |
import java.util.ArrayList; |
import static dk.thoerup.traininfo.R.string.generic_cancel; |
5 |
import java.util.List; |
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.regex.Pattern; |
11 |
|
|
|
import android.app.Activity; |
|
12 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
13 |
import android.app.Dialog; |
import android.app.Dialog; |
14 |
|
import android.app.ListActivity; |
15 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
16 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
17 |
import android.content.Intent; |
import android.content.Intent; |
18 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
19 |
import android.os.Bundle; |
import android.os.Bundle; |
20 |
|
import android.util.Log; |
21 |
|
import android.view.View; |
22 |
import android.widget.ListView; |
import android.widget.ListView; |
23 |
import android.widget.TextView; |
import android.widget.TextView; |
24 |
|
import dk.thoerup.android.traininfo.common.DepartureEntry; |
25 |
|
import dk.thoerup.android.traininfo.common.StationEntry; |
26 |
|
import dk.thoerup.android.traininfo.common.TimetableBean; |
27 |
|
import dk.thoerup.android.traininfo.common.TimetableEntry; |
28 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
29 |
import dk.thoerup.traininfo.provider.TimetableProvider; |
import dk.thoerup.traininfo.provider.TimetableProvider; |
30 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
31 |
|
|
32 |
public class TimetableList extends Activity { |
public class TimetableList extends ListActivity { |
33 |
|
|
34 |
private static final int DLG_PROGRESS = 8000; |
private static final int DLG_PROGRESS = 8000; |
35 |
DepartureBean departure; |
DepartureEntry departure; |
36 |
TimetableListAdapter adapter; |
TimetableListAdapter adapter; |
37 |
TimetableFetcher fetcher; |
TimetableFetcher fetcher; |
38 |
List<TimetableBean> timetables; |
TimetableBean timetables; |
39 |
|
int commFailCounter = 0; |
40 |
|
|
41 |
TimetableProvider provider; |
TimetableProvider provider; |
42 |
|
|
43 |
@SuppressWarnings("unchecked") |
|
44 |
@Override |
@Override |
45 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
46 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
48 |
|
|
49 |
provider = ProviderFactory.getTimetableProvider(); |
provider = ProviderFactory.getTimetableProvider(); |
50 |
|
|
51 |
adapter = new TimetableListAdapter(this); |
adapter = new TimetableListAdapter(this); |
52 |
|
setListAdapter(adapter); |
53 |
ListView lv = (ListView) findViewById(R.id.List); |
|
|
lv.setAdapter(adapter); |
|
54 |
|
|
55 |
|
|
56 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
57 |
departure = (DepartureBean) launchedBy.getSerializableExtra("departure"); |
departure = (DepartureEntry) launchedBy.getSerializableExtra("departure"); |
58 |
|
|
59 |
((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber()); |
((TextView)findViewById(R.id.Train)).setText(departure.getTrainNumber()); |
60 |
((TextView)findViewById(R.id.Status)).setText(departure.getStatus()); |
|
61 |
((TextView)findViewById(R.id.Note)).setText(departure.getNote()); |
((TextView)findViewById(R.id.Location)).setText(departure.getLocation()); |
62 |
((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString()); |
((TextView)findViewById(R.id.Updated)).setText( getLastUpdateString( departure.getUpdated() )); |
63 |
|
|
64 |
|
String status = departure.getStatus() != null ? departure.getStatus() : ""; |
65 |
|
if ( Pattern.matches("[0-9]+.+min", status) ) { |
66 |
|
status += " " + this.getString(R.string.departurebean_delayed); |
67 |
|
} |
68 |
|
String note = departure.getNote() != null ? departure.getNote() : ""; |
69 |
|
note = note.replace("Aflyst", this.getString(R.string.timetablelist_cancelled) ); |
70 |
|
note = note.replace("Kører kun til", this.getString(R.string.timetablelist_goesonlyto) ); |
71 |
|
note = note.replace("Afgår fra", this.getString(R.string.timetablelist_startsat) ); |
72 |
|
note = note.replace("Erstattet af", this.getString(R.string.timetablelist_replacedby) ); |
73 |
|
note = note.replace("Eksterne forhold", this.getString(R.string.timetablelist_externalconditions) ); |
74 |
|
note = note.replace("Teknisk fejl på et signal", this.getString(R.string.timetablelist_technicalerroronsignal) ); |
75 |
|
note = note.replace("Materielforhold", this.getString(R.string.timetablelist_equipmentrelated) ); |
76 |
|
note = note.replace("Passagerforhold", this.getString(R.string.timetablelist_passengerrelated) ); |
77 |
|
note = note.replace("Forventet rettidig afgang", this.getString(R.string.timetablelist_expectedtimelydeparture) ); |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
((TextView)findViewById(R.id.Status)).setText(status); |
82 |
|
((TextView)findViewById(R.id.Note)).setText(note); |
83 |
|
|
84 |
|
|
85 |
|
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
86 |
|
|
87 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
88 |
startTimetableFetcher(); |
startTimetableFetcher(); |
89 |
} else { |
} else { |
90 |
timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables"); |
timetables = (TimetableBean) savedInstanceState.getSerializable("timetables"); |
91 |
adapter.setTimetable(timetables); |
adapter.setTimetable(timetables); |
92 |
} |
} |
93 |
} |
} |
94 |
|
|
95 |
|
@Override |
96 |
|
protected void onDestroy() { |
97 |
|
super.onDestroy(); |
98 |
|
|
99 |
|
if (fetcher != null) { |
100 |
|
fetcher.cancel(true); |
101 |
|
} |
102 |
|
} |
103 |
|
|
104 |
@Override |
@Override |
105 |
|
protected void onListItemClick(ListView l, View v, int position, long id) { |
106 |
|
super.onListItemClick(l, v, position, id); |
107 |
|
|
108 |
|
TimetableEntry tt = timetables.entries.get(position); |
109 |
|
|
110 |
|
|
111 |
|
Intent intent = new Intent(this, DepartureList.class); |
112 |
|
intent.putExtra("stationbean", tt.getStationEntry() ); |
113 |
|
startActivity(intent); |
114 |
|
|
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
@Override |
120 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
121 |
{ |
{ |
122 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
123 |
outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables); |
outState.putSerializable("timetables", (TimetableBean) timetables); |
124 |
} |
} |
125 |
|
|
|
/* case DLG_DETAILS: |
|
|
DepartureBean currentDeparture = departures.get(selectedItemId); |
|
|
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
|
|
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
|
|
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
|
|
((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin()); |
|
|
((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation()); |
|
|
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
|
|
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
|
|
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
|
|
detailsDialog = dialog; |
|
|
break; |
|
|
|
|
|
*/ |
|
126 |
|
|
127 |
@Override |
@Override |
128 |
protected void onPrepareDialog(int id, Dialog dialog) { |
protected void onPrepareDialog(int id, Dialog dialog) { |
139 |
protected Dialog onCreateDialog(int id) { |
protected Dialog onCreateDialog(int id) { |
140 |
switch (id) { |
switch (id) { |
141 |
case DLG_PROGRESS: |
case DLG_PROGRESS: |
142 |
ProgressDialog dlg = new ProgressDialog(this); |
ProgressDialog dlg = new ProgressDialog(this); |
143 |
dlg.setMessage("Fetch timetable data"); |
dlg.setMessage( getString(timetablelist_fetchdata) ); |
144 |
dlg.setCancelable(true); |
dlg.setCancelable(true); |
145 |
return dlg; |
return dlg; |
146 |
default: |
default: |
151 |
void startTimetableFetcher() { |
void startTimetableFetcher() { |
152 |
showDialog(DLG_PROGRESS); |
showDialog(DLG_PROGRESS); |
153 |
fetcher = new TimetableFetcher(); |
fetcher = new TimetableFetcher(); |
154 |
fetcher.execute(departure.getTrainNumber()); |
fetcher.execute(departure.getType(), departure.getTrainNumber()); |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
|
public String getLastUpdateString(int lastUpdate) { |
159 |
|
String minutes = this.getString(R.string.departurebean_minutes); |
160 |
|
String unknown = this.getString(R.string.departurebean_unknown); |
161 |
|
switch (lastUpdate) { |
162 |
|
case 1: |
163 |
|
return "<3 " + minutes; |
164 |
|
case 2: |
165 |
|
return "3-10 " + minutes; |
166 |
|
case 3: |
167 |
|
return ">3 " + minutes; |
168 |
|
case 4: |
169 |
|
return unknown; |
170 |
|
default: |
171 |
|
return ""; |
172 |
|
} |
173 |
} |
} |
174 |
|
|
175 |
|
|
176 |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
177 |
|
|
|
boolean success; |
|
178 |
|
|
179 |
@Override |
@Override |
180 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
181 |
super.onPostExecute(result); |
super.onPostExecute(result); |
182 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
183 |
|
|
184 |
|
//TODO: differentiate between a communication error and a downed bane.dk site |
185 |
if (success) { |
if (timetables != null && timetables.errorCode == null) { |
186 |
|
commFailCounter = 0; |
187 |
|
TimetableList.this.getListView().invalidateViews(); |
188 |
adapter.setTimetable(timetables); |
adapter.setTimetable(timetables); |
189 |
if (timetables.size() == 0) { |
if (timetables.entries.size() == 0) { |
190 |
MessageBox.showMessage(TimetableList.this, "No timetable found"); |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true); |
191 |
} |
} |
192 |
} else { // communication or parse error |
} else { // communication or parse error |
193 |
|
commFailCounter++; |
194 |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
195 |
builder.setMessage("Error finding departures"); |
|
196 |
|
|
197 |
|
if (timetables != null && timetables.errorCode != null ) { //got an error xml back |
198 |
|
commFailCounter = 10; |
199 |
|
builder.setMessage( getString(R.string.no_backend) ); |
200 |
|
} else { |
201 |
|
builder.setMessage(getString(timetablelist_fetcherror)); |
202 |
|
} |
203 |
|
|
204 |
builder.setCancelable(true); |
builder.setCancelable(true); |
205 |
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { |
if (commFailCounter < 3) { |
206 |
public void onClick(DialogInterface dialog, int id) { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
207 |
dialog.dismiss(); |
public void onClick(DialogInterface dialog, int id) { |
208 |
startTimetableFetcher(); |
dialog.dismiss(); |
209 |
|
startTimetableFetcher(); |
210 |
} |
|
211 |
}); |
} |
212 |
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
}); |
213 |
|
} |
214 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
215 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
216 |
dialog.dismiss(); |
dialog.dismiss(); |
217 |
|
TimetableList.this.finish(); |
218 |
} |
} |
219 |
}); |
}); |
220 |
builder.show(); |
|
221 |
|
try { |
222 |
|
builder.show(); |
223 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
224 |
|
Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
225 |
|
} |
226 |
|
|
227 |
} |
} |
228 |
|
|
229 |
} |
} |
230 |
|
|
231 |
@Override |
@Override |
232 |
protected Void doInBackground(String... arg0) { |
protected Void doInBackground(String... arg0) { |
233 |
String trainID = arg0[0]; |
String type = arg0[0]; |
234 |
success = provider.lookupTimetable(trainID); |
String trainID = arg0[1]; |
235 |
timetables = provider.getTimetable(); |
timetables = provider.lookupTimetable(type, trainID); |
236 |
|
|
237 |
return null; |
return null; |
238 |
} |
} |