/[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 564 by torben, Thu Jan 28 09:16:33 2010 UTC revision 948 by torben, Fri Jul 2 14:58:44 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 46  public class StationList extends ListAct Line 48  public class StationList extends ListAct
48                    
49          public static final int OPTIONS_MAP = 2003;          public static final int OPTIONS_MAP = 2003;
50          public static final int OPTIONS_GPSINFO = 2004;          public static final int OPTIONS_GPSINFO = 2004;
   
           
   
51                    
52          public static final int DLG_PROGRESS = 3001;          public static final int DLG_PROGRESS = 3001;
53          public static final int DLG_STATIONNAME = 3002;          public static final int DLG_STATIONNAME = 3002;
54                    
55            
56            public static final int GPS_TIMEOUT_MS = 17500; //how long are we willing to wait for gps fix -in milliseconds
57            
58            
59          static enum LookupMethod {          static enum LookupMethod {
60                  ByLocation,                  ByLocation,
61                  ByName,                  ByName,
# Line 68  public class StationList extends ListAct Line 71  public class StationList extends ListAct
71          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
72                    
73          GeoPair location = new GeoPair();          GeoPair location = new GeoPair();
74            
75            boolean isLaunchedforShortcut;
76          boolean isRunning = false;          boolean isRunning = false;
77          List<StationBean> stations = new ArrayList<StationBean>();          List<StationBean> stations = new ArrayList<StationBean>();
78                    
# Line 110  public class StationList extends ListAct Line 114  public class StationList extends ListAct
114                  listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");                  listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
115                  setTitle();                  setTitle();
116                                    
117                    isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);
118                    
119                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
120    
121                                                    
# Line 118  public class StationList extends ListAct Line 124  public class StationList extends ListAct
124                                  startLookup();                                  startLookup();
125                                  break;                                  break;
126                          case ListSearch:                          case ListSearch:
127                                  this.showDialog(DLG_STATIONNAME);                                  this.showDialogSafe(DLG_STATIONNAME);
128                                  break;                                  break;
129                          case ListFavorites:                          case ListFavorites:
130                                  startFavoriteLookup();                                  startFavoriteLookup();
# Line 134  public class StationList extends ListAct Line 140  public class StationList extends ListAct
140                  }                  }
141                                    
142          }          }
143            
144    
145            @Override
146            protected void onDestroy() {
147                    super.onDestroy();
148                    
149                    if (findStationsTask != null) {
150                            findStationsTask.cancel(true);
151                    }
152                    if (locationLookup != null) {
153                            locationLookup.stopSearch();
154                    }
155                    isRunning = false;
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 151  public class StationList extends ListAct Line 173  public class StationList extends ListAct
173                  }                  }
174                    
175                  setTitle(dialogTitle);                  setTitle(dialogTitle);
176                    
177          }          }
178                    
179                    
180            /* these 3 dialogs helper functions are very rude and ugly hack
181             * to remove these auto-reported exceptions
182             * - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running?
183             * - java.lang.IllegalArgumentException: View not attached to window manager
184             */
185            
186            public void showDialogSafe(int id) {
187                    try {
188                            showDialog(id);
189                    } catch (Exception e) {
190                            Log.e("StationList", "showDialog failed", e);
191                    }
192            }
193            
194            public void dismissDialogSafe(int id) {
195                    try {
196                            dismissDialog(id);
197                    } catch (Exception e) {
198                            Log.e("StationList", "dismissDialog failed", e);
199                    }
200            }
201            public void dismissDialogSafe(Dialog dlg) {
202                    try {
203                            dlg.dismiss();
204                    } catch (Exception e) {
205                            Log.e("StationList", "dismissDialog failed", e);
206                    }
207            }
208            
209            public void builderShowSafe(AlertDialog.Builder builder) {
210                    try {
211                            builder.show();
212                    } catch (Exception e) {
213                            Log.e("StationList", "builder.show() failed", e);
214                    }
215                    
216            }
217            /* EOF rude and ugly dialog hack */
218            
219    
220    
221      @Override      @Override
# Line 213  public class StationList extends ListAct Line 275  public class StationList extends ListAct
275                                  message.append( getString(stationlist_nolocation) );                                  message.append( getString(stationlist_nolocation) );
276                          }                                                }                      
277                                                    
278                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString(), false);
279                          break;                          break;
280                  default:                  default:
281                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 270  public class StationList extends ListAct Line 332  public class StationList extends ListAct
332                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
333                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
334                                          dialog.dismiss();                                          dialog.dismiss();
335                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
336                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
337                                                    startNameSearch(search);
338                                          } else {                                          } else {
339                                                  showMessageAndClose( getString(stationlist_twocharmin) );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
340                                          }                                          }
# Line 312  public class StationList extends ListAct Line 375  public class StationList extends ListAct
375                                                                    
376                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
377                                    
378                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
379                  intent.putExtra("stationbean", station);                          Intent i = new Intent();
380                  startActivity(intent);                          i.putExtra("station", station);
381                            setResult(Activity.RESULT_OK, i);
382                            finish();
383                    } else {                
384                            Intent intent = new Intent(this, DepartureList.class);
385                            intent.putExtra("stationbean", station);
386                            startActivity(intent);
387                    }
388          }          }
389    
390          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
# Line 323  public class StationList extends ListAct Line 393  public class StationList extends ListAct
393          public void startLookup() {          public void startLookup() {
394                  isRunning = true;                                isRunning = true;              
395                  dialogMessage = getString( stationlist_waitforlocation );                  dialogMessage = getString( stationlist_waitforlocation );
396                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
397                                    
398                  locationLookup.locateStations();                  locationLookup.locateStations();
399                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS);
400          }          }
401                    
402          void startNameSearch(String name) {          void startNameSearch(String name) {
403                  dialogMessage = getString( stationlist_findbyname );                  dialogMessage = getString( stationlist_findbyname );
404                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
405    
406                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
407                  findStationsTask.searchByName(name, locationLookup.getLocation());                  findStationsTask.searchByName(name);
408                  findStationsTask.execute();                  findStationsTask.execute();
409                                    
410          }          }
# Line 343  public class StationList extends ListAct Line 413  public class StationList extends ListAct
413                                    
414                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
415                          dialogMessage = getString( stationlist_loadfavorites );                          dialogMessage = getString( stationlist_loadfavorites );
416                          showDialog(DLG_PROGRESS);                          showDialogSafe(DLG_PROGRESS);
417    
418                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
419                          findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation());                          findStationsTask.searchByIds( favorites.toString() );
420                          findStationsTask.execute();                          findStationsTask.execute();
421                  } else {                  } else {
422                          showMessageAndClose( getString( stationlist_nofavorites ) );                          showMessageAndClose( getString( stationlist_nofavorites ) );
# Line 358  public class StationList extends ListAct Line 428  public class StationList extends ListAct
428          void startLocatorTask()          void startLocatorTask()
429          {          {
430                  dialogMessage = getString( stationlist_findingnearby );                  dialogMessage = getString( stationlist_findingnearby );
431                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
432                                    
433                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
434                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
435                  findStationsTask.execute();                      findStationsTask.execute();    
436          }          }
           
   
         /* TODO: Remove this no longer needed function  
         String lookupAddress(double latitude, double longitude) {  
                   
                 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);  
                 }  
437                                    
                 return sb.toString();  
         }*/  
           
438                    
439          ////////////////////////////////////////////////////////////////////////////          ////////////////////////////////////////////////////////////////////////////
440          // Inner classes          // Inner classes
# Line 403  public class StationList extends ListAct Line 445  public class StationList extends ListAct
445    
446                          switch (msg.what) {                          switch (msg.what) {
447                          case GOTLOCATION:                          case GOTLOCATION:
448                                  dismissDialog(DLG_PROGRESS);                                  dismissDialogSafe(DLG_PROGRESS);
449                                                                    
450                                  startLocatorTask();                                  startLocatorTask();
451                                  location = GeoPair.fromLocation( locationLookup.getLocation() );                                  location = GeoPair.fromLocation( locationLookup.getLocation() );
# Line 411  public class StationList extends ListAct Line 453  public class StationList extends ListAct
453                                  break;                                  break;
454    
455                          case NOPROVIDER:                          case NOPROVIDER:
456                                  dismissDialog(DLG_PROGRESS);                                  dismissDialogSafe(DLG_PROGRESS);
457                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
458                                    //StationList.this.finish();
459                                  break;                                  break;
460                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
461                                  if (isRunning) {                                  if (isRunning) {
# Line 420  public class StationList extends ListAct Line 463  public class StationList extends ListAct
463                                          if (locationLookup.hasLocation()) {                                          if (locationLookup.hasLocation()) {
464                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
465                                          } else {                                                                                          } else {                                                
466                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialogSafe(DLG_PROGRESS);
467                                                                                                    
468                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
469                                                  builder.setMessage(  getString( stationlist_gpstimeout) );                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
# Line 436  public class StationList extends ListAct Line 479  public class StationList extends ListAct
479                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
480                                                                  dialog.dismiss();                                                                  dialog.dismiss();
481                                                          }                                                                                                                }                                                      
482                                                  });                                                                                                                                              });
483                                                  builder.show();                                                  builderShowSafe(builder); // builder.show()
484    
485                                          }                                          }
486                                  }                                  }
# Line 456  public class StationList extends ListAct Line 499  public class StationList extends ListAct
499                  Location loc;                  Location loc;
500                  String ids;                  String ids;
501                                    
502                  public void searchByName(String n, Location l) {                  public void searchByName(String n) {
503                                                    
504                          method = LookupMethod.ByName;                          method = LookupMethod.ByName;
                         loc = l;  
505                          name = n;                          name = n;
506                  }                  }
507                                    
# Line 468  public class StationList extends ListAct Line 510  public class StationList extends ListAct
510                          loc = l;                          loc = l;
511                  }                  }
512                                    
513                  public void searchByIds(String id, Location l) {                  public void searchByIds(String id) {
514                                                    
515                          method = LookupMethod.ByList;                          method = LookupMethod.ByList;
                         loc = l;  
516                          ids = id;                          ids = id;
517                  }                  }
518                                    
# Line 501  public class StationList extends ListAct Line 542  public class StationList extends ListAct
542                          }                          }
543                                                    
544                                                    
                         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);  
                                         }  
                                 }  
   
                         }                                                
                           
