/[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 1078 by torben, Sat Sep 18 07:18:59 2010 UTC revision 1220 by torben, Sun Jan 30 21:14:19 2011 UTC
# Line 23  import static dk.thoerup.traininfo.R.str Line 23  import static dk.thoerup.traininfo.R.str
23  import static dk.thoerup.traininfo.R.string.stationlist_nostations;  import static dk.thoerup.traininfo.R.string.stationlist_nostations;
24  import static dk.thoerup.traininfo.R.string.stationlist_obtainedby;  import static dk.thoerup.traininfo.R.string.stationlist_obtainedby;
25  import static dk.thoerup.traininfo.R.string.stationlist_removefavorite;  import static dk.thoerup.traininfo.R.string.stationlist_removefavorite;
26    import static dk.thoerup.traininfo.R.string.stationlist_satellitecount;
27  import static dk.thoerup.traininfo.R.string.stationlist_search;  import static dk.thoerup.traininfo.R.string.stationlist_search;
28  import static dk.thoerup.traininfo.R.string.stationlist_stationadded;  import static dk.thoerup.traininfo.R.string.stationlist_stationadded;
29  import static dk.thoerup.traininfo.R.string.stationlist_stationmap;  import static dk.thoerup.traininfo.R.string.stationlist_stationmap;
# Line 33  import static dk.thoerup.traininfo.R.str Line 34  import static dk.thoerup.traininfo.R.str
34    
35  import java.util.ArrayList;  import java.util.ArrayList;
36    
   
37  import android.app.Activity;  import android.app.Activity;
38  import android.app.AlertDialog;  import android.app.AlertDialog;
39  import android.app.Dialog;  import android.app.Dialog;
# 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;
51    import android.util.Log;
52  import android.view.ContextMenu;  import android.view.ContextMenu;
53  import android.view.LayoutInflater;  import android.view.LayoutInflater;
54  import android.view.Menu;  import android.view.Menu;
# Line 69  import dk.thoerup.traininfo.util.IntSet; Line 70  import dk.thoerup.traininfo.util.IntSet;
70  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
71    
72  public class StationList extends ListActivity  {  public class StationList extends ListActivity  {
73          public static final int GOTLOCATION = 1001;  
74          public static final int GOTSTATIONLIST = 1002;          
         public static final int NOPROVIDER = 1003;  
         public static final int LOCATIONFIXTIMEOUT = 1004;  
75                    
76          public static final int OPTIONS_MAP = 2003;          public static final int OPTIONS_MAP = 2003;
77          public static final int OPTIONS_GPSINFO = 2004;          public static final int OPTIONS_GPSINFO = 2004;
# Line 98  public class StationList extends ListAct Line 97  public class StationList extends ListAct
97          FindStationsTask findStationsTask;          FindStationsTask findStationsTask;
98          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
99                    
100          GeoPair location = new GeoPair();          //GeoPair location = new GeoPair();
101    
102          boolean isLaunchedforShortcut;          boolean isLaunchedforShortcut;
103          boolean isRunning = false;  
104          StationBean stations = new StationBean();          StationBean stations = new StationBean();
105                    
106          StationProvider stationProvider = ProviderFactory.getStationProvider();          StationProvider stationProvider = ProviderFactory.getStationProvider();
# Line 113  public class StationList extends ListAct Line 112  public class StationList extends ListAct
112    
113          WelcomeScreen.ListType listType;          WelcomeScreen.ListType listType;
114          SharedPreferences prefs;          SharedPreferences prefs;
115                                    
116          ///////////////////////////////////////////////////////////////////////////////////////////          ///////////////////////////////////////////////////////////////////////////////////////////
117          //Activity call backs          //Activity call backs
118                    
# Line 130  public class StationList extends ListAct Line 129  public class StationList extends ListAct
129                  ListView lv = getListView();                  ListView lv = getListView();
130                  lv.setOnCreateContextMenuListener(contextMenu);                  lv.setOnCreateContextMenuListener(contextMenu);
131                                    
132                  locationLookup = new LocationLookup(this, stationsFetched);                  locationLookup = new LocationLookup(this);
133                                    
134    
135                  prefs = getSharedPreferences("TrainStation", 0);                  prefs = getSharedPreferences("TrainStation", 0);
# Line 151  public class StationList extends ListAct Line 150  public class StationList extends ListAct
150                                                    
151                          switch (listType) {                          switch (listType) {
152                          case ListNearest:                          case ListNearest:
153                                  startLookup();                                  startNearestLookup();
154                                  break;                                  break;
155                          case ListSearch:                                                          case ListSearch:                                
156                                  showDialog(DLG_STATIONNAME);                                  showDialog(DLG_STATIONNAME);
# Line 166  public class StationList extends ListAct Line 165  public class StationList extends ListAct
165                  } else {                  } else {
166                          stations = (StationBean) savedInstanceState.getSerializable("stations");                          stations = (StationBean) savedInstanceState.getSerializable("stations");
167                          adapter.setStations(stations);                          adapter.setStations(stations);
                         location = (GeoPair) savedInstanceState.getSerializable("location");  
168                  }                  }
169                                    
170          }          }
# Line 180  public class StationList extends ListAct Line 178  public class StationList extends ListAct
178          protected void onDestroy() {          protected void onDestroy() {
179                  super.onDestroy();                  super.onDestroy();
180                                    
                 isRunning = false;  
181                                    
182                  if (locationLookup != null) {                  if (locationLookup != null) {
183                          locationLookup.stopSearch();                          locationLookup.stopSearch();
# Line 219  public class StationList extends ListAct Line 216  public class StationList extends ListAct
216          if (dialog != null && dialog.isShowing())          if (dialog != null && dialog.isShowing())
217                  dialog.dismiss();                  dialog.dismiss();
218          outState.putSerializable("stations", (StationBean) stations);          outState.putSerializable("stations", (StationBean) stations);
         outState.putSerializable("location", location);  
219                    
220      }      }
221                    
# Line 329  public class StationList extends ListAct Line 325  public class StationList extends ListAct
325                                          dialog.dismiss();                                          dialog.dismiss();
326                                          String search = et.getText().toString().trim();                                          String search = et.getText().toString().trim();
327                                          if (search.length() >= 2) {                                          if (search.length() >= 2) {
328                                                  startNameSearch(search);                                                  startNameLookup(search);
329                                          } else {                                          } else {
330                                                  showMessageAndClose( getString(stationlist_twocharmin) );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
331                                          }                                          }
# Line 385  public class StationList extends ListAct Line 381  public class StationList extends ListAct
381          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
382          //          //
383    
384          public void startLookup() {          public void startNearestLookup() {
                 isRunning = true;                
385                  dialogMessage = getString( stationlist_waitforlocation );                  dialogMessage = getString( stationlist_waitforlocation );
386                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
387                                    
388                  locationLookup.locateStations();                  locationLookup.locateStations();
389                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS);                  stationsFetched.sendEmptyMessageDelayed(0, 500);
390          }          }
391                    
392          void startNameSearch(String name) {          void startNameLookup(String name) {
393                  dialogMessage = getString( stationlist_findbyname );                  dialogMessage = getString( stationlist_findbyname );
394                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
395    
# Line 423  public class StationList extends ListAct Line 418  public class StationList extends ListAct
418          void startLocatorTask()          void startLocatorTask()
419          {          {
420                  dialogMessage = getString( stationlist_findingnearby );                  dialogMessage = getString( stationlist_findingnearby );
421                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);              
422                                    
423                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
424                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
# Line 438  public class StationList extends ListAct Line 433  public class StationList extends ListAct
433                  @Override                  @Override
434                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
435    
436                          switch (msg.what) {                          LocationLookup.LookupStates state = locationLookup.getState();
437    
438    
439                            switch (state) {
440                          case GOTLOCATION:                          case GOTLOCATION:
441                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
442                                    
443                                  startLocatorTask();                                  startLocatorTask();
444                                  location = GeoPair.fromLocation( locationLookup.getLocation() );  
445                                                                    return;
                                 break;  
446    
447                          case NOPROVIDER:                          case NOPROVIDER:
448                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
449                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
450                                  //StationList.this.finish();                                  //StationList.this.finish();
451                                  break;                                  return;
452                          case LOCATIONFIXTIMEOUT:                                                          case IDLE:
453                                  if (isRunning) {                                  Log.e("TrainInfo", "How did this happen ???");
454                                          locationLookup.stopSearch();                                  dismissDialog(DLG_PROGRESS); // how did we get here ??
455                                          if (locationLookup.hasLocation()) {                                  return;
456                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                  
457                                          } else {                                                                          }
                                                 dismissDialog(DLG_PROGRESS);  
                                                   
                                                 AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                  
                                                 builder.setMessage(  getString( stationlist_gpstimeout) );  
                                                 builder.setCancelable(true);  
                                                 builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {  
                                                         public void onClick(DialogInterface dialog, int id) {  
                                                                 dialog.dismiss();  
                                                                 startLookup();  
                                                                   
                                                         }  
                                                 });  
                                                 builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {  
                                                         public void onClick(DialogInterface dialog, int id) {  
                                                                 dialog.dismiss();  
                                                         }                                                        
                                                 });  
                                                 builder.show();  
458    
459                                          }  
460    
461                            if (locationLookup.elapsedTime() >=  GPS_TIMEOUT_MS) {
462                                    try {
463                                            dismissDialog(DLG_PROGRESS);
464                                    } catch (IllegalArgumentException ex) {
465                                            // I get  stacktraces that reports an exception is thrown here -
466                                            // but i can not recreate the situation that causes said exception
467                                            Log.e("TrainInfo", "Why the f*** is this exception thrown here ?? " + ex.getMessage() );
468                                  }                                  }
469                                  break;  
470                                    locationLookup.stopSearch();
471    
472                                    if (locationLookup.hasLocation()) {
473                                            startLocatorTask();
474                                    } else {                                                
475                                            AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
476                                            builder.setMessage(  getString( stationlist_gpstimeout) );
477                                            builder.setCancelable(true);
478                                            builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
479                                                    public void onClick(DialogInterface dialog, int id) {
480                                                            dialog.dismiss();
481                                                            startNearestLookup();
482    
483                                                    }
484                                            });
485                                            builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
486                                                    public void onClick(DialogInterface dialog, int id) {
487                                                            dialog.dismiss();
488                                                            StationList.this.finish(); // Close this Activity
489                                                    }                                                      
490                                            });
491                                            builder.show();
492    
493                                    }
494                            } else {
495                                    if (locationLookup.hasGps()) {
496                                            int count = locationLookup.getSatCount();
497                                            String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count;
498                                            dialog.setMessage( dialogMessage );
499                                    }
500                                    this.sendEmptyMessageDelayed(0, 500);
501                          }                          }
502                          isRunning = false;                          
503                  }                  }
504          };          }
505    
506    
507    
508                    
509          class FindStationsTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
# Line 523  public class StationList extends ListAct Line 543  public class StationList extends ListAct
543    
544                          switch (method) {                          switch (method) {
545                          case ByLocation:                          case ByLocation:
546                                  stations = stationProvider.lookupStations(loc);                                  stations = stationProvider.lookupStationsByLocation(loc);
547                                  break;                                  break;
548                          case ByName:                          case ByName:
549                                  stations = stationProvider.lookupStationsByName(name);                                  stations = stationProvider.lookupStationsByName(name);
# Line 576  public class StationList extends ListAct Line 596  public class StationList extends ListAct
596                                                          runner = new Runnable() {                                                          runner = new Runnable() {
597                                                                  @Override                                                                  @Override
598                                                                  public void run() {                                                                  public void run() {
599                                                                          startNameSearch( FindStationsTask.this.name );                                                                          startNameLookup( FindStationsTask.this.name );
600                                                                  }                                                                  }
601                                                          };                                                          };
602                                                          break;                                                          break;

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

  ViewVC Help
Powered by ViewVC 1.1.20