--- android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2010/09/14 16:00:57 1053 +++ android/TrainInfo/src/dk/thoerup/traininfo/DepartureList.java 2011/05/03 12:35:34 1432 @@ -27,11 +27,14 @@ 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.StationEntry; 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 { @@ -39,6 +42,7 @@ 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; @@ -56,7 +60,9 @@ DepartureFetcher fetcher; MetroFetcher metroFetcher; - StationBean station; + StationEntry station; + + String trainType = "REGIONAL"; boolean arrival = false; @@ -72,7 +78,7 @@ Intent launchedBy = getIntent(); - station = (StationBean) launchedBy.getSerializableExtra("stationbean"); + station = (StationEntry) launchedBy.getSerializableExtra("stationbean"); ((TextView) findViewById(R.id.stationName)).setText( station.getName() ); @@ -82,6 +88,8 @@ 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); @@ -112,13 +120,52 @@ } }); + 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) { - arrivalBtn.setBackgroundResource(R.drawable.custom_button); - departureBtn.setBackgroundResource(R.drawable.custom_button); + 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(); @@ -130,7 +177,7 @@ // findViewById(R.id.header).setOnClickListener( mapLauncher ); - int distance = station.getDistance(); + int distance = station.getCalcdist(); if (distance != 0) { NumberFormat format = NumberFormat.getNumberInstance(); format.setMaximumFractionDigits(1); @@ -151,13 +198,39 @@ metro = ProviderFactory.getMetroProvider(); - if (station.isRegional() == false && station.isSTrain() == false) { + 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 ); metroView.setVisibility( View.VISIBLE ); departureBtn.setVisibility( View.GONE ); arrivalBtn.setVisibility(View.GONE); - metroBtn.setVisibility( View.GONE ); + metroBtn.setVisibility( View.GONE ); + + if (savedInstanceState == null) { startMetroFetcher(); @@ -272,12 +345,16 @@ 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); - + 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; } @@ -287,8 +364,12 @@ boolean res; switch(item.getItemId()) { case MENU_MAP: - Uri uri = Uri.parse("geo:" + station.getLatitude() + "," + station.getLongitude()); - startActivity( new Intent(Intent.ACTION_VIEW, uri)); + 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: @@ -297,6 +378,11 @@ 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); } @@ -347,26 +433,45 @@ pgDialog.dismiss(); - - if (departures != null) { + + 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() ) { - findViewById(R.id.notifIcon).setVisibility(View.VISIBLE); - } + 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); + 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); - builder.setMessage("Error finding departures"); + 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) { @@ -383,7 +488,7 @@ } }); - try { + 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 @@ -393,7 +498,7 @@ @Override protected Void doInBackground(Integer... params) { - departures = provider.lookupDepartures(params[0], DepartureList.this.arrival); + departures = provider.lookupDepartures(params[0], DepartureList.this.arrival, trainType); return null; } @@ -411,6 +516,7 @@ TextView h1 = new TextView(this); h1.setText("Metro"); + h1.setTextSize(16); h1.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); @@ -418,10 +524,16 @@ params.span = 2; head.addView(h1, params); + + TextView h2 = new TextView(this); - h2.setText("Om minutte"); + h2.setTextSize(16); h2.setTypeface( Typeface.defaultFromStyle(Typeface.BOLD)); - head.addView(h2); + h2.setText("Om minutter"); + + params = new TableRow.LayoutParams(); + params.weight = 2; + head.addView(h2,params); @@ -433,14 +545,17 @@ Log.e("Test", "" + entry.destination); TextView v1 = new TextView(this); - v1.setText( entry.metro ); + 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); @@ -479,11 +594,11 @@ builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); - DepartureList.this.finish(); + DepartureList.this.finish(); //TODO: should we really close the activity ?? } }); - try { + 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