545                          return null;                          return null;
546                  }                  }
547    
548                  @Override                  @Override
549                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
550                          super.onPostExecute(result);                          super.onPostExecute(result);
551                          dialog.dismiss();                          dismissDialogSafe(dialog);
552                                                    
553                                                    
554                          if (success) {                                                    if (success) {                          
555                                  if (stationProvider.getStations().size() == 0)                                  if (stationProvider.getStations().size() == 0) {
556                                          MessageBox.showMessage(StationList.this, getString(stationlist_nostations) ); // this should not be possible !?!                                          showMessageAndClose(getString(stationlist_nostations));
557                                    }
558                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
559    
560                                    StationList.this.getListView().invalidateViews();
561                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
562                                                                    
563                                    
564                          } else { //communication or parse errors                          } else { //communication or parse errors
565                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
566                                  builder.setMessage(getString(stationlist_nearbyerror));                                                          builder.setMessage(getString(stationlist_fetcherror));                          
567                                  builder.setCancelable(true);                                  builder.setCancelable(true);
568                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
569                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
570                                                  dialog.dismiss();                                                  dialog.dismiss();
571                                                                                                    
572                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
573                                                          @Override                                                  switch (method) {
574                                                          public void run() {                                                  case ByLocation:
575                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
576                                                          }                                                                  @Override
577                                                  });                                                                  public void run() {
578                                                                            startLocatorTask();                                                            
579                                                                    }
580                                                            };
581                                                            break;
582                                                    case ByName:
583                                                            runner = new Runnable() {
584                                                                    @Override
585                                                                    public void run() {
586                                                                            startNameSearch( FindStationsTask.this.name );
587                                                                    }
588                                                            };
589                                                            break;
590                                                    case ByList:
591                                                            runner = new Runnable() {
592                                                                    @Override
593                                                                    public void run() {
594                                                                            startFavoriteLookup();
595                                                                    }
596                                                            };
597                                                            break;
598                                                    }
599                                                    
600                                                    stationsFetched.post( runner );
601                                          }                                          }
602                                  });                                  });
603                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
604                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
605                                                  dialog.dismiss();                                                  dialog.dismiss();
606                                                    StationList.this.finish();
607                                          }                                                                                                }                                                      
608                                  });                                                                                                                              });
609                                  builder.show();                                                          
610                                    builderShowSafe(builder); // builder.show()
611                          }                          }
612                  }                  }
613          }          }

Legend:
Removed from v.564  
changed lines
  Added in v.948

  ViewVC Help
Powered by ViewVC 1.1.20