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