/[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 1006 by torben, Mon Aug 2 23:18:53 2010 UTC revision 1558 by torben, Fri Jul 8 13:10:53 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
3  import java.util.ArrayList;  import static dk.thoerup.traininfo.R.string.app_name;
4  import java.util.List;  import static dk.thoerup.traininfo.R.string.generic_cancel;
5    import static dk.thoerup.traininfo.R.string.generic_retry;
6    import static dk.thoerup.traininfo.R.string.stationlist_accuracy;
7    import static dk.thoerup.traininfo.R.string.stationlist_addfavorite;
8    import static dk.thoerup.traininfo.R.string.stationlist_favorites;
9    import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo;
10    import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout;
11    import static dk.thoerup.traininfo.R.string.stationlist_latitude;
12    import static dk.thoerup.traininfo.R.string.stationlist_locationinfo;
13    import static dk.thoerup.traininfo.R.string.stationlist_longitude;
14    import static dk.thoerup.traininfo.R.string.stationlist_nearbystations;
15    import static dk.thoerup.traininfo.R.string.stationlist_nofavorites;
16    import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider;
17    import static dk.thoerup.traininfo.R.string.stationlist_obtainedby;
18    import static dk.thoerup.traininfo.R.string.stationlist_removefavorite;
19    import static dk.thoerup.traininfo.R.string.stationlist_satellitecount;
20    import static dk.thoerup.traininfo.R.string.stationlist_search;
21    import static dk.thoerup.traininfo.R.string.stationlist_stationadded;
22    import static dk.thoerup.traininfo.R.string.stationlist_stationmap;
23    import static dk.thoerup.traininfo.R.string.stationlist_stationremoved;
24    import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation;
25    
26    import java.util.ArrayList;
27    
28  import android.app.Activity;  import android.app.Activity;
29  import android.app.AlertDialog;  import android.app.AlertDialog;
# Line 14  import android.content.Intent; Line 35  import android.content.Intent;
35  import android.content.SharedPreferences;  import android.content.SharedPreferences;
36  import android.content.SharedPreferences.Editor;  import android.content.SharedPreferences.Editor;
37  import android.location.Location;  import android.location.Location;
 import android.os.AsyncTask;  
38  import android.os.Bundle;  import android.os.Bundle;
39  import android.os.Handler;  import android.os.Handler;
40  import android.os.Message;  import android.os.Message;
41    import android.text.Editable;
42    import android.text.TextWatcher;
43    import android.util.Log;
44  import android.view.ContextMenu;  import android.view.ContextMenu;
45  import android.view.LayoutInflater;  import android.view.ContextMenu.ContextMenuInfo;
46  import android.view.Menu;  import android.view.Menu;
47  import android.view.MenuItem;  import android.view.MenuItem;
48  import android.view.View;  import android.view.View;
 import android.view.ContextMenu.ContextMenuInfo;  
49  import android.view.View.OnCreateContextMenuListener;  import android.view.View.OnCreateContextMenuListener;
50  import android.widget.AdapterView;  import android.widget.AdapterView;
 import android.widget.EditText;  
51  import android.widget.ListView;  import android.widget.ListView;
52    import android.widget.TextView;
53  import android.widget.Toast;  import android.widget.Toast;
54    import dk.thoerup.android.traininfo.common.StationBean;
55    import dk.thoerup.android.traininfo.common.StationEntry;
56    import dk.thoerup.traininfo.WelcomeScreen.ListType;
57  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
58  import dk.thoerup.traininfo.provider.StationProvider;  import dk.thoerup.traininfo.provider.StationProvider;
59  import dk.thoerup.traininfo.stationmap.GeoPair;  import dk.thoerup.traininfo.stationmap.GeoPair;
# Line 37  import dk.thoerup.traininfo.stationmap.S Line 61  import dk.thoerup.traininfo.stationmap.S
61  import dk.thoerup.traininfo.util.IntSet;  import dk.thoerup.traininfo.util.IntSet;
62  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
63    
 import static dk.thoerup.traininfo.R.string.*;  
   
64  public class StationList extends ListActivity  {  public class StationList extends ListActivity  {
65          public static final int GOTLOCATION = 1001;  
66          public static final int GOTSTATIONLIST = 1002;          
         public static final int NOPROVIDER = 1003;  
         public static final int LOCATIONFIXTIMEOUT = 1004;  
67                    
68          public static final int OPTIONS_MAP = 2003;          public static final int OPTIONS_MAP = 103;
69          public static final int OPTIONS_GPSINFO = 2004;          public static final int OPTIONS_GPSINFO = 104;
70                    
71          public static final int DLG_PROGRESS = 3001;          public static final int DLG_PROGRESS = 3001;
         public static final int DLG_STATIONNAME = 3002;  
72                    
73                    
74          public static final int GPS_TIMEOUT_MS = 17500; //how long are we willing to wait for gps fix -in milliseconds          public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds
75                    
76                    
77          static enum LookupMethod {          static enum LookupMethod {
# Line 66  public class StationList extends ListAct Line 85  public class StationList extends ListAct
85          String dialogMessage = "";          String dialogMessage = "";
86          ProgressDialog dialog;          ProgressDialog dialog;
87          LocationLookup locationLookup = null;          LocationLookup locationLookup = null;
         FindStationsTask findStationsTask;  
88          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
89                    
90          GeoPair location = new GeoPair();          //GeoPair location = new GeoPair();
91    
92          boolean isLaunchedforShortcut;          boolean isLaunchedforShortcut;
93          boolean isRunning = false;  
94          List<StationBean> stations = new ArrayList<StationBean>();          StationBean stations = new StationBean();
95                    
96          StationProvider stationProvider = ProviderFactory.getStationProvider();          StationProvider stationProvider = ProviderFactory.getStationProvider();
97                                    
# Line 84  public class StationList extends ListAct Line 102  public class StationList extends ListAct
102    
103          WelcomeScreen.ListType listType;          WelcomeScreen.ListType listType;
104          SharedPreferences prefs;          SharedPreferences prefs;
105                                    
106          ///////////////////////////////////////////////////////////////////////////////////////////          ///////////////////////////////////////////////////////////////////////////////////////////
107          //Activity call backs          //Activity call backs
108                    
109          @SuppressWarnings("unchecked")  
110          @Override          @Override
111          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
112                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
113                  setContentView(R.layout.stationlist);                  setContentView(R.layout.stationlist);
114                                    
115                    listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
116                                    
117                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this, (listType == ListType.ListNearest) );
118                  setListAdapter(adapter);                  setListAdapter(adapter);
119                                    
120                  ListView lv = getListView();                  ListView lv = getListView();
121                  lv.setOnCreateContextMenuListener(contextMenu);                  lv.setOnCreateContextMenuListener(contextMenu);
122                                    
123                  locationLookup = new LocationLookup(this, stationsFetched);                  locationLookup = new LocationLookup(this);
124                                    
125    
126                  prefs = getSharedPreferences("TrainStation", 0);                  prefs = getSharedPreferences("TrainStation", 0);
# Line 110  public class StationList extends ListAct Line 129  public class StationList extends ListAct
129                          favorites.fromString(favoriteString);                          favorites.fromString(favoriteString);
130                  }                  }
131                                    
                 listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");  
132                  setTitle();                  setTitle();
133                                    
134                  isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);                  isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);
135                                    
136                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
137                    
138                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
139    
140                                                    
141                          switch (listType) {                          switch (listType) {
142                          case ListNearest:                          case ListNearest:
143                                  startLookup();                                  startNearestLookup();
144                                  break;                                  break;
145                          case ListSearch:                                                          case ListSearch:                                
146                                  showDialog(DLG_STATIONNAME); //TODO: this.showDialogSafe(DLG_STATIONNAME);                                  enableNamesearchField();
147                                  break;                                  break;
148                          case ListFavorites:                          case ListFavorites:
149                                  startFavoriteLookup();                                  startFavoriteLookup();
# Line 133  public class StationList extends ListAct Line 153  public class StationList extends ListAct
153                          }                          }
154                                                    
155                  } else {                  } else {
156                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");                          stations = (StationBean) savedInstanceState.getSerializable("stations");
157                          adapter.setStations(stations);                          adapter.setStations(stations);
                         location = (GeoPair) savedInstanceState.getSerializable("location");  
158                  }                  }
159                                    
160          }          }
161                    
162            
163    
164    
165    
166    
167          @Override          @Override
168          protected void onDestroy() {          protected void onDestroy() {
169                  super.onDestroy();                  super.onDestroy();
170                                    
171                  if (findStationsTask != null) {                  stationsFetched.removeMessages(0);
172                          findStationsTask.cancel(true);                  
173                  }                  
174                  if (locationLookup != null) {                  if (locationLookup != null) {
175                          locationLookup.stopSearch();                          locationLookup.stopSearch();
176                  }                  }
177                  isRunning = false;          
178          }          }
179    
180                    
# Line 176  public class StationList extends ListAct Line 199  public class StationList extends ListAct
199          }          }
200                    
201                    
202          /* these 3 dialogs helper functions are very rude and ugly hack          public void enableNamesearchField() {
203           * to remove these auto-reported exceptions                  final TextView stationSearch = (TextView) findViewById(R.id.stationsearch);
204           * - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running?                  stationSearch.setVisibility( View.VISIBLE );
205           * - java.lang.IllegalArgumentException: View not attached to window manager                  stationSearch.addTextChangedListener( new TextWatcher() {
206           */  
207                            @Override
208                            public void afterTextChanged(Editable s) {
209                                    if (s.length() > 0) {
210                                            startNameLookup( s.toString() );
211                                    } else {
212                                            stations = new StationBean();
213                                            getListView().invalidateViews();
214                                            adapter.setStations( stations );        
215                                    }
216                            }
217    
218          /*                          @Override
219          public void showDialogSafe(int id) {                          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
220                  try {                          }
221                          showDialog(id);  
222                  } catch (Exception e) {                          @Override
223                          Log.e("StationList", "showDialog failed", e);                          public void onTextChanged(CharSequence s, int start, int before, int count) {
224                  }                          }
225          }                          
226                            });
227          public void dismissDialogSafe(int id) {                  
228                  try {                  
                         dismissDialog(id);  
                 } catch (Exception e) {  
                         Log.e("StationList", "dismissDialog failed", e);  
                 }  
         }  
         public void dismissDialogSafe(Dialog dlg) {  
                 try {  
                         dlg.dismiss();  
                 } catch (Exception e) {  
                         Log.e("StationList", "dismissDialog failed", e);  
                 }  
229          }          }
           
         public void builderShowSafe(AlertDialog.Builder builder) {  
                 try {  
                         builder.show();  
                 } catch (Exception e) {  
                         Log.e("StationList", "builder.show() failed", e);  
                 }  
230                                    
         }*/  
           
         /* EOF rude and ugly dialog hack */  
           
   
