--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2011/04/04 22:14:40 1261 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2011/05/03 16:28:56 1434 @@ -11,6 +11,7 @@ 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.graphics.Typeface; @@ -27,11 +28,12 @@ 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; @@ -42,7 +44,8 @@ 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; DepartureListAdapter adapter; @@ -64,7 +67,7 @@ String trainType = "REGIONAL"; - boolean arrival = false; + boolean arrival = false; int commFailCounter = 0; @@ -74,7 +77,7 @@ setContentView(R.layout.departurelist); adapter = new DepartureListAdapter(this); - setListAdapter(adapter); + setListAdapter(adapter); Intent launchedBy = getIntent(); @@ -335,12 +338,14 @@ - - @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); @@ -353,32 +358,37 @@ 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; default: res = super.onOptionsItemSelected(item); } @@ -429,8 +439,8 @@ pgDialog.dismiss(); - - 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); @@ -453,13 +463,21 @@ } if (departures.entries.size() == 0) { - MessageBox.showMessage(DepartureList.this, "No departures found", true); + int msgId = (arrival==false) ? R.string.departurelist_nodepartures : R.string.departurelist_noarrivals; + MessageBox.showMessage(DepartureList.this, getString(msgId), false); } } 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) { @@ -476,7 +494,7 @@ } }); - try { + try { //TODO: is this still necessary after the 0.9.4.1 fix ? builder.show(); } catch (android.view.WindowManager.BadTokenException e) { Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running @@ -582,11 +600,11 @@ builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); - DepartureList.this.finish(); + DepartureList.this.finish(); //TODO: should we really close the activity ?? } }); - try { + try { //TODO: is this still necessary after the 0.9.4.1 fix ? builder.show(); } catch (android.view.WindowManager.BadTokenException e) { Log.i("DepartureList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running