--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2009/09/11 09:12:45 317 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2011/05/03 12:38:04 1433 @@ -1,46 +1,75 @@ package dk.thoerup.traininfo; +import static dk.thoerup.traininfo.R.string.departurelist_fetcharrivals; +import static dk.thoerup.traininfo.R.string.departurelist_fetchdepartures; +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.ActivityNotFoundException; +import android.content.DialogInterface; import android.content.Intent; -import android.location.Address; -import android.location.Geocoder; +import android.graphics.Typeface; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; import android.widget.ListView; +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.StationEntry; import dk.thoerup.traininfo.provider.DepartureProvider; +import dk.thoerup.traininfo.provider.MetroProvider; import dk.thoerup.traininfo.provider.ProviderFactory; import dk.thoerup.traininfo.util.MessageBox; 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; + static final int MENU_METROMAP= 102; + DepartureListAdapter adapter; DepartureProvider provider; - List departures; + DepartureBean departures; + + MetroBean metroBean; + MetroProvider metro; int selectedItemId; //DepartureBean currentDeparture; ProgressDialog pgDialog; - Dialog detailsDialog; + DepartureFetcher fetcher; + MetroFetcher metroFetcher; + + StationEntry station; - double latitude,longitude; + String trainType = "REGIONAL"; - @SuppressWarnings("unchecked") + boolean arrival = false; + + int commFailCounter = 0; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -50,64 +79,233 @@ setListAdapter(adapter); Intent launchedBy = getIntent(); + + station = (StationEntry) 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); + final Button metroBtn = (Button) findViewById(R.id.metrobtn); + final Button regionalBtn = (Button) findViewById(R.id.regionalbtn); + final Button stogBtn = (Button) findViewById(R.id.stogbtn); + + final View metroView = findViewById(R.id.metroonly); + + departureBtn.setOnClickListener( new OnClickListener() { + @Override + public void onClick(View arg0) { + arrivalBtn.setBackgroundResource(R.drawable.custom_button); + departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); + metroBtn.setBackgroundResource(R.drawable.custom_button); + + getListView().setVisibility( View.VISIBLE ); + metroView.setVisibility( View.GONE ); + 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); + metroBtn.setBackgroundResource(R.drawable.custom_button); + + getListView().setVisibility( View.VISIBLE ); + metroView.setVisibility( View.GONE ); + arrival = true; + startDepartureFetcher(); + } + }); + + regionalBtn.setOnClickListener( new OnClickListener() { + @Override + public void onClick(View arg0) { + regionalBtn.setBackgroundResource(R.drawable.custom_button_hilight); + stogBtn.setBackgroundResource(R.drawable.custom_button); + metroBtn.setBackgroundResource(R.drawable.custom_button); + + departureBtn.setVisibility( View.VISIBLE ); + arrivalBtn.setVisibility( View.VISIBLE ); + + getListView().setVisibility( View.VISIBLE ); + metroView.setVisibility( View.GONE ); + trainType = "REGIONAL"; + startDepartureFetcher(); + } + }); + stogBtn.setOnClickListener( new OnClickListener() { + @Override + public void onClick(View arg0) { + regionalBtn.setBackgroundResource(R.drawable.custom_button); + stogBtn.setBackgroundResource(R.drawable.custom_button_hilight); + metroBtn.setBackgroundResource(R.drawable.custom_button); + + + departureBtn.setVisibility( View.VISIBLE ); + arrivalBtn.setVisibility( View.VISIBLE ); + + getListView().setVisibility( View.VISIBLE ); + metroView.setVisibility( View.GONE ); + trainType = "STOG"; + startDepartureFetcher(); + } + }); + + + + metroBtn.setOnClickListener( new OnClickListener() { + @Override + public void onClick(View v) { + regionalBtn.setBackgroundResource(R.drawable.custom_button); + stogBtn.setBackgroundResource(R.drawable.custom_button); + metroBtn.setBackgroundResource(R.drawable.custom_button_hilight); + + departureBtn.setVisibility( View.GONE ); + arrivalBtn.setVisibility( View.GONE ); + + getListView().setVisibility( View.GONE ); + metroView.setVisibility( View.VISIBLE ); + startMetroFetcher(); + } + }); + + + + + // findViewById(R.id.header).setOnClickListener( mapLauncher ); + + int distance = station.getCalcdist(); + if (distance != 0) { + NumberFormat format = NumberFormat.getNumberInstance(); + format.setMaximumFractionDigits(1); + format.setMinimumFractionDigits(1); - findViewById(R.id.header).setOnClickListener( mapLauncher ); + ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); + } else { + ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); + } + + ProviderFactory.purgeOldEntries(); //cleanup before fetching more data + + Log.e("Station", station.toCSV() ); - 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." ); + if (station.isMetro() == false) { + metroBtn.setVisibility( View.GONE ); + } + metro = ProviderFactory.getMetroProvider(); - provider = ProviderFactory.getDepartureProvider(); + if (station.isRegional() == false ) { + regionalBtn.setVisibility(View.GONE); + } + + if (station.isStrain() == false ) { + stogBtn.setVisibility(View.GONE); + } + + if (station.isRegional() == true && station.isStrain() == false ) { + if ( station.isMetro() == false ) + regionalBtn.setVisibility(View.GONE); + trainType = "REGIONAL"; + } + + if (station.isRegional() == false && station.isStrain() == true) { + if (station.isMetro() == false) + stogBtn.setVisibility(View.GONE); + + stogBtn.setBackgroundResource(R.drawable.custom_button_hilight); + trainType = "STOG"; + + } + - fetcher = new DepartureFetcher(); - if (savedInstanceState == null) { - showDialog(DLG_PROGRESS); - fetcher.execute(stationId); + if (station.isRegional() == false && station.isStrain() == false) { + getListView().setVisibility( View.GONE ); + metroView.setVisibility( View.VISIBLE ); + + departureBtn.setVisibility( View.GONE ); + arrivalBtn.setVisibility(View.GONE); + metroBtn.setVisibility( View.GONE ); + + + + if (savedInstanceState == null) { + startMetroFetcher(); + } else { + metroBean = (MetroBean) savedInstanceState.getSerializable("metro"); + loadMetroData(); + } + } 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"); + + if ( (departures != null) && (departures.entries != null) ) { + 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); + outState.putSerializable("metro", metroBean); } + + @Override + protected void onDestroy() { + super.onDestroy(); + + if (fetcher != null) { + fetcher.cancel(true); + } + + if (metroFetcher != null) { + metroFetcher.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 +314,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 +326,83 @@ 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, getString(R.string.departurelist_showonmap) ); + item.setIcon(android.R.drawable.ic_menu_mapmode); + + item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) ); + item.setIcon(android.R.drawable.ic_menu_info_details); + + boolean notifEnabled = hasNotifications(); + item.setEnabled(notifEnabled); + + 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; + switch(item.getItemId()) { + case MENU_MAP: + try { + Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16"); + startActivity( new Intent(Intent.ACTION_VIEW, uri)); + } catch (ActivityNotFoundException anfe) { + Toast.makeText(this, "Could not launch google maps", Toast.LENGTH_LONG).show(); + } + 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; + case MENU_METROMAP: + Intent metroMap = new Intent(this,dk.thoerup.traininfo.MetroMap.class); + startActivity(metroMap); + res = true; + break; + default: + res = super.onOptionsItemSelected(item); + } + return res; + } + + void startDepartureFetcher() { + showDialog(DLG_PROGRESS); + fetcher = new DepartureFetcher(); + fetcher.execute(station.getId()); + } + + void startMetroFetcher() { + showDialog(DLG_PROGRESS); + metroFetcher = new MetroFetcher(); + metroFetcher.execute(station.getId()); + } + class DialogDismisser implements View.OnClickListener { Dialog dlg; @@ -169,38 +414,206 @@ 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 { - String addr; @Override protected void onPostExecute(Void result) { super.onPostExecute(result); - adapter.setDepartures(departures); - pgDialog.dismiss(); - if (departures.size() == 0) - MessageBox.showMessage(DepartureList.this, "No departures found"); + pgDialog.dismiss(); + + 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); + notifIcon.setVisibility(View.VISIBLE); + notifIcon.setClickable(true); + notifIcon.setOnClickListener( new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent i = new Intent(DepartureList.this, dk.thoerup.traininfo.NotificationList.class); + i.putExtra(NotificationList.EXTRA_NOTIFICATIONS, departures.notifications); + startActivity(i); + } + }); + } + + if (departures.entries.size() == 0) { + 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); + + 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) { + dialog.dismiss(); + startDepartureFetcher(); + + } + }); + } + builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + DepartureList.this.finish(); + } + }); + + 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 + } + } } @Override protected Void doInBackground(Integer... params) { - provider.lookupDepartures(params[0]); - departures = provider.getDepartures(); + departures = provider.lookupDepartures(params[0], DepartureList.this.arrival, trainType); + return null; + } + + } + + public void loadMetroData() { + ((TextView) findViewById(R.id.operations)).setText( metroBean.operationInfo ); + ((TextView) findViewById(R.id.plan)).setText( metroBean.plan ); + + + TableLayout table = (TableLayout) findViewById(R.id.metrotable); + table.removeAllViews(); + + TableRow head = new TableRow(this); + + TextView h1 = new TextView(this); + h1.setText("Metro"); + h1.setTextSize(16); + h1.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); + + + TableRow.LayoutParams params = new TableRow.LayoutParams(); + params.span = 2; + head.addView(h1, params); + + + + TextView h2 = new TextView(this); + h2.setTextSize(16); + h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); + h2.setText("Om minutter"); + + params = new TableRow.LayoutParams(); + params.weight = 2; + head.addView(h2,params); + + + + table.addView(head); + + for (MetroEntry entry : metroBean.entries) { + TableRow row = new TableRow(this); + + Log.e("Test", "" + entry.destination); + + TextView v1 = new TextView(this); + v1.setTextSize(16); + v1.setText( entry.metro ); + row.addView(v1); + + TextView v2 = new TextView(this); + v2.setTextSize(16); + v2.setText( entry.destination ); + row.addView(v2); + + TextView v3 = new TextView(this); + v3.setTextSize(16); + v3.setText( entry.minutes ); + row.addView(v3); + + table.addView(row); + + } + findViewById(R.id.rootView).requestLayout(); + } + + class MetroFetcher extends AsyncTask { + + @Override + protected void onPostExecute(Void result) { + super.onPostExecute(result); + + + + pgDialog.dismiss(); + + if (metroBean != null) { + loadMetroData(); + } else { // communication or parse error + commFailCounter++; + AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); + builder.setMessage("Error finding metro data"); + builder.setCancelable(true); + if (commFailCounter < 3) { + builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + startMetroFetcher(); + + } + }); + } + builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + DepartureList.this.finish(); //TODO: should we really close the activity ?? + } + }); + + 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 + } + } + } + + @Override + protected Void doInBackground(Integer... params) { + metroBean = metro.lookupMetroInfo(params[0]); return null; } } + }