/[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 489 by torben, Thu Oct 29 14:20:16 2009 UTC revision 1164 by torben, Wed Oct 6 10:42:51 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
3    import static dk.thoerup.traininfo.R.string.app_name;
4    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_search;
27    import static dk.thoerup.traininfo.R.string.stationlist_stationadded;
28    import static dk.thoerup.traininfo.R.string.stationlist_stationmap;
29    import static dk.thoerup.traininfo.R.string.stationlist_stationremoved;
30    import static dk.thoerup.traininfo.R.string.stationlist_stationsearch;
31    import static dk.thoerup.traininfo.R.string.stationlist_twocharmin;
32    import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation;
33    import static dk.thoerup.traininfo.R.string.stationlist_satellitecount;
34    
35  import java.util.ArrayList;  import java.util.ArrayList;
 import java.util.List;  
 import java.util.Locale;  
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 12  import android.content.DialogInterface; Line 43  import android.content.DialogInterface;
43  import android.content.Intent;  import android.content.Intent;
44  import android.content.SharedPreferences;  import android.content.SharedPreferences;
45  import android.content.SharedPreferences.Editor;  import android.content.SharedPreferences.Editor;
 import android.location.Address;  
 import android.location.Geocoder;  
46  import android.location.Location;  import android.location.Location;
47  import android.os.AsyncTask;  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;
 import android.util.Log;  
51  import android.view.ContextMenu;  import android.view.ContextMenu;
52  import android.view.LayoutInflater;  import android.view.LayoutInflater;
53  import android.view.Menu;  import android.view.Menu;
# Line 31  import android.widget.AdapterView; Line 59  import android.widget.AdapterView;
59  import android.widget.EditText;  import android.widget.EditText;
60  import android.widget.ListView;  import android.widget.ListView;
61  import android.widget.Toast;  import android.widget.Toast;
62    import dk.thoerup.android.traininfo.common.StationBean;
63    import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
64  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
65  import dk.thoerup.traininfo.provider.StationProvider;  import dk.thoerup.traininfo.provider.StationProvider;
66  import dk.thoerup.traininfo.stationmap.GeoPair;  import dk.thoerup.traininfo.stationmap.GeoPair;
# Line 39  import dk.thoerup.traininfo.util.IntSet; Line 69  import dk.thoerup.traininfo.util.IntSet;
69  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
70    
71  public class StationList extends ListActivity  {  public class StationList extends ListActivity  {
72          public static final int GOTLOCATION = 1001;  
73          public static final int GOTSTATIONLIST = 1002;          
         public static final int NOPROVIDER = 1003;  
         public static final int LOCATIONFIXTIMEOUT = 1004;  
74                    
75          public static final int OPTIONS_MAP = 2003;          public static final int OPTIONS_MAP = 2003;
76          public static final int OPTIONS_GPSINFO = 2004;          public static final int OPTIONS_GPSINFO = 2004;
   
           
   
77                    
78          public static final int DLG_PROGRESS = 3001;          public static final int DLG_PROGRESS = 3001;
79          public static final int DLG_STATIONNAME = 3002;          public static final int DLG_STATIONNAME = 3002;
80                    
81            
82            public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds
83            
84            
85          static enum LookupMethod {          static enum LookupMethod {
86                  ByLocation,                  ByLocation,
87                  ByName,                  ByName,
# Line 67  public class StationList extends ListAct Line 96  public class StationList extends ListAct
96          FindStationsTask findStationsTask;          FindStationsTask findStationsTask;
97          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
98                    
99          GeoPair location = new GeoPair();          //GeoPair location = new GeoPair();
100            
101          boolean isRunning = false;          boolean isLaunchedforShortcut;
102          List<StationBean> stations = new ArrayList<StationBean>();  
103            StationBean stations = new StationBean();
104                    
105          StationProvider stationProvider = ProviderFactory.getStationProvider();          StationProvider stationProvider = ProviderFactory.getStationProvider();
106                                    
# Line 81  public class StationList extends ListAct Line 111  public class StationList extends ListAct
111    
112          WelcomeScreen.ListType listType;          WelcomeScreen.ListType listType;
113          SharedPreferences prefs;          SharedPreferences prefs;
114                                    
115          ///////////////////////////////////////////////////////////////////////////////////////////          ///////////////////////////////////////////////////////////////////////////////////////////
116          //Activity call backs          //Activity call backs
117                    
118          @SuppressWarnings("unchecked")  
119          @Override          @Override
120          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
121                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
# Line 98  public class StationList extends ListAct Line 128  public class StationList extends ListAct
128                  ListView lv = getListView();                  ListView lv = getListView();
129                  lv.setOnCreateContextMenuListener(contextMenu);                  lv.setOnCreateContextMenuListener(contextMenu);
130                                    
131                  locationLookup = new LocationLookup(this, stationsFetched);                  locationLookup = new LocationLookup(this);
132                                    
133    
134                  prefs = getSharedPreferences("TrainStation", 0);                  prefs = getSharedPreferences("TrainStation", 0);
# Line 110  public class StationList extends ListAct Line 140  public class StationList extends ListAct
140                  listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");                  listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
141                  setTitle();                  setTitle();
142                                    
143                    isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);
144                    
145                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
146                    
147                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
148    
149                                                    
# Line 117  public class StationList extends ListAct Line 151  public class StationList extends ListAct
151                          case ListNearest:                          case ListNearest:
152                                  startLookup();                                  startLookup();
153                                  break;                                  break;
154                          case ListSearch:                          case ListSearch:                                
155                                  this.showDialog(DLG_STATIONNAME);                                  showDialog(DLG_STATIONNAME);
156                                  break;                                  break;
157                          case ListFavorites:                          case ListFavorites:
158                                  startFavoriteLookup();                                  startFavoriteLookup();
# Line 128  public class StationList extends ListAct Line 162  public class StationList extends ListAct
162                          }                          }
163                                                    
164                  } else {                  } else {
165                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");                          stations = (StationBean) savedInstanceState.getSerializable("stations");
166                          adapter.setStations(stations);                          adapter.setStations(stations);
                         location = (GeoPair) savedInstanceState.getSerializable("location");  
167                  }                  }
168                                    
169          }          }
170            
171            
172    
173    
174    
175    
176            @Override
177            protected void onDestroy() {
178                    super.onDestroy();
179                    
180                    
181                    if (locationLookup != null) {
182                            locationLookup.stopSearch();
183                    }
184                    if (findStationsTask != null) {
185                            findStationsTask.cancel(true);
186                    }              
187            }
188    
189            
190          protected void setTitle() {          protected void setTitle() {
191                  String dialogTitle = getResources().getString(R.string.app_name);                  String dialogTitle = getResources().getString(app_name);
192                  switch (listType) {                  switch (listType) {
193                  case ListNearest:                  case ListNearest:
194                          dialogTitle += " - Nearby stations";                          dialogTitle += " - " + getString(stationlist_nearbystations);
195                          break;                          break;
196                  case ListSearch:                  case ListSearch:
197                          dialogTitle += " - Search";                          dialogTitle += " - " + getString(stationlist_search);
198                          break;                          break;
199                  case ListFavorites:                  case ListFavorites:
200                          dialogTitle += " - Favorites";                          dialogTitle += " - " + getString(stationlist_favorites);
201                          break;                          break;
202                  default:                  default:
203                          dialogTitle = "";//not possible                                                          dialogTitle = "";//not possible                                
204                  }                  }
205                    
206                  setTitle(dialogTitle);                  setTitle(dialogTitle);
207                    
208          }          }
209                    
210                            
   
