/[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 1028 by torben, Wed Sep 8 06:25: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                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
119                    
120                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
121    
122                                                    
# Line 118  public class StationList extends ListAct Line 124  public class StationList extends ListAct
124                          case ListNearest:                          case ListNearest:
125                                  startLookup();                                  startLookup();
126                                  break;                                  break;
127                          case ListSearch:                          case ListSearch:                                
128                                  this.showDialog(DLG_STATIONNAME);                                  showDialog(DLG_STATIONNAME);
129                                  break;                                  break;
130                          case ListFavorites:                          case ListFavorites:
131                                  startFavoriteLookup();                                  startFavoriteLookup();
# Line 135  public class StationList extends ListAct Line 141  public class StationList extends ListAct
141                  }                  }
142                                    
143          }          }
144            
145            
146    
147    
148    
149    
150            @Override
151            protected void onDestroy() {
152                    super.onDestroy();
153                    
154                    isRunning = false;
155                    
156                    if (locationLookup != null) {
157                            locationLookup.stopSearch();
158                    }
159                    if (findStationsTask != null) {
160                            findStationsTask.cancel(true);
161                    }              
162            }
163    
164            
165          protected void setTitle() {          protected void setTitle() {
166                  String dialogTitle = getResources().getString(app_name);                  String dialogTitle = getResources().getString(app_name);
167                  switch (listType) {                  switch (listType) {
# Line 152  public class StationList extends ListAct Line 179  public class StationList extends ListAct
179                  }                  }
180                    
181                  setTitle(dialogTitle);                  setTitle(dialogTitle);
182                    
183          }          }
184                    
185                            
   
186    
187      @Override      @Override
188      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
# Line 214  public class StationList extends ListAct Line 241  public class StationList extends ListAct
241                                  message.append( getString(stationlist_nolocation) );                                  message.append( getString(stationlist_nolocation) );
242                          }                                                }                      
243                                                    
244                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString(), false);
245                          break;                          break;
246                  default:                  default:
247                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 271  public class StationList extends ListAct Line 298  public class StationList extends ListAct
298                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
299                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
300                                          dialog.dismiss();                                          dialog.dismiss();
301                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
302                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
303                                                    startNameSearch(search);
304                                          } else {                                          } else {
305                                                  showMessageAndClose( getString(stationlist_twocharmin) );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
306                                          }                                          }
# Line 313  public class StationList extends ListAct Line 341  public class StationList extends ListAct
341                                                                    
342                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
343                                    
344                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
345                  intent.putExtra("stationbean", station);                          Intent i = new Intent();
346                  startActivity(intent);                          i.putExtra("station", station);
347                            setResult(Activity.RESULT_OK, i);
348                            finish();
349                    } else {                
350                            Intent intent = new Intent(this, DepartureList.class);
351                            intent.putExtra("stationbean", station);
352                            startActivity(intent);
353                    }
354          }          }
355    
356          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
# Line 327  public class StationList extends ListAct Line 362  public class StationList extends ListAct
362                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
363                                    
364                  locationLookup.locateStations();                  locationLookup.locateStations();
365                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS);
366          }          }
367                    
368          void startNameSearch(String name) {          void startNameSearch(String name) {
# Line 385  public class StationList extends ListAct Line 420  public class StationList extends ListAct
420    
421                          case NOPROVIDER:                          case NOPROVIDER:
422                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
423                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
424                                    //StationList.this.finish();
425                                  break;                                  break;
426                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
427                                  if (isRunning) {                                  if (isRunning) {
# Line 409  public class StationList extends ListAct Line 445  public class StationList extends ListAct
445                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
446                                                                  dialog.dismiss();                                                                  dialog.dismiss();
447                                                          }                                                                                                                }                                                      
448                                                  });                                                                                                                                              });
449                                                  builder.show();                                                  builder.show();
450    
451                                          }                                          }
452                                  }                                  }
# Line 424  public class StationList extends ListAct Line 460  public class StationList extends ListAct
460          class FindStationsTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
461                                    
462                  LookupMethod method = LookupMethod.MethodNone;                  LookupMethod method = LookupMethod.MethodNone;
                 boolean success;  
463                  String name;                  String name;
464                  Location loc;                  Location loc;
465                  String ids;                  String ids;
# Line 459  public class StationList extends ListAct Line 494  public class StationList extends ListAct
494    
495                          switch (method) {                          switch (method) {
496                          case ByLocation:                          case ByLocation:
497                                  success = stationProvider.lookupStations(loc);                                  stations = stationProvider.lookupStations(loc);
498                                  break;                                  break;
499                          case ByName:                          case ByName:
500                                  success = stationProvider.lookupStationsByName(name);                                  stations = stationProvider.lookupStationsByName(name);
501                                  break;                                  break;
502                          case ByList:                          case ByList:
503                                  success = stationProvider.lookupStationsByIds(ids);                                  stations = stationProvider.lookupStationsByIds(ids);
504                                  break;                                  break;
505                          default:                          default:
506                                  success = false; // not possible                                          stations = null; // not possible        
507                          }                          }
508                                                    
509                                                    
# Line 481  public class StationList extends ListAct Line 516  public class StationList extends ListAct
516                          dialog.dismiss();                          dialog.dismiss();
517                                                    
518                                                    
519                          if (success) {                                                    if (stations != null) {                        
520                                  if (stationProvider.getStations().size() == 0) {                                  if (stations.size() == 0) {
521                                          showMessageAndClose(getString(stationlist_nostations));                                          showMessageAndClose(getString(stationlist_nostations));
522                                  }                                  }
523                                  stations = stationProvider.getStations();  
524                                    StationList.this.getListView().invalidateViews();
525                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
526                                                                    
527                                    
528                          } else { //communication or parse errors                          } else { //communication or parse errors
529                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
530                                  builder.setMessage(getString(stationlist_nearbyerror));                                                          builder.setMessage(getString(stationlist_fetcherror));                          
531                                  builder.setCancelable(true);                                  builder.setCancelable(true);
532                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
533                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
534                                                  dialog.dismiss();                                                  dialog.dismiss();
535                                                                                                    
536                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
537                                                          @Override                                                  switch (method) {
538                                                          public void run() {                                                  case ByLocation:
539                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
540                                                          }                                                                  @Override
541                                                  });                                                                  public void run() {
542                                                                            startLocatorTask();                                                            
543                                                                    }
544                                                            };
545                                                            break;
546                                                    case ByName:
547                                                            runner = new Runnable() {
548                                                                    @Override
549                                                                    public void run() {
550                                                                            startNameSearch( FindStationsTask.this.name );
551                                                                    }
552                                                            };
553                                                            break;
554                                                    case ByList:
555                                                            runner = new Runnable() {
556                                                                    @Override
557                                                                    public void run() {
558                                                                            startFavoriteLookup();
559                                                                    }
560                                                            };
561                                                            break;
562                                                    }
563                                                    
564                                                    stationsFetched.post( runner );
565                                          }                                          }
566                                  });                                  });
567                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
568                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
569                                                  dialog.dismiss();                                                  dialog.dismiss();
570                                                    StationList.this.finish();
571                                          }                                                                                                }                                                      
572                                  });                                                      });
573                                  try {                                  
574                                          builder.show();                                  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  
                                 }  
575                          }                          }
576                  }                  }
577          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.20