/[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 1027 by torben, Wed Sep 8 06:03:45 2010 UTC
# Line 4  import java.util.ArrayList; Line 4  import java.util.ArrayList;
4  import java.util.List;  import java.util.List;
5    
6    
7    import android.app.Activity;
8  import android.app.AlertDialog;  import android.app.AlertDialog;
9  import android.app.Dialog;  import android.app.Dialog;
10  import android.app.ListActivity;  import android.app.ListActivity;
# Line 46  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 68  public class StationList extends ListAct Line 70  public class StationList extends ListAct
70          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
71                    
72          GeoPair location = new GeoPair();          GeoPair location = new GeoPair();
73            
74            boolean isLaunchedforShortcut;
75          boolean isRunning = false;          boolean isRunning = false;
76          List<StationBean> stations = new ArrayList<StationBean>();          List<StationBean> stations = new ArrayList<StationBean>();
77                    
# Line 110  public class StationList extends ListAct Line 113  public class StationList extends ListAct
113                  listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");                  listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
114                  setTitle();                  setTitle();
115                                    
116                    isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);
117                    
118                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
119    
120                                                    
# Line 117  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.showDialog(DLG_STATIONNAME);                                  showDialog(DLG_STATIONNAME);
127                                  break;                                  break;
128                          case ListFavorites:                          case ListFavorites:
129                                  startFavoriteLookup();                                  startFavoriteLookup();
# Line 134  public class StationList extends ListAct Line 139  public class StationList extends ListAct
139                  }                  }
140                                    
141          }          }
142            
143            
144            
145    
146            @Override
147            protected void onStart() {
148                    super.onStart();
149                    ProviderFactory.purgeOldEntries();
150            }
151    
152    
153    
154    
155            @Override
156            protected void onDestroy() {
157                    super.onDestroy();
158                    
159                    isRunning = false;
160                    
161                    if (locationLookup != null) {
162                            locationLookup.stopSearch();
163                    }
164                    if (findStationsTask != null) {
165                            findStationsTask.cancel(true);
166                    }              
167            }
168    
169            
170          protected void setTitle() {          protected void setTitle() {
171                  String dialogTitle = getResources().getString(app_name);                  String dialogTitle = getResources().getString(app_name);
172                  switch (listType) {                  switch (listType) {
# Line 151  public class StationList extends ListAct Line 184  public class StationList extends ListAct
184                  }                  }
185                    
186                  setTitle(dialogTitle);                  setTitle(dialogTitle);
187                    
188          }          }
189                    
190                            
   
191    
192      @Override      @Override
193      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
# Line 207  public class StationList extends ListAct Line 240  public class StationList extends ListAct
240                          if (loc != null) {                          if (loc != null) {
241                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
242                                  message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");                                  message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
243                                  message.append( getString(stationlist_latitude) ).append( loc.getLatitude()).append("\n");                                  message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
244                                  message.append( getString(stationlist_longitude) ).append( loc.getLongitude() ).append("\n");                                  message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
245                          } else {                          } else {
246                                  message.append( getString(stationlist_nolocation) );                                  message.append( getString(stationlist_nolocation) );
247                          }                                                }                      
248                                                    
249                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString(), false);
250                          break;                          break;
251                  default:                  default:
252                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 270  public class StationList extends ListAct Line 303  public class StationList extends ListAct
303                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
304                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
305                                          dialog.dismiss();                                          dialog.dismiss();
306                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
307                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
308                                                    startNameSearch(search);
309                                          } else {                                          } else {
310                                                  showMessageAndClose( getString(stationlist_twocharmin) );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
311                                          }                                          }
# Line 312  public class StationList extends ListAct Line 346  public class StationList extends ListAct
346                                                                    
347                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
348                                    
349                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
350                  intent.putExtra("stationbean", station);                          Intent i = new Intent();
351                  startActivity(intent);                          i.putExtra("station", station);
352                            setResult(Activity.RESULT_OK, i);
353                            finish();
354                    } else {                
355                            Intent intent = new Intent(this, DepartureList.class);
356                            intent.putExtra("stationbean", station);
357                            startActivity(intent);
358                    }
359          }          }
360    
361          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
# Line 326  public class StationList extends ListAct Line 367  public class StationList extends ListAct
367                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
368                                    
369                  locationLookup.locateStations();                  locationLookup.locateStations();
370                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS);
371          }          }
372                    
373          void startNameSearch(String name) {          void startNameSearch(String name) {
# Line 334  public class StationList extends ListAct Line 375  public class StationList extends ListAct
375                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
376    
377                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
378                  findStationsTask.searchByName(name, locationLookup.getLocation());                  findStationsTask.searchByName(name);
379                  findStationsTask.execute();                  findStationsTask.execute();
380                                    
381          }          }
# Line 346  public class StationList extends ListAct Line 387  public class StationList extends ListAct
387                          showDialog(DLG_PROGRESS);                          showDialog(DLG_PROGRESS);
388    
389                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
390                          findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation());                          findStationsTask.searchByIds( favorites.toString() );
391                          findStationsTask.execute();                          findStationsTask.execute();
392                  } else {                  } else {
393                          showMessageAndClose( getString( stationlist_nofavorites ) );                          showMessageAndClose( getString( stationlist_nofavorites ) );
# Line 364  public class StationList extends ListAct Line 405  public class StationList extends ListAct
405                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
406                  findStationsTask.execute();                      findStationsTask.execute();    
407          }          }
           
   
         /* TODO: Remove this no longer needed function  
         String lookupAddress(double latitude, double longitude) {  
408                                    
                 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();  
         }*/  
           
