/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/StationList.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/StationList.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 557 by torben, Wed Jan 27 10:04:28 2010 UTC revision 1078 by torben, Sat Sep 18 07:18:59 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
3    import static dk.thoerup.traininfo.R.string.app_name;
4    import static dk.thoerup.traininfo.R.string.generic_cancel;
5    import static dk.thoerup.traininfo.R.string.generic_retry;
6    import static dk.thoerup.traininfo.R.string.generic_search;
7    import static dk.thoerup.traininfo.R.string.stationlist_accuracy;
8    import static dk.thoerup.traininfo.R.string.stationlist_addfavorite;
9    import static dk.thoerup.traininfo.R.string.stationlist_favorites;
10    import static dk.thoerup.traininfo.R.string.stationlist_fetcherror;
11    import static dk.thoerup.traininfo.R.string.stationlist_findbyname;
12    import static dk.thoerup.traininfo.R.string.stationlist_findingnearby;
13    import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo;
14    import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout;
15    import static dk.thoerup.traininfo.R.string.stationlist_latitude;
16    import static dk.thoerup.traininfo.R.string.stationlist_loadfavorites;
17    import static dk.thoerup.traininfo.R.string.stationlist_locationinfo;
18    import static dk.thoerup.traininfo.R.string.stationlist_longitude;
19    import static dk.thoerup.traininfo.R.string.stationlist_nearbystations;
20    import static dk.thoerup.traininfo.R.string.stationlist_nofavorites;
21    import static dk.thoerup.traininfo.R.string.stationlist_nolocation;
22    import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider;
23    import static dk.thoerup.traininfo.R.string.stationlist_nostations;
24    import static dk.thoerup.traininfo.R.string.stationlist_obtainedby;
25    import static dk.thoerup.traininfo.R.string.stationlist_removefavorite;
26    import static dk.thoerup.traininfo.R.string.stationlist_search;
27    import static dk.thoerup.traininfo.R.string.stationlist_stationadded;
28    import static dk.thoerup.traininfo.R.string.stationlist_stationmap;
29    import static dk.thoerup.traininfo.R.string.stationlist_stationremoved;
30    import static dk.thoerup.traininfo.R.string.stationlist_stationsearch;
31    import static dk.thoerup.traininfo.R.string.stationlist_twocharmin;
32    import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation;
33    
34  import java.util.ArrayList;  import java.util.ArrayList;
 import java.util.List;  
35    
36    
37    import android.app.Activity;
38  import android.app.AlertDialog;  import android.app.AlertDialog;
39  import android.app.Dialog;  import android.app.Dialog;
40  import android.app.ListActivity;  import android.app.ListActivity;
# Line 17  import android.os.AsyncTask; Line 48  import android.os.AsyncTask;
48  import android.os.Bundle;  import android.os.Bundle;
49  import android.os.Handler;  import android.os.Handler;
50  import android.os.Message;  import android.os.Message;
   
51  import android.view.ContextMenu;  import android.view.ContextMenu;
52  import android.view.LayoutInflater;  import android.view.LayoutInflater;
53  import android.view.Menu;  import android.view.Menu;
# Line 29  import android.widget.AdapterView; Line 59  import android.widget.AdapterView;
59  import android.widget.EditText;  import android.widget.EditText;
60  import android.widget.ListView;  import android.widget.ListView;
61  import android.widget.Toast;  import android.widget.Toast;
62    import dk.thoerup.android.traininfo.common.StationBean;
63    import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
64  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
65  import dk.thoerup.traininfo.provider.StationProvider;  import dk.thoerup.traininfo.provider.StationProvider;
66  import dk.thoerup.traininfo.stationmap.GeoPair;  import dk.thoerup.traininfo.stationmap.GeoPair;
# Line 44  public class StationList extends ListAct Line 76  public class StationList extends ListAct
76                    
77          public static final int OPTIONS_MAP = 2003;          public static final int OPTIONS_MAP = 2003;
78          public static final int OPTIONS_GPSINFO = 2004;          public static final int OPTIONS_GPSINFO = 2004;
   
           
   
