/[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 557 by torben, Wed Jan 27 10:04:28 2010 UTC revision 1408 by torben, Mon May 2 11:54:17 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.generic_search;
7    import static dk.thoerup.traininfo.R.string.stationlist_accuracy;
8    import static dk.thoerup.traininfo.R.string.stationlist_addfavorite;
9    import static dk.thoerup.traininfo.R.string.stationlist_favorites;
10    import static dk.thoerup.traininfo.R.string.stationlist_fetcherror;
11    import static dk.thoerup.traininfo.R.string.stationlist_findbyname;
12    import static dk.thoerup.traininfo.R.string.stationlist_findingnearby;
13    import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo;
14    import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout;
15    import static dk.thoerup.traininfo.R.string.stationlist_latitude;
16    import static dk.thoerup.traininfo.R.string.stationlist_loadfavorites;
17    import static dk.thoerup.traininfo.R.string.stationlist_locationinfo;
18    import static dk.thoerup.traininfo.R.string.stationlist_longitude;
19    import static dk.thoerup.traininfo.R.string.stationlist_nearbystations;
20    import static dk.thoerup.traininfo.R.string.stationlist_nofavorites;
21    import static dk.thoerup.traininfo.R.string.stationlist_nolocation;
22    import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider;
23    import static dk.thoerup.traininfo.R.string.stationlist_nostations;
24    import static dk.thoerup.traininfo.R.string.stationlist_obtainedby;
25    import static dk.thoerup.traininfo.R.string.stationlist_removefavorite;
26    import static dk.thoerup.traininfo.R.string.stationlist_satellitecount;
27    import static dk.thoerup.traininfo.R.string.stationlist_search;
28    import static dk.thoerup.traininfo.R.string.stationlist_stationadded;
29    import static dk.thoerup.traininfo.R.string.stationlist_stationmap;
30    import static dk.thoerup.traininfo.R.string.stationlist_stationremoved;
31    import static dk.thoerup.traininfo.R.string.stationlist_stationsearch;
32    import static dk.thoerup.traininfo.R.string.stationlist_twocharmin;
33    import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation;
34    
35    import java.util.ArrayList;
36    
37    import android.app.Activity;
38  import android.app.AlertDialog;  import android.app.AlertDialog;
39  import android.app.Dialog;  import android.app.Dialog;
40  import android.app.ListActivity;  import android.app.ListActivity;
# Line 17  import android.os.AsyncTask; Line 48  import android.os.AsyncTask;
48  import android.os.Bundle;  import android.os.Bundle;
49  import android.os.Handler;  import android.os.Handler;
50  import android.os.Message;  import android.os.Message;
51    import android.util.Log;
52  import android.view.ContextMenu;  import android.view.ContextMenu;
53    import android.view.ContextMenu.ContextMenuInfo;
54  import android.view.LayoutInflater;  import android.view.LayoutInflater;
55  import android.view.Menu;  import android.view.Menu;
56  import android.view.MenuItem;  import android.view.MenuItem;
57  import android.view.View;  import android.view.View;
 import android.view.ContextMenu.ContextMenuInfo;  
58  import android.view.View.OnCreateContextMenuListener;  import android.view.View.OnCreateContextMenuListener;
59  import android.widget.AdapterView;  import android.widget.AdapterView;
60  import android.widget.EditText;  import android.widget.EditText;
61  import android.widget.ListView;  import android.widget.ListView;
62  import android.widget.Toast;  import android.widget.Toast;
63    import dk.thoerup.android.traininfo.common.StationBean;
64    import dk.thoerup.android.traininfo.common.StationEntry;
65  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
66  import dk.thoerup.traininfo.provider.StationProvider;  import dk.thoerup.traininfo.provider.StationProvider;
67  import dk.thoerup.traininfo.stationmap.GeoPair;  import dk.thoerup.traininfo.stationmap.GeoPair;
# Line 37  import dk.thoerup.traininfo.util.IntSet; Line 70  import dk.thoerup.traininfo.util.IntSet;
70  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
71    
72  public class StationList extends ListActivity  {  public class StationList extends ListActivity  {
73          public static final int GOTLOCATION = 1001;  
74          public static final int GOTSTATIONLIST = 1002;          
         public static final int NOPROVIDER = 1003;  
         public static final int LOCATIONFIXTIMEOUT = 1004;  
75                    
76          public static final int OPTIONS_MAP = 2003;          public static final int OPTIONS_MAP = 2003;
77          public static final int OPTIONS_GPSINFO = 2004;          public static final int OPTIONS_GPSINFO = 2004;
   
           
   
78                    
79          public static final int DLG_PROGRESS = 3001;          public static final int DLG_PROGRESS = 3001;
80          public static final int DLG_STATIONNAME = 3002;          public static final int DLG_STATIONNAME = 3002;
81                    
82            
83            public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds
84            
85            
86          static enum LookupMethod {          static enum LookupMethod {
87                  ByLocation,                  ByLocation,
88                  ByName,                  ByName,
# Line 65  public class StationList extends ListAct Line 97  public class StationList extends ListAct
97          FindStationsTask findStationsTask;          FindStationsTask findStationsTask;
98          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
99                    
100          GeoPair location = new GeoPair();          //GeoPair location = new GeoPair();
101            
102          boolean isRunning = false;          boolean isLaunchedforShortcut;
103          List<StationBean> stations = new ArrayList<StationBean>();  
104            StationBean stations = new StationBean();
105                    
106          StationProvider stationProvider = ProviderFactory.getStationProvider();          StationProvider stationProvider = ProviderFactory.getStationProvider();
107                                    
# Line 79  public class StationList extends ListAct Line 112  public class StationList extends ListAct
112    
113          WelcomeScreen.ListType listType;          WelcomeScreen.ListType listType;
114          SharedPreferences prefs;          SharedPreferences prefs;
115                                    
116          ///////////////////////////////////////////////////////////////////////////////////////////          ///////////////////////////////////////////////////////////////////////////////////////////
117          //Activity call backs          //Activity call backs
118                    
119          @SuppressWarnings("unchecked")  
120          @Override          @Override
121          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
122                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
# Line 96  public class StationList extends ListAct Line 129  public class StationList extends ListAct
129                  ListView lv = getListView();                  ListView lv = getListView();
130                  lv.setOnCreateContextMenuListener(contextMenu);                  lv.setOnCreateContextMenuListener(contextMenu);
131                                    
132                  locationLookup = new LocationLookup(this, stationsFetched);                  locationLookup = new LocationLookup(this);
133                                    
134    
135                  prefs = getSharedPreferences("TrainStation", 0);                  prefs = getSharedPreferences("TrainStation", 0);
# Line 108  public class StationList extends ListAct Line 141  public class StationList extends ListAct
141                  listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");                  listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
142                  setTitle();                  setTitle();
143                                    
144                    isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);
145                    
146                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
147                    
148                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
149    
150                                                    
151                          switch (listType) {                          switch (listType) {
152                          case ListNearest:                          case ListNearest:
153                                  startLookup();                                  startNearestLookup();
154                                  break;                                  break;
155                          case ListSearch:                          case ListSearch:                                
156                                  this.showDialog(DLG_STATIONNAME);                                  showDialog(DLG_STATIONNAME);
157                                  break;                                  break;
158                          case ListFavorites:                          case ListFavorites:
159                                  startFavoriteLookup();                                  startFavoriteLookup();
# Line 126  public class StationList extends ListAct Line 163  public class StationList extends ListAct
163                          }                          }
164                                                    
165                  } else {                  } else {
166                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");                          stations = (StationBean) savedInstanceState.getSerializable("stations");
167                          adapter.setStations(stations);                          adapter.setStations(stations);
                         location = (GeoPair) savedInstanceState.getSerializable("location");  
168                  }                  }
169                                    
170          }          }
171            
172            
173    
174    
175    
176    
177            @Override
178            protected void onDestroy() {
179                    super.onDestroy();
180                    
181                    stationsFetched.removeMessages(0);
182                    
183                    
184                    if (locationLookup != null) {
185                            locationLookup.stopSearch();
186                    }
187                    if (findStationsTask != null) {
188                            findStationsTask.cancel(true);
189                    }              
190            }
191    
192            
193          protected void setTitle() {          protected void setTitle() {
194                  String dialogTitle = getResources().getString(R.string.app_name);                  String dialogTitle = getResources().getString(app_name);
195                  switch (listType) {                  switch (listType) {
196                  case ListNearest:                  case ListNearest:
197                          dialogTitle += " - Nearby stations";                          dialogTitle += " - " + getString(stationlist_nearbystations);
198                          break;                          break;
199                  case ListSearch:                  case ListSearch:
200                          dialogTitle += " - Search";                          dialogTitle += " - " + getString(stationlist_search);
201                          break;                          break;
202                  case ListFavorites:                  case ListFavorites:
203                          dialogTitle += " - Favorites";                          dialogTitle += " - " + getString(stationlist_favorites);
204                          break;                          break;
205                  default:                  default:
206                          dialogTitle = "";//not possible                                                          dialogTitle = "";//not possible                                
207                  }                  }
208                    
209                  setTitle(dialogTitle);                  setTitle(dialogTitle);
210                    
211          }          }
212                    
213                            
   
