/[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 579 by torben, Tue Feb 2 08:52:45 2010 UTC revision 919 by torben, Sat Jun 26 11:06:12 2010 UTC
# Line 4  import java.util.ArrayList; Line 4  import java.util.ArrayList;
4  import java.util.List;  import java.util.List;
5    
6    
7    import android.app.Activity;
8  import android.app.AlertDialog;  import android.app.AlertDialog;
9  import android.app.Dialog;  import android.app.Dialog;
10  import android.app.ListActivity;  import android.app.ListActivity;
# Line 18  import android.os.Bundle; Line 19  import android.os.Bundle;
19  import android.os.Handler;  import android.os.Handler;
20  import android.os.Message;  import android.os.Message;
21    
22    import android.util.Log;
23  import android.view.ContextMenu;  import android.view.ContextMenu;
24  import android.view.LayoutInflater;  import android.view.LayoutInflater;
25  import android.view.Menu;  import android.view.Menu;
# Line 68  public class StationList extends ListAct Line 70  public class StationList extends ListAct
70          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
71                    
72          GeoPair location = new GeoPair();          GeoPair location = new GeoPair();
73            
74            boolean isLaunchedforShortcut;
75          boolean isRunning = false;          boolean isRunning = false;
76          List<StationBean> stations = new ArrayList<StationBean>();          List<StationBean> stations = new ArrayList<StationBean>();
77                    
# Line 110  public class StationList extends ListAct Line 113  public class StationList extends ListAct
113                  listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");                  listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
114                  setTitle();                  setTitle();
115                                    
116                    isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);
117                    
118                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
119    
120                                                    
# Line 118  public class StationList extends ListAct Line 123  public class StationList extends ListAct
123                                  startLookup();                                  startLookup();
124                                  break;                                  break;
125                          case ListSearch:                          case ListSearch:
126                                  this.showDialog(DLG_STATIONNAME);                                  this.showDialogSafe(DLG_STATIONNAME);
127                                  break;                                  break;
128                          case ListFavorites:                          case ListFavorites:
129                                  startFavoriteLookup();                                  startFavoriteLookup();
# Line 134  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 151  public class StationList extends ListAct Line 172  public class StationList extends ListAct
172                  }                  }
173                    
174                  setTitle(dialogTitle);                  setTitle(dialogTitle);
175                    
176            }
177            
178            
179            /* these 3 dialogs helper functions are very rude and ugly hack
180             * to remove these auto-reported exceptions
181             * - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running?
182             * - java.lang.IllegalArgumentException: View not attached to window manager
183             */
184            
185            public void showDialogSafe(int id) {
186                    try {
187                            showDialog(id);
188                    } catch (Exception e) {
189                            Log.e("StationList", "showDialog failed", e);
190                    }
191          }          }
192                    
193            public void dismissDialogSafe(int id) {
194                    try {
195                            dismissDialog(id);
196                    } catch (Exception e) {
197                            Log.e("StationList", "dismissDialog failed", e);
198                    }
199            }
200            public void dismissDialogSafe(Dialog dlg) {
201                    try {
202                            dlg.dismiss();
203                    } catch (Exception e) {
204                            Log.e("StationList", "dismissDialog failed", e);
205                    }
206            }
207            
208            public void builderShowSafe(AlertDialog.Builder builder) {
209                    try {
210                            builder.show();
211                    } catch (Exception e) {
212                            Log.e("StationList", "builder.show() failed", e);
213                    }
214                    
215            }
216            /* EOF rude and ugly dialog hack */
217                    
218    
219    
# Line 213  public class StationList extends ListAct Line 274  public class StationList extends ListAct
274                                  message.append( getString(stationlist_nolocation) );                                  message.append( getString(stationlist_nolocation) );
275                          }                                                }                      
276                                                    
277                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString(), false);
278                          break;                          break;
279                  default:                  default:
280                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 270  public class StationList extends ListAct Line 331  public class StationList extends ListAct
331                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
332                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
333                                          dialog.dismiss();                                          dialog.dismiss();
334                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
335                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
336                                                    startNameSearch(search);
337                                          } else {                                          } else {
338                                                  showMessageAndClose( getString(stationlist_twocharmin) );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
339                                          }                                          }
# Line 312  public class StationList extends ListAct Line 374  public class StationList extends ListAct
374                                                                    
375                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
376                                    
377                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
378                  intent.putExtra("stationbean", station);                          Intent i = new Intent();
379                  startActivity(intent);                          i.putExtra("station", station);
380                            setResult(Activity.RESULT_OK, i);
381                            finish();
382                    } else {                
383                            Intent intent = new Intent(this, DepartureList.class);
384                            intent.putExtra("stationbean", station);
385                            startActivity(intent);
386                    }
387          }          }
388    
389          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
# Line 323  public class StationList extends ListAct Line 392  public class StationList extends ListAct
392          public void startLookup() {          public void startLookup() {
393                  isRunning = true;                                isRunning = true;              
394                  dialogMessage = getString( stationlist_waitforlocation );                  dialogMessage = getString( stationlist_waitforlocation );
395                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
396                                    
397                  locationLookup.locateStations();                  locationLookup.locateStations();
398                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
# Line 331  public class StationList extends ListAct Line 400  public class StationList extends ListAct
400                    
401          void startNameSearch(String name) {          void startNameSearch(String name) {
402                  dialogMessage = getString( stationlist_findbyname );                  dialogMessage = getString( stationlist_findbyname );
403                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
404    
405                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
406                  findStationsTask.searchByName(name);                  findStationsTask.searchByName(name);
# Line 343  public class StationList extends ListAct Line 412  public class StationList extends ListAct
412                                    
413                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
414                          dialogMessage = getString( stationlist_loadfavorites );                          dialogMessage = getString( stationlist_loadfavorites );
415                          showDialog(DLG_PROGRESS);                          showDialogSafe(DLG_PROGRESS);
416    
417                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
418                          findStationsTask.searchByIds( favorites.toString() );                          findStationsTask.searchByIds( favorites.toString() );
# Line 358  public class StationList extends ListAct Line 427  public class StationList extends ListAct
427          void startLocatorTask()          void startLocatorTask()
428          {          {
429                  dialogMessage = getString( stationlist_findingnearby );                  dialogMessage = getString( stationlist_findingnearby );
430                  showDialog(DLG_PROGRESS);                  showDialogSafe(DLG_PROGRESS);
431                                    
432                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
433                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
# Line 375  public class StationList extends ListAct Line 444  public class StationList extends ListAct
444    
445                          switch (msg.what) {                          switch (msg.what) {
446                          case GOTLOCATION:                          case GOTLOCATION:
447                                  dismissDialog(DLG_PROGRESS);                                  dismissDialogSafe(DLG_PROGRESS);
448                                                                    
449                                  startLocatorTask();                                  startLocatorTask();
450                                  location = GeoPair.fromLocation( locationLookup.getLocation() );                                  location = GeoPair.fromLocation( locationLookup.getLocation() );
# Line 383  public class StationList extends ListAct Line 452  public class StationList extends ListAct
452                                  break;                                  break;
453    
454                          case NOPROVIDER:                          case NOPROVIDER:
455                                  dismissDialog(DLG_PROGRESS);                                  dismissDialogSafe(DLG_PROGRESS);
456                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
457                                    //StationList.this.finish();
458                                  break;                                  break;
459                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
460                                  if (isRunning) {                                  if (isRunning) {
# Line 392  public class StationList extends ListAct Line 462  public class StationList extends ListAct
462                                          if (locationLookup.hasLocation()) {                                          if (locationLookup.hasLocation()) {
463                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
464                                          } else {                                                                                          } else {                                                
465                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialogSafe(DLG_PROGRESS);
466                                                                                                    
467                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
468                                                  builder.setMessage(  getString( stationlist_gpstimeout) );                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
# Line 408  public class StationList extends ListAct Line 478  public class StationList extends ListAct
478                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
479                                                                  dialog.dismiss();                                                                  dialog.dismiss();
480                                                          }                                                                                                                }                                                      
481                                                  });                                                                                                                                              });
482                                                  builder.show();                                                  builderShowSafe(builder); // builder.show()
483    
484                                          }                                          }
485                                  }                                  }
# Line 477  public class StationList extends ListAct Line 547  public class StationList extends ListAct
547                  @Override                  @Override
548                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
549                          super.onPostExecute(result);                          super.onPostExecute(result);
550                          dialog.dismiss();                          dismissDialogSafe(dialog);
551                                                    
552                                                    
553                          if (success) {                                                    if (success) {                          
# Line 485  public class StationList extends ListAct Line 555  public class StationList extends ListAct
555                                          showMessageAndClose(getString(stationlist_nostations));                                          showMessageAndClose(getString(stationlist_nostations));
556                                  }                                  }
557                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
558    
559                                    StationList.this.getListView().invalidateViews();
560                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
561                                                                    
562                                    
563                          } else { //communication or parse errors                          } else { //communication or parse errors
564                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
565                                  builder.setMessage(getString(stationlist_nearbyerror));                                                          builder.setMessage(getString(stationlist_fetcherror));                          
566                                  builder.setCancelable(true);                                  builder.setCancelable(true);
567                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
568                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
569                                                  dialog.dismiss();                                                  dialog.dismiss();
570                                                                                                    
571                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
572                                                          @Override                                                  switch (method) {
573                                                          public void run() {                                                  case ByLocation:
574                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
575                                                          }                                                                  @Override
576                                                  });                                                                  public void run() {
577                                                                            startLocatorTask();                                                            
578                                                                    }
579                                                            };
580                                                            break;
581                                                    case ByName:
582                                                            runner = new Runnable() {
583                                                                    @Override
584                                                                    public void run() {
585                                                                            startNameSearch( FindStationsTask.this.name );
586                                                                    }
587                                                            };
588                                                            break;
589                                                    case ByList:
590                                                            runner = new Runnable() {
591                                                                    @Override
592                                                                    public void run() {
593                                                                            startFavoriteLookup();
594                                                                    }
595                                                            };
596                                                            break;
597                                                    }
598                                                    
599                                                    stationsFetched.post( runner );
600                                          }                                          }
601                                  });                                  });
602                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
603                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
604                                                  dialog.dismiss();                                                  dialog.dismiss();
605                                                    StationList.this.finish();
606                                          }                                                                                                }                                                      
607                                  });                                                                                                                              });
608                                  builder.show();                                                          
609                                    builderShowSafe(builder); // builder.show()
610                          }                          }
611                  }                  }
612          }          }

Legend:
Removed from v.579  
changed lines
  Added in v.919

  ViewVC Help
Powered by ViewVC 1.1.20