211    
212      @Override      @Override
213      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
214      {      {
215          if (dialog != null && dialog.isShowing())          if (dialog != null && dialog.isShowing())
216                  dialog.dismiss();                  dialog.dismiss();
217          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (StationBean) stations);
         outState.putSerializable("location", location);  
218                    
219      }      }
220                    
# Line 171  public class StationList extends ListAct Line 223  public class StationList extends ListAct
223          @Override          @Override
224          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
225                  MenuItem item;                  MenuItem item;
226                                                    
227                  item = menu.add(0, OPTIONS_MAP, 0, "Station map");                  item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
228                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
229                                    
230                  item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info");                  item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
231                  item.setIcon(android.R.drawable.ic_menu_mapmode);                                item.setIcon(android.R.drawable.ic_menu_mapmode);              
232                                    
233                  return true;                  return true;
# Line 192  public class StationList extends ListAct Line 244  public class StationList extends ListAct
244                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
245                                                    
246                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
247                          for (StationBean st : stations ) {                          for (StationEntry st : stations.entries ) {
248                                  stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );                                  stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
249                          }                          }
250                                                    
# Line 203  public class StationList extends ListAct Line 255  public class StationList extends ListAct
255                  case OPTIONS_GPSINFO:                  case OPTIONS_GPSINFO:
256                          Location loc = locationLookup.getLocation();                          Location loc = locationLookup.getLocation();
257                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
258                          message.append("Location info:\n");                          message.append( getString(stationlist_locationinfo) ).append(":\n");
259                          message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n");                          if (loc != null) {
260                          message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n");                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
261                                    message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
262                          MessageBox.showMessage(this, message.toString());                                  message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
263                                    message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
264                            } else {
265                                    message.append( getString(stationlist_nolocation) );
266                            }                      
267                            
268                            MessageBox.showMessage(this, message.toString(), false);
269                          break;                          break;
270                  default:                  default:
271                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 225  public class StationList extends ListAct Line 283  public class StationList extends ListAct
283    
284    
285          }          }
286            
287            public void showMessageAndClose(String message) {
288                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
289                    builder.setMessage(message)
290                    .setCancelable(false)
291                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
292                            public void onClick(DialogInterface dialog, int id) {
293                                    dialog.dismiss();
294                                    StationList.this.finish();
295                            }
296                    })
297                    .show();
298            }
299    
300    
301    
# Line 234  public class StationList extends ListAct Line 305  public class StationList extends ListAct
305                  switch (id) {                  switch (id) {
306                  case DLG_PROGRESS:                  case DLG_PROGRESS:
307                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
308                          dlg.setMessage("Wait for location fix");                          dlg.setMessage( getString(stationlist_waitforlocation) );
309                          dlg.setCancelable(false);                          dlg.setCancelable(false);
310                          return dlg;                                              return dlg;                    
311                  case DLG_STATIONNAME:                  case DLG_STATIONNAME:
# Line 244  public class StationList extends ListAct Line 315  public class StationList extends ListAct
315                                                    
316                          AlertDialog.Builder builder = new AlertDialog.Builder(this);                          AlertDialog.Builder builder = new AlertDialog.Builder(this);
317                                                    
318                          builder.setTitle("Station search");                          builder.setTitle( getString(stationlist_stationsearch) );
319                          builder.setView(rootView);                          builder.setView(rootView);
320                          builder.setCancelable(true);                          builder.setCancelable(true);
321                          builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {                          builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
322                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
323                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
324                                          dialog.dismiss();                                          dialog.dismiss();
325                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
326                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
327                                                    startNameSearch(search);
328                                          } else {                                          } else {
329                                                  MessageBox.showMessage(StationList.this, "Two characters minimum" );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
330                                          }                                          }
331                                  }                                  }
332                          });                          });
333                          builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                          builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
334                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
335                                          dialog.dismiss();                                          dialog.dismiss();
336                                            StationList.this.finish(); // Close this Activity
337                                  }                                  }
338                          });                                              });                    
339                          return builder.create();                          return builder.create();
# Line 290  public class StationList extends ListAct Line 363  public class StationList extends ListAct
363          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
364                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
365                                                                    
366                  StationBean station = stations.get(position);                  StationEntry station = stations.entries.get(position);
   
                 double latitude = station.getLatitude();  
                 double longitude = station.getLongitude();  
   
   