214    
215      @Override      @Override
216      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
217      {      {
218          if (dialog != null && dialog.isShowing())          if (dialog != null && dialog.isShowing())
219                  dialog.dismiss();                  dialog.dismiss();
220          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (StationBean) stations);
         outState.putSerializable("location", location);  
221                    
222      }      }
223                    
# Line 169  public class StationList extends ListAct Line 226  public class StationList extends ListAct
226          @Override          @Override
227          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
228                  MenuItem item;                  MenuItem item;
229                                                    
230                  item = menu.add(0, OPTIONS_MAP, 0, "Station map");                  item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
231                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
232                                    
233                  item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info");                  item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
234                  item.setIcon(android.R.drawable.ic_menu_mapmode);                                item.setIcon(android.R.drawable.ic_menu_mapmode);              
235                                    
236                  return true;                  return true;
# Line 190  public class StationList extends ListAct Line 247  public class StationList extends ListAct
247                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
248                                                    
249                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
250                          for (StationBean st : stations ) {                          for (StationEntry st : stations.entries ) {
251                                  stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );                                  stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
252                          }                          }
253                                                    
# Line 201  public class StationList extends ListAct Line 258  public class StationList extends ListAct
258                  case OPTIONS_GPSINFO:                  case OPTIONS_GPSINFO:
259                          Location loc = locationLookup.getLocation();                          Location loc = locationLookup.getLocation();
260                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
261                          message.append("Location info:\n");                          message.append( getString(stationlist_locationinfo) ).append(":\n");
262                          if (loc != null) {                          if (loc != null) {
263                                  message.append("-Obtained by: ").append( loc.getProvider() ).append("\n");                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
264                                  message.append("-Accuracy: ").append( (int)loc.getAccuracy()).append("m\n");                                  message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
265                                  message.append("-Latitude: ").append( loc.getLatitude()).append("\n");                                  message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
266                                  message.append("-Longitude: ").append( loc.getLongitude() ).append("\n");                                  message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
267                          } else {                          } else {
268                                  message.append(" - No location data!");                                  message.append( getString(stationlist_nolocation) );
269                          }                                                }                      
270                                                    
271                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString(), false);
272                          break;                          break;
273                  default:                  default:
274                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 251  public class StationList extends ListAct Line 308  public class StationList extends ListAct
308                  switch (id) {                  switch (id) {
309                  case DLG_PROGRESS:                  case DLG_PROGRESS:
310                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
311                          dlg.setMessage("Wait for location fix");                          dlg.setMessage( getString(stationlist_waitforlocation) );
312                          dlg.setCancelable(false);                          dlg.setCancelable(false);
313                          return dlg;                                              return dlg;                    
314                  case DLG_STATIONNAME:                  case DLG_STATIONNAME:
# Line 261  public class StationList extends ListAct Line 318  public class StationList extends ListAct
318                                                    
319                          AlertDialog.Builder builder = new AlertDialog.Builder(this);                          AlertDialog.Builder builder = new AlertDialog.Builder(this);
320                                                    
321                          builder.setTitle("Station search");                          builder.setTitle( getString(stationlist_stationsearch) );
322                          builder.setView(rootView);                          builder.setView(rootView);
323                          builder.setCancelable(true);                          builder.setCancelable(true);
324                          builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {                          builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
325                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
326                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
327                                          dialog.dismiss();                                          dialog.dismiss();
328                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
329                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
330                                                    startNameLookup(search);
331                                          } else {                                          } else {
332                                                  showMessageAndClose("Two characters minimum");                                                  showMessageAndClose( getString(stationlist_twocharmin) );
333                                          }                                          }
334                                  }                                  }
335                          });                          });
336                          builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                          builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
337                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
338                                          dialog.dismiss();                                          dialog.dismiss();
339                                          StationList.this.finish(); // Close this Activity                                          StationList.this.finish(); // Close this Activity
# Line 308  public class StationList extends ListAct Line 366  public class StationList extends ListAct
366          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
367                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
368                                                                    
369                  StationBean station = stations.get(position);                  StationEntry station = stations.entries.get(position);
370                                    
371                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
372                  intent.putExtra("stationbean", station);                          Intent i = new Intent();
373                  startActivity(intent);                          i.putExtra("station", station);
374                            setResult(Activity.RESULT_OK, i);
375                            finish();
376                    } else {                
377                            Intent intent = new Intent(this, DepartureList.class);
378                            intent.putExtra("stationbean", station);
379                            startActivity(intent);
380                    }
381          }          }
382    
383          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
384          //          //
385    
386          public void startLookup() {          public void startNearestLookup() {
387                  isRunning = true;                  dialogMessage = getString( stationlist_waitforlocation );
                 dialogMessage = "Wait for location fix";  
388                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
389                                    
390                  locationLookup.locateStations();                  locationLookup.locateStations();
391                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(0, 500);
392          }          }
393                    
394          void startNameSearch(String name) {          void startNameLookup(String name) {
395                  dialogMessage = "Finding stations by name";                  dialogMessage = getString( stationlist_findbyname );
396                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
397    
398                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
399                  findStationsTask.searchByName(name, locationLookup.getLocation());                  findStationsTask.searchByName(name);
400                  findStationsTask.execute();                  findStationsTask.execute();
401                                    
402          }          }
# Line 340  public class StationList extends ListAct Line 404  public class StationList extends ListAct
404          public void startFavoriteLookup() {          public void startFavoriteLookup() {
405                                    
406                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
407                          dialogMessage = "Loading favorites";                          dialogMessage = getString( stationlist_loadfavorites );
408                          showDialog(DLG_PROGRESS);                          showDialog(DLG_PROGRESS);
409    
410                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
411                          findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation());                          findStationsTask.searchByIds( favorites.toString() );
412                          findStationsTask.execute();                          findStationsTask.execute();
413                  } else {                  } else {
414                          showMessageAndClose( "Favorite list is empty");                          showMessageAndClose( getString( stationlist_nofavorites ) );
415                  }                  }
416          }          }
417    
# Line 355  public class StationList extends ListAct Line 419  public class StationList extends ListAct
419                    
420          void startLocatorTask()          void startLocatorTask()
421          {          {
422                  dialogMessage = "Finding nearby stations";                  dialogMessage = getString( stationlist_findingnearby );
423                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);              
424                                    
425                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
426                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
427                  findStationsTask.execute();                      findStationsTask.execute();    
428          }          }
           
   
         /* TODO: Remove this no longer needed function  
         String lookupAddress(double latitude, double longitude) {  
429                                    
                 Geocoder coder = new Geocoder(this, new Locale("da"));  
                 StringBuilder sb = new StringBuilder();  
                 Log.i("lookupaddr", "" + latitude + "/" + longitude);  
                 try {  
                         List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);  
                         Address addr = addressList.get(0);  
                           
                           
                         int max = addr.getMaxAddressLineIndex();  
                         for (int i=0; i<max; i++) {  
                                 if (i>0)  
                                         sb.append(", ");  
                                   
                                 sb.append(addr.getAddressLine(i));  
                         }  
                           
                           
                 } catch (Exception e) {  
                         Log.e("DepartureList", "geocoder failed", e);  
                 }  
                   
                 return sb.toString();  
         }*/  
           
