--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2009/09/11 12:24:53 319 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/07/11 14:58:13 982 @@ -1,21 +1,31 @@ package dk.thoerup.traininfo; +import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures; +import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals; +import static dk.thoerup.traininfo.R.string.generic_cancel; +import static dk.thoerup.traininfo.R.string.generic_retry; + + import java.text.NumberFormat; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; + +import android.app.AlertDialog; import android.app.Dialog; import android.app.ListActivity; import android.app.ProgressDialog; +import android.content.DialogInterface; import android.content.Intent; -import android.location.Address; -import android.location.Geocoder; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; +import android.view.ContextMenu; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; +import android.view.ContextMenu.ContextMenuInfo; +import android.view.View.OnClickListener; +import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import dk.thoerup.traininfo.provider.DepartureProvider; @@ -25,22 +35,26 @@ public class DepartureList extends ListActivity { public static final int DLG_PROGRESS = 1; - public static final int DLG_DETAILS = 2; + static final int MENU_MAP = 100; + static final int MENU_NOTIFICATIONS = 101; + DepartureListAdapter adapter; DepartureProvider provider; - List departures; + DepartureBean departures; int selectedItemId; //DepartureBean currentDeparture; ProgressDialog pgDialog; - Dialog detailsDialog; + DepartureFetcher fetcher; + + StationBean station; - double latitude,longitude; + boolean arrival = false; - @SuppressWarnings("unchecked") + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -50,64 +64,116 @@ setListAdapter(adapter); Intent launchedBy = getIntent(); + + station = (StationBean) launchedBy.getSerializableExtra("stationbean"); - latitude = launchedBy.getDoubleExtra("latitude", 0.0); - longitude = launchedBy.getDoubleExtra("longitude", 0.0); - - String name = launchedBy.getStringExtra("name"); - ((TextView) findViewById(R.id.stationName)).setText( name ); + ((TextView) findViewById(R.id.stationName)).setText( station.getName() ); - String address = launchedBy.getStringExtra("address"); - ((TextView) findViewById(R.id.stationAddr)).setText( address ); + + ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); - int stationId = launchedBy.getIntExtra("stationid", -1); + final Button departureBtn = (Button) findViewById(R.id.departurebtn); + final Button arrivalBtn = (Button) findViewById(R.id.arrivalbtn); - findViewById(R.id.header).setOnClickListener( mapLauncher ); + departureBtn.setOnClickListener( new OnClickListener() { + @Override + public void onClick(View arg0) { + arrivalBtn.setBackgroundResource(R.drawable.custom_button); + departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); + arrival = false; + startDepartureFetcher(); + } + }); + arrivalBtn.setOnClickListener( new OnClickListener() { + @Override + public void onClick(View arg0) { + arrivalBtn.setBackgroundResource(R.drawable.custom_button_hilight); + departureBtn.setBackgroundResource(R.drawable.custom_button); + arrival = true; + startDepartureFetcher(); + } + }); - NumberFormat format = NumberFormat.getNumberInstance(); - format.setMaximumFractionDigits(1); - format.setMinimumFractionDigits(1); - int distance = launchedBy.getIntExtra("distance", 0); - ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); - - provider = ProviderFactory.getDepartureProvider(); - fetcher = new DepartureFetcher(); - if (savedInstanceState == null) { - showDialog(DLG_PROGRESS); - fetcher.execute(stationId); + + // findViewById(R.id.header).setOnClickListener( mapLauncher ); + + int distance = station.getDistance(); + if (distance != 0) { + NumberFormat format = NumberFormat.getNumberInstance(); + format.setMaximumFractionDigits(1); + format.setMinimumFractionDigits(1); + + ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); + } else { + ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); + } + + + if (station.isRegional() == false && station.isSTrain() == false) { + getListView().setVisibility( View.GONE ); + findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); + departureBtn.setVisibility( View.GONE ); + arrivalBtn.setVisibility(View.GONE); + } else { - departures = (List) savedInstanceState.getSerializable("departures"); - adapter.setDepartures(departures); - selectedItemId = savedInstanceState.getInt("selectedItemId"); - boolean detailsShowing = savedInstanceState.getBoolean("detailsShowing"); - if (detailsShowing) - showDialog(DLG_DETAILS); + provider = ProviderFactory.getDepartureProvider(); + + if (savedInstanceState == null) { + startDepartureFetcher(); + } else { + departures = (DepartureBean) savedInstanceState.getSerializable("departures"); + adapter.setDepartures(departures.entries); + selectedItemId = savedInstanceState.getInt("selectedItemId"); + + if ( hasNotifications() ) { + findViewById(R.id.notifIcon).setVisibility(View.VISIBLE); + } + + } } } + boolean hasNotifications() { + return (departures != null && departures.notifications.size() > 0); + } + @Override public void onSaveInstanceState(Bundle outState) { if (pgDialog != null && pgDialog.isShowing()) dismissDialog(DLG_PROGRESS); - boolean detailsShowing = (detailsDialog != null && detailsDialog.isShowing()); - if (detailsShowing) { - dismissDialog(DLG_DETAILS); - } - outState.putBoolean("detailsShowing", detailsShowing); + outState.putInt("selectedItemId", selectedItemId); - outState.putSerializable("departures", (ArrayList) departures); + outState.putSerializable("departures", departures); } + + @Override + protected void onDestroy() { + super.onDestroy(); + + if (fetcher != null) { + fetcher.cancel(true); + } + } + + @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); + + selectedItemId = position; + + DepartureEntry dep = departures.entries.get(selectedItemId); - selectedItemId = position; - showDialog(DLG_DETAILS); + Intent intent = new Intent(this, TimetableList.class); + intent.putExtra("departure", dep); + + startActivity(intent); + } @@ -116,20 +182,10 @@ super.onPrepareDialog(id, dialog); switch (id) { - case DLG_DETAILS: - DepartureBean currentDeparture = departures.get(selectedItemId); - ((TextView)dialog.findViewById(R.id.Time)).setText(currentDeparture.getTime()); - ((TextView)dialog.findViewById(R.id.Train)).setText(currentDeparture.getTrainNumber()); - ((TextView)dialog.findViewById(R.id.Destination)).setText( currentDeparture.getDestination()); - ((TextView)dialog.findViewById(R.id.Origin)).setText(currentDeparture.getOrigin()); - ((TextView)dialog.findViewById(R.id.Location)).setText(currentDeparture.getLocation()); - ((TextView)dialog.findViewById(R.id.Updated)).setText(currentDeparture.getLastUpdateString()); - ((TextView)dialog.findViewById(R.id.Status)).setText(currentDeparture.getStatus()); - ((TextView)dialog.findViewById(R.id.Note)).setText(currentDeparture.getNote()); - detailsDialog = dialog; - break; case DLG_PROGRESS: pgDialog = (ProgressDialog) dialog; + int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; + pgDialog.setMessage( getString(messageId) ); break; } } @@ -138,26 +194,63 @@ protected Dialog onCreateDialog(int id) { switch (id) { case DLG_PROGRESS: + ProgressDialog dlg = new ProgressDialog(this); - dlg.setMessage("Fetch departure data"); dlg.setCancelable(true); - return dlg; - case DLG_DETAILS: - //Context mContext = getApplicationContext(); - Dialog dialog = new Dialog(this); - dialog.setCancelable(true); - - dialog.setContentView(R.layout.departuredetails); - dialog.setTitle("Departure details"); - - View root = dialog.findViewById(R.id.layout_root); - root.setOnClickListener( new DialogDismisser(dialog) ); - return dialog; + return dlg; default: return super.onCreateDialog(id); } } + + + + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuItem item; + item = menu.add(0, MENU_MAP, 0, "Show on map"); + item.setIcon(android.R.drawable.ic_menu_mapmode); + + item = menu.add(0, MENU_NOTIFICATIONS, 0, "Notifications"); + item.setIcon(android.R.drawable.ic_menu_info_details); + + + boolean notifEnabled = hasNotifications(); + item.setEnabled(notifEnabled); + + + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + boolean res; + switch(item.getItemId()) { + case MENU_MAP: + Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); + startActivity( new Intent(Intent.ACTION_VIEW, uri)); + res = true; + 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; + break; + default: + res = super.onOptionsItemSelected(item); + } + return res; + } + + void startDepartureFetcher() { + showDialog(DLG_PROGRESS); + fetcher = new DepartureFetcher(); + fetcher.execute(station.getId()); + } + class DialogDismisser implements View.OnClickListener { Dialog dlg; @@ -169,42 +262,73 @@ public void onClick(View v) { if (dlg.isShowing()) dlg.dismiss(); - } + } } - View.OnClickListener mapLauncher = new View.OnClickListener() { + /*View.OnClickListener mapLauncher = new View.OnClickListener() { @Override - public void onClick(View v) { - Uri uri = Uri.parse("geo:" + latitude + "," + longitude); + public void onClick(View v) { + Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); startActivity( new Intent(Intent.ACTION_VIEW, uri)); } - }; - + };*/ + + class DepartureFetcher extends AsyncTask { boolean success; - String addr; + @Override protected void onPostExecute(Void result) { super.onPostExecute(result); - adapter.setDepartures(departures); + pgDialog.dismiss(); if (success) { - if (departures.size() == 0) { - MessageBox.showMessage(DepartureList.this, "No departures found"); + 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); + + + if ( hasNotifications() ) { + findViewById(R.id.notifIcon).setVisibility(View.VISIBLE); + } + + if (departures.entries.size() == 0) { + MessageBox.showMessage(DepartureList.this, "No departures found", true); } } else { // communication or parse error - MessageBox.showMessage(DepartureList.this, "Error finding departures"); + AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); + builder.setMessage("Error finding departures"); + builder.setCancelable(true); + builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + startDepartureFetcher(); + + } + }); + builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + DepartureList.this.finish(); + } + }); + + try { + 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 + } } } @Override protected Void doInBackground(Integer... params) { - success = provider.lookupDepartures(params[0]); - departures = provider.getDepartures(params[0]); + success = provider.lookupDepartures(params[0], DepartureList.this.arrival); + departures = provider.getDepartures(params[0], DepartureList.this.arrival); return null; }