367                                    
368                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
369                  intent.putExtra("name", station.getName());                          Intent i = new Intent();
370                  intent.putExtra("distance", station.getDistance());                          i.putExtra("station", station);
371                  intent.putExtra("latitude", latitude);                          setResult(Activity.RESULT_OK, i);
372                  intent.putExtra("longitude", longitude);                          finish();
373                  intent.putExtra("stationid", station.getId());                  } else {                
374                  intent.putExtra("address", station.getAddress());                          Intent intent = new Intent(this, DepartureList.class);
375                  startActivity(intent);                          intent.putExtra("stationbean", station);
376                            startActivity(intent);
377                    }
378          }          }
379    
380          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
381          //          //
382    
383          public void startLookup() {          public void startLookup() {
384                  isRunning = true;                  dialogMessage = getString( stationlist_waitforlocation );
                 dialogMessage = "Wait for location fix";  
385                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
386                                    
387                  locationLookup.locateStations();                  locationLookup.locateStations();
388                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(0, 500);
389          }          }
390                    
391          void startNameSearch(String name) {          void startNameSearch(String name) {
392                  dialogMessage = "Finding stations by name";                  dialogMessage = getString( stationlist_findbyname );
393                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
394    
395                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
396                  findStationsTask.searchByName(name, locationLookup.getLocation());                  findStationsTask.searchByName(name);
397                  findStationsTask.execute();                  findStationsTask.execute();
398                                    
399          }          }
# Line 332  public class StationList extends ListAct Line 401  public class StationList extends ListAct
401          public void startFavoriteLookup() {          public void startFavoriteLookup() {
402                                    
403                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
404                          dialogMessage = "Loading favorites";                          dialogMessage = getString( stationlist_loadfavorites );
405                          showDialog(DLG_PROGRESS);                          showDialog(DLG_PROGRESS);
406    
407                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
408                          findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation());                          findStationsTask.searchByIds( favorites.toString() );
409                          findStationsTask.execute();                          findStationsTask.execute();
410                  } else {                  } else {
411                          MessageBox.showMessage(this, "Favorite list is empty");                          showMessageAndClose( getString( stationlist_nofavorites ) );
412                  }                  }
413          }          }
414    
# Line 347  public class StationList extends ListAct Line 416  public class StationList extends ListAct
416                    
417          void startLocatorTask()          void startLocatorTask()
418          {          {
419                  dialogMessage = "Finding nearby stations";                  dialogMessage = getString( stationlist_findingnearby );
420                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);              
421                                    
422                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
423                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
424                  findStationsTask.execute();                      findStationsTask.execute();    
425          }          }
           
   
         String lookupAddress(double latitude, double longitude) {  
426                                    
                 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();  
         }  
           