430                    
431          ////////////////////////////////////////////////////////////////////////////          ////////////////////////////////////////////////////////////////////////////
432          // Inner classes          // Inner classes
# Line 399  public class StationList extends ListAct Line 435  public class StationList extends ListAct
435                  @Override                  @Override
436                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
437    
438                          switch (msg.what) {                          LocationLookup.LookupStates state = locationLookup.getState();
439    
440    
441                            switch (state) {
442                          case GOTLOCATION:                          case GOTLOCATION:
443                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
444                                    
445                                  startLocatorTask();                                  startLocatorTask();
446                                  location = GeoPair.fromLocation( locationLookup.getLocation() );  
447                                                                    return;
                                 break;  
448    
449                          case NOPROVIDER:                          case NOPROVIDER:
450                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
451                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
452                                  break;                                  //StationList.this.finish();
453                          case LOCATIONFIXTIMEOUT:                                                                  return;
454                                  if (isRunning) {                          case IDLE:
455                                          locationLookup.stopSearch();                                  Log.e("TrainInfo", "How did this happen ???");
456                                          if (locationLookup.hasLocation()) {                                  dismissDialog(DLG_PROGRESS); // how did we get here ??
457                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                  return;
458                                          } else {                                                                                  
459                                                  dismissDialog(DLG_PROGRESS);                          }
460                                                    
461                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                  
462                                                  builder.setMessage("Location fix timed out");  
463                                                  builder.setCancelable(true);                          if (locationLookup.elapsedTime() >=  GPS_TIMEOUT_MS) {
464                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  dismissDialog(DLG_PROGRESS);
465                                                          public void onClick(DialogInterface dialog, int id) {  
466                                                                  dialog.dismiss();  
467                                                                  startLookup();                                  locationLookup.stopSearch();
468                                                                    
469                                                          }                                  if (locationLookup.hasLocation()) {
470                                                  });                                          startLocatorTask();
471                                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  } else {                                                
472                                                          public void onClick(DialogInterface dialog, int id) {                                          AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
473                                                                  dialog.dismiss();                                          builder.setMessage(  getString( stationlist_gpstimeout) );
474                                                          }                                                                                                builder.setCancelable(true);
475                                                  });                                                                                                                                      builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
476                                                  builder.show();                                                  public void onClick(DialogInterface dialog, int id) {
477                                                            dialog.dismiss();
478                                                            startNearestLookup();
479    
480                                                    }
481                                            });
482                                            builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
483                                                    public void onClick(DialogInterface dialog, int id) {
484                                                            dialog.dismiss();
485                                                            StationList.this.finish(); // Close this Activity
486                                                    }                                                      
487                                            });
488                                            builder.show();
489    
                                         }  
