/[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 478 by torben, Wed Oct 28 08:40:32 2009 UTC revision 1025 by torben, Tue Aug 31 08:49:15 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;
 import java.util.Locale;  
5    
6    
7    import android.app.Activity;
8  import android.app.AlertDialog;  import android.app.AlertDialog;
9  import android.app.Dialog;  import android.app.Dialog;
10  import android.app.ListActivity;  import android.app.ListActivity;
# Line 12  import android.content.DialogInterface; Line 13  import android.content.DialogInterface;
13  import android.content.Intent;  import android.content.Intent;
14  import android.content.SharedPreferences;  import android.content.SharedPreferences;
15  import android.content.SharedPreferences.Editor;  import android.content.SharedPreferences.Editor;
 import android.location.Address;  
 import android.location.Geocoder;  
16  import android.location.Location;  import android.location.Location;
17  import android.os.AsyncTask;  import android.os.AsyncTask;
18  import android.os.Bundle;  import android.os.Bundle;
19  import android.os.Handler;  import android.os.Handler;
20  import android.os.Message;  import android.os.Message;
21  import android.util.Log;  
22  import android.view.ContextMenu;  import android.view.ContextMenu;
23  import android.view.LayoutInflater;  import android.view.LayoutInflater;
24  import android.view.Menu;  import android.view.Menu;
# Line 38  import dk.thoerup.traininfo.stationmap.S Line 37  import dk.thoerup.traininfo.stationmap.S
37  import dk.thoerup.traininfo.util.IntSet;  import dk.thoerup.traininfo.util.IntSet;
38  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
39    
40    import static dk.thoerup.traininfo.R.string.*;
41    
42  public class StationList extends ListActivity  {  public class StationList extends ListActivity  {
43          public static final int GOTLOCATION = 1001;          public static final int GOTLOCATION = 1001;
44          public static final int GOTSTATIONLIST = 1002;          public static final int GOTSTATIONLIST = 1002;
45          public static final int NOPROVIDER = 1003;          public static final int NOPROVIDER = 1003;
46          public static final int LOCATIONFIXTIMEOUT = 1004;          public static final int LOCATIONFIXTIMEOUT = 1004;
47                    
         public static final int OPTIONS_RESCAN = 2001;  
         public static final int OPTIONS_NAMESEARCH = 2002;  
48          public static final int OPTIONS_MAP = 2003;          public static final int OPTIONS_MAP = 2003;
49          public static final int OPTIONS_ABOUT = 2004;          public static final int OPTIONS_GPSINFO = 2004;
         public static final int OPTIONS_FAVORITES = 2005;  
           
   
50                    
51          public static final int DLG_PROGRESS = 3001;          public static final int DLG_PROGRESS = 3001;
52          public static final int DLG_STATIONNAME = 3002;          public static final int DLG_STATIONNAME = 3002;
53                    
54            
55            public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds
56            
57            
58          static enum LookupMethod {          static enum LookupMethod {
59                  ByLocation,                  ByLocation,
60                  ByName,                  ByName,
# Line 65  public class StationList extends ListAct Line 65  public class StationList extends ListAct
65                    
66          String dialogMessage = "";          String dialogMessage = "";
67          ProgressDialog dialog;          ProgressDialog dialog;
68          LocationLookup locator = null;          LocationLookup locationLookup = null;
69          FindStationsTask findStationsTask;          FindStationsTask findStationsTask;
70          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
71                    
72          GeoPair location = new GeoPair();          GeoPair location = new GeoPair();
73            
74            boolean isLaunchedforShortcut;
75          boolean isRunning = false;          boolean isRunning = false;
76          List<StationBean> stations = new ArrayList<StationBean>();          List<StationBean> stations = new ArrayList<StationBean>();
77                    
# Line 80  public class StationList extends ListAct Line 81  public class StationList extends ListAct
81                    
82          FavoritesMenu contextMenu = new FavoritesMenu();          FavoritesMenu contextMenu = new FavoritesMenu();
83          IntSet favorites = new IntSet();          IntSet favorites = new IntSet();
84            
85          boolean showingFavorites = false;          WelcomeScreen.ListType listType;
           
86          SharedPreferences prefs;          SharedPreferences prefs;
87                    
88          ///////////////////////////////////////////////////////////////////////////////////////////          ///////////////////////////////////////////////////////////////////////////////////////////
# Line 92  public class StationList extends ListAct Line 92  public class StationList extends ListAct
92          @Override          @Override
93          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
94                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
95                  setContentView(R.layout.main);                  setContentView(R.layout.stationlist);
96                                    
97                                    
98                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this);
# Line 101  public class StationList extends ListAct Line 101  public class StationList extends ListAct
101                  ListView lv = getListView();                  ListView lv = getListView();
102                  lv.setOnCreateContextMenuListener(contextMenu);                  lv.setOnCreateContextMenuListener(contextMenu);
103                                    
104                  locator = new LocationLookup(this, stationsFetched);                  locationLookup = new LocationLookup(this, stationsFetched);
105                                    
106    
107                  prefs = getSharedPreferences("TrainStation", 0);                  prefs = getSharedPreferences("TrainStation", 0);
# Line 110  public class StationList extends ListAct Line 110  public class StationList extends ListAct
110                          favorites.fromString(favoriteString);                          favorites.fromString(favoriteString);
111                  }                  }
112                                    
113                    listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
114                    setTitle();
115                    
116                    isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);
117                    
118                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
119                          startLookup();  
120                            
121                            switch (listType) {
122                            case ListNearest:
123                                    startLookup();
124                                    break;
125                            case ListSearch:                                
126                                    showDialog(DLG_STATIONNAME);
127                                    break;
128                            case ListFavorites:
129                                    startFavoriteLookup();
130                                    break;
131                            default:
132                                    // Not possible !?!
133                            }
134                            
135                  } else {                  } else {
136                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
137                          adapter.setStations(stations);                          adapter.setStations(stations);
138                          location = (GeoPair) savedInstanceState.getSerializable("location");                          location = (GeoPair) savedInstanceState.getSerializable("location");
139                  }                  }
140                    
141          }          }
142            
143    
144            @Override
145            protected void onDestroy() {
146                    super.onDestroy();
147                    
148                    isRunning = false;
149                    
150                    if (locationLookup != null) {
151                            locationLookup.stopSearch();
152                    }
153                    if (findStationsTask != null) {
154                            findStationsTask.cancel(true);
155                    }              
156            }
157    
158            
159            protected void setTitle() {
160                    String dialogTitle = getResources().getString(app_name);
161                    switch (listType) {
162                    case ListNearest:
163                            dialogTitle += " - " + getString(stationlist_nearbystations);
164                            break;
165                    case ListSearch:
166                            dialogTitle += " - " + getString(stationlist_search);
167                            break;
168                    case ListFavorites:
169                            dialogTitle += " - " + getString(stationlist_favorites);
170                            break;
171                    default:
172                            dialogTitle = "";//not possible                                
173                    }
174            
175                    setTitle(dialogTitle);
176                    
177            }
178            
179                    
180    
181      @Override      @Override
182      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
# Line 127  public class StationList extends ListAct Line 185  public class StationList extends ListAct
185                  dialog.dismiss();                  dialog.dismiss();
186          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (ArrayList<StationBean>) stations);
187          outState.putSerializable("location", location);          outState.putSerializable("location", location);
188            
189      }      }
190                    
191                    
# Line 135  public class StationList extends ListAct Line 194  public class StationList extends ListAct
194          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
195                  MenuItem item;                  MenuItem item;
196                                    
197                  item = menu.add(0, OPTIONS_RESCAN, 0, "Nearest stations");                  item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
                 item.setIcon(android.R.drawable.ic_menu_mylocation);  
                   
                 item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station");  
                 item.setIcon(android.R.drawable.ic_menu_search);  
                   
                 item = menu.add(0, OPTIONS_FAVORITES, 0, "Favorites");  
                 item.setIcon(android.R.drawable.ic_menu_agenda);  
                   
                 item = menu.add(0, OPTIONS_MAP, 0, "Station map");  