427                    
428          ////////////////////////////////////////////////////////////////////////////          ////////////////////////////////////////////////////////////////////////////
429          // Inner classes          // Inner classes
# Line 390  public class StationList extends ListAct Line 432  public class StationList extends ListAct
432                  @Override                  @Override
433                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
434    
435                          switch (msg.what) {                          LocationLookup.LookupStates state = locationLookup.getState();
436    
437    
438                            switch (state) {
439                          case GOTLOCATION:                          case GOTLOCATION:
440                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
441                                    
442                                  startLocatorTask();                                  startLocatorTask();
443                                  location = GeoPair.fromLocation( locationLookup.getLocation() );  
444                                                                    return;
                                 break;  
445    
446                          case NOPROVIDER:                          case NOPROVIDER:
447                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
448                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
449                                  break;                                  //StationList.this.finish();
450                          case LOCATIONFIXTIMEOUT:                                                                  return;
451                                  if (isRunning) {                          }
452                                          locationLookup.stopSearch();  
453                                          if (locationLookup.hasLocation()) {  
454                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );  
455                                          } else {                                                                          if (locationLookup.elapsedTime() >=  GPS_TIMEOUT_MS) {
456                                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
457                                                    
458                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  locationLookup.stopSearch();
459                                                  builder.setMessage("Location fix timed out");  
460                                                  builder.setCancelable(true);                                  if (locationLookup.hasLocation()) {
461                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                          startLocatorTask();
462                                                          public void onClick(DialogInterface dialog, int id) {                                  } else {                                                
463                                                                  dialog.dismiss();                                          AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
464                                                                  startLookup();                                          builder.setMessage(  getString( stationlist_gpstimeout) );
465                                                                                                            builder.setCancelable(true);
466                                                          }                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
467                                                  });                                                  public void onClick(DialogInterface dialog, int id) {
468                                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                                          dialog.dismiss();
469                                                          public void onClick(DialogInterface dialog, int id) {                                                          startLookup();
470                                                                  dialog.dismiss();  
471                                                          }                                                                                                        }
472                                                  });                                                                                                                                      });
473                                                  builder.show();                                          builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
474                                                    public void onClick(DialogInterface dialog, int id) {
475                                                            dialog.dismiss();
476                                                            StationList.this.finish(); // Close this Activity
477                                                    }                                                      
478                                            });
479                                            builder.show();
480    
                                         }  
