/[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 401 by torben, Tue Oct 6 19:13:34 2009 UTC revision 566 by torben, Thu Jan 28 10:34:50 2010 UTC
# Line 2  package dk.thoerup.traininfo; Line 2  package dk.thoerup.traininfo;
2    
3  import java.util.ArrayList;  import java.util.ArrayList;
4  import java.util.List;  import java.util.List;
5  import java.util.Locale;  
6    
7  import android.app.AlertDialog;  import android.app.AlertDialog;
8  import android.app.Dialog;  import android.app.Dialog;
# Line 10  import android.app.ListActivity; Line 10  import android.app.ListActivity;
10  import android.app.ProgressDialog;  import android.app.ProgressDialog;
11  import android.content.DialogInterface;  import android.content.DialogInterface;
12  import android.content.Intent;  import android.content.Intent;
13  import android.location.Address;  import android.content.SharedPreferences;
14  import android.location.Geocoder;  import android.content.SharedPreferences.Editor;
15  import android.location.Location;  import android.location.Location;
16  import android.os.AsyncTask;  import android.os.AsyncTask;
17  import android.os.Bundle;  import android.os.Bundle;
18  import android.os.Handler;  import android.os.Handler;
19  import android.os.Message;  import android.os.Message;
20  import android.util.Log;  
21    import android.view.ContextMenu;
22  import android.view.LayoutInflater;  import android.view.LayoutInflater;
23  import android.view.Menu;  import android.view.Menu;
24  import android.view.MenuItem;  import android.view.MenuItem;
25  import android.view.View;  import android.view.View;
26    import android.view.ContextMenu.ContextMenuInfo;
27    import android.view.View.OnCreateContextMenuListener;
28    import android.widget.AdapterView;
29  import android.widget.EditText;  import android.widget.EditText;
30  import android.widget.ListView;  import android.widget.ListView;
31    import android.widget.Toast;
32  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
33  import dk.thoerup.traininfo.provider.StationProvider;  import dk.thoerup.traininfo.provider.StationProvider;
34  import dk.thoerup.traininfo.stationmap.GeoPair;  import dk.thoerup.traininfo.stationmap.GeoPair;
35  import dk.thoerup.traininfo.stationmap.StationMapView;  import dk.thoerup.traininfo.stationmap.StationMapView;
36    import dk.thoerup.traininfo.util.IntSet;
37  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
38    
39    import static dk.thoerup.traininfo.R.string.*;
40    
41  public class StationList extends ListActivity  {  public class StationList extends ListActivity  {
42          public static final int GOTLOCATION = 1001;          public static final int GOTLOCATION = 1001;
43          public static final int GOTSTATIONLIST = 1002;          public static final int GOTSTATIONLIST = 1002;
44          public static final int NOPROVIDER = 1003;          public static final int NOPROVIDER = 1003;
45          public static final int LOCATIONFIXTIMEOUT = 1004;          public static final int LOCATIONFIXTIMEOUT = 1004;
46                    
         public static final int OPTIONS_RESCAN = 2001;  
         public static final int OPTIONS_NAMESEARCH = 2002;  
47          public static final int OPTIONS_MAP = 2003;          public static final int OPTIONS_MAP = 2003;
48          public static final int OPTIONS_ABOUT = 2004;          public static final int OPTIONS_GPSINFO = 2004;
49    
50            
51    
52                    
53          public static final int DLG_PROGRESS = 3001;          public static final int DLG_PROGRESS = 3001;
54          public static final int DLG_STATIONNAME = 3002;          public static final int DLG_STATIONNAME = 3002;
55                    
56          /** Called when the activity is first created. */          static enum LookupMethod {
57                    ByLocation,
58                    ByName,
59                    ByList,
60                    MethodNone
61            }
62            
63            
64          String dialogMessage = "";          String dialogMessage = "";
65          ProgressDialog dialog;          ProgressDialog dialog;
66          LocationLookup locator = null;          LocationLookup locationLookup = null;
67          LocatorTask locatorTask;          FindStationsTask findStationsTask;
68          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
69                    
70          GeoPair location = new GeoPair();          GeoPair location = new GeoPair();
# Line 61  public class StationList extends ListAct Line 76  public class StationList extends ListAct
76                                    
77          StationListAdapter adapter = null;          StationListAdapter adapter = null;
78                    
79          static enum LookupMethod {          FavoritesMenu contextMenu = new FavoritesMenu();
80                  ByLocation,          IntSet favorites = new IntSet();
81                  ByName,  
82                  MethodNone          WelcomeScreen.ListType listType;
83          }          SharedPreferences prefs;
84                    
85          ///////////////////////////////////////////////////////////////////////////////////////////          ///////////////////////////////////////////////////////////////////////////////////////////
86          //Activity call backs          //Activity call backs
# Line 74  public class StationList extends ListAct Line 89  public class StationList extends ListAct
89          @Override          @Override
90          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
91                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
92                  setContentView(R.layout.main);                  setContentView(R.layout.stationlist);
93                                    
94                                    
95                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this);
96                  setListAdapter(adapter);                  setListAdapter(adapter);
97                                    
98                  locator = new LocationLookup(this, stationsFetched);                  ListView lv = getListView();
99                    lv.setOnCreateContextMenuListener(contextMenu);
100                    
101                    locationLookup = new LocationLookup(this, stationsFetched);
102                    
103    
104                    prefs = getSharedPreferences("TrainStation", 0);
105                    String favoriteString = prefs.getString("favorites", "");
106                    if (! favoriteString.equals("") ) {
107                            favorites.fromString(favoriteString);
108                    }
109                    
110                    listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
111                    setTitle();
112                    
113                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
114                          startLookup();  
115                            
116                            switch (listType) {
117                            case ListNearest:
118                                    startLookup();
119                                    break;
120                            case ListSearch:
121                                    this.showDialog(DLG_STATIONNAME);
122                                    break;
123                            case ListFavorites:
124                                    startFavoriteLookup();
125                                    break;
126                            default:
127                                    // Not possible !?!
128                            }
129                            
130                  } else {                  } else {
131                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
132                          adapter.setStations(stations);                          adapter.setStations(stations);
133                          location = (GeoPair) savedInstanceState.getSerializable("location");                          location = (GeoPair) savedInstanceState.getSerializable("location");
134                  }                  }
135                    
136            }
137            protected void setTitle() {
138                    String dialogTitle = getResources().getString(app_name);
139                    switch (listType) {
140                    case ListNearest:
141                            dialogTitle += " - " + getString(stationlist_nearbystations);
142                            break;
143                    case ListSearch:
144                            dialogTitle += " - " + getString(stationlist_search);
145                            break;
146                    case ListFavorites:
147                            dialogTitle += " - " + getString(stationlist_favorites);
148                            break;
149                    default:
150                            dialogTitle = "";//not possible                                
151                    }
152            
153                    setTitle(dialogTitle);
154          }          }
155            
156            
157    
158    
159      @Override      @Override
# Line 98  public class StationList extends ListAct Line 163  public class StationList extends ListAct
163                  dialog.dismiss();                  dialog.dismiss();
164          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (ArrayList<StationBean>) stations);
165          outState.putSerializable("location", location);          outState.putSerializable("location", location);
166            
167      }      }
168                    
169                    
170    
171          @Override          @Override
172          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
173                  menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations");                  MenuItem item;
174                  menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station");                  
175                  menu.add(0, OPTIONS_MAP, 0, "Show station map");                  item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
176                  menu.add(0, OPTIONS_ABOUT, 0, "About");                  item.setIcon(android.R.drawable.ic_menu_mapmode);
177                    
178                    item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
179                    item.setIcon(android.R.drawable.ic_menu_mapmode);              
180                    
181                  return true;                  return true;
182          }          }
183    
# Line 115  public class StationList extends ListAct Line 185  public class StationList extends ListAct
185          public boolean onOptionsItemSelected(MenuItem item) {          public boolean onOptionsItemSelected(MenuItem item) {
186                  boolean retval = true;                  boolean retval = true;
187    
188                                    //TODO: Cleanup
189                  switch (item.getItemId()) {                  switch (item.getItemId()) {
                 case OPTIONS_RESCAN:  
                         startLookup();  
                         break;  
                 case OPTIONS_NAMESEARCH:  
                         showDialog(DLG_STATIONNAME);  
                         break;  
190                  case OPTIONS_MAP:                  case OPTIONS_MAP:
191                                                    
192                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
                         intent.putExtra("userlocation", location );  
193                                                    
194                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
195                          for (StationBean st : stations ) {                          for (StationBean st : stations ) {
# Line 137  public class StationList extends ListAct Line 200  public class StationList extends ListAct
200                                                    
201                          startActivity(intent);                          startActivity(intent);
202                          break;                          break;
203                  case OPTIONS_ABOUT:                  case OPTIONS_GPSINFO:
204                          String ver = this.getResources().getString(R.string.app_version);                          Location loc = locationLookup.getLocation();
                           
                         Location loc = locator.getLocation();  
205                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
206                          message.append("TrainInfo DK v").append(ver).append("\n");                          message.append( getString(stationlist_locationinfo) ).append(":\n");
207                          message.append("By Torben Nielsen\n");                          if (loc != null) {
208                          message.append("\n");                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
209                          message.append("Location info:\n");                                  message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
210                          message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n");                                  message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
211                          message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n");                                  message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
212                            } else {
213                                    message.append( getString(stationlist_nolocation) );
214                            }                      
215                            
216                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString());
217                          break;                          break;
218                  default:                  default:
# Line 157  public class StationList extends ListAct Line 221  public class StationList extends ListAct
221                                    
222                  return retval;                  return retval;
223          }          }
224            
225            
226    
227            @Override
228            public boolean onContextItemSelected(MenuItem item) {
229                    contextMenu.onContextItemSelected(item);
230                    return true;
231    
232    
233            }
234            
235            public void showMessageAndClose(String message) {
236                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
237                    builder.setMessage(message)
238                    .setCancelable(false)
239                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
240                            public void onClick(DialogInterface dialog, int id) {
241                                    dialog.dismiss();
242                                    StationList.this.finish();
243                            }
244                    })
245                    .show();
246            }
247    
248    
249    
250    
251          @Override          @Override
252          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
253                  switch (id) {                  switch (id) {
254                  case DLG_PROGRESS:                  case DLG_PROGRESS:
255                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
256                          dlg.setMessage("Wait for location fix");                          dlg.setMessage( getString(stationlist_waitforlocation) );
257                          dlg.setCancelable(false);                          dlg.setCancelable(false);
258                          return dlg;                                              return dlg;                    
259                  case DLG_STATIONNAME:                  case DLG_STATIONNAME:
# Line 173  public class StationList extends ListAct Line 263  public class StationList extends ListAct
263                                                    
264                          AlertDialog.Builder builder = new AlertDialog.Builder(this);                          AlertDialog.Builder builder = new AlertDialog.Builder(this);
265                                                    
266                          builder.setTitle("Station search");                          builder.setTitle( getString(stationlist_stationsearch) );
267                          builder.setView(rootView);                          builder.setView(rootView);
268                          builder.setCancelable(true);                          builder.setCancelable(true);
269                          builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {                          builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
270                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
271                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
272                                          dialog.dismiss();                                          dialog.dismiss();
273                                          if (et.getText().toString().length() >= 2) {                                          if (et.getText().toString().length() >= 2) {
274                                                  startNameSearch(et.getText().toString());                                                  startNameSearch(et.getText().toString());
275                                          } else {                                          } else {
276                                                  MessageBox.showMessage(StationList.this, "Two characters minimum" );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
277                                          }                                          }
278                                  }                                  }
279                          });                          });
280                          builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                          builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
281                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
282                                          dialog.dismiss();                                          dialog.dismiss();
283                                            StationList.this.finish(); // Close this Activity
284                                  }                                  }
285                          });                                              });                    
286                          return builder.create();                          return builder.create();
# Line 220  public class StationList extends ListAct Line 311  public class StationList extends ListAct
311                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
312                                                                    
313                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
   
                 double latitude = station.getLatitude();  
                 double longitude = station.getLongitude();  
   
   
