/[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 844 by torben, Sun Jun 13 14:52:49 2010 UTC revision 1025 by torben, Tue Aug 31 08:49:15 2010 UTC
# Line 19  import android.os.Bundle; Line 19  import android.os.Bundle;
19  import android.os.Handler;  import android.os.Handler;
20  import android.os.Message;  import android.os.Message;
21    
 import android.util.Log;  
22  import android.view.ContextMenu;  import android.view.ContextMenu;
23  import android.view.LayoutInflater;  import android.view.LayoutInflater;
24  import android.view.Menu;  import android.view.Menu;
# Line 48  public class StationList extends ListAct Line 47  public class StationList extends ListAct
47                    
48          public static final int OPTIONS_MAP = 2003;          public static final int OPTIONS_MAP = 2003;
49          public static final int OPTIONS_GPSINFO = 2004;          public static final int OPTIONS_GPSINFO = 2004;
   
           
   
50                    
51          public static final int DLG_PROGRESS = 3001;          public static final int DLG_PROGRESS = 3001;
52          public static final int DLG_STATIONNAME = 3002;          public static final int DLG_STATIONNAME = 3002;
53                    
54            
55            public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds
56            
57            
58          static enum LookupMethod {          static enum LookupMethod {
59                  ByLocation,                  ByLocation,
60                  ByName,                  ByName,
# Line 122  public class StationList extends ListAct Line 122  public class StationList extends ListAct
122                          case ListNearest:                          case ListNearest:
123                                  startLookup();                                  startLookup();
124                                  break;                                  break;
125                          case ListSearch:                          case ListSearch:                                
126                                  this.showDialogSafe(DLG_STATIONNAME);                                  showDialog(DLG_STATIONNAME);
127                                  break;                                  break;
128                          case ListFavorites:                          case ListFavorites:
129                                  startFavoriteLookup();                                  startFavoriteLookup();
# Line 139  public class StationList extends ListAct Line 139  public class StationList extends ListAct
139                  }                  }
140                                    
141          }          }
142            
143    
144            @Override
145            protected void onDestroy() {
146                    super.onDestroy();
147                    
148                    isRunning = false;
149                    
150                    if (locationLookup != null) {
151                            locationLookup.stopSearch();
152                    }
153                    if (findStationsTask != null) {
154                            findStationsTask.cancel(true);
155                    }              
156            }
157    
158            
159          protected void setTitle() {          protected void setTitle() {
160                  String dialogTitle = getResources().getString(app_name);                  String dialogTitle = getResources().getString(app_name);
161                  switch (listType) {                  switch (listType) {
# Line 159  public class StationList extends ListAct Line 176  public class StationList extends ListAct
176                                    
177          }          }
178                    
           
         /* these 3 dialogs helper functions are very rude and ugly hack  
          * to remove these auto-reported exceptions  
          * - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running?  
          * - java.lang.IllegalArgumentException: View not attached to window manager  
          */  
           
         public void showDialogSafe(int id) {  
                 try {  
                         showDialog(id);  
                 } catch (Exception e) {  
                         Log.e("StationList", "showDialog failed", e);  
                 }  
         }  
           
         public void dismissDialogSafe(int id) {  
                 try {  
                         dismissDialog(id);  
                 } catch (Exception e) {  
                         Log.e("StationList", "dismissDialog failed", e);  
                 }  
         }  
         public void dismissDialogSafe(Dialog dlg) {  
                 try {  
                         dlg.dismiss();  
                 } catch (Exception e) {  
                         Log.e("StationList", "dismissDialog failed", e);  
                 }  
         }  
           
         public void builderShowSafe(AlertDialog.Builder builder) {  
                 try {  
                         builder.show();  
                 } catch (Exception e) {  
                         Log.e("StationList", "builder.show() failed", e);  
                 }  
179                                    
         }  
         /* EOF rude and ugly dialog hack */  
           
   
180    
181      @Override      @Override
182      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
# Line 258  public class StationList extends ListAct Line 235  public class StationList extends ListAct
235                                  message.append( getString(stationlist_nolocation) );                                  message.append( getString(stationlist_nolocation) );
236                          }                                                }                      
237                                                    
238                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString(), false);
239                          break;                          break;
240                  default:                  default:
241                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 376  public class StationList extends ListAct Line 353  public class StationList extends ListAct
353          public void startLookup() {          public void startLookup() {
354                  isRunning = true;                                isRunning = true;              
355                  dialogMessage = getString( stationlist_waitforlocation );                  dialogMessage = getString( stationlist_waitforlocation );
356                  showDialogSafe(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
357                                    
358                  locationLookup.locateStations();                  locationLookup.locateStations();
359                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS);
360          }          }
361                    
362          void startNameSearch(String name) {          void startNameSearch(String name) {
363                  dialogMessage = getString( stationlist_findbyname );                  dialogMessage = getString( stationlist_findbyname );
364                  showDialogSafe(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
365    
366                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
367                  findStationsTask.searchByName(name);                  findStationsTask.searchByName(name);
# Line 396  public class StationList extends ListAct Line 373  public class StationList extends ListAct
373                                    
374                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
375                          dialogMessage = getString( stationlist_loadfavorites );                          dialogMessage = getString( stationlist_loadfavorites );
376                          showDialogSafe(DLG_PROGRESS);                          showDialog(DLG_PROGRESS);
377    
378                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
379                          findStationsTask.searchByIds( favorites.toString() );                          findStationsTask.searchByIds( favorites.toString() );
# Line 411  public class StationList extends ListAct Line 388  public class StationList extends ListAct
388          void startLocatorTask()          void startLocatorTask()
389          {          {
390                  dialogMessage = getString( stationlist_findingnearby );                  dialogMessage = getString( stationlist_findingnearby );
391                  showDialogSafe(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
392                                    
393                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
394                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
# Line 428  public class StationList extends ListAct Line 405  public class StationList extends ListAct
405    
406                          switch (msg.what) {                          switch (msg.what) {
407                          case GOTLOCATION:                          case GOTLOCATION:
408                                  dismissDialogSafe(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
409                                                                    
410                                  startLocatorTask();                                  startLocatorTask();
411                                  location = GeoPair.fromLocation( locationLookup.getLocation() );                                  location = GeoPair.fromLocation( locationLookup.getLocation() );
# Line 436  public class StationList extends ListAct Line 413  public class StationList extends ListAct
413                                  break;                                  break;
414    
415                          case NOPROVIDER:                          case NOPROVIDER:
416                                  dismissDialogSafe(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
417                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
418                                    //StationList.this.finish();
419                                  break;                                  break;
420                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
421                                  if (isRunning) {                                  if (isRunning) {
# Line 445  public class StationList extends ListAct Line 423  public class StationList extends ListAct
423                                          if (locationLookup.hasLocation()) {                                          if (locationLookup.hasLocation()) {
424                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
425                                          } else {                                                                                          } else {                                                
426                                                  dismissDialogSafe(DLG_PROGRESS);                                                  dismissDialog(DLG_PROGRESS);
427                                                                                                    
428                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
429                                                  builder.setMessage(  getString( stationlist_gpstimeout) );                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
# Line 462  public class StationList extends ListAct Line 440  public class StationList extends ListAct
440                                                                  dialog.dismiss();                                                                  dialog.dismiss();
441                                                          }                                                                                                                }                                                      
442                                                  });                                                  });
443                                                  builderShowSafe(builder); // builder.show()                                                  builder.show();
444    
445                                          }                                          }
446                                  }                                  }
# Line 476  public class StationList extends ListAct Line 454  public class StationList extends ListAct
454          class FindStationsTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
455                                    
456                  LookupMethod method = LookupMethod.MethodNone;                  LookupMethod method = LookupMethod.MethodNone;
                 boolean success;  
457                  String name;                  String name;
458                  Location loc;                  Location loc;
459                  String ids;                  String ids;
# Line 511  public class StationList extends ListAct Line 488  public class StationList extends ListAct
488    
489                          switch (method) {                          switch (method) {
490                          case ByLocation:                          case ByLocation:
491                                  success = stationProvider.lookupStations(loc);                                  stations = stationProvider.lookupStations(loc);
492                                  break;                                  break;
493                          case ByName:                          case ByName:
494                                  success = stationProvider.lookupStationsByName(name);                                  stations = stationProvider.lookupStationsByName(name);
495                                  break;                                  break;
496                          case ByList:                          case ByList:
497                                  success = stationProvider.lookupStationsByIds(ids);                                  stations = stationProvider.lookupStationsByIds(ids);
498                                  break;                                  break;
499                          default:                          default:
500                                  success = false; // not possible                                          stations = null; // not possible        
501                          }                          }
502                                                    
503                                                    
# Line 530  public class StationList extends ListAct Line 507  public class StationList extends ListAct
507                  @Override                  @Override
508                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
509                          super.onPostExecute(result);                          super.onPostExecute(result);
510                          dismissDialogSafe(dialog);                          dialog.dismiss();
511                                                    
512                                                    
513                          if (success) {                                                    if (stations != null) {                        
514                                  if (stationProvider.getStations().size() == 0) {                                  if (stations.size() == 0) {
515                                          showMessageAndClose(getString(stationlist_nostations));                                          showMessageAndClose(getString(stationlist_nostations));
516                                  }                                  }
517                                  stations = stationProvider.getStations();  
518                                    StationList.this.getListView().invalidateViews();
519                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
520                                                                    
521                                    
522                          } else { //communication or parse errors                          } else { //communication or parse errors
523                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
524                                  builder.setMessage(getString(stationlist_fetcherror));                                                            builder.setMessage(getString(stationlist_fetcherror));                          
# Line 586  public class StationList extends ListAct Line 565  public class StationList extends ListAct
565                                          }                                                                                                }                                                      
566                                  });                                  });
567                                                                    
568                                  builderShowSafe(builder); // builder.show()                                  builder.show();
569                          }                          }
570                  }                  }
571          }          }

Legend:
Removed from v.844  
changed lines
  Added in v.1025

  ViewVC Help
Powered by ViewVC 1.1.20