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.content.SharedPreferences; |
18 |
import android.graphics.Typeface; |
import android.graphics.Typeface; |
19 |
import android.net.Uri; |
import android.net.Uri; |
20 |
import android.os.AsyncTask; |
import android.os.AsyncTask; |
21 |
import android.os.Bundle; |
import android.os.Bundle; |
22 |
|
import android.preference.PreferenceManager; |
23 |
import android.util.Log; |
import android.util.Log; |
24 |
import android.view.Menu; |
import android.view.Menu; |
25 |
import android.view.MenuItem; |
import android.view.MenuItem; |
30 |
import android.widget.TableLayout; |
import android.widget.TableLayout; |
31 |
import android.widget.TableRow; |
import android.widget.TableRow; |
32 |
import android.widget.TextView; |
import android.widget.TextView; |
33 |
|
import android.widget.Toast; |
34 |
import dk.thoerup.android.traininfo.common.DepartureBean; |
import dk.thoerup.android.traininfo.common.DepartureBean; |
35 |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
36 |
import dk.thoerup.android.traininfo.common.MetroBean; |
import dk.thoerup.android.traininfo.common.MetroBean; |
37 |
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
38 |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
import dk.thoerup.android.traininfo.common.StationEntry; |
39 |
import dk.thoerup.traininfo.provider.DepartureProvider; |
import dk.thoerup.traininfo.provider.DepartureProvider; |
40 |
import dk.thoerup.traininfo.provider.MetroProvider; |
import dk.thoerup.traininfo.provider.MetroProvider; |
41 |
import dk.thoerup.traininfo.provider.ProviderFactory; |
import dk.thoerup.traininfo.provider.ProviderFactory; |
42 |
|
|
43 |
|
import dk.thoerup.traininfo.util.FavoritesHelper; |
44 |
import dk.thoerup.traininfo.util.MessageBox; |
import dk.thoerup.traininfo.util.MessageBox; |
45 |
|
import dk.thoerup.traininfo.util.StationEntryCsv; |
46 |
|
|
47 |
public class DepartureList extends ListActivity { |
public class DepartureList extends ListActivity { |
48 |
|
|
49 |
public static final int DLG_PROGRESS = 1; |
public static final int DLG_PROGRESS = 1; |
50 |
static final int MENU_MAP = 100; |
static final int MENU_MAP = 100; |
51 |
static final int MENU_NOTIFICATIONS = 101; |
static final int MENU_NOTIFICATIONS = 101; |
52 |
static final int MENU_METROMAP= 102; |
static final int MENU_METROMAP = 102; |
53 |
|
static final int MENU_TOGGLEDETAILS= 103; |
54 |
|
|
55 |
|
static final int MENU_FAVORITES_ADD = 104; |
56 |
|
static final int MENU_FAVORITES_REMOVE = 105; |
57 |
|
|
58 |
|
|
59 |
DepartureListAdapter adapter; |
DepartureListAdapter adapter; |
64 |
MetroProvider metro; |
MetroProvider metro; |
65 |
|
|
66 |
int selectedItemId; |
int selectedItemId; |
67 |
|
|
68 |
|
FavoritesHelper favorites; |
69 |
|
|
70 |
//DepartureBean currentDeparture; |
//DepartureBean currentDeparture; |
71 |
|
|
72 |
ProgressDialog pgDialog; |
ProgressDialog pgDialog; |
78 |
|
|
79 |
String trainType = "REGIONAL"; |
String trainType = "REGIONAL"; |
80 |
|
|
81 |
boolean arrival = false; |
boolean arrival = false; |
82 |
|
|
83 |
int commFailCounter = 0; |
int commFailCounter = 0; |
84 |
|
|
86 |
protected void onCreate(Bundle savedInstanceState) { |
protected void onCreate(Bundle savedInstanceState) { |
87 |
super.onCreate(savedInstanceState); |
super.onCreate(savedInstanceState); |
88 |
setContentView(R.layout.departurelist); |
setContentView(R.layout.departurelist); |
89 |
|
|
90 |
|
favorites = new FavoritesHelper(this); |
91 |
|
|
92 |
adapter = new DepartureListAdapter(this); |
adapter = new DepartureListAdapter(this); |
93 |
setListAdapter(adapter); |
setListAdapter(adapter); |
94 |
|
|
95 |
Intent launchedBy = getIntent(); |
Intent launchedBy = getIntent(); |
96 |
|
|
97 |
station = (StationEntry) launchedBy.getSerializableExtra("stationbean"); |
station = (StationEntry) launchedBy.getSerializableExtra("stationbean"); |
206 |
|
|
207 |
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
ProviderFactory.purgeOldEntries(); //cleanup before fetching more data |
208 |
|
|
209 |
Log.e("Station", station.toCSV() ); |
Log.e("Station", StationEntryCsv.toCSV(station) ); |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
if (station.isMetro() == false) { |
if (station.isMetro() == false) { |
214 |
metroBtn.setVisibility( View.GONE ); |
metroBtn.setVisibility( View.GONE ); |
238 |
trainType = "STOG"; |
trainType = "STOG"; |
239 |
|
|
240 |
} |
} |
241 |
|
//Both enabled - use preferred from preferences |
242 |
|
if (station.isRegional() == true && station.isStrain() == true ) { |
243 |
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); |
244 |
|
trainType = prefs.getString("traintype", "REGIONAL"); //default value is gps |
245 |
|
|
246 |
|
if (trainType.equals("STOG") ) { |
247 |
|
stogBtn.setBackgroundResource(R.drawable.custom_button_hilight); |
248 |
|
regionalBtn.setBackgroundResource(R.drawable.custom_button); |
249 |
|
} |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
if (station.isRegional() == false && station.isStrain() == false) { |
if (station.isRegional() == false && station.isStrain() == false) { |
288 |
} |
} |
289 |
} |
} |
290 |
|
|
291 |
|
|
292 |
|
|
293 |
boolean hasNotifications() { |
boolean hasNotifications() { |
294 |
return (departures != null && departures.notifications.size() > 0); |
return (departures != null && departures.notifications.size() > 0); |
295 |
} |
} |
325 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
protected void onListItemClick(ListView l, View v, int position, long id) { |
326 |
super.onListItemClick(l, v, position, id); |
super.onListItemClick(l, v, position, id); |
327 |
|
|
328 |
|
//how can this happen ?? |
329 |
|
if (departures == null || departures.entries == null || departures.entries.size() == 0) { |
330 |
|
Toast.makeText(this, "No departures in list ?!?", Toast.LENGTH_LONG).show(); //TODO: translate |
331 |
|
return; |
332 |
|
} |
333 |
|
|
334 |
selectedItemId = position; |
selectedItemId = position; |
335 |
|
|
336 |
DepartureEntry dep = departures.entries.get(selectedItemId); |
DepartureEntry dep = departures.entries.get(selectedItemId); |
371 |
|
|
372 |
|
|
373 |
|
|
374 |
|
@Override |
375 |
|
public boolean onPrepareOptionsMenu(Menu menu) { |
376 |
|
super.onPrepareOptionsMenu(menu); |
377 |
|
|
378 |
|
/////////////////////// |
379 |
|
|
380 |
|
MenuItem item = menu.findItem( MENU_NOTIFICATIONS ); |
381 |
|
boolean notifEnabled = hasNotifications(); |
382 |
|
item.setEnabled(notifEnabled); |
383 |
|
|
384 |
|
////////////////////////// |
385 |
|
|
386 |
|
|
387 |
|
|
388 |
|
|
389 |
|
return true; |
390 |
|
} |
391 |
|
|
392 |
@Override |
@Override |
393 |
public boolean onCreateOptionsMenu(Menu menu) { |
public boolean onCreateOptionsMenu(Menu menu) { |
394 |
MenuItem item; |
MenuItem item; |
395 |
|
|
396 |
|
|
397 |
|
item = menu.add(0, MENU_TOGGLEDETAILS, 0, getString(R.string.departurelist_toggledetails)); |
398 |
|
item.setIcon(android.R.drawable.ic_menu_view); |
399 |
|
|
400 |
item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) ); |
item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) ); |
401 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
402 |
|
|
405 |
|
|
406 |
boolean notifEnabled = hasNotifications(); |
boolean notifEnabled = hasNotifications(); |
407 |
item.setEnabled(notifEnabled); |
item.setEnabled(notifEnabled); |
408 |
|
|
409 |
|
/////////////////////////////////////// |
410 |
|
if ( ! favorites.hasFavorite( this.station.getId() ) ) { |
411 |
|
item = menu.add(0, MENU_FAVORITES_ADD, 0, getString(dk.thoerup.traininfo.R.string.stationlist_addfavorite) ); |
412 |
|
} else { |
413 |
|
item = menu.add(0, MENU_FAVORITES_REMOVE, 0, getString(dk.thoerup.traininfo.R.string.stationlist_removefavorite) ); |
414 |
|
} |
415 |
|
///////////////////////////// |
416 |
|
|
417 |
if (station.isMetro()) { |
if (station.isMetro()) { |
418 |
item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?! |
item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?! |
419 |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
item.setIcon(android.R.drawable.ic_menu_mapmode); |
420 |
} |
} |
|
|
|
421 |
|
|
422 |
return true; |
return true; |
423 |
} |
} |
424 |
|
|
425 |
@Override |
@Override |
426 |
public boolean onOptionsItemSelected(MenuItem item) { |
public boolean onOptionsItemSelected(MenuItem item) { |
427 |
boolean res; |
boolean res = true; |
428 |
switch(item.getItemId()) { |
switch(item.getItemId()) { |
429 |
case MENU_MAP: |
case MENU_MAP: |
430 |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16"); |
Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16"); |
431 |
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
|
432 |
res = true; |
try { |
433 |
|
startActivity( new Intent(Intent.ACTION_VIEW, uri)); |
434 |
|
} catch (ActivityNotFoundException anfe) { |
435 |
|
Toast.makeText(this, "Could not launch google maps", Toast.LENGTH_LONG).show(); |
436 |
|
} |
437 |
|
|
438 |
break; |
break; |
439 |
case MENU_NOTIFICATIONS: |
case MENU_NOTIFICATIONS: |
440 |
Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); |
Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); |
441 |
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
442 |
startActivity(i); |
startActivity(i); |
|
res = true; |
|
443 |
break; |
break; |
444 |
case MENU_METROMAP: |
case MENU_METROMAP: |
445 |
Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class); |
Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class); |
446 |
startActivity(metroMap); |
startActivity(metroMap); |
|
res = true; |
|
447 |
break; |
break; |
448 |
|
case MENU_TOGGLEDETAILS: |
449 |
|
adapter.toggleShowDetails(); |
450 |
|
break; |
451 |
|
case MENU_FAVORITES_ADD: |
452 |
|
favorites.addFavorite( this.station.getId() ); |
453 |
|
Toast.makeText(this, getString(dk.thoerup.traininfo.R.string.stationlist_stationadded), Toast.LENGTH_SHORT).show(); |
454 |
|
break; |
455 |
|
case MENU_FAVORITES_REMOVE: |
456 |
|
favorites.removeFavorite( station.getId() ); |
457 |
|
Toast.makeText(this, getString(dk.thoerup.traininfo.R.string.stationlist_stationremoved), Toast.LENGTH_SHORT).show(); |
458 |
|
break; |
459 |
default: |
default: |
460 |
res = super.onOptionsItemSelected(item); |
res = super.onOptionsItemSelected(item); |
461 |
} |
} |
506 |
|
|
507 |
|
|
508 |
pgDialog.dismiss(); |
pgDialog.dismiss(); |
509 |
|
|
510 |
if (departures != null) { |
if (departures != null && departures.errorCode == null) { |
511 |
commFailCounter = 0; |
commFailCounter = 0; |
512 |
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 |
513 |
adapter.setDepartures(departures.entries); |
adapter.setDepartures(departures.entries); |
514 |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
DepartureList.this.getListView().setVisibility(View.VISIBLE); |
515 |
|
|
516 |
|
|
517 |
if ( hasNotifications() ) { |
// handle notification icon. |
518 |
findViewById(R.id.notifIcon).setVisibility(View.VISIBLE); |
View notifIcon = findViewById(R.id.notifIcon); |
519 |
|
if ( hasNotifications() ) { |
520 |
|
notifIcon.setVisibility(View.VISIBLE); |
521 |
|
notifIcon.setClickable(true); |
522 |
|
notifIcon.setOnClickListener( new View.OnClickListener() { |
523 |
|
@Override |
524 |
|
public void onClick(View v) { |
525 |
|
Intent i = new Intent(DepartureList.this, dk.thoerup.traininfo.NotificationList.class); |
526 |
|
i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); |
527 |
|
startActivity(i); |
528 |
|
} |
529 |
|
}); |
530 |
|
} else { |
531 |
|
notifIcon.setVisibility(View.INVISIBLE); |
532 |
} |
} |
533 |
|
|
534 |
if (departures.entries.size() == 0) { |
if (departures.entries.size() == 0) { |
535 |
MessageBox.showMessage(DepartureList.this, "No departures found", true); |
int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals; |
536 |
|
MessageBox.showMessage(DepartureList.this, getString(msgId), false); |
537 |
} |
} |
538 |
} else { // communication or parse error |
} else { // communication or parse error |
539 |
commFailCounter++; |
commFailCounter++; |
540 |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); |
541 |
builder.setMessage("Error finding departures"); |
|
542 |
|
if (departures != null && departures.errorCode != null ) { //got an error xml back |
543 |
|
commFailCounter = 10; |
544 |
|
builder.setMessage( getString(R.string.no_backend) ); |
545 |
|
} else { |
546 |
|
builder.setMessage( getString(R.string.departurelist_fetcherror) ); |
547 |
|
} |
548 |
builder.setCancelable(true); |
builder.setCancelable(true); |
549 |
|
|
550 |
if (commFailCounter < 3) { |
if (commFailCounter < 3) { |
551 |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { |
552 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
563 |
} |
} |
564 |
}); |
}); |
565 |
|
|
566 |
try { |
try { //TODO: is this still necessary after the 0.9.4.1 fix ? |
567 |
builder.show(); |
builder.show(); |
568 |
} catch (android.view.WindowManager.BadTokenException e) { |
} catch (android.view.WindowManager.BadTokenException e) { |
569 |
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 |
669 |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { |
670 |
public void onClick(DialogInterface dialog, int id) { |
public void onClick(DialogInterface dialog, int id) { |
671 |
dialog.dismiss(); |
dialog.dismiss(); |
672 |
DepartureList.this.finish(); |
DepartureList.this.finish(); //TODO: should we really close the activity ?? |
673 |
} |
} |
674 |
}); |
}); |
675 |
|
|
676 |
try { |
try { //TODO: is this still necessary after the 0.9.4.1 fix ? |
677 |
builder.show(); |
builder.show(); |
678 |
} catch (android.view.WindowManager.BadTokenException e) { |
} catch (android.view.WindowManager.BadTokenException e) { |
679 |
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 |