11 |
import android.app.Dialog; |
import android.app.Dialog; |
12 |
import android.app.ListActivity; |
import android.app.ListActivity; |
13 |
import android.app.ProgressDialog; |
import android.app.ProgressDialog; |
14 |
|
import android.content.ActivityNotFoundException; |
15 |
import android.content.DialogInterface; |
import android.content.DialogInterface; |
16 |
import android.content.Intent; |
import android.content.Intent; |
17 |
import android.graphics.Typeface; |
import android.graphics.Typeface; |
28 |
import android.widget.TableLayout; |
import android.widget.TableLayout; |
29 |
import android.widget.TableRow; |
import android.widget.TableRow; |
30 |
import android.widget.TextView; |
import android.widget.TextView; |
31 |
|
import android.widget.Toast; |
32 |
import dk.thoerup.android.traininfo.common.DepartureBean; |
import dk.thoerup.android.traininfo.common.DepartureBean; |
33 |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
34 |
import dk.thoerup.android.traininfo.common.MetroBean; |
import dk.thoerup.android.traininfo.common.MetroBean; |
35 |
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
36 |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
import dk.thoerup.android.traininfo.common.StationEntry; |
37 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
38 |
import dk.thoerup.traininfo.provider.MetroProvider; |
import dk.thoerup.traininfo.provider.MetroProvider; |
39 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
44 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
45 |
static final int MENU_MAP = 100; |
static final int MENU_MAP = 100; |
46 |
static final int MENU_NOTIFICATIONS = 101; |
static final int MENU_NOTIFICATIONS = 101; |
47 |
static final int MENU_METROMAP= 102; |
static final int MENU_METROMAP = 102; |
48 |
|
static final int MENU_TOGGLEDETAILS= 103; |
49 |
|
|
50 |
|
|
51 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
67 |
|
|
68 |
String trainType = "REGIONAL"; |
String trainType = "REGIONAL"; |
69 |
|
|
70 |
boolean arrival = false; |
boolean arrival = false; |
71 |
|
|
72 |
int commFailCounter = 0; |
int commFailCounter = 0; |
73 |
|
|
77 |
setContentView(R.layout.departurelist); |
setContentView(R.layout.departurelist); |
78 |
|
|
79 |
adapter = new DepartureListAdapter(this); |
adapter = new DepartureListAdapter(this); |
80 |
setListAdapter(adapter); |
setListAdapter(adapter); |
81 |
|
|
82 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
83 |
|
|
338 |
|
|
339 |
|
|
340 |
|
|
341 |
|
@Override |
342 |
|
public boolean onPrepareOptionsMenu(Menu menu) { |
343 |
|
super.onPrepareOptionsMenu(menu); |
344 |
|
|
345 |
|
MenuItem item = menu.findItem( MENU_NOTIFICATIONS ); |
346 |
|
boolean notifEnabled = hasNotifications(); |
347 |
|
item.setEnabled(notifEnabled); |
348 |
|
|
349 |
|
return true; |
350 |
|
} |
351 |
|
|
352 |
@Override |
@Override |
353 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
354 |
MenuItem item; |
MenuItem item; |
355 |
|
|
356 |
|
|
357 |
|
item = menu.add(0, MENU_TOGGLEDETAILS, 0, getString(R.string.departurelist_toggledetails)); |
358 |
|
item.setIcon(android.R.drawable.ic_menu_view); |
359 |
|
|
360 |
item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) ); |
item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) ); |
361 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
362 |
|
|
369 |
if (station.isMetro()) { |
if (station.isMetro()) { |
370 |
item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?! |
item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?! |
371 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
372 |
} |
} |
|
|
|
373 |
|
|
374 |
return true; |
return true; |
375 |
} |
} |
376 |
|
|
377 |
@Override |
@Override |
378 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
379 |
boolean res; |
boolean res = true; |
380 |
switch(item.getItemId()) { |
switch(item.getItemId()) { |
381 |
case MENU_MAP: |
case MENU_MAP: |
382 |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16"); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16"); |
383 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
|
384 |
res = true; |
try { |
385 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
386 |
|
} catch (ActivityNotFoundException anfe) { |
387 |
|
Toast.makeText(this, "Could not launch google maps", Toast.LENGTH_LONG).show(); |
388 |
|
} |
389 |
|
|
390 |
break; |
break; |
391 |
case MENU_NOTIFICATIONS: |
case MENU_NOTIFICATIONS: |
392 |
Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); |
Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); |
393 |
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
394 |
startActivity(i); |
startActivity(i); |
|
res = true; |
|
395 |
break; |
break; |
396 |
case MENU_METROMAP: |
case MENU_METROMAP: |
397 |
Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class); |
Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class); |
398 |
startActivity(metroMap); |
startActivity(metroMap); |
|
res = true; |
|
399 |
break; |
break; |
400 |
|
case MENU_TOGGLEDETAILS: |
401 |
|
adapter.toggleShowDetails(); |
402 |
|
break; |
403 |
default: |
default: |
404 |
res = super.onOptionsItemSelected(item); |
res = super.onOptionsItemSelected(item); |
405 |
} |
} |
450 |
|
|
451 |
|
|
452 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
453 |
|
|
454 |
if (departures != null) { |
if (departures != null && departures.errorCode == null) { |
455 |
commFailCounter = 0; |
commFailCounter = 0; |
456 |
DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html |
DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html |
457 |
adapter.setDepartures(departures.entries); |
adapter.setDepartures(departures.entries); |
458 |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
459 |
|
|
460 |
|
|
461 |
// handle notification icon. |
// handle notification icon. |
462 |
if ( hasNotifications() ) { |
View notifIcon = findViewById(R.id.notifIcon); |
463 |
View notifIcon = findViewById(R.id.notifIcon); |
if ( hasNotifications() ) { |
464 |
notifIcon.setVisibility(View.VISIBLE); |
notifIcon.setVisibility(View.VISIBLE); |
465 |
notifIcon.setClickable(true); |
notifIcon.setClickable(true); |
466 |
notifIcon.setOnClickListener( new View.OnClickListener() { |
notifIcon.setOnClickListener( new View.OnClickListener() { |
471 |
startActivity(i); |
startActivity(i); |
472 |
} |
} |
473 |
}); |
}); |
474 |
} |
} else { |
475 |
|
notifIcon.setVisibility(View.INVISIBLE); |
476 |
|
} |
477 |
|
|
478 |
if (departures.entries.size() != 0) { |
if (departures.entries.size() == 0) { |
479 |
int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals; |
int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals; |
480 |
MessageBox.showMessage(DepartureList.this, getString(msgId), false); |
MessageBox.showMessage(DepartureList.this, getString(msgId), false); |
481 |
} |
} |
482 |
} else { // communication or parse error |
} else { // communication or parse error |
483 |
commFailCounter++; |
commFailCounter++; |
484 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
485 |
builder.setMessage("Error finding departures"); |
|
486 |
|
if (departures != null && departures.errorCode != null ) { //got an error xml back |
487 |
|
commFailCounter = 10; |
488 |
|
builder.setMessage( getString(R.string.no_backend) ); |
489 |
|
} else { |
490 |
|
builder.setMessage( getString(R.string.departurelist_fetcherror) ); |
491 |
|
} |
492 |
builder.setCancelable(true); |
builder.setCancelable(true); |
493 |
|
|
494 |
if (commFailCounter < 3) { |
if (commFailCounter < 3) { |
495 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
496 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
507 |
} |
} |
508 |
}); |
}); |
509 |
|
|
510 |
try { |
try { //TODO: is this still necessary after the 0.9.4.1 fix ? |
511 |
builder.show(); |
builder.show(); |
512 |
} catch (android.view.WindowManager.BadTokenException e) { |
} catch (android.view.WindowManager.BadTokenException e) { |
513 |
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
613 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
614 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
615 |
dialog.dismiss(); |
dialog.dismiss(); |
616 |
DepartureList.this.finish(); |
DepartureList.this.finish(); //TODO: should we really close the activity ?? |
617 |
} |
} |
618 |
}); |
}); |
619 |
|
|
620 |
try { |
try { //TODO: is this still necessary after the 0.9.4.1 fix ? |
621 |
builder.show(); |
builder.show(); |
622 |
} catch (android.view.WindowManager.BadTokenException e) { |
} catch (android.view.WindowManager.BadTokenException e) { |
623 |
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |
Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running |