7 |
import static dk.thoerup.traininfo.R.string.timetablelist_fetcherror; |
import static dk.thoerup.traininfo.R.string.timetablelist_fetcherror; |
8 |
import static dk.thoerup.traininfo.R.string.timetablelist_nodata; |
import static dk.thoerup.traininfo.R.string.timetablelist_nodata; |
9 |
|
|
10 |
import java.util.ArrayList; |
import java.util.regex.Pattern; |
|
import java.util.List; |
|
11 |
|
|
12 |
import android.app.AlertDialog; |
import android.app.AlertDialog; |
13 |
import android.app.Dialog; |
import android.app.Dialog; |
21 |
import android.view.View; |
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; |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
25 |
|
import dk.thoerup.android.traininfo.common.StationBean.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; |
32 |
public class TimetableList extends ListActivity { |
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); |
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.Location)).setText(departure.getLocation()); |
((TextView)findViewById(R.id.Location)).setText(departure.getLocation()); |
62 |
((TextView)findViewById(R.id.Note)).setText(departure.getNote()); |
((TextView)findViewById(R.id.Updated)).setText( getLastUpdateString( departure.getUpdated() )); |
63 |
((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString(this)); |
|
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_equipmentcircumstances) ); |
76 |
|
|
77 |
|
|
78 |
|
((TextView)findViewById(R.id.Status)).setText(status); |
79 |
|
((TextView)findViewById(R.id.Note)).setText(note); |
80 |
|
|
81 |
|
|
82 |
|
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
83 |
|
|
84 |
if (savedInstanceState == null) { |
if (savedInstanceState == null) { |
85 |
startTimetableFetcher(); |
startTimetableFetcher(); |
86 |
} else { |
} else { |
87 |
timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables"); |
timetables = (TimetableBean) savedInstanceState.getSerializable("timetables"); |
88 |
adapter.setTimetable(timetables); |
adapter.setTimetable(timetables); |
89 |
} |
} |
90 |
} |
} |
91 |
|
|
92 |
|
@Override |
93 |
|
protected void onDestroy() { |
94 |
|
super.onDestroy(); |
95 |
|
|
96 |
|
if (fetcher != null) { |
97 |
|
fetcher.cancel(true); |
98 |
|
} |
99 |
|
} |
100 |
|
|
101 |
@Override |
@Override |
102 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
103 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
104 |
|
|
105 |
TimetableBean tt = timetables.get(position); |
TimetableEntry tt = timetables.entries.get(position); |
106 |
|
|
107 |
StationBean station = new StationBean(); |
StationEntry station = new StationEntry(); |
108 |
station.setName( tt.getStation() ); |
station.setName( tt.getStation() ); |
109 |
station.setId( tt.getStationId() ); |
station.setId( tt.getStationId() ); |
110 |
station.setRegional(true); |
station.setIsRegional(true); |
111 |
|
|
112 |
|
|
113 |
Intent intent = new Intent(this, DepartureList.class); |
Intent intent = new Intent(this, DepartureList.class); |
114 |
intent.putExtra("stationbean", station); |
intent.putExtra("stationbean", station); |
122 |
public void onSaveInstanceState(Bundle outState) |
public void onSaveInstanceState(Bundle outState) |
123 |
{ |
{ |
124 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
125 |
outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables); |
outState.putSerializable("timetables", (TimetableBean) timetables); |
126 |
} |
} |
127 |
|
|
128 |
|
|
156 |
fetcher.execute(departure.getType(), departure.getTrainNumber()); |
fetcher.execute(departure.getType(), departure.getTrainNumber()); |
157 |
} |
} |
158 |
|
|
159 |
|
|
160 |
|
public String getLastUpdateString(int lastUpdate) { |
161 |
|
String minutes = this.getString(R.string.departurebean_minutes); |
162 |
|
String unknown = this.getString(R.string.departurebean_unknown); |
163 |
|
switch (lastUpdate) { |
164 |
|
case 1: |
165 |
|
return "<3 " + minutes; |
166 |
|
case 2: |
167 |
|
return "3-10 " + minutes; |
168 |
|
case 3: |
169 |
|
return ">3 " + minutes; |
170 |
|
case 4: |
171 |
|
return unknown; |
172 |
|
default: |
173 |
|
return ""; |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
class TimetableFetcher extends AsyncTask<String,Void,Void> { |
179 |
|
|
|
boolean success; |
|
180 |
|
|
181 |
@Override |
@Override |
182 |
protected void onPostExecute(Void result) { |
protected void onPostExecute(Void result) { |
184 |
dismissDialog(DLG_PROGRESS); |
dismissDialog(DLG_PROGRESS); |
185 |
|
|
186 |
|
|
187 |
if (success) { |
if (timetables != null) { |
188 |
|
commFailCounter = 0; |
189 |
|
TimetableList.this.getListView().invalidateViews(); |
190 |
adapter.setTimetable(timetables); |
adapter.setTimetable(timetables); |
191 |
if (timetables.size() == 0) { |
if (timetables.entries.size() == 0) { |
192 |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata)); |
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true); |
193 |
} |
} |
194 |
} else { // communication or parse error |
} else { // communication or parse error |
195 |
|
commFailCounter++; |
196 |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
197 |
builder.setMessage(getString(timetablelist_fetcherror)); |
builder.setMessage(getString(timetablelist_fetcherror)); |
198 |
builder.setCancelable(true); |
builder.setCancelable(true); |
199 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
if (commFailCounter < 3) { |
200 |
public void onClick(DialogInterface dialog, int id) { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
201 |
dialog.dismiss(); |
public void onClick(DialogInterface dialog, int id) { |
202 |
startTimetableFetcher(); |
dialog.dismiss(); |
203 |
|
startTimetableFetcher(); |
204 |
} |
|
205 |
}); |
} |
206 |
|
}); |
207 |
|
} |
208 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
209 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
210 |
dialog.dismiss(); |
dialog.dismiss(); |
211 |
|
TimetableList.this.finish(); |
212 |
} |
} |
213 |
}); |
}); |
214 |
|
|
226 |
protected Void doInBackground(String... arg0) { |
protected Void doInBackground(String... arg0) { |
227 |
String type = arg0[0]; |
String type = arg0[0]; |
228 |
String trainID = arg0[1]; |
String trainID = arg0[1]; |
229 |
success = provider.lookupTimetable(type, trainID); |
timetables = provider.lookupTimetable(type, trainID); |
|
timetables = provider.getTimetable(type, trainID); |
|
230 |
|
|
231 |
return null; |
return null; |
232 |
} |
} |