--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/07/11 14:58:13 982 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/09/14 16:00:57 1053 @@ -1,35 +1,37 @@ 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.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 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.graphics.Typeface; 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.TableLayout; +import android.widget.TableRow; import android.widget.TextView; import dk.thoerup.traininfo.provider.DepartureProvider; +import dk.thoerup.traininfo.provider.MetroProvider; import dk.thoerup.traininfo.provider.ProviderFactory; +import dk.thoerup.traininfo.provider.MetroProvider.MetroBean; +import dk.thoerup.traininfo.provider.MetroProvider.MetroEntry; import dk.thoerup.traininfo.util.MessageBox; public class DepartureList extends ListActivity { @@ -43,17 +45,22 @@ DepartureProvider provider; DepartureBean departures; + MetroBean metroBean; + MetroProvider metro; + int selectedItemId; //DepartureBean currentDeparture; ProgressDialog pgDialog; DepartureFetcher fetcher; + MetroFetcher metroFetcher; StationBean station; boolean arrival = false; - + + int commFailCounter = 0; @Override protected void onCreate(Bundle savedInstanceState) { @@ -74,12 +81,19 @@ 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 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(); } @@ -89,11 +103,28 @@ 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(); } }); + metroBtn.setOnClickListener( new OnClickListener() { + @Override + public void onClick(View v) { + arrivalBtn.setBackgroundResource(R.drawable.custom_button); + departureBtn.setBackgroundResource(R.drawable.custom_button); + metroBtn.setBackgroundResource(R.drawable.custom_button_hilight); + + getListView().setVisibility( View.GONE ); + metroView.setVisibility( View.VISIBLE ); + startMetroFetcher(); + } + }); + @@ -109,13 +140,31 @@ } else { ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); } + + ProviderFactory.purgeOldEntries(); //cleanup before fetching more data + Log.e("Station", station.toCSV() ); + + if (station.isMetro() == false) { + metroBtn.setVisibility( View.GONE ); + } + metro = ProviderFactory.getMetroProvider(); + if (station.isRegional() == false && station.isSTrain() == false) { getListView().setVisibility( View.GONE ); - findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); + 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 { provider = ProviderFactory.getDepartureProvider(); @@ -124,7 +173,10 @@ startDepartureFetcher(); } else { departures = (DepartureBean) savedInstanceState.getSerializable("departures"); - adapter.setDepartures(departures.entries); + + if ( (departures != null) && (departures.entries != null) ) { + adapter.setDepartures(departures.entries); + } selectedItemId = savedInstanceState.getInt("selectedItemId"); if ( hasNotifications() ) { @@ -148,6 +200,7 @@ outState.putInt("selectedItemId", selectedItemId); outState.putSerializable("departures", departures); + outState.putSerializable("metro", metroBean); } @@ -159,6 +212,10 @@ if (fetcher != null) { fetcher.cancel(true); } + + if (metroFetcher != null) { + metroFetcher.cancel(true); + } } @Override @@ -210,10 +267,11 @@ @Override public boolean onCreateOptionsMenu(Menu menu) { MenuItem item; - item = menu.add(0, MENU_MAP, 0, "Show on map"); + + 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, "Notifications"); + item = menu.add(0, MENU_NOTIFICATIONS, 0, getString(R.string.departurelist_notifications) ); item.setIcon(android.R.drawable.ic_menu_info_details); @@ -251,6 +309,12 @@ fetcher.execute(station.getId()); } + void startMetroFetcher() { + showDialog(DLG_PROGRESS); + metroFetcher = new MetroFetcher(); + metroFetcher.execute(station.getId()); + } + class DialogDismisser implements View.OnClickListener { Dialog dlg; @@ -277,8 +341,6 @@ class DepartureFetcher extends AsyncTask { - boolean success; - @Override protected void onPostExecute(Void result) { super.onPostExecute(result); @@ -286,7 +348,8 @@ pgDialog.dismiss(); - if (success) { + 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); @@ -300,16 +363,119 @@ 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(getString(generic_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(); + } }); + + 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); + 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.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); + + + TableRow.LayoutParams params = new TableRow.LayoutParams(); + params.span = 2; + head.addView(h1, params); + + TextView h2 = new TextView(this); + h2.setText("Om minutte"); + h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); + head.addView(h2); + + + + table.addView(head); + + for (MetroEntry entry : metroBean.entries) { + TableRow row = new TableRow(this); + + Log.e("Test", "" + entry.destination); + + TextView v1 = new TextView(this); + v1.setText( entry.metro ); + row.addView(v1); + + TextView v2 = new TextView(this); + v2.setText( entry.destination ); + row.addView(v2); + + TextView v3 = new TextView(this); + 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(); @@ -326,11 +492,11 @@ } @Override - protected Void doInBackground(Integer... params) { - success = provider.lookupDepartures(params[0], DepartureList.this.arrival); - departures = provider.getDepartures(params[0], DepartureList.this.arrival); + protected Void doInBackground(Integer... params) { + metroBean = metro.lookupMetroInfo(params[0]); return null; } } + }