231    
232      @Override      @Override
233      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
234      {      {
235          if (dialog != null && dialog.isShowing())          if (dialog != null && dialog.isShowing())
236                  dialog.dismiss();                  dialog.dismiss();
237          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (StationBean) stations);
         outState.putSerializable("location", location);  
238                    
239      }      }
240                    
# Line 239  public class StationList extends ListAct Line 248  public class StationList extends ListAct
248                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
249                                    
250                  item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));                  item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
251                  item.setIcon(android.R.drawable.ic_menu_mapmode);                                item.setIcon(android.R.drawable.ic_menu_info_details);
252                    boolean hasLoc = (locationLookup.getLocation() != null);
253                    item.setEnabled(hasLoc);
254                                    
255                  return true;                  return true;
256          }          }
257            
258    
259    
260    
261          @Override          @Override
262          public boolean onOptionsItemSelected(MenuItem item) {          public boolean onOptionsItemSelected(MenuItem item) {
263                  boolean retval = true;                  boolean retval = true;
264    
265                  //TODO: Cleanup  
266                  switch (item.getItemId()) {                  switch (item.getItemId()) {
267                  case OPTIONS_MAP:                  case OPTIONS_MAP:
268                                                    
269                            if ( stations == null || stations.entries == null || stations.entries.size() == 0 ) {
270                                    Toast.makeText(this, "No stations to show on map", Toast.LENGTH_SHORT).show(); //TODO: Translate
271                                    return true;
272                            }
273                            
274                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
275                                                    
276                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
277                          for (StationBean st : stations ) {                          for (StationEntry st : stations.entries ) {
278                                  stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );                                  stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
279                          }                          }
280                                                    
# Line 267  public class StationList extends ListAct Line 286  public class StationList extends ListAct
286                          Location loc = locationLookup.getLocation();                          Location loc = locationLookup.getLocation();
287                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
288                          message.append( getString(stationlist_locationinfo) ).append(":\n");                          message.append( getString(stationlist_locationinfo) ).append(":\n");
289                          if (loc != null) {                          
290                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");                          message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
291                                  message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");                          message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
292                                  message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");                          message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
293                                  message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");                          message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
294                          } else {                                                  
                                 message.append( getString(stationlist_nolocation) );  
                         }                        
295                                                    
296                          MessageBox.showMessage(this, message.toString(), false);                          MessageBox.showMessage(this, message.toString(), false);
297                          break;                          break;
# Line 319  public class StationList extends ListAct Line 336  public class StationList extends ListAct
336                          dlg.setMessage( getString(stationlist_waitforlocation) );                          dlg.setMessage( getString(stationlist_waitforlocation) );
337                          dlg.setCancelable(false);                          dlg.setCancelable(false);
338                          return dlg;                                              return dlg;                    
                 case DLG_STATIONNAME:  
                         LayoutInflater factory = LayoutInflater.from(this);  
                         final View rootView = factory.inflate(R.layout.textinput, null);  
                           
                           
                         AlertDialog.Builder builder = new AlertDialog.Builder(this);  
                           
                         builder.setTitle( getString(stationlist_stationsearch) );  
                         builder.setView(rootView);  
                         builder.setCancelable(true);  
                         builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {  
                                 public void onClick(DialogInterface dialog, int which) {  
                                         EditText et = (EditText) rootView.findViewById(R.id.EditText);  
                                         dialog.dismiss();  
                                         String search = et.getText().toString().trim();  
                                         if (search.length() >= 2) {  
                                                 startNameSearch(search);  
                                         } else {  
                                                 showMessageAndClose( getString(stationlist_twocharmin) );  
                                         }  
                                 }  
                         });  
                         builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {  
                                 public void onClick(DialogInterface dialog, int which) {  
                                         dialog.dismiss();  
                                         StationList.this.finish(); // Close this Activity  
                                 }  
                         });                      
                         return builder.create();  
339                                                    
340                  default:                  default:
341                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
# Line 374  public class StationList extends ListAct Line 362  public class StationList extends ListAct
362          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
363                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
364                                                                    
365                  StationBean station = stations.get(position);                  StationEntry station = stations.entries.get(position);
366                                    
367                  if (isLaunchedforShortcut == true) {                  if (isLaunchedforShortcut == true) {
368                          Intent i = new Intent();                          Intent i = new Intent();
# Line 391  public class StationList extends ListAct Line 379  public class StationList extends ListAct
379          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
380          //          //
381    
382          public void startLookup() {          public void startNearestLookup() {
                 isRunning = true;                
383                  dialogMessage = getString( stationlist_waitforlocation );                  dialogMessage = getString( stationlist_waitforlocation );
384                  showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
385                                    
386                  locationLookup.locateStations();                  locationLookup.locateStations();
387                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS);                  stationsFetched.sendEmptyMessageDelayed(0, 500);
388          }          }
389                    
390          void startNameSearch(String name) {          void startNameLookup(String name) {
                 dialogMessage = getString( stationlist_findbyname );  
                 showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS);  
   
                 findStationsTask = new FindStationsTask();  
                 findStationsTask.searchByName(name);  
                 findStationsTask.execute();  
391                                    
392                    stations = stationProvider.lookupStationsByName(name);
393                    getListView().invalidateViews();
394                    adapter.setStations( stations );                                
395          }          }
396                    
397          public void startFavoriteLookup() {          public void startFavoriteLookup() {
398                                    
399                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
400                          dialogMessage = getString( stationlist_loadfavorites );                          
401                          showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS);                          stations = stationProvider.lookupStationsByIds( favorites.toString() );
402                            getListView().invalidateViews();
403                          findStationsTask = new FindStationsTask();                          adapter.setStations( stations );                
404                          findStationsTask.searchByIds( favorites.toString() );                          
405                          findStationsTask.execute();                          
406                  } else {                  } else {
407                          showMessageAndClose( getString( stationlist_nofavorites ) );                          showMessageAndClose( getString( stationlist_nofavorites ) );
408                  }                  }
# Line 426  public class StationList extends ListAct Line 410  public class StationList extends ListAct
410    
411    
412                    
413          void startLocatorTask()          void startNearestLookupPhase2()
414          {          {      
415                  dialogMessage = getString( stationlist_findingnearby );                  stations = stationProvider.lookupStationsByLocation( locationLookup.getLocation() );
416                  showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS);                  getListView().invalidateViews();
417                                    adapter.setStations( stations );
                 findStationsTask = new FindStationsTask();  
                 findStationsTask.searchByLocation( locationLookup.getLocation() );  
                 findStationsTask.execute();      
418          }          }
419                                    
420                    
# Line 444  public class StationList extends ListAct Line 425  public class StationList extends ListAct
425                  @Override                  @Override
426                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
427    
428                          switch (msg.what) {                          LocationLookup.LookupStates state = locationLookup.getState();
429    
430    
431                            switch (state) {
432                          case GOTLOCATION:                          case GOTLOCATION:
433                                  dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
434                                    
435                                  startLocatorTask();                                  startNearestLookupPhase2();
436                                  location = GeoPair.fromLocation( locationLookup.getLocation() );  
437                                                                    return;
                                 break;  
438    
439                          case NOPROVIDER:                          case NOPROVIDER:
440                                  dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
441                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
442                                  //StationList.this.finish();                                  //StationList.this.finish();
443                                  break;                                  return;
444                          case LOCATIONFIXTIMEOUT:                                                          case IDLE:
445                                  if (isRunning) {                                  Log.e("TrainInfo", "How did this happen ???");
446                                          locationLookup.stopSearch();                                  dismissDialog(DLG_PROGRESS); // how did we get here ??
447                                          if (locationLookup.hasLocation()) {                                  return;
448                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                  
                                         } else {                                                  
                                                 dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS);  
                                                   
                                                 AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                  
                                                 builder.setMessage(  getString( stationlist_gpstimeout) );  
                                                 builder.setCancelable(true);  
                                                 builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {  
                                                         public void onClick(DialogInterface dialog, int id) {  
                                                                 dialog.dismiss();  
                                                                 startLookup();  
                                                                   
                                                         }  
                                                 });  
                                                 builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {  
                                                         public void onClick(DialogInterface dialog, int id) {  
                                                                 dialog.dismiss();  
                                                         }                                                        
                                                 });  
                                                 builder.show();//TODO:builderShowSafe(builder);  
   
                                         }  
                                 }  
                                 break;  
