/[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 435 by torben, Sat Oct 10 11:34:35 2009 UTC revision 480 by torben, Wed Oct 28 12:52:52 2009 UTC
# Line 55  public class StationList extends ListAct Line 55  public class StationList extends ListAct
55          public static final int DLG_PROGRESS = 3001;          public static final int DLG_PROGRESS = 3001;
56          public static final int DLG_STATIONNAME = 3002;          public static final int DLG_STATIONNAME = 3002;
57                    
58          /** Called when the activity is first created. */          static enum LookupMethod {
59                    ByLocation,
60                    ByName,
61                    ByList,
62                    MethodNone
63            }
64            
65            
66          String dialogMessage = "";          String dialogMessage = "";
67          ProgressDialog dialog;          ProgressDialog dialog;
68          LocationLookup locator = null;          LocationLookup locator = null;
69          LocatorTask locatorTask;          FindStationsTask findStationsTask;
70          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
71                    
72          GeoPair location = new GeoPair();          GeoPair location = new GeoPair();
# Line 74  public class StationList extends ListAct Line 81  public class StationList extends ListAct
81          FavoritesMenu contextMenu = new FavoritesMenu();          FavoritesMenu contextMenu = new FavoritesMenu();
82          IntSet favorites = new IntSet();          IntSet favorites = new IntSet();
83                    
84          static enum LookupMethod {          boolean showingFavorites = false;
85                  ByLocation,          String dialogTitle;
                 ByName,  
                 ByList,  
                 MethodNone  
         }  
           
86          SharedPreferences prefs;          SharedPreferences prefs;
87                    
88          ///////////////////////////////////////////////////////////////////////////////////////////          ///////////////////////////////////////////////////////////////////////////////////////////
# Line 114  public class StationList extends ListAct Line 116  public class StationList extends ListAct
116                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
117                          adapter.setStations(stations);                          adapter.setStations(stations);
118                          location = (GeoPair) savedInstanceState.getSerializable("location");                          location = (GeoPair) savedInstanceState.getSerializable("location");
119                            dialogTitle = savedInstanceState.getString("title");
120                            setTitle(dialogTitle);
121                  }                  }
122          }          }
123    
# Line 125  public class StationList extends ListAct Line 129  public class StationList extends ListAct
129                  dialog.dismiss();                  dialog.dismiss();
130          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (ArrayList<StationBean>) stations);
131          outState.putSerializable("location", location);          outState.putSerializable("location", location);
132            outState.putString("title", dialogTitle);
133      }      }
134                    
135                    
# Line 133  public class StationList extends ListAct Line 138  public class StationList extends ListAct
138          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
139                  MenuItem item;                  MenuItem item;
140                                    
141                  item = menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations");                  item = menu.add(0, OPTIONS_RESCAN, 0, "Nearest stations");
142                  item.setIcon(android.R.drawable.ic_menu_mylocation);                  item.setIcon(android.R.drawable.ic_menu_mylocation);
143                                    
144                  item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station");                  item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station");
# Line 142  public class StationList extends ListAct Line 147  public class StationList extends ListAct
147                  item = menu.add(0, OPTIONS_FAVORITES, 0, "Favorites");                  item = menu.add(0, OPTIONS_FAVORITES, 0, "Favorites");
148                  item.setIcon(android.R.drawable.ic_menu_agenda);                  item.setIcon(android.R.drawable.ic_menu_agenda);
149                                    
150                  item = menu.add(0, OPTIONS_MAP, 0, "Show station map");                  item = menu.add(0, OPTIONS_MAP, 0, "Station map");
151                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
152                                    
153                  item = menu.add(0, OPTIONS_ABOUT, 0, "About");                  item = menu.add(0, OPTIONS_ABOUT, 0, "About");
# Line 307  public class StationList extends ListAct Line 312  public class StationList extends ListAct
312                  dialogMessage = "Finding stations by name";                  dialogMessage = "Finding stations by name";
313                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
314    
315                  locatorTask = new LocatorTask();                  findStationsTask = new FindStationsTask();
316                  locatorTask.searchByName(name, locator.getLocation());                  findStationsTask.searchByName(name, locator.getLocation());
317                  locatorTask.execute();                  findStationsTask.execute();
318                                    
319          }          }
320                    
321          public void startFavoriteLookup() {          public void startFavoriteLookup() {
322                                    
323                  if (favorites.toString().length() > 0) {                  if (favorites.size() > 0) {
324                          dialogMessage = "Loading favorites";                          dialogMessage = "Loading favorites";
325                          showDialog(DLG_PROGRESS);                          showDialog(DLG_PROGRESS);
326    
327                          locatorTask = new LocatorTask();                          findStationsTask = new FindStationsTask();
328                          locatorTask.searchByIds(favorites.toString(), locator.getLocation());                          findStationsTask.searchByIds(favorites.toString(), locator.getLocation());
329                          locatorTask.execute();                          findStationsTask.execute();
330                  } else {                  } else {
331                          MessageBox.showMessage(this, "Favorite list is empty");                          MessageBox.showMessage(this, "Favorite list is empty");
332                  }                  }
# Line 334  public class StationList extends ListAct Line 339  public class StationList extends ListAct
339                  dialogMessage = "Finding nearby stations";                  dialogMessage = "Finding nearby stations";
340                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
341                                    
342                  locatorTask = new LocatorTask();                  findStationsTask = new FindStationsTask();
343                  locatorTask.searchByLocation( locator.getLocation() );                  findStationsTask.searchByLocation( locator.getLocation() );
344                  locatorTask.execute();                    findStationsTask.execute();    
345          }          }
346                    
347    
# Line 421  public class StationList extends ListAct Line 426  public class StationList extends ListAct
426          };          };
427    
428                    
429          class LocatorTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
430                                    
431                  LookupMethod method = LookupMethod.MethodNone;                  LookupMethod method = LookupMethod.MethodNone;
432                  boolean success;                  boolean success;
# Line 458  public class StationList extends ListAct Line 463  public class StationList extends ListAct
463                                    
464                  @Override                  @Override
465                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
466                    
467                          if (method.equals(LookupMethod.ByLocation))                          switch (method) {
468                            case ByLocation:
469                                  success = stationProvider.lookupStations(loc);                                  success = stationProvider.lookupStations(loc);
470                                                            break;
471                          if (method.equals(LookupMethod.ByName))                          case ByName:
472                                  success = stationProvider.lookupStationsByName(name);                                  success = stationProvider.lookupStationsByName(name);
473                                                            break;
474                          if (method.equals(LookupMethod.ByList))                          case ByList:
475                                  success = stationProvider.lookupStationsByIds(ids);                                  success = stationProvider.lookupStationsByIds(ids);
476                                    break;
477                            default:
478                                    success = false; // not possible        
479                            }
480                            
481                                                    
482                          Location dummy = new Location("gps");                          Location dummy = new Location("gps");
483                          List<StationBean> stations = stationProvider.getStations();                          List<StationBean> stations = stationProvider.getStations();
# Line 475  public class StationList extends ListAct Line 486  public class StationList extends ListAct
486                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());
487                                  station.setAddress(addr);                                  station.setAddress(addr);
488                                                                    
489                                    
490                                  if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) {                                  if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) {
491                                          dummy.setLatitude(station.getLatitude());                                          if (loc != null) { //only do the distance calc if we have a location
492                                          dummy.setLongitude(station.getLongitude());                                                  dummy.setLatitude(station.getLatitude());
493                                          station.setDistance( (int)loc.distanceTo(dummy) );                                                  dummy.setLongitude(station.getLongitude());
494                                                    station.setDistance( (int)loc.distanceTo(dummy) );
495                                            } else {
496                                                    station.setDistance(0);
497                                            }
498                                  }                                  }
499    
500                          }                                                                        }                                              
501                                                    
502                          return null;                          return null;
# Line 490  public class StationList extends ListAct Line 507  public class StationList extends ListAct
507                          super.onPostExecute(result);                          super.onPostExecute(result);
508                          dialog.dismiss();                          dialog.dismiss();
509                                                    
510                            //set title
511                            switch (method) {
512                            case ByLocation:
513                                    dialogTitle = "Traininfo DK - Nearby stations";
514                                    break;
515                            case ByName:
516                                    dialogTitle = "Traininfo DK - Search";
517                                    break;
518                            case ByList:
519                                    dialogTitle = "Traininfo DK - Favorites";
520                                    break;
521                            default:
522                                    dialogTitle = "";//not possible                                
523                            }                              
524                            
525                            StationList.this.setTitle(dialogTitle);
526                            //set title end
527                            
528                          if (success) {                                                    if (success) {                          
529                                  if (stationProvider.getStations().size() == 0)                                  if (stationProvider.getStations().size() == 0)
530                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
531                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
532                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );
533                                    
534                                    showingFavorites = method.equals(LookupMethod.ByList);                          
535                                                                    
536                          } else { //communication or parse errors                          } else { //communication or parse errors
537                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
# Line 537  public class StationList extends ListAct Line 574  public class StationList extends ListAct
574                          selectedPosition = info.position;                          selectedPosition = info.position;
575                          int stationID = stations.get(selectedPosition).getId();                          int stationID = stations.get(selectedPosition).getId();
576    
577                          if (!favorites.hasInt(stationID)) {                          if (!favorites.contains(stationID)) {
578                                  menu.add(0, FAVORITES_ADD, 0, "Add to favorites");                                  menu.add(0, FAVORITES_ADD, 0, "Add to favorites");
579                          } else {                          } else {
580                                  menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites");                                  menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites");
# Line 553  public class StationList extends ListAct Line 590  public class StationList extends ListAct
590                                  favorites.add(stationID);                                  favorites.add(stationID);
591                                  Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show();
592                          } else {                          } else {
593                                    
594                                  favorites.remove(stationID);                                  favorites.remove(stationID);
595                                  Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show();
596                                    
597                                    if (showingFavorites) {
598                                            stations.remove(selectedPosition);
599                                            adapter.notifyDataSetChanged();
600                                    }
601                          }                          }
602                          Editor ed = prefs.edit();                          Editor ed = prefs.edit();
603                          ed.putString("favorites", favorites.toString());                          ed.putString("favorites", favorites.toString());

Legend:
Removed from v.435  
changed lines
  Added in v.480

  ViewVC Help
Powered by ViewVC 1.1.20