/[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 652 by torben, Tue Apr 20 14:17:34 2010 UTC revision 918 by torben, Sat Jun 26 11:02:53 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 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            }
152    
153            
154          protected void setTitle() {          protected void setTitle() {
155                  String dialogTitle = getResources().getString(app_name);                  String dialogTitle = getResources().getString(app_name);
156                  switch (listType) {                  switch (listType) {
# Line 152  public class StationList extends ListAct Line 168  public class StationList extends ListAct
168                  }                  }
169                    
170                  setTitle(dialogTitle);                  setTitle(dialogTitle);
171                    
172          }          }
173                    
174                    
175            /* these 3 dialogs helper functions are very rude and ugly hack
176             * to remove these auto-reported exceptions
177             * - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running?
178             * - java.lang.IllegalArgumentException: View not attached to window manager
179             */
180            
181            public void showDialogSafe(int id) {
182                    try {
183                            showDialog(id);
184                    } catch (Exception e) {
185                            Log.e("StationList", "showDialog failed", e);
186                    }
187            }
188            
189            public void dismissDialogSafe(int id) {
190                    try {
191                            dismissDialog(id);
192                    } catch (Exception e) {
193                            Log.e("StationList", "dismissDialog failed", e);
194                    }
195            }
196            public void dismissDialogSafe(Dialog dlg) {
197                    try {
198                            dlg.dismiss();
199                    } catch (Exception e) {
200                            Log.e("StationList", "dismissDialog failed", e);
201                    }
202            }
203            
204            public void builderShowSafe(AlertDialog.Builder builder) {
205                    try {
206                            builder.show();
207                    } catch (Exception e) {
208                            Log.e("StationList", "builder.show() failed", e);
209                    }
210                    
211            }
212            /* EOF rude and ugly dialog hack */
213            
214    
215    
216      @Override      @Override
# Line 214  public class StationList extends ListAct Line 270  public class StationList extends ListAct
270                                  message.append( getString(stationlist_nolocation) );                                  message.append( getString(stationlist_nolocation) );
271                          }                                                }                      
272                                                    
273                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString(), false);
274                          break;                          break;
275                  default:                  default:
276                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 271  public class StationList extends ListAct Line 327  public class StationList extends ListAct
327                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
328                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
329                                          dialog.dismiss();                                          dialog.dismiss();
330                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
331                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
332                                                    startNameSearch(search);
333                                          } else {                                          } else {
334                                                  showMessageAndClose( getString(stationlist_twocharmin) );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
335                                          }                                          }
# Line 313  public class StationList extends ListAct Line 370  public class StationList extends ListAct
370                                                                    
371                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
372                                    
373                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
374                  intent.putExtra("stationbean", station);                          Intent i = new Intent();
375                  startActivity(intent);                          i.putExtra("station", station);
376                            setResult(Activity.RESULT_OK, i);
377                            finish();
378                    } else {                
379                            Intent intent = new Intent(this, DepartureList.class);
380                            intent.putExtra("stationbean", station);
381                            startActivity(intent);
382                    }
383          }          }
384    
385          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
# Line 324  public class StationList extends ListAct Line 388  public class StationList extends ListAct
388          public void startLookup() {          public void startLookup() {
389                  isRunning = true;                                isRunning = true;              
390                  dialogMessage = getString( stationlist_waitforlocation );                  dialogMessage = getString( stationlist_waitforlocation );
391                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
392                                    
393                  locationLookup.locateStations();                  locationLookup.locateStations();
394                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
# Line 332  public class StationList extends ListAct Line 396  public class StationList extends ListAct
396                    
397          void startNameSearch(String name) {          void startNameSearch(String name) {
398                  dialogMessage = getString( stationlist_findbyname );                  dialogMessage = getString( stationlist_findbyname );
399                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
400    
401                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
402                  findStationsTask.searchByName(name);                  findStationsTask.searchByName(name);
# Line 344  public class StationList extends ListAct Line 408  public class StationList extends ListAct
408                                    
409                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
410                          dialogMessage = getString( stationlist_loadfavorites );                          dialogMessage = getString( stationlist_loadfavorites );
411                          showDialog(DLG_PROGRESS);                          showDialogSafe(DLG_PROGRESS);
412    
413                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
414                          findStationsTask.searchByIds( favorites.toString() );                          findStationsTask.searchByIds( favorites.toString() );
# Line 359  public class StationList extends ListAct Line 423  public class StationList extends ListAct
423          void startLocatorTask()          void startLocatorTask()
424          {          {
425                  dialogMessage = getString( stationlist_findingnearby );                  dialogMessage = getString( stationlist_findingnearby );
426                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
427                                    
428                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
429                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
# Line 376  public class StationList extends ListAct Line 440  public class StationList extends ListAct
440    
441                          switch (msg.what) {                          switch (msg.what) {
442                          case GOTLOCATION:                          case GOTLOCATION:
443                                  dismissDialog(DLG_PROGRESS);                                  dismissDialogSafe(DLG_PROGRESS);
444                                                                    
445                                  startLocatorTask();                                  startLocatorTask();
446                                  location = GeoPair.fromLocation( locationLookup.getLocation() );                                  location = GeoPair.fromLocation( locationLookup.getLocation() );
# Line 384  public class StationList extends ListAct Line 448  public class StationList extends ListAct
448                                  break;                                  break;
449    
450                          case NOPROVIDER:                          case NOPROVIDER:
451                                  dismissDialog(DLG_PROGRESS);                                  dismissDialogSafe(DLG_PROGRESS);
452                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
453                                    //StationList.this.finish();
454                                  break;                                  break;
455                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
456                                  if (isRunning) {                                  if (isRunning) {
# Line 393  public class StationList extends ListAct Line 458  public class StationList extends ListAct
458                                          if (locationLookup.hasLocation()) {                                          if (locationLookup.hasLocation()) {
459                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
460                                          } else {                                                                                          } else {                                                
461                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialogSafe(DLG_PROGRESS);
462                                                                                                    
463                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
464                                                  builder.setMessage(  getString( stationlist_gpstimeout) );                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
# Line 409  public class StationList extends ListAct Line 474  public class StationList extends ListAct
474                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
475                                                                  dialog.dismiss();                                                                  dialog.dismiss();
476                                                          }                                                                                                                }                                                      
477                                                  });                                                                                                                                              });
478                                                  builder.show();                                                  builderShowSafe(builder); // builder.show()
479    
480                                          }                                          }
481                                  }                                  }
# Line 478  public class StationList extends ListAct Line 543  public class StationList extends ListAct
543                  @Override                  @Override
544                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
545                          super.onPostExecute(result);                          super.onPostExecute(result);
546                          dialog.dismiss();                          dismissDialogSafe(dialog);
547                                                    
548                                                    
549                          if (success) {                                                    if (success) {                          
# Line 486  public class StationList extends ListAct Line 551  public class StationList extends ListAct
551                                          showMessageAndClose(getString(stationlist_nostations));                                          showMessageAndClose(getString(stationlist_nostations));
552                                  }                                  }
553                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
554    
555                                    StationList.this.getListView().invalidateViews();
556                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
557                                                                    
558                                    
559                          } else { //communication or parse errors                          } else { //communication or parse errors
560                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
561                                  builder.setMessage(getString(stationlist_nearbyerror));                                                          builder.setMessage(getString(stationlist_fetcherror));                          
562                                  builder.setCancelable(true);                                  builder.setCancelable(true);
563                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
564                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
# Line 530  public class StationList extends ListAct Line 598  public class StationList extends ListAct
598                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
599                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
600                                                  dialog.dismiss();                                                  dialog.dismiss();
601                                                    StationList.this.finish();
602                                          }                                                                                                }                                                      
603                                  });                                                      });
604                                  try {                                  
605                                          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  
                                 }  
606                          }                          }
607                  }                  }
608          }          }

Legend:
Removed from v.652  
changed lines
  Added in v.918

  ViewVC Help
Powered by ViewVC 1.1.20