198                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
199                                    
200                  item = menu.add(0, OPTIONS_ABOUT, 0, "About");                  item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
201                  item.setIcon(android.R.drawable.ic_menu_info_details);                  item.setIcon(android.R.drawable.ic_menu_mapmode);              
202                    
203                  return true;                  return true;
204          }          }
205    
# Line 156  public class StationList extends ListAct Line 207  public class StationList extends ListAct
207          public boolean onOptionsItemSelected(MenuItem item) {          public boolean onOptionsItemSelected(MenuItem item) {
208                  boolean retval = true;                  boolean retval = true;
209    
210                                    //TODO: Cleanup
211                  switch (item.getItemId()) {                  switch (item.getItemId()) {
                 case OPTIONS_RESCAN:  
                         startLookup();  
                         break;  
                 case OPTIONS_NAMESEARCH:  
                         showDialog(DLG_STATIONNAME);  
                         break;  
                 case OPTIONS_FAVORITES:  
                         startFavoriteLookup();  
                         break;  
212                  case OPTIONS_MAP:                  case OPTIONS_MAP:
213                                                    
214                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
                         intent.putExtra("userlocation", location );  
215                                                    
216                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
217                          for (StationBean st : stations ) {                          for (StationBean st : stations ) {
# Line 181  public class StationList extends ListAct Line 222  public class StationList extends ListAct
222                                                    
223                          startActivity(intent);                          startActivity(intent);
224                          break;                          break;
225                  case OPTIONS_ABOUT:                  case OPTIONS_GPSINFO:
226                          String ver = this.getResources().getString(R.string.app_version);                          Location loc = locationLookup.getLocation();
                           
                         Location loc = locator.getLocation();  
227                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
228                          message.append("TrainInfo DK v").append(ver).append("\n");                          message.append( getString(stationlist_locationinfo) ).append(":\n");
229                          message.append("By Torben Nielsen\n");                          if (loc != null) {
230                          message.append("\n");                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
231                          message.append("Location info:\n");                                  message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
232                          message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n");                                  message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
233                          message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n");                                  message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
234                            } else {
235                          MessageBox.showMessage(this, message.toString());                                  message.append( getString(stationlist_nolocation) );
236                            }                      
237                            
238                            MessageBox.showMessage(this, message.toString(), false);
239                          break;                          break;
240                  default:                  default:
241                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 211  public class StationList extends ListAct Line 253  public class StationList extends ListAct
253    
254    
255          }          }
256            
257            public void showMessageAndClose(String message) {
258                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
259                    builder.setMessage(message)
260                    .setCancelable(false)
261                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
262                            public void onClick(DialogInterface dialog, int id) {
263                                    dialog.dismiss();
264                                    StationList.this.finish();
265                            }
266                    })
267                    .show();
268            }
269    
270    
271    
# Line 220  public class StationList extends ListAct Line 275  public class StationList extends ListAct
275                  switch (id) {                  switch (id) {
276                  case DLG_PROGRESS:                  case DLG_PROGRESS:
277                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
278                          dlg.setMessage("Wait for location fix");                          dlg.setMessage( getString(stationlist_waitforlocation) );
279                          dlg.setCancelable(false);                          dlg.setCancelable(false);
280                          return dlg;                                              return dlg;                    
281                  case DLG_STATIONNAME:                  case DLG_STATIONNAME:
# Line 230  public class StationList extends ListAct Line 285  public class StationList extends ListAct
285                                                    
286                          AlertDialog.Builder builder = new AlertDialog.Builder(this);                          AlertDialog.Builder builder = new AlertDialog.Builder(this);
287                                                    
288                          builder.setTitle("Station search");                          builder.setTitle( getString(stationlist_stationsearch) );
289                          builder.setView(rootView);                          builder.setView(rootView);
290                          builder.setCancelable(true);                          builder.setCancelable(true);
291                          builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {                          builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
292                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
293                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
294                                          dialog.dismiss();                                          dialog.dismiss();
295                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
296                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
297                                                    startNameSearch(search);
298                                          } else {                                          } else {
299                                                  MessageBox.showMessage(StationList.this, "Two characters minimum" );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
300                                          }                                          }
301                                  }                                  }
302                          });                          });
303                          builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                          builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
304                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
305                                          dialog.dismiss();                                          dialog.dismiss();
306                                            StationList.this.finish(); // Close this Activity
307                                  }                                  }
308                          });                                              });                    
309                          return builder.create();                          return builder.create();
# Line 277  public class StationList extends ListAct Line 334  public class StationList extends ListAct
334                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
335                                                                    
336                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
   
                 double latitude = station.getLatitude();  
                 double longitude = station.getLongitude();  
   
   
