/[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 561 by torben, Thu Jan 28 08:55:19 2010 UTC revision 713 by torben, Fri May 7 15:13:07 2010 UTC
# Line 18  import android.os.Bundle; Line 18  import android.os.Bundle;
18  import android.os.Handler;  import android.os.Handler;
19  import android.os.Message;  import android.os.Message;
20    
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 118  public class StationList extends ListAct Line 119  public class StationList extends ListAct
119                                  startLookup();                                  startLookup();
120                                  break;                                  break;
121                          case ListSearch:                          case ListSearch:
122                                  this.showDialog(DLG_STATIONNAME);                                  this.showDialogSafe(DLG_STATIONNAME);
123                                  break;                                  break;
124                          case ListFavorites:                          case ListFavorites:
125                                  startFavoriteLookup();                                  startFavoriteLookup();
# Line 151  public class StationList extends ListAct Line 152  public class StationList extends ListAct
152                  }                  }
153                    
154                  setTitle(dialogTitle);                  setTitle(dialogTitle);
155                    
156            }
157            
158            
159            /* these 3 dialogs helper functions are very rude and ugly hack
160             * to remove these auto-reported exceptions
161             * - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running?
162             * - java.lang.IllegalArgumentException: View not attached to window manager
163             */
164            
165            public void showDialogSafe(int id) {
166                    try {
167                            showDialog(id);
168                    } catch (Exception e) {
169                            Log.e("StationList", "showDialog failed", e);
170                    }
171            }
172            
173            public void dismissDialogSafe(int id) {
174                    try {
175                            dismissDialog(id);
176                    } catch (Exception e) {
177                            Log.e("StationList", "dismissDialog failed", e);
178                    }
179          }          }
180                    
181            public void builderShowSafe(AlertDialog.Builder builder) {
182                    try {
183                            builder.show();
184                    } catch (Exception e) {
185                            Log.e("StationList", "builder.show() failed", e);
186                    }
187                    
188            }
189            /* EOF rude and ugly dialog hack */
190                    
191    
192    
# Line 207  public class StationList extends ListAct Line 241  public class StationList extends ListAct
241                          if (loc != null) {                          if (loc != null) {
242                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
243                                  message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");                                  message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
244                                  message.append( getString(stationlist_latitude) ).append( loc.getLatitude()).append("\n");                                  message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
245                                  message.append( getString(stationlist_longitude) ).append( loc.getLongitude() ).append("\n");                                  message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
246                          } else {                          } else {
247                                  message.append( getString(stationlist_nolocation) );                                  message.append( getString(stationlist_nolocation) );
248                          }                                                }                      
# Line 270  public class StationList extends ListAct Line 304  public class StationList extends ListAct
304                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
305                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
306                                          dialog.dismiss();                                          dialog.dismiss();
307                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
308                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
309                                                    startNameSearch(search);
310                                          } else {                                          } else {
311                                                  showMessageAndClose( getString(stationlist_twocharmin) );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
312                                          }                                          }
# Line 323  public class StationList extends ListAct Line 358  public class StationList extends ListAct
358          public void startLookup() {          public void startLookup() {
359                  isRunning = true;                                isRunning = true;              
360                  dialogMessage = getString( stationlist_waitforlocation );                  dialogMessage = getString( stationlist_waitforlocation );
361                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
362                                    
363                  locationLookup.locateStations();                  locationLookup.locateStations();
364                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
# Line 331  public class StationList extends ListAct Line 366  public class StationList extends ListAct
366                    
367          void startNameSearch(String name) {          void startNameSearch(String name) {
368                  dialogMessage = getString( stationlist_findbyname );                  dialogMessage = getString( stationlist_findbyname );
369                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
370    
371                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
372                  findStationsTask.searchByName(name, locationLookup.getLocation());                  findStationsTask.searchByName(name);
373                  findStationsTask.execute();                  findStationsTask.execute();
374                                    
375          }          }
# Line 343  public class StationList extends ListAct Line 378  public class StationList extends ListAct
378                                    
379                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
380                          dialogMessage = getString( stationlist_loadfavorites );                          dialogMessage = getString( stationlist_loadfavorites );
381                          showDialog(DLG_PROGRESS);                          showDialogSafe(DLG_PROGRESS);
382    
383                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
384                          findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation());                          findStationsTask.searchByIds( favorites.toString() );
385                          findStationsTask.execute();                          findStationsTask.execute();
386                  } else {                  } else {
387                          showMessageAndClose( getString( stationlist_nofavorites ) );                          showMessageAndClose( getString( stationlist_nofavorites ) );
# Line 358  public class StationList extends ListAct Line 393  public class StationList extends ListAct
393          void startLocatorTask()          void startLocatorTask()
394          {          {
395                  dialogMessage = getString( stationlist_findingnearby );                  dialogMessage = getString( stationlist_findingnearby );
396                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
397                                    
398                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
399                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
400                  findStationsTask.execute();                      findStationsTask.execute();    
401          }          }
           
   
         /* TODO: Remove this no longer needed function  
         String lookupAddress(double latitude, double longitude) {  
402                                    
                 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);  
                 }  
                   
                 return sb.toString();  
         }*/  
           
403                    
404          ////////////////////////////////////////////////////////////////////////////          ////////////////////////////////////////////////////////////////////////////
405          // Inner classes          // Inner classes
# Line 403  public class StationList extends ListAct Line 410  public class StationList extends ListAct
410    
411                          switch (msg.what) {                          switch (msg.what) {
412                          case GOTLOCATION:                          case GOTLOCATION:
413                                  dismissDialog(DLG_PROGRESS);                                  dismissDialogSafe(DLG_PROGRESS);
414                                                                    
415                                  startLocatorTask();                                  startLocatorTask();
416                                  location = GeoPair.fromLocation( locationLookup.getLocation() );                                  location = GeoPair.fromLocation( locationLookup.getLocation() );
# Line 411  public class StationList extends ListAct Line 418  public class StationList extends ListAct
418                                  break;                                  break;
419    
420                          case NOPROVIDER:                          case NOPROVIDER:
421                                  dismissDialog(DLG_PROGRESS);                                  dismissDialogSafe(DLG_PROGRESS);
422                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );
423                                  break;                                  break;
424                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
# Line 420  public class StationList extends ListAct Line 427  public class StationList extends ListAct
427                                          if (locationLookup.hasLocation()) {                                          if (locationLookup.hasLocation()) {
428                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
429                                          } else {                                                                                          } else {                                                
430                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialogSafe(DLG_PROGRESS);
431                                                                                                    
432                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
433                                                  builder.setMessage(  getString( stationlist_gpstimeout) );                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
# Line 436  public class StationList extends ListAct Line 443  public class StationList extends ListAct
443                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
444                                                                  dialog.dismiss();                                                                  dialog.dismiss();
445                                                          }                                                                                                                }                                                      
446                                                  });                                                                                                                                              });
447                                                  builder.show();                                                  builderShowSafe(builder); // builder.show()
448    
449                                          }                                          }
450                                  }                                  }
# Line 456  public class StationList extends ListAct Line 463  public class StationList extends ListAct
463                  Location loc;                  Location loc;
464                  String ids;                  String ids;
465                                    
466                  public void searchByName(String n, Location l) {                  public void searchByName(String n) {
467                                                    
468                          method = LookupMethod.ByName;                          method = LookupMethod.ByName;
                         loc = l;  
469                          name = n;                          name = n;
470                  }                  }
471                                    
# Line 468  public class StationList extends ListAct Line 474  public class StationList extends ListAct
474                          loc = l;                          loc = l;
475                  }                  }
476                                    
477                  public void searchByIds(String id, Location l) {                  public void searchByIds(String id) {
478                                                    
479                          method = LookupMethod.ByList;                          method = LookupMethod.ByList;
                         loc = l;  
480                          ids = id;                          ids = id;
481                  }                  }
482                                    
# Line 501  public class StationList extends ListAct Line 506  public class StationList extends ListAct
506                          }                          }
507                                                    
508                                                    
                         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);  
                                         }  
                                 }  
   
                         }                                                
                           