79                    
80          public static final int DLG_PROGRESS = 3001;          public static final int DLG_PROGRESS = 3001;
81          public static final int DLG_STATIONNAME = 3002;          public static final int DLG_STATIONNAME = 3002;
82                    
83            
84            public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds
85            
86            
87          static enum LookupMethod {          static enum LookupMethod {
88                  ByLocation,                  ByLocation,
89                  ByName,                  ByName,
# Line 66  public class StationList extends ListAct Line 99  public class StationList extends ListAct
99          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
100                    
101          GeoPair location = new GeoPair();          GeoPair location = new GeoPair();
102            
103            boolean isLaunchedforShortcut;
104          boolean isRunning = false;          boolean isRunning = false;
105          List<StationBean> stations = new ArrayList<StationBean>();          StationBean stations = new StationBean();
106                    
107          StationProvider stationProvider = ProviderFactory.getStationProvider();          StationProvider stationProvider = ProviderFactory.getStationProvider();
108                                    
# Line 83  public class StationList extends ListAct Line 117  public class StationList extends ListAct
117          ///////////////////////////////////////////////////////////////////////////////////////////          ///////////////////////////////////////////////////////////////////////////////////////////
118          //Activity call backs          //Activity call backs
119                    
120          @SuppressWarnings("unchecked")  
121          @Override          @Override
122          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
123                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
# Line 108  public class StationList extends ListAct Line 142  public class StationList extends ListAct
142                  listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");                  listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
143                  setTitle();                  setTitle();
144                                    
145                    isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);
146                    
147                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
148                    
149                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
150    
151                                                    
# Line 115  public class StationList extends ListAct Line 153  public class StationList extends ListAct
153                          case ListNearest:                          case ListNearest:
154                                  startLookup();                                  startLookup();
155                                  break;                                  break;
156                          case ListSearch:                          case ListSearch:                                
157                                  this.showDialog(DLG_STATIONNAME);                                  showDialog(DLG_STATIONNAME);
158                                  break;                                  break;
159                          case ListFavorites:                          case ListFavorites:
160                                  startFavoriteLookup();                                  startFavoriteLookup();
# Line 126  public class StationList extends ListAct Line 164  public class StationList extends ListAct
164                          }                          }
165                                                    
166                  } else {                  } else {
167                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");                          stations = (StationBean) savedInstanceState.getSerializable("stations");
168                          adapter.setStations(stations);                          adapter.setStations(stations);
169                          location = (GeoPair) savedInstanceState.getSerializable("location");                          location = (GeoPair) savedInstanceState.getSerializable("location");
170                  }                  }
171                                    
172          }          }
173            
174            
175    
176    
177    
178    
179            @Override
180            protected void onDestroy() {
181                    super.onDestroy();
182                    
183                    isRunning = false;
184                    
185                    if (locationLookup != null) {
186                            locationLookup.stopSearch();
187                    }
188                    if (findStationsTask != null) {
189                            findStationsTask.cancel(true);
190                    }              
191            }
192    
193            
194          protected void setTitle() {          protected void setTitle() {
195                  String dialogTitle = getResources().getString(R.string.app_name);                  String dialogTitle = getResources().getString(app_name);
196                  switch (listType) {                  switch (listType) {
197                  case ListNearest:                  case ListNearest:
198                          dialogTitle += " - Nearby stations";                          dialogTitle += " - " + getString(stationlist_nearbystations);
199                          break;                          break;
200                  case ListSearch:                  case ListSearch:
201                          dialogTitle += " - Search";                          dialogTitle += " - " + getString(stationlist_search);
202                          break;                          break;
203                  case ListFavorites:                  case ListFavorites:
204                          dialogTitle += " - Favorites";                          dialogTitle += " - " + getString(stationlist_favorites);
205                          break;                          break;
206                  default:                  default:
207                          dialogTitle = "";//not possible                                                          dialogTitle = "";//not possible                                
208                  }                  }
209                    
210                  setTitle(dialogTitle);                  setTitle(dialogTitle);
211                    
212          }          }
213                    
214                            
   
