--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/06/25 23:59:58 906 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2011/03/31 18:20:42 1250 @@ -1,14 +1,11 @@ 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 java.util.ArrayList; -import java.util.List; import android.app.AlertDialog; import android.app.Dialog; @@ -16,27 +13,44 @@ 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.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; @@ -44,12 +58,16 @@ ProgressDialog pgDialog; DepartureFetcher fetcher; + MetroFetcher metroFetcher; - StationBean station; + StationEntry station; - boolean arrival = false; + String trainType = "REGIONAL"; - @SuppressWarnings("unchecked") + boolean arrival = false; + + int commFailCounter = 0; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -60,7 +78,7 @@ Intent launchedBy = getIntent(); - station = (StationBean) launchedBy.getSerializableExtra("stationbean"); + station = (StationEntry) launchedBy.getSerializableExtra("stationbean"); ((TextView) findViewById(R.id.stationName)).setText( station.getName() ); @@ -69,12 +87,21 @@ 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(); } @@ -84,17 +111,73 @@ 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 ); + // findViewById(R.id.header).setOnClickListener( mapLauncher ); - int distance = station.getDistance(); + int distance = station.getCalcdist(); if (distance != 0) { NumberFormat format = NumberFormat.getNumberInstance(); format.setMaximumFractionDigits(1); @@ -104,13 +187,57 @@ } 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 ); + } - if (station.isRegional() == false && station.isSTrain() == false) { + metro = ProviderFactory.getMetroProvider(); + + 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"; + + } + + + 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(); @@ -118,13 +245,25 @@ if (savedInstanceState == null) { startDepartureFetcher(); } else { - departures = (List) savedInstanceState.getSerializable("departures"); - adapter.setDepartures(departures); - selectedItemId = savedInstanceState.getInt("selectedItemId"); + 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) { @@ -133,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); @@ -177,6 +332,58 @@ 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); @@ -184,6 +391,12 @@ fetcher.execute(station.getId()); } + void startMetroFetcher() { + showDialog(DLG_PROGRESS); + metroFetcher = new MetroFetcher(); + metroFetcher.execute(station.getId()); + } + class DialogDismisser implements View.OnClickListener { Dialog dlg; @@ -198,20 +411,18 @@ } } - View.OnClickListener mapLauncher = new View.OnClickListener() { + /*View.OnClickListener mapLauncher = new View.OnClickListener() { @Override 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; - @Override protected void onPostExecute(Void result) { super.onPostExecute(result); @@ -219,22 +430,144 @@ pgDialog.dismiss(); - if (success) { - adapter.setDepartures(departures); - if (departures.size() == 0) { + 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); + + + 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 + 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, 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(); @@ -251,11 +584,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; } } + }