--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/03/17 15:27:11 630 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/09/08 06:25:13 1028 @@ -4,6 +4,7 @@ import java.util.List; +import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ListActivity; @@ -18,7 +19,6 @@ import android.os.Handler; import android.os.Message; -import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; @@ -47,13 +47,14 @@ public static final int OPTIONS_MAP = 2003; public static final int OPTIONS_GPSINFO = 2004; - - - public static final int DLG_PROGRESS = 3001; public static final int DLG_STATIONNAME = 3002; + + public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds + + static enum LookupMethod { ByLocation, ByName, @@ -69,7 +70,8 @@ StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); GeoPair location = new GeoPair(); - + + boolean isLaunchedforShortcut; boolean isRunning = false; List stations = new ArrayList(); @@ -111,6 +113,10 @@ listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); setTitle(); + isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); + + ProviderFactory.purgeOldEntries(); //cleanup before fetching more data + if (savedInstanceState == null) { @@ -118,8 +124,8 @@ case ListNearest: startLookup(); break; - case ListSearch: - this.showDialog(DLG_STATIONNAME); + case ListSearch: + showDialog(DLG_STATIONNAME); break; case ListFavorites: startFavoriteLookup(); @@ -135,6 +141,27 @@ } } + + + + + + + @Override + protected void onDestroy() { + super.onDestroy(); + + isRunning = false; + + if (locationLookup != null) { + locationLookup.stopSearch(); + } + if (findStationsTask != null) { + findStationsTask.cancel(true); + } + } + + protected void setTitle() { String dialogTitle = getResources().getString(app_name); switch (listType) { @@ -152,10 +179,10 @@ } setTitle(dialogTitle); + } - - + @Override public void onSaveInstanceState(Bundle outState) @@ -214,7 +241,7 @@ message.append( getString(stationlist_nolocation) ); } - MessageBox.showMessage(this, message.toString()); + MessageBox.showMessage(this, message.toString(), false); break; default: retval = super.onOptionsItemSelected(item); @@ -271,8 +298,9 @@ public void onClick(DialogInterface dialog, int which) { EditText et = (EditText) rootView.findViewById(R.id.EditText); dialog.dismiss(); - if (et.getText().toString().length() >= 2) { - startNameSearch(et.getText().toString()); + String search = et.getText().toString().trim(); + if (search.length() >= 2) { + startNameSearch(search); } else { showMessageAndClose( getString(stationlist_twocharmin) ); } @@ -313,9 +341,16 @@ StationBean station = stations.get(position); - Intent intent = new Intent(this, DepartureList.class); - intent.putExtra("stationbean", station); - startActivity(intent); + if (isLaunchedforShortcut == true) { + Intent i = new Intent(); + i.putExtra("station", station); + setResult(Activity.RESULT_OK, i); + finish(); + } else { + Intent intent = new Intent(this, DepartureList.class); + intent.putExtra("stationbean", station); + startActivity(intent); + } } ///////////////////////////////////////////////////////////// @@ -327,7 +362,7 @@ showDialog(DLG_PROGRESS); locationLookup.locateStations(); - stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); + stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); } void startNameSearch(String name) { @@ -385,7 +420,8 @@ case NOPROVIDER: dismissDialog(DLG_PROGRESS); - MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); + MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); + //StationList.this.finish(); break; case LOCATIONFIXTIMEOUT: if (isRunning) { @@ -409,8 +445,8 @@ public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } - }); - builder.show(); + }); + builder.show(); } } @@ -424,7 +460,6 @@ class FindStationsTask extends AsyncTask { LookupMethod method = LookupMethod.MethodNone; - boolean success; String name; Location loc; String ids; @@ -459,16 +494,16 @@ switch (method) { case ByLocation: - success = stationProvider.lookupStations(loc); + stations = stationProvider.lookupStations(loc); break; case ByName: - success = stationProvider.lookupStationsByName(name); + stations = stationProvider.lookupStationsByName(name); break; case ByList: - success = stationProvider.lookupStationsByIds(ids); + stations = stationProvider.lookupStationsByIds(ids); break; default: - success = false; // not possible + stations = null; // not possible } @@ -481,39 +516,62 @@ dialog.dismiss(); - if (success) { - if (stationProvider.getStations().size() == 0) { + if (stations != null) { + if (stations.size() == 0) { showMessageAndClose(getString(stationlist_nostations)); } - stations = stationProvider.getStations(); + + StationList.this.getListView().invalidateViews(); adapter.setStations( stations ); + } else { //communication or parse errors AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); - builder.setMessage(getString(stationlist_nearbyerror)); + builder.setMessage(getString(stationlist_fetcherror)); builder.setCancelable(true); builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); - stationsFetched.post( new Runnable() { - @Override - public void run() { - startLocatorTask(); - } - }); + Runnable runner = null; + switch (method) { + case ByLocation: + runner = new Runnable() { + @Override + public void run() { + startLocatorTask(); + } + }; + break; + case ByName: + runner = new Runnable() { + @Override + public void run() { + startNameSearch( FindStationsTask.this.name ); + } + }; + break; + case ByList: + runner = new Runnable() { + @Override + public void run() { + startFavoriteLookup(); + } + }; + break; + } + + stationsFetched.post( runner ); } }); builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); + StationList.this.finish(); } - }); - try { - builder.show(); - } catch (android.view.WindowManager.BadTokenException e) { - Log.i("StationList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running - } + }); + + builder.show(); } } }