215    
216      @Override      @Override
217      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
218      {      {
219          if (dialog != null && dialog.isShowing())          if (dialog != null && dialog.isShowing())
220                  dialog.dismiss();                  dialog.dismiss();
221          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (StationBean) stations);
222          outState.putSerializable("location", location);          outState.putSerializable("location", location);
223                    
224      }      }
# Line 169  public class StationList extends ListAct Line 228  public class StationList extends ListAct
228          @Override          @Override
229          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
230                  MenuItem item;                  MenuItem item;
231                                                    
232                  item = menu.add(0, OPTIONS_MAP, 0, "Station map");                  item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
233                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
234                                    
235                  item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info");                  item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
236                  item.setIcon(android.R.drawable.ic_menu_mapmode);                                item.setIcon(android.R.drawable.ic_menu_mapmode);              
237                                    
238                  return true;                  return true;
# Line 190  public class StationList extends ListAct Line 249  public class StationList extends ListAct
249                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
250                                                    
251                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
252                          for (StationBean st : stations ) {                          for (StationEntry st : stations.entries ) {
253                                  stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );                                  stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
254                          }                          }
255                                                    
# Line 201  public class StationList extends ListAct Line 260  public class StationList extends ListAct
260                  case OPTIONS_GPSINFO:                  case OPTIONS_GPSINFO:
261                          Location loc = locationLookup.getLocation();                          Location loc = locationLookup.getLocation();
262                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
263                          message.append("Location info:\n");                          message.append( getString(stationlist_locationinfo) ).append(":\n");
264                          if (loc != null) {                          if (loc != null) {
265                                  message.append("-Obtained by: ").append( loc.getProvider() ).append("\n");                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
266                                  message.append("-Accuracy: ").append( (int)loc.getAccuracy()).append("m\n");                                  message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
267                                  message.append("-Latitude: ").append( loc.getLatitude()).append("\n");                                  message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
268                                  message.append("-Longitude: ").append( loc.getLongitude() ).append("\n");                                  message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
269                          } else {                          } else {
270                                  message.append(" - No location data!");                                  message.append( getString(stationlist_nolocation) );
271                          }                                                }                      
272                                                    
273                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString(), false);
274                          break;                          break;
275                  default:                  default:
276                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 251  public class StationList extends ListAct Line 310  public class StationList extends ListAct
310                  switch (id) {                  switch (id) {
311                  case DLG_PROGRESS:                  case DLG_PROGRESS:
312                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
313                          dlg.setMessage("Wait for location fix");                          dlg.setMessage( getString(stationlist_waitforlocation) );
314                          dlg.setCancelable(false);                          dlg.setCancelable(false);
315                          return dlg;                                              return dlg;                    
316                  case DLG_STATIONNAME:                  case DLG_STATIONNAME:
# Line 261  public class StationList extends ListAct Line 320  public class StationList extends ListAct
320                                                    
321                          AlertDialog.Builder builder = new AlertDialog.Builder(this);                          AlertDialog.Builder builder = new AlertDialog.Builder(this);
322                                                    
323                          builder.setTitle("Station search");                          builder.setTitle( getString(stationlist_stationsearch) );
324                          builder.setView(rootView);                          builder.setView(rootView);
325                          builder.setCancelable(true);                          builder.setCancelable(true);
326                          builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {                          builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
327                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
328                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
329                                          dialog.dismiss();                                          dialog.dismiss();
330                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
331                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
332                                                    startNameSearch(search);
333                                          } else {                                          } else {
334                                                  showMessageAndClose("Two characters minimum");                                                  showMessageAndClose( getString(stationlist_twocharmin) );
335                                          }                                          }
336                                  }                                  }
337                          });                          });
338                          builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                          builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
339                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
340                                          dialog.dismiss();                                          dialog.dismiss();
341                                          StationList.this.finish(); // Close this Activity                                          StationList.this.finish(); // Close this Activity
# Line 308  public class StationList extends ListAct Line 368  public class StationList extends ListAct
368          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
369                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
370                                                                    
371                  StationBean station = stations.get(position);                  StationEntry station = stations.entries.get(position);
372                                    
373                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
374                  intent.putExtra("stationbean", station);                          Intent i = new Intent();
375                  startActivity(intent);                          i.putExtra("station", station);
376                            setResult(Activity.RESULT_OK, i);
377                            finish();
378                    } else {                
379                            Intent intent = new Intent(this, DepartureList.class);
380                            intent.putExtra("stationbean", station);
381                            startActivity(intent);
382                    }
383          }          }
384    
385          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
386          //          //
387    
388          public void startLookup() {          public void startLookup() {
389                  isRunning = true;                  isRunning = true;              
390                  dialogMessage = "Wait for location fix";                  dialogMessage = getString( stationlist_waitforlocation );
391                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
392                                    
393                  locationLookup.locateStations();                  locationLookup.locateStations();
394                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS);
395          }          }
396                    
397          void startNameSearch(String name) {          void startNameSearch(String name) {
398                  dialogMessage = "Finding stations by name";                  dialogMessage = getString( stationlist_findbyname );
399                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
400    
401                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
402                  findStationsTask.searchByName(name, locationLookup.getLocation());                  findStationsTask.searchByName(name);
403                  findStationsTask.execute();                  findStationsTask.execute();
404                                    
405          }          }
# Line 340  public class StationList extends ListAct Line 407  public class StationList extends ListAct
407          public void startFavoriteLookup() {          public void startFavoriteLookup() {
408                                    
409                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
410                          dialogMessage = "Loading favorites";                          dialogMessage = getString( stationlist_loadfavorites );
411                          showDialog(DLG_PROGRESS);                          showDialog(DLG_PROGRESS);
412    
413                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
414                          findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation());                          findStationsTask.searchByIds( favorites.toString() );
415                          findStationsTask.execute();                          findStationsTask.execute();
416                  } else {                  } else {
417                          showMessageAndClose( "Favorite list is empty");                          showMessageAndClose( getString( stationlist_nofavorites ) );
418                  }                  }
419          }          }
420    
# Line 355  public class StationList extends ListAct Line 422  public class StationList extends ListAct
422                    
423          void startLocatorTask()          void startLocatorTask()
424          {          {
425                  dialogMessage = "Finding nearby stations";                  dialogMessage = getString( stationlist_findingnearby );
426                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
427                                    
428                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
429                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
430                  findStationsTask.execute();                      findStationsTask.execute();    
431          }          }
           
   
         /* TODO: Remove this no longer needed function  
         String lookupAddress(double latitude, double longitude) {  
                   
                 Geocoder coder = new Geocoder(this, new Locale("da"));  
                 StringBuilder sb = new StringBuilder();  
                 Log.i("lookupaddr", "" + latitude + "/" + longitude);  
                 try {  
                         List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);  
                         Address addr = addressList.get(0);  
                           
                           
                         int max = addr.getMaxAddressLineIndex();  
                         for (int i=0; i<max; i++) {  
                                 if (i>0)  
                                         sb.append(", ");  
                                   
                                 sb.append(addr.getAddressLine(i));  
                         }  
                           
                           
                 } catch (Exception e) {  
                         Log.e("DepartureList", "geocoder failed", e);  
                 }  
432                                    
                 return sb.toString();  
         }*/  
           