509                          return null;                          return null;
510                  }                  }
511    
# Line 528  public class StationList extends ListAct Line 516  public class StationList extends ListAct
516                                                    
517                                                    
518                          if (success) {                                                    if (success) {                          
519                                  if (stationProvider.getStations().size() == 0)                                  if (stationProvider.getStations().size() == 0) {
520                                          MessageBox.showMessage(StationList.this, getString(stationlist_nostations) ); // this should not be possible !?!                                          showMessageAndClose(getString(stationlist_nostations));
521                                    }
522                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
523                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
524                                                                    
# Line 541  public class StationList extends ListAct Line 530  public class StationList extends ListAct
530                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
531                                                  dialog.dismiss();                                                  dialog.dismiss();
532                                                                                                    
533                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
534                                                          @Override                                                  switch (method) {
535                                                          public void run() {                                                  case ByLocation:
536                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
537                                                          }                                                                  @Override
538                                                  });                                                                  public void run() {
539                                                                            startLocatorTask();                                                            
540                                                                    }
541                                                            };
542                                                            break;
543                                                    case ByName:
544                                                            runner = new Runnable() {
545                                                                    @Override
546                                                                    public void run() {
547                                                                            startNameSearch( FindStationsTask.this.name );
548                                                                    }
549                                                            };
550                                                            break;
551                                                    case ByList:
552                                                            runner = new Runnable() {
553                                                                    @Override
554                                                                    public void run() {
555                                                                            startFavoriteLookup();
556                                                                    }
557                                                            };
558                                                            break;
559                                                    }
560                                                    
561                                                    stationsFetched.post( runner );
562                                          }                                          }
563                                  });                                  });
564                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
565                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
566                                                  dialog.dismiss();                                                  dialog.dismiss();
567                                          }                                                                                                }                                                      
568                                  });                                                                                                                              });
569                                  builder.show();                                                          
570                                    builderShowSafe(builder); // builder.show()
571                          }                          }
572                  }                  }
573          }          }
# Line 577  public class StationList extends ListAct Line 590  public class StationList extends ListAct
590                          if (!favorites.contains(stationID)) {                          if (!favorites.contains(stationID)) {
591                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
592                          } else {                          } else {
593                                  menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_addfavorite) );                                  menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
594                          }                          }
595                                                    
596                  }                  }

Legend:
Removed from v.561  
changed lines
  Added in v.713

  ViewVC Help
Powered by ViewVC 1.1.20