490                                  }                                  }
491                                  break;                          } else {
492                                    if (locationLookup.hasGps()) {
493                                            int count = locationLookup.getSatCount();
494                                            String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count;
495                                            dialog.setMessage( dialogMessage );
496                                    }
497                                    this.sendEmptyMessageDelayed(0, 500);
498                          }                          }
499                          isRunning = false;                          
500                  }                  }
501          };          }
502    
503    
504    
505                    
506          class FindStationsTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
507                                    
508                  LookupMethod method = LookupMethod.MethodNone;                  LookupMethod method = LookupMethod.MethodNone;
                 boolean success;  
509                  String name;                  String name;
510                  Location loc;                  Location loc;
511                  String ids;                  String ids;
512                                    
513                  public void searchByName(String n, Location l) {                  public void searchByName(String n) {
514                                                    
515                          method = LookupMethod.ByName;                          method = LookupMethod.ByName;
                         loc = l;  
516                          name = n;                          name = n;
517                  }                  }
518                                    
# Line 466  public class StationList extends ListAct Line 521  public class StationList extends ListAct
521                          loc = l;                          loc = l;
522                  }                  }
523                                    
524                  public void searchByIds(String id, Location l) {                  public void searchByIds(String id) {
525                                                    
526                          method = LookupMethod.ByList;                          method = LookupMethod.ByList;
                         loc = l;  
527                          ids = id;                          ids = id;
528                  }                  }
529                                    
# Line 486  public class StationList extends ListAct Line 540  public class StationList extends ListAct
540    
541                          switch (method) {                          switch (method) {
542                          case ByLocation:                          case ByLocation:
543                                  success = stationProvider.lookupStations(loc);                                  stations = stationProvider.lookupStationsByLocation(loc);
544                                  break;                                  break;
545                          case ByName:                          case ByName:
546                                  success = stationProvider.lookupStationsByName(name);                                  stations = stationProvider.lookupStationsByName(name);
547                                  break;                                  break;
548                          case ByList:                          case ByList:
549                                  success = stationProvider.lookupStationsByIds(ids);                                  stations = stationProvider.lookupStationsByIds(ids);
550                                  break;                                  break;
551                          default:                          default:
552                                  success = false; // not possible                                          stations = null; // not possible        
553                          }                          }
554                                                    
555                                                    
                         Location dummy = new Location("gps");  
                         List<StationBean> stations = stationProvider.getStations();  
                           
                         for (StationBean station : stations) {  
                                                                   
                                 if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) {  
                                         if (loc != null) { //only do the distance calc if we have a location  
                                                 dummy.setLatitude(station.getLatitude());  
                                                 dummy.setLongitude(station.getLongitude());  
                                                 station.setDistance( (int)loc.distanceTo(dummy) );  
                                         } else {  
                                                 station.setDistance(0);  
                                         }  
                                 }  
   
                         }                                                
                           
