/[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 725 by torben, Tue May 11 05:38:18 2010 UTC revision 1019 by torben, Wed Aug 25 06:18:13 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    
 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 47  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 69  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 111  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 122  public class StationList extends ListAct
122                          case ListNearest:                          case ListNearest:
123                                  startLookup();                                  startLookup();
124                                  break;                                  break;
125                          case ListSearch:                          case ListSearch:                                
126                                  this.showDialogSafe(DLG_STATIONNAME);                                  showDialog(DLG_STATIONNAME);
127                                  break;                                  break;
128                          case ListFavorites:                          case ListFavorites:
129                                  startFavoriteLookup();                                  startFavoriteLookup();
# Line 135  public class StationList extends ListAct Line 139  public class StationList extends ListAct
139                  }                  }
140                                    
141          }          }
142            
143    
144            @Override
145            protected void onDestroy() {
146                    super.onDestroy();
147                    
148                    if (findStationsTask != null) {
149                            findStationsTask.cancel(true);
150                    }
151                    if (locationLookup != null) {
152                            locationLookup.stopSearch();
153                    }
154                    isRunning = false;
155            }
156    
157            
158          protected void setTitle() {          protected void setTitle() {
159                  String dialogTitle = getResources().getString(app_name);                  String dialogTitle = getResources().getString(app_name);
160                  switch (listType) {                  switch (listType) {
# Line 155  public class StationList extends ListAct Line 175  public class StationList extends ListAct
175                                    
176          }          }
177                    
           
         /* these 3 dialogs helper functions are very rude and ugly hack  
          * to remove these auto-reported exceptions  
          * - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running?  
          * - java.lang.IllegalArgumentException: View not attached to window manager  
          */  
           
         public void showDialogSafe(int id) {  
                 try {  
                         showDialog(id);  
                 } catch (Exception e) {  
                         Log.e("StationList", "showDialog failed", e);  
                 }  
         }  
           
         public void dismissDialogSafe(int id) {  
                 try {  
                         dismissDialog(id);  
                 } catch (Exception e) {  
                         Log.e("StationList", "dismissDialog failed", e);  
                 }  
         }  
         public void dismissDialogSafe(Dialog dlg) {  
                 try {  
                         dlg.dismiss();  
                 } catch (Exception e) {  
                         Log.e("StationList", "dismissDialog failed", e);  
                 }  
         }  
           
         public void builderShowSafe(AlertDialog.Builder builder) {  
                 try {  
                         builder.show();  
                 } catch (Exception e) {  
                         Log.e("StationList", "builder.show() failed", e);  
                 }  
178                                    
         }  
         /* EOF rude and ugly dialog hack */  
           
   
179    
180      @Override      @Override
181      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
# Line 254  public class StationList extends ListAct Line 234  public class StationList extends ListAct
234                                  message.append( getString(stationlist_nolocation) );                                  message.append( getString(stationlist_nolocation) );
235                          }                                                }                      
236                                                    
237                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString(), false);
238                          break;                          break;
239                  default:                  default:
240                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 354  public class StationList extends ListAct Line 334  public class StationList extends ListAct
334                                                                    
335                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
336                                    
337                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
338                  intent.putExtra("stationbean", station);                          Intent i = new Intent();
339                  startActivity(intent);                          i.putExtra("station", station);
340                            setResult(Activity.RESULT_OK, i);
341                            finish();
342                    } else {                
343                            Intent intent = new Intent(this, DepartureList.class);
344                            intent.putExtra("stationbean", station);
345                            startActivity(intent);
346                    }
347          }          }
348    
349          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
# Line 365  public class StationList extends ListAct Line 352  public class StationList extends ListAct
352          public void startLookup() {          public void startLookup() {
353                  isRunning = true;                                isRunning = true;              
354                  dialogMessage = getString( stationlist_waitforlocation );                  dialogMessage = getString( stationlist_waitforlocation );
355                  showDialogSafe(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
356                                    
357                  locationLookup.locateStations();                  locationLookup.locateStations();
358                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS);
359          }          }
360                    
361          void startNameSearch(String name) {          void startNameSearch(String name) {
362                  dialogMessage = getString( stationlist_findbyname );                  dialogMessage = getString( stationlist_findbyname );
363                  showDialogSafe(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
364    
365                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
366                  findStationsTask.searchByName(name);                  findStationsTask.searchByName(name);
# Line 385  public class StationList extends ListAct Line 372  public class StationList extends ListAct
372                                    
373                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
374                          dialogMessage = getString( stationlist_loadfavorites );                          dialogMessage = getString( stationlist_loadfavorites );
375                          showDialogSafe(DLG_PROGRESS);                          showDialog(DLG_PROGRESS);
376    
377                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
378                          findStationsTask.searchByIds( favorites.toString() );                          findStationsTask.searchByIds( favorites.toString() );
# Line 400  public class StationList extends ListAct Line 387  public class StationList extends ListAct
387          void startLocatorTask()          void startLocatorTask()
388          {          {
389                  dialogMessage = getString( stationlist_findingnearby );                  dialogMessage = getString( stationlist_findingnearby );
390                  showDialogSafe(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
391                                    
392                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
393                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
# Line 417  public class StationList extends ListAct Line 404  public class StationList extends ListAct
404    
405                          switch (msg.what) {                          switch (msg.what) {
406                          case GOTLOCATION:                          case GOTLOCATION:
407                                  dismissDialogSafe(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
408                                                                    
409                                  startLocatorTask();                                  startLocatorTask();
410                                  location = GeoPair.fromLocation( locationLookup.getLocation() );                                  location = GeoPair.fromLocation( locationLookup.getLocation() );
# Line 425  public class StationList extends ListAct Line 412  public class StationList extends ListAct
412                                  break;                                  break;
413    
414                          case NOPROVIDER:                          case NOPROVIDER:
415                                  dismissDialogSafe(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
416                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
417                                    //StationList.this.finish();
418                                  break;                                  break;
419                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
420                                  if (isRunning) {                                  if (isRunning) {
# Line 434  public class StationList extends ListAct Line 422  public class StationList extends ListAct
422                                          if (locationLookup.hasLocation()) {                                          if (locationLookup.hasLocation()) {
423                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
424                                          } else {                                                                                          } else {                                                
425                                                  dismissDialogSafe(DLG_PROGRESS);                                                  dismissDialog(DLG_PROGRESS);
426                                                                                                    
427                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
428                                                  builder.setMessage(  getString( stationlist_gpstimeout) );                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
# Line 451  public class StationList extends ListAct Line 439  public class StationList extends ListAct
439                                                                  dialog.dismiss();                                                                  dialog.dismiss();
440                                                          }                                                                                                                }                                                      
441                                                  });                                                  });
442                                                  builderShowSafe(builder); // builder.show()                                                  builder.show();
443    
444                                          }                                          }
445                                  }                                  }
# Line 465  public class StationList extends ListAct Line 453  public class StationList extends ListAct
453          class FindStationsTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
454                                    
455                  LookupMethod method = LookupMethod.MethodNone;                  LookupMethod method = LookupMethod.MethodNone;
                 boolean success;  
456                  String name;                  String name;
457                  Location loc;                  Location loc;
458                  String ids;                  String ids;
# Line 500  public class StationList extends ListAct Line 487  public class StationList extends ListAct
487    
488                          switch (method) {                          switch (method) {
489                          case ByLocation:                          case ByLocation:
490                                  success = stationProvider.lookupStations(loc);                                  stations = stationProvider.lookupStations(loc);
491                                  break;                                  break;
492                          case ByName:                          case ByName:
493                                  success = stationProvider.lookupStationsByName(name);                                  stations = stationProvider.lookupStationsByName(name);
494                                  break;                                  break;
495                          case ByList:                          case ByList:
496                                  success = stationProvider.lookupStationsByIds(ids);                                  stations = stationProvider.lookupStationsByIds(ids);
497                                  break;                                  break;
498                          default:                          default:
499                                  success = false; // not possible                                          stations = null; // not possible        
500                          }                          }
501                                                    
502                                                    
# Line 519  public class StationList extends ListAct Line 506  public class StationList extends ListAct
506                  @Override                  @Override
507                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
508                          super.onPostExecute(result);                          super.onPostExecute(result);
509                          dismissDialogSafe(dialog);                          dialog.dismiss();
510                                                    
511                                                    
512                          if (success) {                                                    if (stations != null) {                        
513                                  if (stationProvider.getStations().size() == 0) {                                  if (stations.size() == 0) {
514                                          showMessageAndClose(getString(stationlist_nostations));                                          showMessageAndClose(getString(stationlist_nostations));
515                                  }                                  }
516                                  stations = stationProvider.getStations();  
517                                    StationList.this.getListView().invalidateViews();
518                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
519                                                                    
520                                    
521                          } else { //communication or parse errors                          } else { //communication or parse errors
522                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
523                                  builder.setMessage(getString(stationlist_nearbyerror));                                                          builder.setMessage(getString(stationlist_fetcherror));                          
524                                  builder.setCancelable(true);                                  builder.setCancelable(true);
525                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
526                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
# Line 571  public class StationList extends ListAct Line 560  public class StationList extends ListAct
560                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
561                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
562                                                  dialog.dismiss();                                                  dialog.dismiss();
563                                                    StationList.this.finish();
564                                          }                                                                                                }                                                      
565                                  });                                  });
566                                                                    
567                                  builderShowSafe(builder); // builder.show()                                  builder.show();
568                          }                          }
569                  }                  }
570          }          }

Legend:
Removed from v.725  
changed lines
  Added in v.1019

  ViewVC Help
Powered by ViewVC 1.1.20