--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/12/25 20:51:29 1207 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2011/07/10 07:45:38 1577 @@ -3,34 +3,25 @@ import static dk.thoerup.traininfo.R.string.app_name; import static dk.thoerup.traininfo.R.string.generic_cancel; import static dk.thoerup.traininfo.R.string.generic_retry; -import static dk.thoerup.traininfo.R.string.generic_search; import static dk.thoerup.traininfo.R.string.stationlist_accuracy; import static dk.thoerup.traininfo.R.string.stationlist_addfavorite; import static dk.thoerup.traininfo.R.string.stationlist_favorites; -import static dk.thoerup.traininfo.R.string.stationlist_fetcherror; -import static dk.thoerup.traininfo.R.string.stationlist_findbyname; -import static dk.thoerup.traininfo.R.string.stationlist_findingnearby; import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo; import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout; import static dk.thoerup.traininfo.R.string.stationlist_latitude; -import static dk.thoerup.traininfo.R.string.stationlist_loadfavorites; import static dk.thoerup.traininfo.R.string.stationlist_locationinfo; import static dk.thoerup.traininfo.R.string.stationlist_longitude; import static dk.thoerup.traininfo.R.string.stationlist_nearbystations; import static dk.thoerup.traininfo.R.string.stationlist_nofavorites; -import static dk.thoerup.traininfo.R.string.stationlist_nolocation; import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider; -import static dk.thoerup.traininfo.R.string.stationlist_nostations; import static dk.thoerup.traininfo.R.string.stationlist_obtainedby; import static dk.thoerup.traininfo.R.string.stationlist_removefavorite; +import static dk.thoerup.traininfo.R.string.stationlist_satellitecount; import static dk.thoerup.traininfo.R.string.stationlist_search; import static dk.thoerup.traininfo.R.string.stationlist_stationadded; import static dk.thoerup.traininfo.R.string.stationlist_stationmap; import static dk.thoerup.traininfo.R.string.stationlist_stationremoved; -import static dk.thoerup.traininfo.R.string.stationlist_stationsearch; -import static dk.thoerup.traininfo.R.string.stationlist_twocharmin; import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; -import static dk.thoerup.traininfo.R.string.stationlist_satellitecount; import java.util.ArrayList; @@ -44,23 +35,25 @@ import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.location.Location; -import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.Log; import android.view.ContextMenu; -import android.view.LayoutInflater; +import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnCreateContextMenuListener; import android.widget.AdapterView; -import android.widget.EditText; import android.widget.ListView; +import android.widget.TextView; import android.widget.Toast; import dk.thoerup.android.traininfo.common.StationBean; -import dk.thoerup.android.traininfo.common.StationBean.StationEntry; +import dk.thoerup.android.traininfo.common.StationEntry; +import dk.thoerup.traininfo.WelcomeScreen.ListType; import dk.thoerup.traininfo.provider.ProviderFactory; import dk.thoerup.traininfo.provider.StationProvider; import dk.thoerup.traininfo.stationmap.GeoPair; @@ -72,11 +65,10 @@ - public static final int OPTIONS_MAP = 2003; - public static final int OPTIONS_GPSINFO = 2004; + public static final int OPTIONS_MAP = 103; + public static final int OPTIONS_GPSINFO = 104; 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 @@ -93,7 +85,6 @@ String dialogMessage = ""; ProgressDialog dialog; LocationLookup locationLookup = null; - FindStationsTask findStationsTask; StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); //GeoPair location = new GeoPair(); @@ -121,8 +112,9 @@ super.onCreate(savedInstanceState); setContentView(R.layout.stationlist); + listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); - adapter = new StationListAdapter(this); + adapter = new StationListAdapter(this, (listType == ListType.ListNearest) ); setListAdapter(adapter); ListView lv = getListView(); @@ -137,22 +129,24 @@ favorites.fromString(favoriteString); } - listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); setTitle(); isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); ProviderFactory.purgeOldEntries(); //cleanup before fetching more data + if (listType == ListType.ListSearch) { + enableNamesearchField(); + } + if (savedInstanceState == null) { - - + switch (listType) { case ListNearest: startNearestLookup(); break; case ListSearch: - showDialog(DLG_STATIONNAME); + // do nothing here break; case ListFavorites: startFavoriteLookup(); @@ -162,6 +156,8 @@ } } else { + ((TextView)findViewById(R.id.stationsearch)).setText( savedInstanceState.getString( "search") ); + stations = (StationBean) savedInstanceState.getSerializable("stations"); adapter.setStations(stations); } @@ -177,13 +173,13 @@ protected void onDestroy() { super.onDestroy(); + stationsFetched.removeMessages(0); + if (locationLookup != null) { locationLookup.stopSearch(); } - if (findStationsTask != null) { - findStationsTask.cancel(true); - } + } @@ -207,6 +203,35 @@ } + + public void enableNamesearchField() { + final TextView stationSearch = (TextView) findViewById(R.id.stationsearch); + stationSearch.setVisibility( View.VISIBLE ); + stationSearch.addTextChangedListener( new TextWatcher() { + + @Override + public void afterTextChanged(Editable s) { + if (s.length() > 0) { + startNameLookup( s.toString() ); + } else { + stations = new StationBean(); + getListView().invalidateViews(); + adapter.setStations( stations ); + } + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + }); + + + } @Override @@ -215,6 +240,7 @@ if (dialog != null && dialog.isShowing()) dialog.dismiss(); outState.putSerializable("stations", (StationBean) stations); + outState.putString("search", ((TextView)findViewById(R.id.stationsearch)).getText().toString() ); } @@ -228,19 +254,29 @@ item.setIcon(android.R.drawable.ic_menu_mapmode); item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); - item.setIcon(android.R.drawable.ic_menu_mapmode); + item.setIcon(android.R.drawable.ic_menu_info_details); + boolean hasLoc = (locationLookup.getLocation() != null); + item.setEnabled(hasLoc); return true; } + + + @Override public boolean onOptionsItemSelected(MenuItem item) { boolean retval = true; - //TODO: Cleanup + switch (item.getItemId()) { case OPTIONS_MAP: + if ( stations == null || stations.entries == null || stations.entries.size() == 0 ) { + Toast.makeText(this, "No stations to show on map", Toast.LENGTH_SHORT).show(); //TODO: Translate + return true; + } + Intent intent = new Intent(this,StationMapView.class); ArrayList stationPoints = new ArrayList(); @@ -256,14 +292,12 @@ Location loc = locationLookup.getLocation(); StringBuffer message = new StringBuffer(); message.append( getString(stationlist_locationinfo) ).append(":\n"); - if (loc != null) { - message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); - message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); - message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); - message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); - } else { - message.append( getString(stationlist_nolocation) ); - } + + message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); + message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); + message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); + message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); + MessageBox.showMessage(this, message.toString(), false); break; @@ -308,35 +342,6 @@ dlg.setMessage( getString(stationlist_waitforlocation) ); dlg.setCancelable(false); return dlg; - case DLG_STATIONNAME: - LayoutInflater factory = LayoutInflater.from(this); - final View rootView = factory.inflate(R.layout.textinput, null); - - - AlertDialog.Builder builder = new AlertDialog.Builder(this); - - builder.setTitle( getString(stationlist_stationsearch) ); - builder.setView(rootView); - builder.setCancelable(true); - builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - EditText et = (EditText) rootView.findViewById(R.id.EditText); - dialog.dismiss(); - String search = et.getText().toString().trim(); - if (search.length() >= 2) { - startNameLookup(search); - } else { - showMessageAndClose( getString(stationlist_twocharmin) ); - } - } - }); - builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - StationList.this.finish(); // Close this Activity - } - }); - return builder.create(); default: return super.onCreateDialog(id); @@ -389,24 +394,21 @@ } void startNameLookup(String name) { - dialogMessage = getString( stationlist_findbyname ); - showDialog(DLG_PROGRESS); - - findStationsTask = new FindStationsTask(); - findStationsTask.searchByName(name); - findStationsTask.execute(); + stations = stationProvider.lookupStationsByName(name); + getListView().invalidateViews(); + adapter.setStations( stations ); } public void startFavoriteLookup() { if (favorites.size() > 0) { - dialogMessage = getString( stationlist_loadfavorites ); - showDialog(DLG_PROGRESS); - - findStationsTask = new FindStationsTask(); - findStationsTask.searchByIds( favorites.toString() ); - findStationsTask.execute(); + + stations = stationProvider.lookupStationsByIds( favorites.toString() ); + getListView().invalidateViews(); + adapter.setStations( stations ); + + } else { showMessageAndClose( getString( stationlist_nofavorites ) ); } @@ -414,14 +416,11 @@ - void startLocatorTask() - { - dialogMessage = getString( stationlist_findingnearby ); - showDialog(DLG_PROGRESS); - - findStationsTask = new FindStationsTask(); - findStationsTask.searchByLocation( locationLookup.getLocation() ); - findStationsTask.execute(); + void startNearestLookupPhase2() + { + stations = stationProvider.lookupStationsByLocation( locationLookup.getLocation() ); + getListView().invalidateViews(); + adapter.setStations( stations ); } @@ -439,7 +438,7 @@ case GOTLOCATION: dismissDialog(DLG_PROGRESS); - startLocatorTask(); + startNearestLookupPhase2(); return; @@ -448,6 +447,11 @@ MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); //StationList.this.finish(); return; + case IDLE: + Log.e("TrainInfo", "How did this happen ???"); + dismissDialog(DLG_PROGRESS); // how did we get here ?? + return; + } @@ -455,10 +459,11 @@ if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { dismissDialog(DLG_PROGRESS); + locationLookup.stopSearch(); if (locationLookup.hasLocation()) { - startLocatorTask(); + startNearestLookupPhase2(); } else { AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); builder.setMessage( getString( stationlist_gpstimeout) ); @@ -494,126 +499,6 @@ - class FindStationsTask extends AsyncTask { - - LookupMethod method = LookupMethod.MethodNone; - String name; - Location loc; - String ids; - - public void searchByName(String n) { - - method = LookupMethod.ByName; - name = n; - } - - public void searchByLocation(Location l) { - method = LookupMethod.ByLocation; - loc = l; - } - - public void searchByIds(String id) { - - method = LookupMethod.ByList; - ids = id; - } - - @Override - protected void onPreExecute() { - - if (method.equals(LookupMethod.MethodNone)) - throw new RuntimeException("Method not set"); - super.onPreExecute(); - } - - @Override - protected Void doInBackground(Void... params) { - - switch (method) { - case ByLocation: - stations = stationProvider.lookupStationsByLocation(loc); - break; - case ByName: - stations = stationProvider.lookupStationsByName(name); - break; - case ByList: - stations = stationProvider.lookupStationsByIds(ids); - break; - default: - stations = null; // not possible - } - - - return null; - } - - @Override - protected void onPostExecute(Void result) { - super.onPostExecute(result); - dialog.dismiss(); - - - if (stations != null) { - if (stations.entries.size() == 0) { - showMessageAndClose(getString(stationlist_nostations)); - } - - StationList.this.getListView().invalidateViews(); - adapter.setStations( stations ); - - - } else { //communication or parse errors - AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); - 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(); - - 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() { - startNameLookup( 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(); - } - }); - - builder.show(); - } - } - } - - class FavoritesMenu implements OnCreateContextMenuListener { private final static int FAVORITES_ADD = 9001; private final static int FAVORITES_REMOVE = 9002;