--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/08/03 06:12:10 1007 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/09/14 16:30:16 1055 @@ -1,20 +1,19 @@ 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; @@ -25,9 +24,14 @@ 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 { @@ -41,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) { @@ -72,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(); } @@ -87,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(); + } + }); + @@ -107,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(); @@ -149,6 +200,7 @@ outState.putInt("selectedItemId", selectedItemId); outState.putSerializable("departures", departures); + outState.putSerializable("metro", metroBean); } @@ -160,6 +212,10 @@ if (fetcher != null) { fetcher.cancel(true); } + + if (metroFetcher != null) { + metroFetcher.cancel(true); + } } @Override @@ -253,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; @@ -279,7 +341,6 @@ class DepartureFetcher extends AsyncTask { - @Override protected void onPostExecute(Void result) { super.onPostExecute(result); @@ -288,6 +349,7 @@ pgDialog.dismiss(); 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); @@ -301,16 +363,122 @@ 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.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); + h2.setText("Om minutter"); + + 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.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(); @@ -327,10 +495,11 @@ } @Override - protected Void doInBackground(Integer... params) { - departures = provider.lookupDepartures(params[0], DepartureList.this.arrival); + protected Void doInBackground(Integer... params) { + metroBean = metro.lookupMetroInfo(params[0]); return null; } } + }