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.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; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import dk.thoerup.traininfo.provider.DepartureProvider; import dk.thoerup.traininfo.provider.ProviderFactory; import dk.thoerup.traininfo.util.MessageBox; public class DepartureList extends ListActivity { public static final int DLG_PROGRESS = 1; DepartureListAdapter adapter; DepartureProvider provider; List departures; int selectedItemId; //DepartureBean currentDeparture; ProgressDialog pgDialog; DepartureFetcher fetcher; StationBean station; boolean arrival = false; @SuppressWarnings("unchecked") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.departurelist); adapter = new DepartureListAdapter(this); setListAdapter(adapter); Intent launchedBy = getIntent(); station = (StationBean) 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); departureBtn.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { arrivalBtn.setBackgroundResource(R.drawable.custom_button); departureBtn.setBackgroundResource(R.drawable.custom_button_hilight); 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); arrival = true; startDepartureFetcher(); } }); findViewById(R.id.header).setOnClickListener( mapLauncher ); int distance = station.getDistance(); if (distance != 0) { NumberFormat format = NumberFormat.getNumberInstance(); format.setMaximumFractionDigits(1); format.setMinimumFractionDigits(1); ((TextView) findViewById(R.id.stationDistance)).setText( format.format((double)distance/1000.0) + " km." ); } else { ((TextView) findViewById(R.id.stationDistance)).setVisibility(View.GONE); } if (station.isRegional() == false && station.isSTrain() == false) { getListView().setVisibility( View.GONE ); findViewById(R.id.metroonly).setVisibility( View.VISIBLE ); departureBtn.setVisibility( View.GONE ); arrivalBtn.setVisibility(View.GONE); } else { provider = ProviderFactory.getDepartureProvider(); if (savedInstanceState == null) { startDepartureFetcher(); } else { departures = (List) savedInstanceState.getSerializable("departures"); adapter.setDepartures(departures); selectedItemId = savedInstanceState.getInt("selectedItemId"); } } } @Override public void onSaveInstanceState(Bundle outState) { if (pgDialog != null && pgDialog.isShowing()) dismissDialog(DLG_PROGRESS); outState.putInt("selectedItemId", selectedItemId); outState.putSerializable("departures", (ArrayList) departures); } @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); Intent intent = new Intent(this, TimetableList.class); intent.putExtra("departure", dep); startActivity(intent); } @Override protected void onPrepareDialog(int id, Dialog dialog) { super.onPrepareDialog(id, dialog); switch (id) { case DLG_PROGRESS: pgDialog = (ProgressDialog) dialog; int messageId = arrival == false ? departurelist_fetchdepartures : departurelist_fetcharrivals; pgDialog.setMessage( getString(messageId) ); break; } } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DLG_PROGRESS: ProgressDialog dlg = new ProgressDialog(this); dlg.setCancelable(true); return dlg; default: return super.onCreateDialog(id); } } void startDepartureFetcher() { showDialog(DLG_PROGRESS); fetcher = new DepartureFetcher(); fetcher.execute(station.getId()); } class DialogDismisser implements View.OnClickListener { Dialog dlg; public DialogDismisser(Dialog d) { dlg = d; } @Override public void onClick(View v) { if (dlg.isShowing()) dlg.dismiss(); } } 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); pgDialog.dismiss(); if (success) { DepartureList.this.getListView().setVisibility(View.GONE); //Experimental, inspired by http://osdir.com/ml/Android-Developers/2010-04/msg01198.html adapter.setDepartures(departures); DepartureList.this.getListView().setVisibility(View.VISIBLE); if (departures.size() == 0) { MessageBox.showMessage(DepartureList.this, "No departures found", true); } } else { // communication or parse error AlertDialog.Builder builder = new AlertDialog.Builder(DepartureList.this); builder.setMessage("Error finding departures"); builder.setCancelable(true); 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(); 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) { success = provider.lookupDepartures(params[0], DepartureList.this.arrival); departures = provider.getDepartures(params[0], DepartureList.this.arrival); return null; } } }