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