481                                  }                                  }
482                                  break;                          } else {
483                                    if (locationLookup.hasGps()) {
484                                            int count = locationLookup.getSatCount();
485                                            String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count;
486                                            dialog.setMessage( dialogMessage );
487                                    }
488                                    this.sendEmptyMessageDelayed(0, 500);
489                          }                          }
490                          isRunning = false;                          
491                  }                  }
492          };          }
493    
494    
495    
496                    
497          class FindStationsTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
498                                    
499                  LookupMethod method = LookupMethod.MethodNone;                  LookupMethod method = LookupMethod.MethodNone;
                 boolean success;  
500                  String name;                  String name;
501                  Location loc;                  Location loc;
502                  String ids;                  String ids;
503                                    
504                  public void searchByName(String n, Location l) {                  public void searchByName(String n) {
505                                                    
506                          method = LookupMethod.ByName;                          method = LookupMethod.ByName;
                         loc = l;  
507                          name = n;                          name = n;
508                  }                  }
509                                    
# Line 457  public class StationList extends ListAct Line 512  public class StationList extends ListAct
512                          loc = l;                          loc = l;
513                  }                  }
514                                    
515                  public void searchByIds(String id, Location l) {                  public void searchByIds(String id) {
516                                                    
517                          method = LookupMethod.ByList;                          method = LookupMethod.ByList;
                         loc = l;  
518                          ids = id;                          ids = id;
519                  }                  }
520                                    
# Line 477  public class StationList extends ListAct Line 531  public class StationList extends ListAct
531    
532                          switch (method) {                          switch (method) {
533                          case ByLocation:                          case ByLocation:
534                                  success = stationProvider.lookupStations(loc);                                  stations = stationProvider.lookupStationsByLocation(loc);
535                                  break;                                  break;
536                          case ByName:                          case ByName:
537                                  success = stationProvider.lookupStationsByName(name);                                  stations = stationProvider.lookupStationsByName(name);
538                                  break;                                  break;
539                          case ByList:                          case ByList:
540                                  success = stationProvider.lookupStationsByIds(ids);                                  stations = stationProvider.lookupStationsByIds(ids);
541                                  break;                                  break;
542                          default:                          default:
543                                  success = false; // not possible                                          stations = null; // not possible        
544                          }                          }
545                                                    
546                                                    
                         Location dummy = new Location("gps");  
                         List<StationBean> stations = stationProvider.getStations();  
                           
                         for (StationBean station : stations) {  
                                 String addr = lookupAddress(station.getLatitude(), station.getLongitude());  
                                 station.setAddress(addr);  
                                   
                                   
                                 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);  
                                         }  
                                 }  
   
                         }                                                
                           
