--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2009/09/30 06:02:03 364 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2011/04/04 22:14:40 1261 @@ -1,9 +1,11 @@ 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; @@ -11,27 +13,44 @@ import android.app.ProgressDialog; 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 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.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; + 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; @@ -39,11 +58,16 @@ ProgressDialog pgDialog; DepartureFetcher fetcher; - int stationId; + 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); @@ -53,38 +77,193 @@ setListAdapter(adapter); Intent launchedBy = getIntent(); + + station = (StationEntry) launchedBy.getSerializableExtra("stationbean"); + + ((TextView) findViewById(R.id.stationName)).setText( station.getName() ); + + + ((TextView) findViewById(R.id.stationAddr)).setText( station.getAddress() ); + + 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); - latitude = launchedBy.getDoubleExtra("latitude", 0.0); - longitude = launchedBy.getDoubleExtra("longitude", 0.0); + final View metroView = findViewById(R.id.metroonly); - String name = launchedBy.getStringExtra("name"); - ((TextView) findViewById(R.id.stationName)).setText( name ); + 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); - String address = launchedBy.getStringExtra("address"); - ((TextView) findViewById(R.id.stationAddr)).setText( address ); + + 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(); + } + }); + + + - stationId = launchedBy.getIntExtra("stationid", -1); + // findViewById(R.id.header).setOnClickListener( mapLauncher ); - findViewById(R.id.header).setOnClickListener( mapLauncher ); + int distance = station.getCalcdist(); + if (distance != 0) { + NumberFormat format = NumberFormat.getNumberInstance(); + format.setMaximumFractionDigits(1); + format.setMinimumFractionDigits(1); - 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." ); + ((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() ); - provider = ProviderFactory.getDepartureProvider(); + if (station.isMetro() == false) { + metroBtn.setVisibility( View.GONE ); + } + + metro = ProviderFactory.getMetroProvider(); + + if (station.isRegional() == false ) { + regionalBtn.setVisibility(View.GONE); + } + + if (station.isStrain() == false ) { + stogBtn.setVisibility(View.GONE); + } - if (savedInstanceState == null) { - startDepartureFetcher(); + 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"; + + } + + + 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"); + 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) { @@ -93,16 +272,32 @@ 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; - DepartureBean dep = departures.get(selectedItemId); + DepartureEntry dep = departures.entries.get(selectedItemId); Intent intent = new Intent(this, TimetableList.class); intent.putExtra("departure", dep); @@ -119,6 +314,8 @@ switch (id) { case DLG_PROGRESS: pgDialog = (ProgressDialog) dialog; + int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; + pgDialog.setMessage( getString(messageId) ); break; } } @@ -127,19 +324,77 @@ 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; 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: + Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude() + "?z=16"); + 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; + 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(stationId); + fetcher.execute(station.getId()); + } + + void startMetroFetcher() { + showDialog(DLG_PROGRESS); + metroFetcher = new MetroFetcher(); + metroFetcher.execute(station.getId()); } class DialogDismisser implements View.OnClickListener { @@ -153,25 +408,21 @@ 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); @@ -179,37 +430,176 @@ pgDialog.dismiss(); - if (success) { - adapter.setDepartures(departures); - if (departures.size() == 0) { - MessageBox.showMessage(DepartureList.this, "No departures found"); + if (departures != 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) { + MessageBox.showMessage(DepartureList.this, "No departures found", true); } } else { // communication or parse error + commFailCounter++; AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); builder.setMessage("Error finding departures"); builder.setCancelable(true); - builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { + 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(); - startDepartureFetcher(); - - } + DepartureList.this.finish(); + } }); - builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + + 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) { + 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(); } - }); - builder.show(); + }); + + 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]); + protected Void doInBackground(Integer... params) { + metroBean = metro.lookupMetroInfo(params[0]); return null; } } + }