314                                    
315                  Intent intent = new Intent(this, DepartureList.class);                  Intent intent = new Intent(this, DepartureList.class);
316                  intent.putExtra("name", station.getName());                  intent.putExtra("stationbean", station);
                 intent.putExtra("distance", station.getDistance());  
                 intent.putExtra("latitude", latitude);  
                 intent.putExtra("longitude", longitude);  
                 intent.putExtra("stationid", station.getId());  
                 intent.putExtra("address", station.getAddress());  
317                  startActivity(intent);                  startActivity(intent);
318          }          }
319    
# Line 240  public class StationList extends ListAct Line 321  public class StationList extends ListAct
321          //          //
322    
323          public void startLookup() {          public void startLookup() {
324                  isRunning = true;                  isRunning = true;              
325                  dialogMessage = "Wait for location fix";                  dialogMessage = getString( stationlist_waitforlocation );
326                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
327                                    
328                  locator.locateStations();                  locationLookup.locateStations();
329                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
330          }          }
331                    
332          void startNameSearch(String name) {          void startNameSearch(String name) {
333                  dialogMessage = "Finding stations by name";                  dialogMessage = getString( stationlist_findbyname );
334                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
335    
336                  locatorTask = new LocatorTask();                  findStationsTask = new FindStationsTask();
337                  locatorTask.searchByName(name, locator.getLocation());                  findStationsTask.searchByName(name, locationLookup.getLocation());
338                  locatorTask.execute();                  findStationsTask.execute();
339                    
340            }
341            
342            public void startFavoriteLookup() {
343                                    
344                    if (favorites.size() > 0) {
345                            dialogMessage = getString( stationlist_loadfavorites );
346                            showDialog(DLG_PROGRESS);
347    
348                            findStationsTask = new FindStationsTask();
349                            findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation());
350                            findStationsTask.execute();
351                    } else {
352                            showMessageAndClose( getString( stationlist_nofavorites ) );
353                    }
354          }          }
355    
356    
357                    
358          void startLocatorTask()          void startLocatorTask()
359          {          {
360                  dialogMessage = "Finding nearby stations";                  dialogMessage = getString( stationlist_findingnearby );
361                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
362                                    
363                  locatorTask = new LocatorTask();                  findStationsTask = new FindStationsTask();
364                  locatorTask.searchByLocation( locator.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
365                  locatorTask.execute();                    findStationsTask.execute();    
366          }          }
367                    
368    
369            /* TODO: Remove this no longer needed function
370          String lookupAddress(double latitude, double longitude) {          String lookupAddress(double latitude, double longitude) {
371                                    
372                  Geocoder coder = new Geocoder(this, new Locale("da"));                  Geocoder coder = new Geocoder(this, new Locale("da"));
# Line 295  public class StationList extends ListAct Line 391  public class StationList extends ListAct
391                  }                  }
392                                    
393                  return sb.toString();                  return sb.toString();
394          }          }*/
395                    
396                    
397          ////////////////////////////////////////////////////////////////////////////          ////////////////////////////////////////////////////////////////////////////
# Line 310  public class StationList extends ListAct Line 406  public class StationList extends ListAct
406                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
407                                                                    
408                                  startLocatorTask();                                  startLocatorTask();
409                                  location = GeoPair.fromLocation( locator.getLocation() );                                  location = GeoPair.fromLocation( locationLookup.getLocation() );
410                                                                    
411                                  break;                                  break;
412    
413                          case NOPROVIDER:                          case NOPROVIDER:
414                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
415                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );
416                                  break;                                  break;
417                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
418                                  if (isRunning) {                                  if (isRunning) {
419                                          locator.stopSearch();                                          locationLookup.stopSearch();
420                                          if (locator.hasLocation()) {                                          if (locationLookup.hasLocation()) {
421                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
422                                          } else {                                                                                          } else {                                                
423                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialog(DLG_PROGRESS);
424                                                                                                    
425                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
426                                                  builder.setMessage("GPS fix timed out");                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
427                                                  builder.setCancelable(true);                                                  builder.setCancelable(true);
428                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
429                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
430                                                                  dialog.dismiss();                                                                  dialog.dismiss();
431                                                                  startLookup();                                                                  startLookup();
432                                                                                                                                    
433                                                          }                                                          }
434                                                  });                                                  });
435                                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                                  builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
436                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
437                                                                  dialog.dismiss();                                                                  dialog.dismiss();
438                                                          }                                                                                                                }                                                      
# Line 352  public class StationList extends ListAct Line 448  public class StationList extends ListAct
448          };          };
449    
450                    
451          class LocatorTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
452                                    
453                  LookupMethod method = LookupMethod.MethodNone;                  LookupMethod method = LookupMethod.MethodNone;
454                  boolean success;                  boolean success;
455                  String name;                  String name;
456                  Location loc;                  Location loc;
457                    String ids;
458                                    
459                  public void searchByName(String n, Location l) {                  public void searchByName(String n, Location l) {
460                                                    
# Line 371  public class StationList extends ListAct Line 468  public class StationList extends ListAct
468                          loc = l;                          loc = l;
469                  }                  }
470                                    
471                    public void searchByIds(String id, Location l) {
472                            
473                            method = LookupMethod.ByList;
474                            loc = l;
475                            ids = id;
476                    }
477                    
478                  @Override                  @Override
479                  protected void onPreExecute() {                  protected void onPreExecute() {
480    
# Line 381  public class StationList extends ListAct Line 485  public class StationList extends ListAct
485                                    
486                  @Override                  @Override
487                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
488                    
489                          if (method.equals(LookupMethod.ByLocation))                          switch (method) {
490                            case ByLocation:
491                                  success = stationProvider.lookupStations(loc);                                  success = stationProvider.lookupStations(loc);
492                                    break;
493                            case ByName:
494                                    success = stationProvider.lookupStationsByName(name);
495                                    break;
496                            case ByList:
497                                    success = stationProvider.lookupStationsByIds(ids);
498                                    break;
499                            default:
500                                    success = false; // not possible        
501                            }
502                                                    
                         if (method.equals(LookupMethod.ByName))  
                                 success = stationProvider.lookupStations(name);  
503                                                    
504                          Location dummy = new Location("gps");                          Location dummy = new Location("gps");
505                          List<StationBean> stations = stationProvider.getStations();                          List<StationBean> stations = stationProvider.getStations();
506                                                    
507                          for (StationBean station : stations) {                          for (StationBean station : stations) {
508                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());                                                                  
509                                  station.setAddress(addr);                                  if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) {
510                                                                            if (loc != null) { //only do the distance calc if we have a location
511                                  if (method.equals(LookupMethod.ByName) ) {                                                  dummy.setLatitude(station.getLatitude());
512                                          dummy.setLatitude(station.getLatitude());                                                  dummy.setLongitude(station.getLongitude());
513                                          dummy.setLongitude(station.getLongitude());                                                  station.setDistance( (int)loc.distanceTo(dummy) );
514                                          station.setDistance( (int)loc.distanceTo(dummy) );                                          } else {
515                                                    station.setDistance(0);
516                                            }
517                                  }                                  }
518    
519                          }                                                                        }                                              
520                                                    
521                          return null;                          return null;
# Line 410  public class StationList extends ListAct Line 526  public class StationList extends ListAct
526                          super.onPostExecute(result);                          super.onPostExecute(result);
527                          dialog.dismiss();                          dialog.dismiss();
528                                                    
529                            
530                          if (success) {                                                    if (success) {                          
531                                  if (stationProvider.getStations().size() == 0)                                  if (stationProvider.getStations().size() == 0) {
532                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                          showMessageAndClose(getString(stationlist_nostations));
533                                    }
534                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
535                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
536                                                                    
537                          } else { //communication or parse errors                          } else { //communication or parse errors
538                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
539                                  builder.setMessage("Error on finding nearby stations");                                  builder.setMessage(getString(stationlist_nearbyerror));                        
540                                  builder.setCancelable(true);                                  builder.setCancelable(true);
541                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
542                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
543                                                  dialog.dismiss();                                                  dialog.dismiss();
544                                                                                                    
# Line 432  public class StationList extends ListAct Line 550  public class StationList extends ListAct
550                                                  });                                                  });
551                                          }                                          }
552                                  });                                  });
553                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
554                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
555                                                  dialog.dismiss();                                                  dialog.dismiss();
556                                          }                                                                                                }                                                      
# Line 441  public class StationList extends ListAct Line 559  public class StationList extends ListAct
559                          }                          }
560                  }                  }
561          }          }
562            
563            
564            class FavoritesMenu implements OnCreateContextMenuListener {
565                    private final static int FAVORITES_ADD = 9001;
566                    private final static int FAVORITES_REMOVE = 9002;
567                    
568                    private int selectedPosition;
569                    
570                    
571                    @Override
572                    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
573                                                    
574                            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
575                            selectedPosition = info.position;
576                            int stationID = stations.get(selectedPosition).getId();
577    
578                            if (!favorites.contains(stationID)) {
579                                    menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
580                            } else {
581                                    menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
582                            }
583                            
584                    }
585                    
586                    public void onContextItemSelected(MenuItem item) {
587                            StationBean sb = stations.get(selectedPosition);
588                            
589                            int stationID = sb.getId();
590                            if (item.getItemId() == FAVORITES_ADD) {
591                                    favorites.add(stationID);
592                                    Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
593                            } else {
594                                    
595                                    favorites.remove(stationID);
596                                    Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
597                                    
598                                    
599                                    if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
600                                            stations.remove(selectedPosition);
601                                            adapter.notifyDataSetChanged();
602                                    }
603                            }
604                            Editor ed = prefs.edit();
605                            ed.putString("favorites", favorites.toString());
606                            ed.commit();
607                    }
608            }
609  }  }

Legend:
Removed from v.401  
changed lines
  Added in v.566

  ViewVC Help
Powered by ViewVC 1.1.20