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