/[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 1142 by torben, Tue Sep 28 14:58:45 2010 UTC revision 1207 by torben, Sat Dec 25 20:51:29 2010 UTC
# Line 48  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;
 import android.util.Log;  
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 70  import dk.thoerup.traininfo.util.IntSet; Line 69  import dk.thoerup.traininfo.util.IntSet;
69  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
70    
71  public class StationList extends ListActivity  {  public class StationList extends ListActivity  {
72          public enum LookupStates {  
                 GOTLOCATION,  
                 GOTSTATIONLIST,  
                 NOPROVIDER,  
                 LOCATIONFIXTIMEOUT,  
                 GPS_SAT_COUNT  
         }  
73                    
74                    
75          public static final int OPTIONS_MAP = 2003;          public static final int OPTIONS_MAP = 2003;
# Line 103  public class StationList extends ListAct Line 96  public class StationList extends ListAct
96          FindStationsTask findStationsTask;          FindStationsTask findStationsTask;
97          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
98                    
99          GeoPair location = new GeoPair();          //GeoPair location = new GeoPair();
100    
101          boolean isLaunchedforShortcut;          boolean isLaunchedforShortcut;
102          boolean isRunning = false;  
103          StationBean stations = new StationBean();          StationBean stations = new StationBean();
104                    
105          StationProvider stationProvider = ProviderFactory.getStationProvider();          StationProvider stationProvider = ProviderFactory.getStationProvider();
# Line 118  public class StationList extends ListAct Line 111  public class StationList extends ListAct
111    
112          WelcomeScreen.ListType listType;          WelcomeScreen.ListType listType;
113          SharedPreferences prefs;          SharedPreferences prefs;
114                                    
115          ///////////////////////////////////////////////////////////////////////////////////////////          ///////////////////////////////////////////////////////////////////////////////////////////
116          //Activity call backs          //Activity call backs
117                    
# Line 135  public class StationList extends ListAct Line 128  public class StationList extends ListAct
128                  ListView lv = getListView();                  ListView lv = getListView();
129                  lv.setOnCreateContextMenuListener(contextMenu);                  lv.setOnCreateContextMenuListener(contextMenu);
130                                    
131                  locationLookup = new LocationLookup(this, stationsFetched);                  locationLookup = new LocationLookup(this);
132                                    
133    
134                  prefs = getSharedPreferences("TrainStation", 0);                  prefs = getSharedPreferences("TrainStation", 0);
# Line 156  public class StationList extends ListAct Line 149  public class StationList extends ListAct
149                                                    
150                          switch (listType) {                          switch (listType) {
151                          case ListNearest:                          case ListNearest:
152                                  startLookup();                                  startNearestLookup();
153                                  break;                                  break;
154                          case ListSearch:                                                          case ListSearch:                                
155                                  showDialog(DLG_STATIONNAME);                                  showDialog(DLG_STATIONNAME);
# Line 171  public class StationList extends ListAct Line 164  public class StationList extends ListAct
164                  } else {                  } else {
165                          stations = (StationBean) savedInstanceState.getSerializable("stations");                          stations = (StationBean) savedInstanceState.getSerializable("stations");
166                          adapter.setStations(stations);                          adapter.setStations(stations);
                         location = (GeoPair) savedInstanceState.getSerializable("location");  
167                  }                  }
168                                    
169          }          }
# Line 185  public class StationList extends ListAct Line 177  public class StationList extends ListAct
177          protected void onDestroy() {          protected void onDestroy() {
178                  super.onDestroy();                  super.onDestroy();
179                                    
                 isRunning = false;  
180                                    
181                  if (locationLookup != null) {                  if (locationLookup != null) {
182                          locationLookup.stopSearch();                          locationLookup.stopSearch();
# Line 224  public class StationList extends ListAct Line 215  public class StationList extends ListAct
215          if (dialog != null && dialog.isShowing())          if (dialog != null && dialog.isShowing())
216                  dialog.dismiss();                  dialog.dismiss();
217          outState.putSerializable("stations", (StationBean) stations);          outState.putSerializable("stations", (StationBean) stations);
         outState.putSerializable("location", location);  
218                    
219      }      }
220                    
# Line 334  public class StationList extends ListAct Line 324  public class StationList extends ListAct
324                                          dialog.dismiss();                                          dialog.dismiss();
325                                          String search = et.getText().toString().trim();                                          String search = et.getText().toString().trim();
326                                          if (search.length() >= 2) {                                          if (search.length() >= 2) {
327                                                  startNameSearch(search);                                                  startNameLookup(search);
328                                          } else {                                          } else {
329                                                  showMessageAndClose( getString(stationlist_twocharmin) );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
330                                          }                                          }
# Line 390  public class StationList extends ListAct Line 380  public class StationList extends ListAct
380          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
381          //          //
382    
383          public void startLookup() {          public void startNearestLookup() {
                 isRunning = true;                
384                  dialogMessage = getString( stationlist_waitforlocation );                  dialogMessage = getString( stationlist_waitforlocation );
385                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
386                                    
387                  locationLookup.locateStations();                  locationLookup.locateStations();
388                  stationsFetched.sendEmptyMessageDelayed(LookupStates.LOCATIONFIXTIMEOUT.ordinal(), GPS_TIMEOUT_MS);                  stationsFetched.sendEmptyMessageDelayed(0, 500);
389          }          }
390                    
391          void startNameSearch(String name) {          void startNameLookup(String name) {
392                  dialogMessage = getString( stationlist_findbyname );                  dialogMessage = getString( stationlist_findbyname );
393                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
394    
# Line 428  public class StationList extends ListAct Line 417  public class StationList extends ListAct
417          void startLocatorTask()          void startLocatorTask()
418          {          {
419                  dialogMessage = getString( stationlist_findingnearby );                  dialogMessage = getString( stationlist_findingnearby );
420                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);              
421                                    
422                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
423                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
# Line 442  public class StationList extends ListAct Line 431  public class StationList extends ListAct
431          class StationsFetchedHandler extends Handler {          class StationsFetchedHandler extends Handler {
432                  @Override                  @Override
433                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
434                          Log.e("Handler", "WHAT:" + msg.what);  
435                                                    LocationLookup.LookupStates state = locationLookup.getState();
436                          LookupStates state = LookupStates.values()[msg.what];  
437    
438                          switch (state) {                          switch (state) {
439                          case GOTLOCATION:                          case GOTLOCATION:
440                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
441                                    
442                                  startLocatorTask();                                  startLocatorTask();
443                                  location = GeoPair.fromLocation( locationLookup.getLocation() );  
444                                                                    return;
                                 break;  
445    
446                          case NOPROVIDER:                          case NOPROVIDER:
447                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
448                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
449                                  //StationList.this.finish();                                  //StationList.this.finish();
450                                  break;                                  return;
451                          case GPS_SAT_COUNT:                          }
452                                  int count = msg.arg1;  
453                                  String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count;  
454                                  dialog.setMessage( dialogMessage );  
455                                  return; //abort immediately                          if (locationLookup.elapsedTime() >=  GPS_TIMEOUT_MS) {
456                          case LOCATIONFIXTIMEOUT:                                                                  dismissDialog(DLG_PROGRESS);
457                                  if (isRunning) {  
458                                          locationLookup.stopSearch();                                  locationLookup.stopSearch();
459                                          if (locationLookup.hasLocation()) {  
460                                                  stationsFetched.sendEmptyMessage(  LookupStates.GOTLOCATION.ordinal() );                                  if (locationLookup.hasLocation()) {
461                                          } else {                                                                                          startLocatorTask();
462                                                  dismissDialog(DLG_PROGRESS);                                  } else {                                                
463                                                                                            AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
464                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                          builder.setMessage(  getString( stationlist_gpstimeout) );
465                                                  builder.setMessage(  getString( stationlist_gpstimeout) );                                          builder.setCancelable(true);
466                                                  builder.setCancelable(true);                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
467                                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                                  public void onClick(DialogInterface dialog, int id) {
468                                                          public void onClick(DialogInterface dialog, int id) {                                                          dialog.dismiss();
469                                                                  dialog.dismiss();                                                          startNearestLookup();
470                                                                  startLookup();  
471                                                                                                                    }
472                                                          }                                          });
473                                                  });                                          builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
474                                                  builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {                                                  public void onClick(DialogInterface dialog, int id) {
475                                                          public void onClick(DialogInterface dialog, int id) {                                                          dialog.dismiss();
476                                                                  dialog.dismiss();                                                          StationList.this.finish(); // Close this Activity
477                                                          }                                                                                                        }                                                      
478                                                  });                                          });
479                                                  builder.show();                                          builder.show();
480    
                                         }  
481                                  }                                  }
482                                  break;                          } else {
483                                    if (locationLookup.hasGps()) {
484                                            int count = locationLookup.getSatCount();
485                                            String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count;
486                                            dialog.setMessage( dialogMessage );
487                                    }
488                                    this.sendEmptyMessageDelayed(0, 500);
489                          }                          }
490                          isRunning = false;                          
491                  }                  }
492          };          }
493    
494    
495    
496                    
497          class FindStationsTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
# Line 536  public class StationList extends ListAct Line 531  public class StationList extends ListAct
531    
532                          switch (method) {                          switch (method) {
533                          case ByLocation:                          case ByLocation:
534                                  stations = stationProvider.lookupStations(loc);                                  stations = stationProvider.lookupStationsByLocation(loc);
535                                  break;                                  break;
536                          case ByName:                          case ByName:
537                                  stations = stationProvider.lookupStationsByName(name);                                  stations = stationProvider.lookupStationsByName(name);
# Line 589  public class StationList extends ListAct Line 584  public class StationList extends ListAct
584                                                          runner = new Runnable() {                                                          runner = new Runnable() {
585                                                                  @Override                                                                  @Override
586                                                                  public void run() {                                                                  public void run() {
587                                                                          startNameSearch( FindStationsTask.this.name );                                                                          startNameLookup( FindStationsTask.this.name );
588                                                                  }                                                                  }
589                                                          };                                                          };
590                                                          break;                                                          break;

Legend:
Removed from v.1142  
changed lines
  Added in v.1207

  ViewVC Help
Powered by ViewVC 1.1.20