337                                    
338                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
339                  intent.putExtra("name", station.getName());                          Intent i = new Intent();
340                  intent.putExtra("distance", station.getDistance());                          i.putExtra("station", station);
341                  intent.putExtra("latitude", latitude);                          setResult(Activity.RESULT_OK, i);
342                  intent.putExtra("longitude", longitude);                          finish();
343                  intent.putExtra("stationid", station.getId());                  } else {                
344                  intent.putExtra("address", station.getAddress());                          Intent intent = new Intent(this, DepartureList.class);
345                  startActivity(intent);                          intent.putExtra("stationbean", station);
346                            startActivity(intent);
347                    }
348          }          }
349    
350          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
351          //          //
352    
353          public void startLookup() {          public void startLookup() {
354                  isRunning = true;                  isRunning = true;              
355                  dialogMessage = "Wait for location fix";                  dialogMessage = getString( stationlist_waitforlocation );
356                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
357                                    
358                  locator.locateStations();                  locationLookup.locateStations();
359                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS);
360          }          }
361                    
362          void startNameSearch(String name) {          void startNameSearch(String name) {
363                  dialogMessage = "Finding stations by name";                  dialogMessage = getString( stationlist_findbyname );
364                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
365    
366                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
367                  findStationsTask.searchByName(name, locator.getLocation());                  findStationsTask.searchByName(name);
368                  findStationsTask.execute();                  findStationsTask.execute();
369                                    
370          }          }
# Line 318  public class StationList extends ListAct Line 372  public class StationList extends ListAct
372          public void startFavoriteLookup() {          public void startFavoriteLookup() {
373                                    
374                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
375                          dialogMessage = "Loading favorites";                          dialogMessage = getString( stationlist_loadfavorites );
376                          showDialog(DLG_PROGRESS);                          showDialog(DLG_PROGRESS);
377    
378                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
379                          findStationsTask.searchByIds(favorites.toString(), locator.getLocation());                          findStationsTask.searchByIds( favorites.toString() );
380                          findStationsTask.execute();                          findStationsTask.execute();
381                  } else {                  } else {
382                          MessageBox.showMessage(this, "Favorite list is empty");                          showMessageAndClose( getString( stationlist_nofavorites ) );
383                  }                  }
384          }          }
385    
# Line 333  public class StationList extends ListAct Line 387  public class StationList extends ListAct
387                    
388          void startLocatorTask()          void startLocatorTask()
389          {          {
390                  dialogMessage = "Finding nearby stations";                  dialogMessage = getString( stationlist_findingnearby );
391                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
392                                    
393                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
394                  findStationsTask.searchByLocation( locator.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
395                  findStationsTask.execute();                      findStationsTask.execute();    
396          }          }
           
   
         String lookupAddress(double latitude, double longitude) {  
397                                    
                 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();  
         }  
           
398                    
399          ////////////////////////////////////////////////////////////////////////////          ////////////////////////////////////////////////////////////////////////////
400          // Inner classes          // Inner classes
# Line 381  public class StationList extends ListAct Line 408  public class StationList extends ListAct
408                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
409                                                                    
410                                  startLocatorTask();                                  startLocatorTask();
411                                  location = GeoPair.fromLocation( locator.getLocation() );                                  location = GeoPair.fromLocation( locationLookup.getLocation() );
412                                                                    
413                                  break;                                  break;
414    
415                          case NOPROVIDER:                          case NOPROVIDER:
416                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
417                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
418                                    //StationList.this.finish();
419                                  break;                                  break;
420                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
421                                  if (isRunning) {                                  if (isRunning) {
422                                          locator.stopSearch();                                          locationLookup.stopSearch();
423                                          if (locator.hasLocation()) {                                          if (locationLookup.hasLocation()) {
424                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
425                                          } else {                                                                                          } else {                                                
426                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialog(DLG_PROGRESS);
427                                                                                                    
428                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
429                                                  builder.setMessage("Location fix timed out");                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
430                                                  builder.setCancelable(true);                                                  builder.setCancelable(true);
431                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
432                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
433                                                                  dialog.dismiss();                                                                  dialog.dismiss();
434                                                                  startLookup();                                                                  startLookup();
435                                                                                                                                    
436                                                          }                                                          }
437                                                  });                                                  });
438                                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                                  builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
439                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
440                                                                  dialog.dismiss();                                                                  dialog.dismiss();
441                                                          }                                                                                                                }                                                      
442                                                  });                                                                                                                                              });
443                                                  builder.show();                                                  builder.show();
444    
445                                          }                                          }
446                                  }                                  }
# Line 426  public class StationList extends ListAct Line 454  public class StationList extends ListAct
454          class FindStationsTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
455                                    
456                  LookupMethod method = LookupMethod.MethodNone;                  LookupMethod method = LookupMethod.MethodNone;
                 boolean success;  
