/[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 578 by torben, Tue Feb 2 08:44:35 2010 UTC revision 844 by torben, Sun Jun 13 14:52:49 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 18  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    
22    import android.util.Log;
23  import android.view.ContextMenu;  import android.view.ContextMenu;
24  import android.view.LayoutInflater;  import android.view.LayoutInflater;
25  import android.view.Menu;  import android.view.Menu;
# 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 118  public class StationList extends ListAct Line 123  public class StationList extends ListAct
123                                  startLookup();                                  startLookup();
124                                  break;                                  break;
125                          case ListSearch:                          case ListSearch:
126                                  this.showDialog(DLG_STATIONNAME);                                  this.showDialogSafe(DLG_STATIONNAME);
127                                  break;                                  break;
128                          case ListFavorites:                          case ListFavorites:
129                                  startFavoriteLookup();                                  startFavoriteLookup();
# Line 151  public class StationList extends ListAct Line 156  public class StationList extends ListAct
156                  }                  }
157                    
158                  setTitle(dialogTitle);                  setTitle(dialogTitle);
159                    
160            }
161            
162            
163            /* these 3 dialogs helper functions are very rude and ugly hack
164             * to remove these auto-reported exceptions
165             * - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running?
166             * - java.lang.IllegalArgumentException: View not attached to window manager
167             */
168            
169            public void showDialogSafe(int id) {
170                    try {
171                            showDialog(id);
172                    } catch (Exception e) {
173                            Log.e("StationList", "showDialog failed", e);
174                    }
175          }          }
176                    
177            public void dismissDialogSafe(int id) {
178                    try {
179                            dismissDialog(id);
180                    } catch (Exception e) {
181                            Log.e("StationList", "dismissDialog failed", e);
182                    }
183            }
184            public void dismissDialogSafe(Dialog dlg) {
185                    try {
186                            dlg.dismiss();
187                    } catch (Exception e) {
188                            Log.e("StationList", "dismissDialog failed", e);
189                    }
190            }
191            
192            public void builderShowSafe(AlertDialog.Builder builder) {
193                    try {
194                            builder.show();
195                    } catch (Exception e) {
196                            Log.e("StationList", "builder.show() failed", e);
197                    }
198                    
199            }
200            /* EOF rude and ugly dialog hack */
201                    
202    
203    
# Line 270  public class StationList extends ListAct Line 315  public class StationList extends ListAct
315                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
316                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
317                                          dialog.dismiss();                                          dialog.dismiss();
318                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
319                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
320                                                    startNameSearch(search);
321                                          } else {                                          } else {
322                                                  showMessageAndClose( getString(stationlist_twocharmin) );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
323                                          }                                          }
# Line 312  public class StationList extends ListAct Line 358  public class StationList extends ListAct
358                                                                    
359                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
360                                    
361                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
362                  intent.putExtra("stationbean", station);                          Intent i = new Intent();
363                  startActivity(intent);                          i.putExtra("station", station);
364                            setResult(Activity.RESULT_OK, i);
365                            finish();
366                    } else {                
367                            Intent intent = new Intent(this, DepartureList.class);
368                            intent.putExtra("stationbean", station);
369                            startActivity(intent);
370                    }
371          }          }
372    
373          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
# Line 323  public class StationList extends ListAct Line 376  public class StationList extends ListAct
376          public void startLookup() {          public void startLookup() {
377                  isRunning = true;                                isRunning = true;              
378                  dialogMessage = getString( stationlist_waitforlocation );                  dialogMessage = getString( stationlist_waitforlocation );
379                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
380                                    
381                  locationLookup.locateStations();                  locationLookup.locateStations();
382                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
# Line 331  public class StationList extends ListAct Line 384  public class StationList extends ListAct
384                    
385          void startNameSearch(String name) {          void startNameSearch(String name) {
386                  dialogMessage = getString( stationlist_findbyname );                  dialogMessage = getString( stationlist_findbyname );
387                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
388    
389                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
390                  findStationsTask.searchByName(name, locationLookup.getLocation());                  findStationsTask.searchByName(name);
391                  findStationsTask.execute();                  findStationsTask.execute();
392                                    
393          }          }
# Line 343  public class StationList extends ListAct Line 396  public class StationList extends ListAct
396                                    
397                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
398                          dialogMessage = getString( stationlist_loadfavorites );                          dialogMessage = getString( stationlist_loadfavorites );
399                          showDialog(DLG_PROGRESS);                          showDialogSafe(DLG_PROGRESS);
400    
401                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
402                          findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation());                          findStationsTask.searchByIds( favorites.toString() );
403                          findStationsTask.execute();                          findStationsTask.execute();
404                  } else {                  } else {
405                          showMessageAndClose( getString( stationlist_nofavorites ) );                          showMessageAndClose( getString( stationlist_nofavorites ) );
# Line 358  public class StationList extends ListAct Line 411  public class StationList extends ListAct
411          void startLocatorTask()          void startLocatorTask()
412          {          {
413                  dialogMessage = getString( stationlist_findingnearby );                  dialogMessage = getString( stationlist_findingnearby );
414                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
415                                    
416                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
417                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
# Line 375  public class StationList extends ListAct Line 428  public class StationList extends ListAct
428    
429                          switch (msg.what) {                          switch (msg.what) {
430                          case GOTLOCATION:                          case GOTLOCATION:
431                                  dismissDialog(DLG_PROGRESS);                                  dismissDialogSafe(DLG_PROGRESS);
432                                                                    
433                                  startLocatorTask();                                  startLocatorTask();
434                                  location = GeoPair.fromLocation( locationLookup.getLocation() );                                  location = GeoPair.fromLocation( locationLookup.getLocation() );
# Line 383  public class StationList extends ListAct Line 436  public class StationList extends ListAct
436                                  break;                                  break;
437    
438                          case NOPROVIDER:                          case NOPROVIDER:
439                                  dismissDialog(DLG_PROGRESS);                                  dismissDialogSafe(DLG_PROGRESS);
440                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );
441                                  break;                                  break;
442                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
# Line 392  public class StationList extends ListAct Line 445  public class StationList extends ListAct
445                                          if (locationLookup.hasLocation()) {                                          if (locationLookup.hasLocation()) {
446                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
447                                          } else {                                                                                          } else {                                                
448                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialogSafe(DLG_PROGRESS);
449                                                                                                    
450                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
451                                                  builder.setMessage(  getString( stationlist_gpstimeout) );                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
# Line 408  public class StationList extends ListAct Line 461  public class StationList extends ListAct
461                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
462                                                                  dialog.dismiss();                                                                  dialog.dismiss();
463                                                          }                                                                                                                }                                                      
464                                                  });                                                                                                                                              });
465                                                  builder.show();                                                  builderShowSafe(builder); // builder.show()
466    
467                                          }                                          }
468                                  }                                  }
# Line 428  public class StationList extends ListAct Line 481  public class StationList extends ListAct
481                  Location loc;                  Location loc;
482                  String ids;                  String ids;
483                                    
484                  public void searchByName(String n, Location l) {                  public void searchByName(String n) {
485                                                    
486                          method = LookupMethod.ByName;                          method = LookupMethod.ByName;
                         loc = l;  
487                          name = n;                          name = n;
488                  }                  }
489                                    
# Line 440  public class StationList extends ListAct Line 492  public class StationList extends ListAct
492                          loc = l;                          loc = l;
493                  }                  }
494                                    
495                  public void searchByIds(String id, Location l) {                  public void searchByIds(String id) {
496                                                    
497                          method = LookupMethod.ByList;                          method = LookupMethod.ByList;
                         loc = l;  
498                          ids = id;                          ids = id;
499                  }                  }
500                                    
# Line 473  public class StationList extends ListAct Line 524  public class StationList extends ListAct
524                          }                          }
525                                                    
526                                                    
                         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);  
                                         }  
                                 }  
   
                         }                                                
                           