556                          return null;                          return null;
557                  }                  }
558    
# Line 525  public class StationList extends ListAct Line 562  public class StationList extends ListAct
562                          dialog.dismiss();                          dialog.dismiss();
563                                                    
564                                                    
565                          if (success) {                                                    if (stations != null) {                        
566                                  if (stationProvider.getStations().size() == 0)                                  if (stations.entries.size() == 0) {
567                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                          showMessageAndClose(getString(stationlist_nostations));
568                                  stations = stationProvider.getStations();                                  }
569    
570                                    StationList.this.getListView().invalidateViews();
571                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
572                                                                    
573                                    
574                          } else { //communication or parse errors                          } else { //communication or parse errors
575                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
576                                  builder.setMessage("Error on finding nearby stations");                                  builder.setMessage(getString(stationlist_fetcherror));                          
577                                  builder.setCancelable(true);                                  builder.setCancelable(true);
578                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
579                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
580                                                  dialog.dismiss();                                                  dialog.dismiss();
581                                                                                                    
582                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
583                                                          @Override                                                  switch (method) {
584                                                          public void run() {                                                  case ByLocation:
585                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
586                                                          }                                                                  @Override
587                                                  });                                                                  public void run() {
588                                                                            startLocatorTask();                                                            
589                                                                    }
590                                                            };
591                                                            break;
592                                                    case ByName:
593                                                            runner = new Runnable() {
594                                                                    @Override
595                                                                    public void run() {
596                                                                            startNameLookup( FindStationsTask.this.name );
597                                                                    }
598                                                            };
599                                                            break;
600                                                    case ByList:
601                                                            runner = new Runnable() {
602                                                                    @Override
603                                                                    public void run() {
604                                                                            startFavoriteLookup();
605                                                                    }
606                                                            };
607                                                            break;
608                                                    }
609                                                    
610                                                    stationsFetched.post( runner );
611                                          }                                          }
612                                  });                                  });
613                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
614                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
615                                                  dialog.dismiss();                                                  dialog.dismiss();
616                                                    StationList.this.finish();
617                                          }                                                                                                }                                                      
618                                  });                                                                                                                              });
619                                  builder.show();                                                          
620                                    builder.show();
621                          }                          }
622                  }                  }
623          }          }
# Line 570  public class StationList extends ListAct Line 635  public class StationList extends ListAct
635                                                                                                    
636                          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;                          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
637                          selectedPosition = info.position;                          selectedPosition = info.position;
638                          int stationID = stations.get(selectedPosition).getId();                          int stationID = stations.entries.get(selectedPosition).getId();
639    
640                          if (!favorites.contains(stationID)) {                          if (!favorites.contains(stationID)) {
641                                  menu.add(0, FAVORITES_ADD, 0, "Add to favorites");                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
642                          } else {                          } else {
643                                  menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites");                                  menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
644                          }                          }
645                                                    
646                  }                  }
647                                    
648                  public void onContextItemSelected(MenuItem item) {                  public void onContextItemSelected(MenuItem item) {
649                          StationBean sb = stations.get(selectedPosition);                          StationEntry sb = stations.entries.get(selectedPosition);
650                                                    
651                          int stationID = sb.getId();                          int stationID = sb.getId();
652                          if (item.getItemId() == FAVORITES_ADD) {                          if (item.getItemId() == FAVORITES_ADD) {
653                                  favorites.add(stationID);                                  favorites.add(stationID);
654                                  Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
655                          } else {                          } else {
656                                                                    
657                                  favorites.remove(stationID);                                  favorites.remove(stationID);
658                                  Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
659                                                                    
660                                                                    
661                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
662                                          stations.remove(selectedPosition);                                          stations.entries.remove(selectedPosition);
663                                          adapter.notifyDataSetChanged();                                          adapter.notifyDataSetChanged();
664                                  }                                  }
665                          }                          }

Legend:
Removed from v.557  
changed lines
  Added in v.1408

  ViewVC Help
Powered by ViewVC 1.1.20