457                  String name;                  String name;
458                  Location loc;                  Location loc;
459                  String ids;                  String ids;
460                                    
461                  public void searchByName(String n, Location l) {                  public void searchByName(String n) {
462                                                    
463                          method = LookupMethod.ByName;                          method = LookupMethod.ByName;
                         loc = l;  
464                          name = n;                          name = n;
465                  }                  }
466                                    
# Line 443  public class StationList extends ListAct Line 469  public class StationList extends ListAct
469                          loc = l;                          loc = l;
470                  }                  }
471                                    
472                  public void searchByIds(String id, Location l) {                  public void searchByIds(String id) {
473                                                    
474                          method = LookupMethod.ByList;                          method = LookupMethod.ByList;
                         loc = l;  
475                          ids = id;                          ids = id;
476                  }                  }
477                                    
# Line 463  public class StationList extends ListAct Line 488  public class StationList extends ListAct
488    
489                          switch (method) {                          switch (method) {
490                          case ByLocation:                          case ByLocation:
491                                  success = stationProvider.lookupStations(loc);                                  stations = stationProvider.lookupStations(loc);
492                                  break;                                  break;
493                          case ByName:                          case ByName:
494                                  success = stationProvider.lookupStationsByName(name);                                  stations = stationProvider.lookupStationsByName(name);
495                                  break;                                  break;
496                          case ByList:                          case ByList:
497                                  success = stationProvider.lookupStationsByIds(ids);                                  stations = stationProvider.lookupStationsByIds(ids);
498                                  break;                                  break;
499                          default:                          default:
500                                  success = false; // not possible                                          stations = null; // not possible        
501                          }                          }
502                                                    
503                                                    
                         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);  
                                         }  
                                 }  
   
                         }                                                
                           
