1 |
package dk.thoerup.traininfo; |
package dk.thoerup.traininfo; |
2 |
|
|
3 |
|
|
4 |
|
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 |
|
import java.util.ArrayList; |
11 |
|
import java.util.List; |
12 |
|
|
13 |
|
import android.app.AlertDialog; |
14 |
|
import android.app.Dialog; |
15 |
import android.app.ListActivity; |
import android.app.ListActivity; |
16 |
|
import android.app.ProgressDialog; |
17 |
|
import android.content.DialogInterface; |
18 |
|
import android.content.Intent; |
19 |
|
import android.os.AsyncTask; |
20 |
import android.os.Bundle; |
import android.os.Bundle; |
21 |
|
import android.util.Log; |
22 |
|
import android.view.View; |
23 |
|
import android.widget.ListView; |
24 |
import android.widget.TextView; |
import android.widget.TextView; |
25 |
|
import android.widget.Toast; |
26 |
|
import dk.thoerup.traininfo.provider.ProviderFactory; |
27 |
|
import dk.thoerup.traininfo.provider.TimetableProvider; |
28 |
|
import dk.thoerup.traininfo.util.MessageBox; |
29 |
|
|
30 |
public class TimetableList extends ListActivity { |
public class TimetableList extends ListActivity { |
31 |
|
|
32 |
|
private static final int DLG_PROGRESS = 8000; |
33 |
|
DepartureBean departure; |
34 |
|
TimetableListAdapter adapter; |
35 |
|
TimetableFetcher fetcher; |
36 |
|
List<TimetableBean> timetables; |
37 |
|
|
38 |
|
TimetableProvider provider; |
39 |
|
|
40 |
|
@SuppressWarnings("unchecked") |
41 |
@Override |
@Override |
42 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
43 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
44 |
setContentView(R.layout.timetablelist); |
setContentView(R.layout.timetablelist); |
45 |
|
|
46 |
|
provider = ProviderFactory.getTimetableProvider(); |
47 |
|
|
48 |
|
adapter = new TimetableListAdapter(this); |
49 |
|
setListAdapter(adapter); |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
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 |
|
((TextView)findViewById(R.id.Location)).setText(departure.getLocation()); |
59 |
|
((TextView)findViewById(R.id.Note)).setText(departure.getNote()); |
60 |
|
((TextView)findViewById(R.id.Updated)).setText(departure.getLastUpdateString(this)); |
61 |
|
|
62 |
|
|
63 |
|
if (savedInstanceState == null) { |
64 |
|
startTimetableFetcher(); |
65 |
|
} else { |
66 |
|
timetables = (List<TimetableBean>) savedInstanceState.getSerializable("timetables"); |
67 |
|
adapter.setTimetable(timetables); |
68 |
|
} |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
@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 |
* case DLG_DETAILS: |
|
90 |
DepartureBean currentDeparture = departures.get(selectedItemId); |
|
91 |
((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); |
@Override |
92 |
((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); |
public void onSaveInstanceState(Bundle outState) |
93 |
((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); |
{ |
94 |
((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin()); |
dismissDialog(DLG_PROGRESS); |
95 |
((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation()); |
outState.putSerializable("timetables", (ArrayList<TimetableBean>) timetables); |
96 |
((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); |
} |
97 |
((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); |
|
98 |
((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); |
|
99 |
detailsDialog = dialog; |
@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; |
break; |
107 |
|
} |
108 |
|
} |
109 |
|
|
110 |
|
@Override |
111 |
|
protected Dialog onCreateDialog(int id) { |
112 |
|
switch (id) { |
113 |
|
case DLG_PROGRESS: |
114 |
|
ProgressDialog dlg = new ProgressDialog(this); |
115 |
|
dlg.setMessage( getString(timetablelist_fetchdata) ); |
116 |
|
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 |
|
fetcher.execute(departure.getType(), departure.getTrainNumber()); |
127 |
|
} |
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 |
|
TimetableList.this.getListView().invalidateViews(); |
141 |
|
adapter.setTimetable(timetables); |
142 |
|
if (timetables.size() == 0) { |
143 |
|
MessageBox.showMessage(TimetableList.this, getString(timetablelist_nodata), true); |
144 |
|
} |
145 |
|
} else { // communication or parse error |
146 |
|
AlertDialog.Builder builder = new AlertDialog.Builder(TimetableList.this); |
147 |
|
builder.setMessage(getString(timetablelist_fetcherror)); |
148 |
|
builder.setCancelable(true); |
149 |
|
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
150 |
|
public void onClick(DialogInterface dialog, int id) { |
151 |
|
dialog.dismiss(); |
152 |
|
startTimetableFetcher(); |
153 |
|
|
154 |
|
} |
155 |
|
}); |
156 |
|
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
157 |
|
public void onClick(DialogInterface dialog, int id) { |
158 |
|
dialog.dismiss(); |
159 |
|
TimetableList.this.finish(); |
160 |
|
} |
161 |
|
}); |
162 |
|
|
163 |
|
try { |
164 |
|
builder.show(); |
165 |
|
} catch (android.view.WindowManager.BadTokenException e) { |
166 |
|
Log.i("TimetableList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
167 |
|
} |
168 |
|
|
169 |
|
} |
170 |
|
|
171 |
|
} |
172 |
|
|
173 |
|
@Override |
174 |
|
protected Void doInBackground(String... arg0) { |
175 |
|
String type = arg0[0]; |
176 |
|
String trainID = arg0[1]; |
177 |
|
success = provider.lookupTimetable(type, trainID); |
178 |
|
timetables = provider.getTimetable(type, trainID); |
179 |
|
|
180 |
|
return null; |
181 |
|
} |
182 |
|
|
183 |
|
} |
184 |
} |
} |