433                    
434          ////////////////////////////////////////////////////////////////////////////          ////////////////////////////////////////////////////////////////////////////
435          // Inner classes          // Inner classes
# Line 410  public class StationList extends ListAct Line 449  public class StationList extends ListAct
449    
450                          case NOPROVIDER:                          case NOPROVIDER:
451                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
452                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
453                                    //StationList.this.finish();
454                                  break;                                  break;
455                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
456                                  if (isRunning) {                                  if (isRunning) {
# Line 421  public class StationList extends ListAct Line 461  public class StationList extends ListAct
461                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialog(DLG_PROGRESS);
462                                                                                                    
463                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
464                                                  builder.setMessage("Location fix timed out");                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
465                                                  builder.setCancelable(true);                                                  builder.setCancelable(true);
466                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
467                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
468                                                                  dialog.dismiss();                                                                  dialog.dismiss();
469                                                                  startLookup();                                                                  startLookup();
470                                                                                                                                    
471                                                          }                                                          }
472                                                  });                                                  });
473                                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                                  builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
474                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
475                                                                  dialog.dismiss();                                                                  dialog.dismiss();
476                                                          }                                                                                                                }                                                      
477                                                  });                                                                                                                                              });
478                                                  builder.show();                                                  builder.show();
479    
480                                          }                                          }
481                                  }                                  }
# Line 449  public class StationList extends ListAct Line 489  public class StationList extends ListAct
489          class FindStationsTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
490                                    
491                  LookupMethod method = LookupMethod.MethodNone;                  LookupMethod method = LookupMethod.MethodNone;
                 boolean success;  