504                          return null;                          return null;
505                  }                  }
506    
# Line 504  public class StationList extends ListAct Line 509  public class StationList extends ListAct
509                          super.onPostExecute(result);                          super.onPostExecute(result);
510                          dialog.dismiss();                          dialog.dismiss();
511                                                    
                         //set title  
                         String title;  
                         switch (method) {  
                         case ByLocation:  
                                 title = "Traininfo DK - Nearby stations";  
                                 break;  
                         case ByName:  
                                 title = "Traininfo DK - Search";  
                                 break;  
                         case ByList:  
                                 title = "Traininfo DK - Favorites";  
                                 break;  
                         default:  
                                 title = "";//not possible                                        
                         }                                
512                                                    
513                          StationList.this.setTitle(title);                          if (stations != null) {                        
514                          //set title end                                  if (stations.size() == 0) {
515                                                                    showMessageAndClose(getString(stationlist_nostations));
516                          if (success) {                                                            }
517                                  if (stationProvider.getStations().size() == 0)  
518                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                  StationList.this.getListView().invalidateViews();
519                                  stations = stationProvider.getStations();                                  adapter.setStations( stations );                                
                                 adapter.setStations( stations );  
520                                                                    
                                 showingFavorites = method.equals(LookupMethod.ByList);                            
521                                                                    
522                          } else { //communication or parse errors                          } else { //communication or parse errors
523                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
524                                  builder.setMessage("Error on finding nearby stations");                                  builder.setMessage(getString(stationlist_fetcherror));                          
525                                  builder.setCancelable(true);                                  builder.setCancelable(true);
526                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
527                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
528                                                  dialog.dismiss();                                                  dialog.dismiss();
529                                                                                                    
530                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
531                                                          @Override                                                  switch (method) {
532                                                          public void run() {                                                  case ByLocation:
533                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
534                                                          }                                                                  @Override
535                                                  });                                                                  public void run() {
536                                                                            startLocatorTask();                                                            
537                                                                    }
538                                                            };
539                                                            break;
540                                                    case ByName:
541                                                            runner = new Runnable() {
542                                                                    @Override
543                                                                    public void run() {
544                                                                            startNameSearch( FindStationsTask.this.name );
545                                                                    }
546                                                            };
547                                                            break;
548                                                    case ByList:
549                                                            runner = new Runnable() {
550                                                                    @Override
551                                                                    public void run() {
552                                                                            startFavoriteLookup();
553                                                                    }
554                                                            };
555                                                            break;
556                                                    }
557                                                    
558                                                    stationsFetched.post( runner );
559                                          }                                          }
560                                  });                                  });
561                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
562                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
563                                                  dialog.dismiss();                                                  dialog.dismiss();
564                                                    StationList.this.finish();
565                                          }                                                                                                }                                                      
566                                  });                                                                                                                              });
567                                  builder.show();                                                          
568                                    builder.show();
569                          }                          }
570                  }                  }
571          }          }
# Line 573  public class StationList extends ListAct Line 586  public class StationList extends ListAct
586                          int stationID = stations.get(selectedPosition).getId();                          int stationID = stations.get(selectedPosition).getId();
587    
588                          if (!favorites.contains(stationID)) {                          if (!favorites.contains(stationID)) {
589                                  menu.add(0, FAVORITES_ADD, 0, "Add to favorites");                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
590                          } else {                          } else {
591                                  menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites");                                  menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
592                          }                          }
593                                                    
594                  }                  }
# Line 586  public class StationList extends ListAct Line 599  public class StationList extends ListAct
599                          int stationID = sb.getId();                          int stationID = sb.getId();
600                          if (item.getItemId() == FAVORITES_ADD) {                          if (item.getItemId() == FAVORITES_ADD) {
601                                  favorites.add(stationID);                                  favorites.add(stationID);
602                                  Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
603                          } else {                          } else {
604                                                                    
605                                  favorites.remove(stationID);                                  favorites.remove(stationID);
606                                  Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
607                                    
608                                                                    
609                                  if (showingFavorites) {                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
610                                          stations.remove(selectedPosition);                                          stations.remove(selectedPosition);
611                                          adapter.notifyDataSetChanged();                                          adapter.notifyDataSetChanged();
612                                  }                                  }

Legend:
Removed from v.478  
changed lines
  Added in v.1025

  ViewVC Help
Powered by ViewVC 1.1.20