527                          return null;                          return null;
528                  }                  }
529    
530                  @Override                  @Override
531                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
532                          super.onPostExecute(result);                          super.onPostExecute(result);
533                          dialog.dismiss();                          dismissDialogSafe(dialog);
534                                                    
535                                                    
536                          if (success) {                                                    if (success) {                          
# Line 508  public class StationList extends ListAct Line 542  public class StationList extends ListAct
542                                                                    
543                          } else { //communication or parse errors                          } else { //communication or parse errors
544                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
545                                  builder.setMessage(getString(stationlist_nearbyerror));                                                          builder.setMessage(getString(stationlist_fetcherror));                          
546                                  builder.setCancelable(true);                                  builder.setCancelable(true);
547                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
548                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
549                                                  dialog.dismiss();                                                  dialog.dismiss();
550                                                                                                    
551                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
552                                                          @Override                                                  switch (method) {
553                                                          public void run() {                                                  case ByLocation:
554                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
555                                                          }                                                                  @Override
556                                                  });                                                                  public void run() {
557                                                                            startLocatorTask();                                                            
558                                                                    }
559                                                            };
560                                                            break;
561                                                    case ByName:
562                                                            runner = new Runnable() {
563                                                                    @Override
564                                                                    public void run() {
565                                                                            startNameSearch( FindStationsTask.this.name );
566                                                                    }
567                                                            };
568                                                            break;
569                                                    case ByList:
570                                                            runner = new Runnable() {
571                                                                    @Override
572                                                                    public void run() {
573                                                                            startFavoriteLookup();
574                                                                    }
575                                                            };
576                                                            break;
577                                                    }
578                                                    
579                                                    stationsFetched.post( runner );
580                                          }                                          }
581                                  });                                  });
582                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
583                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
584                                                  dialog.dismiss();                                                  dialog.dismiss();
585                                                    StationList.this.finish();
586                                          }                                                                                                }                                                      
587                                  });                                                                                                                              });
588                                  builder.show();                                                          
589                                    builderShowSafe(builder); // builder.show()
590                          }                          }
591                  }                  }
592          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.20