492                  String name;                  String name;
493                  Location loc;                  Location loc;
494                  String ids;                  String ids;
495                                    
496                  public void searchByName(String n, Location l) {                  public void searchByName(String n) {
497                                                    
498                          method = LookupMethod.ByName;                          method = LookupMethod.ByName;
                         loc = l;  
499                          name = n;                          name = n;
500                  }                  }
501                                    
# Line 466  public class StationList extends ListAct Line 504  public class StationList extends ListAct
504                          loc = l;                          loc = l;
505                  }                  }
506                                    
507                  public void searchByIds(String id, Location l) {                  public void searchByIds(String id) {
508                                                    
509                          method = LookupMethod.ByList;                          method = LookupMethod.ByList;
                         loc = l;  
510                          ids = id;                          ids = id;
511                  }                  }
512                                    
# Line 486  public class StationList extends ListAct Line 523  public class StationList extends ListAct
523    
524                          switch (method) {                          switch (method) {
525                          case ByLocation:                          case ByLocation:
526                                  success = stationProvider.lookupStations(loc);                                  stations = stationProvider.lookupStations(loc);
527                                  break;                                  break;
528                          case ByName:                          case ByName:
529                                  success = stationProvider.lookupStationsByName(name);                                  stations = stationProvider.lookupStationsByName(name);
530                                  break;                                  break;
531                          case ByList:                          case ByList:
532                                  success = stationProvider.lookupStationsByIds(ids);                                  stations = stationProvider.lookupStationsByIds(ids);
533                                  break;                                  break;
534                          default:                          default:
535                                  success = false; // not possible                                          stations = null; // not possible        
536                          }                          }
537                                                    
538                                                    
                         Location dummy = new Location("gps");  
                         List<StationBean> stations = stationProvider.getStations();  
                           
                         for (StationBean station : stations) {  
                                                                   
                                 if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) {  
                                         if (loc != null) { //only do the distance calc if we have a location  
                                                 dummy.setLatitude(station.getLatitude());  
                                                 dummy.setLongitude(station.getLongitude());  
                                                 station.setDistance( (int)loc.distanceTo(dummy) );  
                                         } else {  
                                                 station.setDistance(0);  
                                         }  
                                 }  
   
                         }                                                
                           
