--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2011/04/05 07:54:45 1264 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2012/03/06 11:02:04 1709 @@ -11,12 +11,15 @@ import android.app.Dialog; import android.app.ListActivity; import android.app.ProgressDialog; +import android.content.ActivityNotFoundException; import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; import android.graphics.Typeface; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; +import android.preference.PreferenceManager; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -27,22 +30,30 @@ import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; +import android.widget.Toast; import dk.thoerup.android.traininfo.common.DepartureBean; import dk.thoerup.android.traininfo.common.DepartureEntry; import dk.thoerup.android.traininfo.common.MetroBean; import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; -import dk.thoerup.android.traininfo.common.StationBean.StationEntry; +import dk.thoerup.android.traininfo.common.StationEntry; import dk.thoerup.traininfo.provider.DepartureProvider; import dk.thoerup.traininfo.provider.MetroProvider; import dk.thoerup.traininfo.provider.ProviderFactory; + +import dk.thoerup.traininfo.util.FavoritesHelper; import dk.thoerup.traininfo.util.MessageBox; +import dk.thoerup.traininfo.util.StationEntryCsv; public class DepartureList extends ListActivity { public static final int DLG_PROGRESS = 1; static final int MENU_MAP = 100; static final int MENU_NOTIFICATIONS = 101; - static final int MENU_METROMAP= 102; + static final int MENU_METROMAP = 102; + static final int MENU_TOGGLEDETAILS= 103; + + static final int MENU_FAVORITES_ADD = 104; + static final int MENU_FAVORITES_REMOVE = 105; DepartureListAdapter adapter; @@ -53,6 +64,9 @@ MetroProvider metro; int selectedItemId; + + FavoritesHelper favorites; + //DepartureBean currentDeparture; ProgressDialog pgDialog; @@ -64,7 +78,7 @@ String trainType = "REGIONAL"; - boolean arrival = false; + boolean arrival = false; int commFailCounter = 0; @@ -72,10 +86,12 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.departurelist); - + + favorites = new FavoritesHelper(this); + adapter = new DepartureListAdapter(this); setListAdapter(adapter); - + Intent launchedBy = getIntent(); station = (StationEntry) launchedBy.getSerializableExtra("stationbean"); @@ -190,7 +206,9 @@ ProviderFactory.purgeOldEntries(); //cleanup before fetching more data - Log.e("Station", station.toCSV() ); + Log.e("Station", StationEntryCsv.toCSV(station) ); + + if (station.isMetro() == false) { metroBtn.setVisibility( View.GONE ); @@ -220,6 +238,16 @@ trainType = "STOG"; } + //Both enabled - use preferred from preferences + if (station.isRegional() == true && station.isStrain() == true ) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + trainType = prefs.getString("traintype", "REGIONAL"); //default value is gps + + if (trainType.equals("STOG") ) { + stogBtn.setBackgroundResource(R.drawable.custom_button_hilight); + regionalBtn.setBackgroundResource(R.drawable.custom_button); + } + } if (station.isRegional() == false && station.isStrain() == false) { @@ -260,6 +288,8 @@ } } + + boolean hasNotifications() { return (departures != null && departures.notifications.size() > 0); } @@ -295,6 +325,12 @@ protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); + //how can this happen ?? + if (departures == null || departures.entries == null || departures.entries.size() == 0) { + Toast.makeText(this, "No departures in list ?!?", Toast.LENGTH_LONG).show(); //TODO: translate + return; + } + selectedItemId = position; DepartureEntry dep = departures.entries.get(selectedItemId); @@ -335,12 +371,32 @@ + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + super.onPrepareOptionsMenu(menu); + + /////////////////////// + + MenuItem item = menu.findItem( MENU_NOTIFICATIONS ); + boolean notifEnabled = hasNotifications(); + item.setEnabled(notifEnabled); + + ////////////////////////// + + + return true; + } + @Override public boolean onCreateOptionsMenu(Menu menu) { MenuItem item; + + item = menu.add(0, MENU_TOGGLEDETAILS, 0, getString(R.string.departurelist_toggledetails)); + item.setIcon(android.R.drawable.ic_menu_view); + item = menu.add(0, MENU_MAP, 0, getString(R.string.departurelist_showonmap) ); item.setIcon(android.R.drawable.ic_menu_mapmode); @@ -349,36 +405,57 @@ boolean notifEnabled = hasNotifications(); item.setEnabled(notifEnabled); + + /////////////////////////////////////// + if ( ! favorites.hasFavorite( this.station.getId() ) ) { + item = menu.add(0, MENU_FAVORITES_ADD, 0, getString(dk.thoerup.traininfo.R.string.stationlist_addfavorite) ); + } else { + item = menu.add(0, MENU_FAVORITES_REMOVE, 0, getString(dk.thoerup.traininfo.R.string.stationlist_removefavorite) ); + } + ///////////////////////////// if (station.isMetro()) { item = menu.add(0, MENU_METROMAP, 0, "Metro" ); //TODO:translate!?! item.setIcon(android.R.drawable.ic_menu_mapmode); - } - + } return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { - boolean res; + boolean res = true; switch(item.getItemId()) { case MENU_MAP: Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16"); - startActivity( new Intent(Intent.ACTION_VIEW, uri)); - res = true; + + try { + startActivity( new Intent(Intent.ACTION_VIEW, uri)); + } catch (ActivityNotFoundException anfe) { + Toast.makeText(this, "Could not launch google maps", Toast.LENGTH_LONG).show(); + } + break; case MENU_NOTIFICATIONS: Intent i = new Intent(this,dk.thoerup.traininfo.NotificationList.class); i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); - startActivity(i); - res = true; + startActivity(i); break; case MENU_METROMAP: Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class); - startActivity(metroMap); - res = true; + startActivity(metroMap); break; + case MENU_TOGGLEDETAILS: + adapter.toggleShowDetails(); + break; + case MENU_FAVORITES_ADD: + favorites.addFavorite( this.station.getId() ); + Toast.makeText(this, getString(dk.thoerup.traininfo.R.string.stationlist_stationadded), Toast.LENGTH_SHORT).show(); + break; + case MENU_FAVORITES_REMOVE: + favorites.removeFavorite( station.getId() ); + Toast.makeText(this, getString(dk.thoerup.traininfo.R.string.stationlist_stationremoved), Toast.LENGTH_SHORT).show(); + break; default: res = super.onOptionsItemSelected(item); } @@ -429,18 +506,17 @@ pgDialog.dismiss(); - //TODO: differentiate whether it was a communication error with my backend - //or that the backend failed because bane.dk was unavailable - if (departures != null) { + + if (departures != null && departures.errorCode == null) { commFailCounter = 0; DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html adapter.setDepartures(departures.entries); DepartureList.this.getListView().setVisibility(View.VISIBLE); - // handle notification icon. - if ( hasNotifications() ) { - View notifIcon = findViewById(R.id.notifIcon); + // handle notification icon. + View notifIcon = findViewById(R.id.notifIcon); + if ( hasNotifications() ) { notifIcon.setVisibility(View.VISIBLE); notifIcon.setClickable(true); notifIcon.setOnClickListener( new View.OnClickListener() { @@ -451,7 +527,9 @@ startActivity(i); } }); - } + } else { + notifIcon.setVisibility(View.INVISIBLE); + } if (departures.entries.size() == 0) { int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals; @@ -459,9 +537,16 @@ } } else { // communication or parse error commFailCounter++; - AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); - builder.setMessage("Error finding departures"); + AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); + + if (departures != null && departures.errorCode != null ) { //got an error xml back + commFailCounter = 10; + builder.setMessage( getString(R.string.no_backend) ); + } else { + builder.setMessage( getString(R.string.departurelist_fetcherror) ); + } builder.setCancelable(true); + if (commFailCounter < 3) { builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) {