/[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 1264 - (show annotations) (download)
Tue Apr 5 07:54:45 2011 UTC (13 years, 1 month ago) by torben
File size: 18006 byte(s)
Damn - this != which was commited in #1262 was only a test and should never have been committed
1 package dk.thoerup.traininfo;
2
3 import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals;
4 import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures;
5 import static dk.thoerup.traininfo.R.string.generic_cancel;
6 import static dk.thoerup.traininfo.R.string.generic_retry;
7
8 import java.text.NumberFormat;
9
10 import android.app.AlertDialog;
11 import android.app.Dialog;
12 import android.app.ListActivity;
13 import android.app.ProgressDialog;
14 import android.content.DialogInterface;
15 import android.content.Intent;
16 import android.graphics.Typeface;
17 import android.net.Uri;
18 import android.os.AsyncTask;
19 import android.os.Bundle;
20 import android.util.Log;
21 import android.view.Menu;
22 import android.view.MenuItem;
23 import android.view.View;
24 import android.view.View.OnClickListener;
25 import android.widget.Button;
26 import android.widget.ListView;
27 import android.widget.TableLayout;
28 import android.widget.TableRow;
29 import android.widget.TextView;
30 import dk.thoerup.android.traininfo.common.DepartureBean;
31 import dk.thoerup.android.traininfo.common.DepartureEntry;
32 import dk.thoerup.android.traininfo.common.MetroBean;
33 import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry;
34 import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
35 import dk.thoerup.traininfo.provider.DepartureProvider;
36 import dk.thoerup.traininfo.provider.MetroProvider;
37 import dk.thoerup.traininfo.provider.ProviderFactory;
38 import dk.thoerup.traininfo.util.MessageBox;
39
40 public class DepartureList extends ListActivity {
41
42 public static final int DLG_PROGRESS = 1;
43 static final int MENU_MAP = 100;
44 static final int MENU_NOTIFICATIONS = 101;
45 static final int MENU_METROMAP= 102;
46
47
48 DepartureListAdapter adapter;
49 DepartureProvider provider;
50 DepartureBean departures;
51
52 MetroBean metroBean;
53 MetroProvider metro;
54
55 int selectedItemId;
56 //DepartureBean currentDeparture;
57
58 ProgressDialog pgDialog;
59
60 DepartureFetcher fetcher;
61 MetroFetcher metroFetcher;
62
63 StationEntry station;
64
65 String trainType = "REGIONAL";
66
67 boolean arrival = false;
68
69 int commFailCounter = 0;
70
71 @Override
72 protected void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
74 setContentView(R.layout.departurelist);
75
76 adapter = new DepartureListAdapter(this);
77 setListAdapter(adapter);
78
79 Intent launchedBy = getIntent();
80
81 station = (StationEntry) launchedBy.getSerializableExtra("stationbean");
82
83 ((TextView) findViewById(R.id.stationName)).setText( station.getName() );
84
85
86 ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() );
87
88 final Button departureBtn = (Button) findViewById(R.id.departurebtn);
89 final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn);
90 final Button metroBtn = (Button) findViewById(R.id.metrobtn);
91 final Button regionalBtn = (Button) findViewById(R.id.regionalbtn);
92 final Button stogBtn = (Button) findViewById(R.id.stogbtn);
93
94 final View metroView = findViewById(R.id.metroonly);
95
96 departureBtn.setOnClickListener( new OnClickListener() {
97 @Override
98 public void onClick(View arg0) {
99 arrivalBtn.setBackgroundResource(R.drawable.custom_button);
100 departureBtn.setBackgroundResource(R.drawable.custom_button_hilight);
101 metroBtn.setBackgroundResource(R.drawable.custom_button);
102
103 getListView().setVisibility( View.VISIBLE );
104 metroView.setVisibility( View.GONE );
105 arrival = false;
106 startDepartureFetcher();
107 }
108 });
109 arrivalBtn.setOnClickListener( new OnClickListener() {
110 @Override
111 public void onClick(View arg0) {
112 arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
113 departureBtn.setBackgroundResource(R.drawable.custom_button);
114 metroBtn.setBackgroundResource(R.drawable.custom_button);
115
116 getListView().setVisibility( View.VISIBLE );
117 metroView.setVisibility( View.GONE );
118 arrival = true;
119 startDepartureFetcher();
120 }
121 });
122
123 regionalBtn.setOnClickListener( new OnClickListener() {
124 @Override
125 public void onClick(View arg0) {
126 regionalBtn.setBackgroundResource(R.drawable.custom_button_hilight);
127 stogBtn.setBackgroundResource(R.drawable.custom_button);
128 metroBtn.setBackgroundResource(R.drawable.custom_button);
129
130 departureBtn.setVisibility( View.VISIBLE );
131 arrivalBtn.setVisibility( View.VISIBLE );
132
133 getListView().setVisibility( View.VISIBLE );
134 metroView.setVisibility( View.GONE );
135 trainType = "REGIONAL";
136 startDepartureFetcher();
137 }
138 });
139 stogBtn.setOnClickListener( new OnClickListener() {
140 @Override
141 public void onClick(View arg0) {
142 regionalBtn.setBackgroundResource(R.drawable.custom_button);
143 stogBtn.setBackgroundResource(R.drawable.custom_button_hilight);
144 metroBtn.setBackgroundResource(R.drawable.custom_button);
145
146
147 departureBtn.setVisibility( View.VISIBLE );
148 arrivalBtn.setVisibility( View.VISIBLE );
149
150 getListView().setVisibility( View.VISIBLE );
151 metroView.setVisibility( View.GONE );
152 trainType = "STOG";
153 startDepartureFetcher();
154 }
155 });
156
157
158
159 metroBtn.setOnClickListener( new OnClickListener() {
160 @Override
161 public void onClick(View v) {
162 regionalBtn.setBackgroundResource(R.drawable.custom_button);
163 stogBtn.setBackgroundResource(R.drawable.custom_button);
164 metroBtn.setBackgroundResource(R.drawable.custom_button_hilight);
165
166 departureBtn.setVisibility( View.GONE );
167 arrivalBtn.setVisibility( View.GONE );
168
169 getListView().setVisibility( View.GONE );
170 metroView.setVisibility( View.VISIBLE );
171 startMetroFetcher();
172 }
173 });
174
175
176
177
178 // findViewById(R.id.header).setOnClickListener( mapLauncher );
179
180 int distance = station.getCalcdist();
181 if (distance != 0) {
182 NumberFormat format = NumberFormat.getNumberInstance();
183 format.setMaximumFractionDigits(1);
184 format.setMinimumFractionDigits(1);
185
186 ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." );
187 } else {
188 ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE);
189 }
190
191 ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
192
193 Log.e("Station", station.toCSV() );
194
195 if (station.isMetro() == false) {
196 metroBtn.setVisibility( View.GONE );
197 }
198
199 metro = ProviderFactory.getMetroProvider();
200
201 if (station.isRegional() == false ) {
202 regionalBtn.setVisibility(View.GONE);
203 }
204
205 if (station.isStrain() == false ) {
206 stogBtn.setVisibility(View.GONE);
207 }
208
209 if (station.isRegional() == true && station.isStrain() == false ) {
210 if ( station.isMetro() == false )
211 regionalBtn.setVisibility(View.GONE);
212 trainType = "REGIONAL";
213 }
214
215 if (station.isRegional() == false && station.isStrain() == true) {
216 if (station.isMetro() == false)
217 stogBtn.setVisibility(View.GONE);
218
219 stogBtn.setBackgroundResource(R.drawable.custom_button_hilight);
220 trainType = "STOG";
221
222 }
223
224
225 if (station.isRegional() == false && station.isStrain() == false) {
226 getListView().setVisibility( View.GONE );
227 metroView.setVisibility( View.VISIBLE );
228
229 departureBtn.setVisibility( View.GONE );
230 arrivalBtn.setVisibility(View.GONE);
231 metroBtn.setVisibility( View.GONE );
232
233
234
235 if (savedInstanceState == null) {
236 startMetroFetcher();
237 } else {
238 metroBean = (MetroBean) savedInstanceState.getSerializable("metro");
239 loadMetroData();
240 }
241
242 } else {
243 provider = ProviderFactory.getDepartureProvider();
244
245 if (savedInstanceState == null) {
246 startDepartureFetcher();
247 } else {
248 departures = (DepartureBean) savedInstanceState.getSerializable("departures");
249
250 if ( (departures != null) && (departures.entries != null) ) {
251 adapter.setDepartures(departures.entries);
252 }
253 selectedItemId = savedInstanceState.getInt("selectedItemId");
254
255 if ( hasNotifications() ) {
256 findViewById(R.id.notifIcon).setVisibility(View.VISIBLE);
257 }
258
259 }
260 }
261 }
262
263 boolean hasNotifications() {
264 return (departures != null && departures.notifications.size() > 0);
265 }
266
267 @Override
268 public void onSaveInstanceState(Bundle outState)
269 {
270 if (pgDialog != null && pgDialog.isShowing())
271 dismissDialog(DLG_PROGRESS);
272
273 outState.putInt("selectedItemId", selectedItemId);
274
275 outState.putSerializable("departures", departures);
276 outState.putSerializable("metro", metroBean);
277 }
278
279
280
281 @Override
282 protected void onDestroy() {
283 super.onDestroy();
284
285 if (fetcher != null) {
286 fetcher.cancel(true);
287 }
288
289 if (metroFetcher != null) {
290 metroFetcher.cancel(true);
291 }
292 }
293
294 @Override
295 protected void onListItemClick(ListView l, View v, int position, long id) {
296 super.onListItemClick(l, v, position, id);
297
298 selectedItemId = position;
299
300 DepartureEntry dep = departures.entries.get(selectedItemId);
301
302 Intent intent = new Intent(this, TimetableList.class);
303 intent.putExtra("departure", dep);
304
305 startActivity(intent);
306
307 }
308
309
310 @Override
311 protected void onPrepareDialog(int id, Dialog dialog) {
312 super.onPrepareDialog(id, dialog);
313
314 switch (id) {
315 case DLG_PROGRESS:
316 pgDialog = (ProgressDialog) dialog;
317 int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals;
318 pgDialog.setMessage( getString(messageId) );
319 break;
320 }
321 }
322
323 @Override
324 protected Dialog onCreateDialog(int id) {
325 switch (id) {
326 case DLG_PROGRESS:
327
328 ProgressDialog dlg = new ProgressDialog(this);
329 dlg.setCancelable(true);
330 return dlg;
331 default:
332 return super.onCreateDialog(id);
333 }
334 }
335
336
337
338
339
340 @Override
341 public boolean onCreateOptionsMenu(Menu menu) {
342 MenuItem item;
343
344 item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) );
345 item.setIcon(android.R.drawable.ic_menu_mapmode);
346
347 item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) );
348 item.setIcon(android.R.drawable.ic_menu_info_details);
349
350 boolean notifEnabled = hasNotifications();
351 item.setEnabled(notifEnabled);
352
353 if (station.isMetro()) {
354 item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?!
355 item.setIcon(android.R.drawable.ic_menu_mapmode);
356 }
357
358
359 return true;
360 }
361
362 @Override
363 public boolean onOptionsItemSelected(MenuItem item) {
364 boolean res;
365 switch(item.getItemId()) {
366 case MENU_MAP:
367 Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16");
368 startActivity( new Intent(Intent.ACTION_VIEW, uri));
369 res = true;
370 break;
371 case MENU_NOTIFICATIONS:
372 Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class);
373 i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
374 startActivity(i);
375 res = true;
376 break;
377 case MENU_METROMAP:
378 Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class);
379 startActivity(metroMap);
380 res = true;
381 break;
382 default:
383 res = super.onOptionsItemSelected(item);
384 }
385 return res;
386 }
387
388 void startDepartureFetcher() {
389 showDialog(DLG_PROGRESS);
390 fetcher = new DepartureFetcher();
391 fetcher.execute(station.getId());
392 }
393
394 void startMetroFetcher() {
395 showDialog(DLG_PROGRESS);
396 metroFetcher = new MetroFetcher();
397 metroFetcher.execute(station.getId());
398 }
399
400 class DialogDismisser implements View.OnClickListener {
401
402 Dialog dlg;
403 public DialogDismisser(Dialog d) {
404 dlg = d;
405 }
406
407 @Override
408 public void onClick(View v) {
409 if (dlg.isShowing())
410 dlg.dismiss();
411 }
412 }
413
414 /*View.OnClickListener mapLauncher = new View.OnClickListener() {
415 @Override
416 public void onClick(View v) {
417 Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude());
418 startActivity( new Intent(Intent.ACTION_VIEW, uri));
419 }
420 };*/
421
422
423
424 class DepartureFetcher extends AsyncTask<Integer, Void, Void> {
425
426 @Override
427 protected void onPostExecute(Void result) {
428 super.onPostExecute(result);
429
430
431 pgDialog.dismiss();
432 //TODO: differentiate whether it was a communication error with my backend
433 //or that the backend failed because bane.dk was unavailable
434 if (departures != null) {
435 commFailCounter = 0;
436 DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html
437 adapter.setDepartures(departures.entries);
438 DepartureList.this.getListView().setVisibility(View.VISIBLE);
439
440
441 // handle notification icon.
442 if ( hasNotifications() ) {
443 View notifIcon = findViewById(R.id.notifIcon);
444 notifIcon.setVisibility(View.VISIBLE);
445 notifIcon.setClickable(true);
446 notifIcon.setOnClickListener( new View.OnClickListener() {
447 @Override
448 public void onClick(View v) {
449 Intent i = new Intent(DepartureList.this, dk.thoerup.traininfo.NotificationList.class);
450 i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications);
451 startActivity(i);
452 }
453 });
454 }
455
456 if (departures.entries.size() == 0) {
457 int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals;
458 MessageBox.showMessage(DepartureList.this, getString(msgId), false);
459 }
460 } else { // communication or parse error
461 commFailCounter++;
462 AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
463 builder.setMessage("Error finding departures");
464 builder.setCancelable(true);
465 if (commFailCounter < 3) {
466 builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
467 public void onClick(DialogInterface dialog, int id) {
468 dialog.dismiss();
469 startDepartureFetcher();
470
471 }
472 });
473 }
474 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
475 public void onClick(DialogInterface dialog, int id) {
476 dialog.dismiss();
477 DepartureList.this.finish();
478 }
479 });
480
481 try { //TODO: is this still necessary after the 0.9.4.1 fix ?
482 builder.show();
483 } catch (android.view.WindowManager.BadTokenException e) {
484 Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
485 }
486 }
487 }
488
489 @Override
490 protected Void doInBackground(Integer... params) {
491 departures = provider.lookupDepartures(params[0], DepartureList.this.arrival, trainType);
492 return null;
493 }
494
495 }
496
497 public void loadMetroData() {
498 ((TextView) findViewById(R.id.operations)).setText( metroBean.operationInfo );
499 ((TextView) findViewById(R.id.plan)).setText( metroBean.plan );
500
501
502 TableLayout table = (TableLayout) findViewById(R.id.metrotable);
503 table.removeAllViews();
504
505 TableRow head = new TableRow(this);
506
507 TextView h1 = new TextView(this);
508 h1.setText("Metro");
509 h1.setTextSize(16);
510 h1.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));
511
512
513 TableRow.LayoutParams params = new TableRow.LayoutParams();
514 params.span = 2;
515 head.addView(h1, params);
516
517
518
519 TextView h2 = new TextView(this);
520 h2.setTextSize(16);
521 h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD));
522 h2.setText("Om minutter");
523
524 params = new TableRow.LayoutParams();
525 params.weight = 2;
526 head.addView(h2,params);
527
528
529
530 table.addView(head);
531
532 for (MetroEntry entry : metroBean.entries) {
533 TableRow row = new TableRow(this);
534
535 Log.e("Test", "" + entry.destination);
536
537 TextView v1 = new TextView(this);
538 v1.setTextSize(16);
539 v1.setText( entry.metro );
540 row.addView(v1);
541
542 TextView v2 = new TextView(this);
543 v2.setTextSize(16);
544 v2.setText( entry.destination );
545 row.addView(v2);
546
547 TextView v3 = new TextView(this);
548 v3.setTextSize(16);
549 v3.setText( entry.minutes );
550 row.addView(v3);
551
552 table.addView(row);
553
554 }
555 findViewById(R.id.rootView).requestLayout();
556 }
557
558 class MetroFetcher extends AsyncTask<Integer, Void, Void> {
559
560 @Override
561 protected void onPostExecute(Void result) {
562 super.onPostExecute(result);
563
564
565
566 pgDialog.dismiss();
567
568 if (metroBean != null) {
569 loadMetroData();
570 } else { // communication or parse error
571 commFailCounter++;
572 AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this);
573 builder.setMessage("Error finding metro data");
574 builder.setCancelable(true);
575 if (commFailCounter < 3) {
576 builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
577 public void onClick(DialogInterface dialog, int id) {
578 dialog.dismiss();
579 startMetroFetcher();
580
581 }
582 });
583 }
584 builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
585 public void onClick(DialogInterface dialog, int id) {
586 dialog.dismiss();
587 DepartureList.this.finish(); //TODO: should we really close the activity ??
588 }
589 });
590
591 try { //TODO: is this still necessary after the 0.9.4.1 fix ?
592 builder.show();
593 } catch (android.view.WindowManager.BadTokenException e) {
594 Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
595 }
596 }
597 }
598
599 @Override
600 protected Void doInBackground(Integer... params) {
601 metroBean = metro.lookupMetroInfo(params[0]);
602 return null;
603 }
604
605 }
606
607 }

  ViewVC Help
Powered by ViewVC 1.1.20