449                          }                          }
                         isRunning = false;  
                 }  
         };  
450    
           
         class FindStationsTask extends AsyncTask<Void,Void,Void> {  
                   
                 LookupMethod method = LookupMethod.MethodNone;  
                 String name;  
                 Location loc;  
                 String ids;  
                   
                 public void searchByName(String n) {  
                           
                         method = LookupMethod.ByName;  
                         name = n;  
                 }  
                   
                 public void searchByLocation(Location l) {  
                         method = LookupMethod.ByLocation;  
                         loc = l;  
                 }  
                   
                 public void searchByIds(String id) {  
                           
                         method = LookupMethod.ByList;  
                         ids = id;  
                 }  
                   
                 @Override  
                 protected void onPreExecute() {  
451    
                         if (method.equals(LookupMethod.MethodNone))  
                                 throw new RuntimeException("Method not set");  
                         super.onPreExecute();  
                 }  
                   
                 @Override  
                 protected Void doInBackground(Void... params) {  
452    
453                          switch (method) {                          if (locationLookup.elapsedTime() >=  GPS_TIMEOUT_MS) {
454                          case ByLocation:                                  dismissDialog(DLG_PROGRESS);
                                 stations = stationProvider.lookupStations(loc);  
                                 break;  
                         case ByName:  
                                 stations = stationProvider.lookupStationsByName(name);  
                                 break;  
                         case ByList:  
                                 stations = stationProvider.lookupStationsByIds(ids);  
                                 break;  
                         default:  
                                 stations = null; // not possible          
                         }  
                           
                           
                         return null;  
                 }  
455    
                 @Override  
                 protected void onPostExecute(Void result) {  
                         super.onPostExecute(result);  
                         dialog.dismiss();//TODO:dismissDialogSafe(dialog);  
                           
                           
                         if (stations != null) {                          
                                 if (stations.size() == 0) {  
                                         showMessageAndClose(getString(stationlist_nostations));  
                                 }  
456    
457                                  StationList.this.getListView().invalidateViews();                                  locationLookup.stopSearch();
458                                  adapter.setStations( stations );                                  
459                                                                    if (locationLookup.hasLocation()) {
460                                                                            startNearestLookupPhase2();
461                          } else { //communication or parse errors                                  } else {                                                
462                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                          AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
463                                  builder.setMessage(getString(stationlist_fetcherror));                                                                    builder.setMessage(  getString( stationlist_gpstimeout) );
464                                  builder.setCancelable(true);                                          builder.setCancelable(true);
465                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
466                                          public void onClick(DialogInterface dialog, int id) {                                                  public void onClick(DialogInterface dialog, int id) {
467                                                  dialog.dismiss();                                                          dialog.dismiss();
468                                                                                                            startNearestLookup();
469                                                  Runnable runner = null;  
                                                 switch (method) {  
                                                 case ByLocation:  
                                                         runner = new Runnable() {  
                                                                 @Override  
                                                                 public void run() {  
                                                                         startLocatorTask();                                                              
                                                                 }  
                                                         };  
                                                         break;  
                                                 case ByName:  
                                                         runner = new Runnable() {  
                                                                 @Override  
                                                                 public void run() {  
                                                                         startNameSearch( FindStationsTask.this.name );  
                                                                 }  
                                                         };  
                                                         break;  
                                                 case ByList:  
                                                         runner = new Runnable() {  
                                                                 @Override  
                                                                 public void run() {  
                                                                         startFavoriteLookup();  
                                                                 }  
                                                         };  
                                                         break;  
470                                                  }                                                  }
471                                                                                            });
472                                                  stationsFetched.post( runner );                                          builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
473                                          }                                                  public void onClick(DialogInterface dialog, int id) {
474                                  });                                                          dialog.dismiss();
475                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {                                                          StationList.this.finish(); // Close this Activity
476                                          public void onClick(DialogInterface dialog, int id) {                                                  }                                                      
477                                                  dialog.dismiss();                                          });
478                                                  StationList.this.finish();                                          builder.show();
479                                          }                                                        
480                                  });                                  }
481                                                            } else {
482                                  builder.show();//TODO:builderShowSafe(builder);                                  if (locationLookup.hasGps()) {
483                                            int count = locationLookup.getSatCount();
484                                            String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count;
485                                            dialog.setMessage( dialogMessage );
486                                    }
487                                    this.sendEmptyMessageDelayed(0, 500);
488                          }                          }
489                            
490                  }                  }
491          }          }
492            
493    
494    
495                    
496          class FavoritesMenu implements OnCreateContextMenuListener {          class FavoritesMenu implements OnCreateContextMenuListener {
497                  private final static int FAVORITES_ADD = 9001;                  private final static int FAVORITES_ADD = 9001;
# Line 624  public class StationList extends ListAct Line 505  public class StationList extends ListAct
505                                                                                                    
506                          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;                          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
507                          selectedPosition = info.position;                          selectedPosition = info.position;
508                          int stationID = stations.get(selectedPosition).getId();                          int stationID = stations.entries.get(selectedPosition).getId();
509    
510                          if (!favorites.contains(stationID)) {                          if (!favorites.contains(stationID)) {
511                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
# Line 635  public class StationList extends ListAct Line 516  public class StationList extends ListAct
516                  }                  }
517                                    
518                  public void onContextItemSelected(MenuItem item) {                  public void onContextItemSelected(MenuItem item) {
519                          StationBean sb = stations.get(selectedPosition);                          StationEntry sb = stations.entries.get(selectedPosition);
520                                                    
521                          int stationID = sb.getId();                          int stationID = sb.getId();
522                          if (item.getItemId() == FAVORITES_ADD) {                          if (item.getItemId() == FAVORITES_ADD) {
# Line 648  public class StationList extends ListAct Line 529  public class StationList extends ListAct
529                                                                    
530                                                                    
531                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
532                                          stations.remove(selectedPosition);                                          stations.entries.remove(selectedPosition);
533                                          adapter.notifyDataSetChanged();                                          adapter.notifyDataSetChanged();
534                                  }                                  }
535                          }                          }

Legend:
Removed from v.1006  
changed lines
  Added in v.1558

  ViewVC Help
Powered by ViewVC 1.1.20