539                          return null;                          return null;
540                  }                  }
541    
# Line 525  public class StationList extends ListAct Line 545  public class StationList extends ListAct
545                          dialog.dismiss();                          dialog.dismiss();
546                                                    
547                                                    
548                          if (success) {                                                    if (stations != null) {                        
549                                  if (stationProvider.getStations().size() == 0)                                  if (stations.entries.size() == 0) {
550                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                          showMessageAndClose(getString(stationlist_nostations));
551                                  stations = stationProvider.getStations();                                  }
552    
553                                    StationList.this.getListView().invalidateViews();
554                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
555                                                                    
556                                    
557                          } else { //communication or parse errors                          } else { //communication or parse errors
558                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
559                                  builder.setMessage("Error on finding nearby stations");                                  builder.setMessage(getString(stationlist_fetcherror));                          
560                                  builder.setCancelable(true);                                  builder.setCancelable(true);
561                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
562                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
563                                                  dialog.dismiss();                                                  dialog.dismiss();
564                                                                                                    
565                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
566                                                          @Override                                                  switch (method) {
567                                                          public void run() {                                                  case ByLocation:
568                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
569                                                          }                                                                  @Override
570                                                  });                                                                  public void run() {
571                                                                            startLocatorTask();                                                            
572                                                                    }
573                                                            };
574                                                            break;
575                                                    case ByName:
576                                                            runner = new Runnable() {
577                                                                    @Override
578                                                                    public void run() {
579                                                                            startNameSearch( FindStationsTask.this.name );
580                                                                    }
581                                                            };
582                                                            break;
583                                                    case ByList:
584                                                            runner = new Runnable() {
585                                                                    @Override
586                                                                    public void run() {
587                                                                            startFavoriteLookup();
588                                                                    }
589                                                            };
590                                                            break;
591                                                    }
592                                                    
593                                                    stationsFetched.post( runner );
594                                          }                                          }
595                                  });                                  });
596                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
597                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
598                                                  dialog.dismiss();                                                  dialog.dismiss();
599                                                    StationList.this.finish();
600                                          }                                                                                                }                                                      
601                                  });                                                                                                                              });
602                                  builder.show();                                                          
603                                    builder.show();
604                          }                          }
605                  }                  }
606          }          }
# Line 570  public class StationList extends ListAct Line 618  public class StationList extends ListAct
618                                                                                                    
619                          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;                          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
620                          selectedPosition = info.position;                          selectedPosition = info.position;
621                          int stationID = stations.get(selectedPosition).getId();                          int stationID = stations.entries.get(selectedPosition).getId();
622    
623                          if (!favorites.contains(stationID)) {                          if (!favorites.contains(stationID)) {
624                                  menu.add(0, FAVORITES_ADD, 0, "Add to favorites");                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
625                          } else {                          } else {
626                                  menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites");                                  menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
627                          }                          }
628                                                    
629                  }                  }
630                                    
631                  public void onContextItemSelected(MenuItem item) {                  public void onContextItemSelected(MenuItem item) {
632                          StationBean sb = stations.get(selectedPosition);                          StationEntry sb = stations.entries.get(selectedPosition);
633                                                    
634                          int stationID = sb.getId();                          int stationID = sb.getId();
635                          if (item.getItemId() == FAVORITES_ADD) {                          if (item.getItemId() == FAVORITES_ADD) {
636                                  favorites.add(stationID);                                  favorites.add(stationID);
637                                  Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
638                          } else {                          } else {
639                                                                    
640                                  favorites.remove(stationID);                                  favorites.remove(stationID);
641                                  Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
642                                                                    
643                                                                    
644                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
645                                          stations.remove(selectedPosition);                                          stations.entries.remove(selectedPosition);
646                                          adapter.notifyDataSetChanged();                                          adapter.notifyDataSetChanged();
647                                  }                                  }
648                          }                          }

Legend:
Removed from v.557  
changed lines
  Added in v.1078

  ViewVC Help
Powered by ViewVC 1.1.20