409                    
410          ////////////////////////////////////////////////////////////////////////////          ////////////////////////////////////////////////////////////////////////////
411          // Inner classes          // Inner classes
# Line 412  public class StationList extends ListAct Line 425  public class StationList extends ListAct
425    
426                          case NOPROVIDER:                          case NOPROVIDER:
427                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
428                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
429                                    //StationList.this.finish();
430                                  break;                                  break;
431                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
432                                  if (isRunning) {                                  if (isRunning) {
# Line 436  public class StationList extends ListAct Line 450  public class StationList extends ListAct
450                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
451                                                                  dialog.dismiss();                                                                  dialog.dismiss();
452                                                          }                                                                                                                }                                                      
453                                                  });                                                                                                                                              });
454                                                  builder.show();                                                  builder.show();
455    
456                                          }                                          }
457                                  }                                  }
# Line 451  public class StationList extends ListAct Line 465  public class StationList extends ListAct
465          class FindStationsTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
466                                    
467                  LookupMethod method = LookupMethod.MethodNone;                  LookupMethod method = LookupMethod.MethodNone;
                 boolean success;  
468                  String name;                  String name;
469                  Location loc;                  Location loc;
470                  String ids;                  String ids;
471                                    
472                  public void searchByName(String n, Location l) {                  public void searchByName(String n) {
473                                                    
474                          method = LookupMethod.ByName;                          method = LookupMethod.ByName;
                         loc = l;  
475                          name = n;                          name = n;
476                  }                  }
477                                    
# Line 468  public class StationList extends ListAct Line 480  public class StationList extends ListAct
480                          loc = l;                          loc = l;
481                  }                  }
482                                    
483                  public void searchByIds(String id, Location l) {                  public void searchByIds(String id) {
484                                                    
485                          method = LookupMethod.ByList;                          method = LookupMethod.ByList;
                         loc = l;  
486                          ids = id;                          ids = id;
487                  }                  }
488                                    
# Line 488  public class StationList extends ListAct Line 499  public class StationList extends ListAct
499    
500                          switch (method) {                          switch (method) {
501                          case ByLocation:                          case ByLocation:
502                                  success = stationProvider.lookupStations(loc);                                  stations = stationProvider.lookupStations(loc);
503                                  break;                                  break;
504                          case ByName:                          case ByName:
505                                  success = stationProvider.lookupStationsByName(name);                                  stations = stationProvider.lookupStationsByName(name);
506                                  break;                                  break;
507                          case ByList:                          case ByList:
508                                  success = stationProvider.lookupStationsByIds(ids);                                  stations = stationProvider.lookupStationsByIds(ids);
509                                  break;                                  break;
510                          default:                          default:
511                                  success = false; // not possible                                          stations = null; // not possible        
512                          }                          }
513                                                    
514                                                    
                         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);  
                                         }  
                                 }  
   
                         }                                                
                           
515                          return null;                          return null;
516                  }                  }
517    
# Line 527  public class StationList extends ListAct Line 521  public class StationList extends ListAct
521                          dialog.dismiss();                          dialog.dismiss();
522                                                    
523                                                    
524                          if (success) {                                                    if (stations != null) {                        
525                                  if (stationProvider.getStations().size() == 0)                                  if (stations.size() == 0) {
526                                          MessageBox.showMessage(StationList.this, getString(stationlist_nostations) ); // this should not be possible !?!                                          showMessageAndClose(getString(stationlist_nostations));
527                                  stations = stationProvider.getStations();                                  }
528    
529                                    StationList.this.getListView().invalidateViews();
530                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
531                                                                    
532                                    
533                          } else { //communication or parse errors                          } else { //communication or parse errors
534                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
535                                  builder.setMessage(getString(stationlist_nearbyerror));                                                          builder.setMessage(getString(stationlist_fetcherror));                          
536                                  builder.setCancelable(true);                                  builder.setCancelable(true);
537                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
538                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
539                                                  dialog.dismiss();                                                  dialog.dismiss();
540                                                                                                    
541                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
542                                                          @Override                                                  switch (method) {
543                                                          public void run() {                                                  case ByLocation:
544                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
545                                                          }                                                                  @Override
546                                                  });                                                                  public void run() {
547                                                                            startLocatorTask();                                                            
548                                                                    }
549                                                            };
550                                                            break;
551                                                    case ByName:
552                                                            runner = new Runnable() {
553                                                                    @Override
554                                                                    public void run() {
555                                                                            startNameSearch( FindStationsTask.this.name );
556                                                                    }
557                                                            };
558                                                            break;
559                                                    case ByList:
560                                                            runner = new Runnable() {
561                                                                    @Override
562                                                                    public void run() {
563                                                                            startFavoriteLookup();
564                                                                    }
565                                                            };
566                                                            break;
567                                                    }
568                                                    
569                                                    stationsFetched.post( runner );
570                                          }                                          }
571                                  });                                  });
572                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
573                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
574                                                  dialog.dismiss();                                                  dialog.dismiss();
575                                                    StationList.this.finish();
576                                          }                                                                                                }                                                      
577                                  });                                                                                                                              });
578                                  builder.show();                                                          
579                                    builder.show();
580                          }                          }
581                  }                  }
582          }          }
# Line 577  public class StationList extends ListAct Line 599  public class StationList extends ListAct
599                          if (!favorites.contains(stationID)) {                          if (!favorites.contains(stationID)) {
600                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
601                          } else {                          } else {
602                                  menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_addfavorite) );                                  menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
603                          }                          }
604                                                    
605                  }                  }

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

  ViewVC Help
Powered by ViewVC 1.1.20