/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java
ViewVC logotype

Contents of /android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 981 - (show annotations) (download)
Sat Jul 10 16:03:10 2010 UTC (13 years, 10 months ago) by torben
File size: 7796 byte(s)
add support for parsing notification messages from departure xml docs
1 package dk.thoerup.traininfo;
2
3 import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures;
4 import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals;
5 import static dk.thoerup.traininfo.R.string.generic_cancel;
6 import static dk.thoerup.traininfo.R.string.generic_retry;
7
8
9 import java.text.NumberFormat;
10
11
12 import android.app.AlertDialog;
13 import android.app.Dialog;
14 import android.app.ListActivity;
15 import android.app.ProgressDialog;
16 import android.content.DialogInterface;
17 import android.content.Intent;
18 import android.net.Uri;
19 import android.os.AsyncTask;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.view.View;
23 import android.view.View.OnClickListener;
24 import android.widget.Button;
25 import android.widget.ListView;
26 import android.widget.TextView;
27 import dk.thoerup.traininfo.provider.DepartureProvider;
28 import dk.thoerup.traininfo.provider.ProviderFactory;
29 import dk.thoerup.traininfo.util.MessageBox;
30
31 public class DepartureList extends ListActivity {
32
33 public static final int DLG_PROGRESS = 1;
34
35
36 DepartureListAdapter adapter;
37 DepartureProvider provider;
38 DepartureBean departures;
39
40 int selectedItemId;
41 //DepartureBean currentDeparture;
42
43 ProgressDialog pgDialog;
44
45 DepartureFetcher fetcher;
46
47 StationBean station;
48
49 boolean arrival = false;
50
51
52 @Override
53 protected void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
55 setContentView(R.layout.departurelist);
56
57 adapter = new DepartureListAdapter(this);
58 setListAdapter(adapter);
59
60 Intent launchedBy = getIntent();
61
62 station = (StationBean) launchedBy.getSerializableExtra("stationbean");
63
64 ((TextView) findViewById(R.id.stationName)).setText( station.getName() );
65
66
67 ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );
68
69 final Button departureBtn = (Button) findViewById(R.id.departurebtn);
70 final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);
71
72 departureBtn.setOnClickListener( new OnClickListener() {
73 @Override
74 public void onClick(View arg0) {
75 arrivalBtn.setBackgroundResource(R.drawable.custom_button);
76 departureBtn.setBackgroundResource(R.drawable.custom_button_hilight);
77 arrival = false;
78 startDepartureFetcher();
79 }
80 });
81 arrivalBtn.setOnClickListener( new OnClickListener() {
82 @Override
83 public void onClick(View arg0) {
84 arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
85 departureBtn.setBackgroundResource(R.drawable.custom_button);
86 arrival = true;
87 startDepartureFetcher();
88 }
89 });
90
91
92
93
94 findViewById(R.id.header).setOnClickListener( mapLauncher );
95
96 int distance = station.getDistance();
97 if (distance != 0) {
98 NumberFormat format = NumberFormat.getNumberInstance();
99 format.setMaximumFractionDigits(1);
100 format.setMinimumFractionDigits(1);
101
102 ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
103 } else {
104 ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE);
105 }
106
107
108 if (station.isRegional() == false && station.isSTrain() == false) {
109 getListView().setVisibility( View.GONE );
110 findViewById(R.id.metroonly).setVisibility( View.VISIBLE );
111 departureBtn.setVisibility( View.GONE );
112 arrivalBtn.setVisibility(View.GONE);
113
114 } else {
115 provider = ProviderFactory.getDepartureProvider();
116
117 if (savedInstanceState == null) {
118 startDepartureFetcher();
119 } else {
120 departures = (DepartureBean) savedInstanceState.getSerializable("departures");
121 adapter.setDepartures(departures.entries);
122 selectedItemId = savedInstanceState.getInt("selectedItemId");
123 }
124 }
125 }
126
127 @Override
128 public void onSaveInstanceState(Bundle outState)
129 {
130 if (pgDialog != null && pgDialog.isShowing())
131 dismissDialog(DLG_PROGRESS);
132
133 outState.putInt("selectedItemId", selectedItemId);
134
135 outState.putSerializable("departures", departures);
136 }
137
138
139
140 @Override
141 protected void onDestroy() {
142 super.onDestroy();
143
144 if (fetcher != null) {
145 fetcher.cancel(true);
146 }
147 }
148
149 @Override
150 protected void onListItemClick(ListView l, View v, int position, long id) {
151 super.onListItemClick(l, v, position, id);
152
153 selectedItemId = position;
154
155 DepartureEntry dep = departures.entries.get(selectedItemId);
156
157 Intent intent = new Intent(this, TimetableList.class);
158 intent.putExtra("departure", dep);
159
160 startActivity(intent);
161
162 }
163
164
165 @Override
166 protected void onPrepareDialog(int id, Dialog dialog) {
167 super.onPrepareDialog(id, dialog);
168
169 switch (id) {
170 case DLG_PROGRESS:
171 pgDialog = (ProgressDialog) dialog;
172 int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals;
173 pgDialog.setMessage( getString(messageId) );
174 break;
175 }
176 }
177
178 @Override
179 protected Dialog onCreateDialog(int id) {
180 switch (id) {
181 case DLG_PROGRESS:
182
183 ProgressDialog dlg = new ProgressDialog(this);
184 dlg.setCancelable(true);
185 return dlg;
186 default:
187 return super.onCreateDialog(id);
188 }
189 }
190
191 void startDepartureFetcher() {
192 showDialog(DLG_PROGRESS);
193 fetcher = new DepartureFetcher();
194 fetcher.execute(station.getId());
195 }
196
197 class DialogDismisser implements View.OnClickListener {
198
199 Dialog dlg;
200 public DialogDismisser(Dialog d) {
201 dlg = d;
202 }
203
204 @Override
205 public void onClick(View v) {
206 if (dlg.isShowing())
207 dlg.dismiss();
208 }
209 }
210
211 View.OnClickListener mapLauncher = new View.OnClickListener() {
212 @Override
213 public void onClick(View v) {
214 Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
215 startActivity( new Intent(Intent.ACTION_VIEW, uri));
216 }
217 };
218
219
220
221 class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
222
223 boolean success;
224
225 @Override
226 protected void onPostExecute(Void result) {
227 super.onPostExecute(result);
228
229
230 pgDialog.dismiss();
231
232 if (success) {
233 DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
234 adapter.setDepartures(departures.entries);
235 DepartureList.this.getListView().setVisibility(View.VISIBLE);
236
237 if (departures.entries.size() == 0) {
238 MessageBox.showMessage(DepartureList.this, "No departures found", true);
239 }
240 } else { // communication or parse error
241 AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
242 builder.setMessage("Error finding departures");
243 builder.setCancelable(true);
244 builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
245 public void onClick(DialogInterface dialog, int id) {
246 dialog.dismiss();
247 startDepartureFetcher();
248
249 }
250 });
251 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
252 public void onClick(DialogInterface dialog, int id) {
253 dialog.dismiss();
254 DepartureList.this.finish();
255 }
256 });
257
258 try {
259 builder.show();
260 } catch (android.view.WindowManager.BadTokenException e) {
261 Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
262 }
263 }
264 }
265
266 @Override
267 protected Void doInBackground(Integer... params) {
268 success = provider.lookupDepartures(params[0], DepartureList.this.arrival);
269 departures = provider.getDepartures(params[0], DepartureList.this.arrival);
270 return null;
271 }
272
273 }
274 }

  ViewVC Help
Powered by ViewVC 1.1.20