/[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 630 by torben, Wed Mar 17 15:27:11 2010 UTC revision 917 by torben, Sat Jun 26 10:50:34 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 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 119  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 152  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 214  public class StationList extends ListAct Line 258  public class StationList extends ListAct
258                                  message.append( getString(stationlist_nolocation) );                                  message.append( getString(stationlist_nolocation) );
259                          }                                                }                      
260                                                    
261                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString(), false);
262                          break;                          break;
263                  default:                  default:
264                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 271  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 313  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 324  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 332  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);                  findStationsTask.searchByName(name);
# Line 344  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() );                          findStationsTask.searchByIds( favorites.toString() );
# Line 359  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 376  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 384  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), true );
441                                    //StationList.this.finish();
442                                  break;                                  break;
443                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
444                                  if (isRunning) {                                  if (isRunning) {
# Line 393  public class StationList extends ListAct Line 446  public class StationList extends ListAct
446                                          if (locationLookup.hasLocation()) {                                          if (locationLookup.hasLocation()) {
447                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
448                                          } else {                                                                                          } else {                                                
449                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialogSafe(DLG_PROGRESS);
450                                                                                                    
451                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
452                                                  builder.setMessage(  getString( stationlist_gpstimeout) );                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
# Line 409  public class StationList extends ListAct Line 462  public class StationList extends ListAct
462                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
463                                                                  dialog.dismiss();                                                                  dialog.dismiss();
464                                                          }                                                                                                                }                                                      
465                                                  });                                                                                                                                              });
466                                                  builder.show();                                                  builderShowSafe(builder); // builder.show()
467    
468                                          }                                          }
469                                  }                                  }
# Line 478  public class StationList extends ListAct Line 531  public class StationList extends ListAct
531                  @Override                  @Override
532                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
533                          super.onPostExecute(result);                          super.onPostExecute(result);
534                          dialog.dismiss();                          dismissDialogSafe(dialog);
535                                                    
536                                                    
537                          if (success) {                                                    if (success) {                          
# Line 486  public class StationList extends ListAct Line 539  public class StationList extends ListAct
539                                          showMessageAndClose(getString(stationlist_nostations));                                          showMessageAndClose(getString(stationlist_nostations));
540                                  }                                  }
541                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
542    
543                                    StationList.this.getListView().invalidateViews();
544                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
545                                                                    
546                                    
547                          } else { //communication or parse errors                          } else { //communication or parse errors
548                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
549                                  builder.setMessage(getString(stationlist_nearbyerror));                                                          builder.setMessage(getString(stationlist_fetcherror));                          
550                                  builder.setCancelable(true);                                  builder.setCancelable(true);
551                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
552                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
553                                                  dialog.dismiss();                                                  dialog.dismiss();
554                                                                                                    
555                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
556                                                          @Override                                                  switch (method) {
557                                                          public void run() {                                                  case ByLocation:
558                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
559                                                          }                                                                  @Override
560                                                  });                                                                  public void run() {
561                                                                            startLocatorTask();                                                            
562                                                                    }
563                                                            };
564                                                            break;
565                                                    case ByName:
566                                                            runner = new Runnable() {
567                                                                    @Override
568                                                                    public void run() {
569                                                                            startNameSearch( FindStationsTask.this.name );
570                                                                    }
571                                                            };
572                                                            break;
573                                                    case ByList:
574                                                            runner = new Runnable() {
575                                                                    @Override
576                                                                    public void run() {
577                                                                            startFavoriteLookup();
578                                                                    }
579                                                            };
580                                                            break;
581                                                    }
582                                                    
583                                                    stationsFetched.post( runner );
584                                          }                                          }
585                                  });                                  });
586                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
587                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
588                                                  dialog.dismiss();                                                  dialog.dismiss();
589                                                    StationList.this.finish();
590                                          }                                                                                                }                                                      
591                                  });                                                      });
592                                  try {                                  
593                                          builder.show();                                  builderShowSafe(builder); // builder.show()
                                 } catch (android.view.WindowManager.BadTokenException e) {                                        
                                         Log.i("StationList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running  
                                 }  
594                          }                          }
595                  }                  }
596          }          }

Legend:
Removed from v.630  
changed lines
  Added in v.917

  ViewVC Help
Powered by ViewVC 1.1.20