547                          return null;                          return null;
548                  }                  }
549    
# Line 519  public class StationList extends ListAct Line 553  public class StationList extends ListAct
553                          dialog.dismiss();                          dialog.dismiss();
554                                                    
555                                                    
556                          if (success) {                                                    if (stations != null) {                        
557                                  if (stationProvider.getStations().size() == 0)                                  if (stations.entries.size() == 0) {
558                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                          showMessageAndClose(getString(stationlist_nostations));
559                                  stations = stationProvider.getStations();                                  }
560    
561                                    StationList.this.getListView().invalidateViews();
562                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
563                                                                    
564                                    
565                          } else { //communication or parse errors                          } else { //communication or parse errors
566                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
567                                  builder.setMessage("Error on finding nearby stations");                                  builder.setMessage(getString(stationlist_fetcherror));                          
568                                  builder.setCancelable(true);                                  builder.setCancelable(true);
569                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
570                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
571                                                  dialog.dismiss();                                                  dialog.dismiss();
572                                                                                                    
573                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
574                                                          @Override                                                  switch (method) {
575                                                          public void run() {                                                  case ByLocation:
576                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
577                                                          }                                                                  @Override
578                                                  });                                                                  public void run() {
579                                                                            startLocatorTask();                                                            
580                                                                    }
581                                                            };
582                                                            break;
583                                                    case ByName:
584                                                            runner = new Runnable() {
585                                                                    @Override
586                                                                    public void run() {
587                                                                            startNameSearch( FindStationsTask.this.name );
588                                                                    }
589                                                            };
590                                                            break;
591                                                    case ByList:
592                                                            runner = new Runnable() {
593                                                                    @Override
594                                                                    public void run() {
595                                                                            startFavoriteLookup();
596                                                                    }
597                                                            };
598                                                            break;
599                                                    }
600                                                    
601                                                    stationsFetched.post( runner );
602                                          }                                          }
603                                  });                                  });
604                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
605                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
606                                                  dialog.dismiss();                                                  dialog.dismiss();
607                                                    StationList.this.finish();
608                                          }                                                                                                }                                                      
609                                  });                                                                                                                              });
610                                  builder.show();                                                          
611                                    builder.show();
612                          }                          }
613                  }                  }
614          }          }
# Line 564  public class StationList extends ListAct Line 626  public class StationList extends ListAct
626                                                                                                    
627                          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;                          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
628                          selectedPosition = info.position;                          selectedPosition = info.position;
629                          int stationID = stations.get(selectedPosition).getId();                          int stationID = stations.entries.get(selectedPosition).getId();
630    
631                          if (!favorites.contains(stationID)) {                          if (!favorites.contains(stationID)) {
632                                  menu.add(0, FAVORITES_ADD, 0, "Add to favorites");                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
633                          } else {                          } else {
634                                  menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites");                                  menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
635                          }                          }
636                                                    
637                  }                  }
638                                    
639                  public void onContextItemSelected(MenuItem item) {                  public void onContextItemSelected(MenuItem item) {
640                          StationBean sb = stations.get(selectedPosition);                          StationEntry sb = stations.entries.get(selectedPosition);
641                                                    
642                          int stationID = sb.getId();                          int stationID = sb.getId();
643                          if (item.getItemId() == FAVORITES_ADD) {                          if (item.getItemId() == FAVORITES_ADD) {
644                                  favorites.add(stationID);                                  favorites.add(stationID);
645                                  Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
646                          } else {                          } else {
647                                                                    
648                                  favorites.remove(stationID);                                  favorites.remove(stationID);
649                                  Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
650                                                                    
651                                                                    
652                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
653                                          stations.remove(selectedPosition);                                          stations.entries.remove(selectedPosition);
654                                          adapter.notifyDataSetChanged();                                          adapter.notifyDataSetChanged();
655                                  }                                  }
656                          }                          }

Legend:
Removed from v.489  
changed lines
  Added in v.1164

  ViewVC Help
Powered by ViewVC 1.1.20