/[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 482 by torben, Thu Oct 29 09:38:13 2009 UTC revision 630 by torben, Wed Mar 17 15:27:11 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 12  import android.content.DialogInterface; Line 12  import android.content.DialogInterface;
12  import android.content.Intent;  import android.content.Intent;
13  import android.content.SharedPreferences;  import android.content.SharedPreferences;
14  import android.content.SharedPreferences.Editor;  import android.content.SharedPreferences.Editor;
 import android.location.Address;  
 import android.location.Geocoder;  
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    
21  import android.util.Log;  import android.util.Log;
22  import android.view.ContextMenu;  import android.view.ContextMenu;
23  import android.view.LayoutInflater;  import android.view.LayoutInflater;
# 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;
# Line 135  public class StationList extends ListAct Line 136  public class StationList extends ListAct
136                                    
137          }          }
138          protected void setTitle() {          protected void setTitle() {
139                  String dialogTitle;                  String dialogTitle = getResources().getString(app_name);
140                  switch (listType) {                  switch (listType) {
141                  case ListNearest:                  case ListNearest:
142                          dialogTitle = "Traininfo DK - Nearby stations";                          dialogTitle += " - " + getString(stationlist_nearbystations);
143                          break;                          break;
144                  case ListSearch:                  case ListSearch:
145                          dialogTitle = "Traininfo DK - Search";                          dialogTitle += " - " + getString(stationlist_search);
146                          break;                          break;
147                  case ListFavorites:                  case ListFavorites:
148                          dialogTitle = "Traininfo DK - Favorites";                          dialogTitle += " - " + getString(stationlist_favorites);
149                          break;                          break;
150                  default:                  default:
151                          dialogTitle = "";//not possible                                                          dialogTitle = "";//not possible                                
# Line 171  public class StationList extends ListAct Line 172  public class StationList extends ListAct
172          @Override          @Override
173          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
174                  MenuItem item;                  MenuItem item;
175                                                    
176                  item = menu.add(0, OPTIONS_MAP, 0, "Station map");                  item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
177                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
178                                    
179                  item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info");                  item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
180                  item.setIcon(android.R.drawable.ic_menu_mapmode);                                item.setIcon(android.R.drawable.ic_menu_mapmode);              
181                                    
182                  return true;                  return true;
# Line 190  public class StationList extends ListAct Line 191  public class StationList extends ListAct
191                  case OPTIONS_MAP:                  case OPTIONS_MAP:
192                                                    
193                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
                         intent.putExtra("userlocation", location );  
194                                                    
195                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
196                          for (StationBean st : stations ) {                          for (StationBean st : stations ) {
# Line 204  public class StationList extends ListAct Line 204  public class StationList extends ListAct
204                  case OPTIONS_GPSINFO:                  case OPTIONS_GPSINFO:
205                          Location loc = locationLookup.getLocation();                          Location loc = locationLookup.getLocation();
206                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
207                          message.append("Location info:\n");                          message.append( getString(stationlist_locationinfo) ).append(":\n");
208                          message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n");                          if (loc != null) {
209                          message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n");                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
210                                    message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
211                                    message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
212                                    message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
213                            } else {
214                                    message.append( getString(stationlist_nolocation) );
215                            }                      
216                            
217                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString());
218                          break;                          break;
219                  default:                  default:
# Line 226  public class StationList extends ListAct Line 232  public class StationList extends ListAct
232    
233    
234          }          }
235            
236            public void showMessageAndClose(String message) {
237                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
238                    builder.setMessage(message)
239                    .setCancelable(false)
240                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
241                            public void onClick(DialogInterface dialog, int id) {
242                                    dialog.dismiss();
243                                    StationList.this.finish();
244                            }
245                    })
246                    .show();
247            }
248    
249    
250    
# Line 235  public class StationList extends ListAct Line 254  public class StationList extends ListAct
254                  switch (id) {                  switch (id) {
255                  case DLG_PROGRESS:                  case DLG_PROGRESS:
256                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
257                          dlg.setMessage("Wait for location fix");                          dlg.setMessage( getString(stationlist_waitforlocation) );
258                          dlg.setCancelable(false);                          dlg.setCancelable(false);
259                          return dlg;                                              return dlg;                    
260                  case DLG_STATIONNAME:                  case DLG_STATIONNAME:
# Line 245  public class StationList extends ListAct Line 264  public class StationList extends ListAct
264                                                    
265                          AlertDialog.Builder builder = new AlertDialog.Builder(this);                          AlertDialog.Builder builder = new AlertDialog.Builder(this);
266                                                    
267                          builder.setTitle("Station search");                          builder.setTitle( getString(stationlist_stationsearch) );
268                          builder.setView(rootView);                          builder.setView(rootView);
269                          builder.setCancelable(true);                          builder.setCancelable(true);
270                          builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {                          builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
271                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
272                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
273                                          dialog.dismiss();                                          dialog.dismiss();
274                                          if (et.getText().toString().length() >= 2) {                                          if (et.getText().toString().length() >= 2) {
275                                                  startNameSearch(et.getText().toString());                                                  startNameSearch(et.getText().toString());
276                                          } else {                                          } else {
277                                                  MessageBox.showMessage(StationList.this, "Two characters minimum" );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
278                                          }                                          }
279                                  }                                  }
280                          });                          });
281                          builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                          builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
282                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
283                                          dialog.dismiss();                                          dialog.dismiss();
284                                            StationList.this.finish(); // Close this Activity
285                                  }                                  }
286                          });                                              });                    
287                          return builder.create();                          return builder.create();
# Line 292  public class StationList extends ListAct Line 312  public class StationList extends ListAct
312                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
313                                                                    
314                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
   
                 double latitude = station.getLatitude();  
                 double longitude = station.getLongitude();  
   
   
315                                    
316                  Intent intent = new Intent(this, DepartureList.class);                  Intent intent = new Intent(this, DepartureList.class);
317                  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());  
318                  startActivity(intent);                  startActivity(intent);
319          }          }
320    
# Line 312  public class StationList extends ListAct Line 322  public class StationList extends ListAct
322          //          //
323    
324          public void startLookup() {          public void startLookup() {
325                  isRunning = true;                  isRunning = true;              
326                  dialogMessage = "Wait for location fix";                  dialogMessage = getString( stationlist_waitforlocation );
327                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
328                                    
329                  locationLookup.locateStations();                  locationLookup.locateStations();
# Line 321  public class StationList extends ListAct Line 331  public class StationList extends ListAct
331          }          }
332                    
333          void startNameSearch(String name) {          void startNameSearch(String name) {
334                  dialogMessage = "Finding stations by name";                  dialogMessage = getString( stationlist_findbyname );
335                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
336    
337                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
338                  findStationsTask.searchByName(name, locationLookup.getLocation());                  findStationsTask.searchByName(name);
339                  findStationsTask.execute();                  findStationsTask.execute();
340                                    
341          }          }
# Line 333  public class StationList extends ListAct Line 343  public class StationList extends ListAct
343          public void startFavoriteLookup() {          public void startFavoriteLookup() {
344                                    
345                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
346                          dialogMessage = "Loading favorites";                          dialogMessage = getString( stationlist_loadfavorites );
347                          showDialog(DLG_PROGRESS);                          showDialog(DLG_PROGRESS);
348    
349                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
350                          findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation());                          findStationsTask.searchByIds( favorites.toString() );
351                          findStationsTask.execute();                          findStationsTask.execute();
352                  } else {                  } else {
353                          MessageBox.showMessage(this, "Favorite list is empty");                          showMessageAndClose( getString( stationlist_nofavorites ) );
354                  }                  }
355          }          }
356    
# Line 348  public class StationList extends ListAct Line 358  public class StationList extends ListAct
358                    
359          void startLocatorTask()          void startLocatorTask()
360          {          {
361                  dialogMessage = "Finding nearby stations";                  dialogMessage = getString( stationlist_findingnearby );
362                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
363                                    
364                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
365                  findStationsTask.searchByLocation( locationLookup.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
366                  findStationsTask.execute();                      findStationsTask.execute();    
367          }          }
           
   
         String lookupAddress(double latitude, double longitude) {  
                   
                 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);  
                 }  
368                                    
                 return sb.toString();  
         }  
           
369                    
370          ////////////////////////////////////////////////////////////////////////////          ////////////////////////////////////////////////////////////////////////////
371          // Inner classes          // Inner classes
# Line 402  public class StationList extends ListAct Line 385  public class StationList extends ListAct
385    
386                          case NOPROVIDER:                          case NOPROVIDER:
387                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
388                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) );
389                                  break;                                  break;
390                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
391                                  if (isRunning) {                                  if (isRunning) {
# Line 413  public class StationList extends ListAct Line 396  public class StationList extends ListAct
396                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialog(DLG_PROGRESS);
397                                                                                                    
398                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
399                                                  builder.setMessage("Location fix timed out");                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
400                                                  builder.setCancelable(true);                                                  builder.setCancelable(true);
401                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
402                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
403                                                                  dialog.dismiss();                                                                  dialog.dismiss();
404                                                                  startLookup();                                                                  startLookup();
405                                                                                                                                    
406                                                          }                                                          }
407                                                  });                                                  });
408                                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                                  builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
409                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
410                                                                  dialog.dismiss();                                                                  dialog.dismiss();
411                                                          }                                                                                                                }                                                      
# Line 446  public class StationList extends ListAct Line 429  public class StationList extends ListAct
429                  Location loc;                  Location loc;
430                  String ids;                  String ids;
431                                    
432                  public void searchByName(String n, Location l) {                  public void searchByName(String n) {
433                                                    
434                          method = LookupMethod.ByName;                          method = LookupMethod.ByName;
                         loc = l;  
435                          name = n;                          name = n;
436                  }                  }
437                                    
# Line 458  public class StationList extends ListAct Line 440  public class StationList extends ListAct
440                          loc = l;                          loc = l;
441                  }                  }
442                                    
443                  public void searchByIds(String id, Location l) {                  public void searchByIds(String id) {
444                                                    
445                          method = LookupMethod.ByList;                          method = LookupMethod.ByList;
                         loc = l;  
446                          ids = id;                          ids = id;
447                  }                  }
448                                    
# Line 491  public class StationList extends ListAct Line 472  public class StationList extends ListAct
472                          }                          }
473                                                    
474                                                    
                         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);  
                                         }  
                                 }  
   
                         }                                                
                           
475                          return null;                          return null;
476                  }                  }
477    
# Line 521  public class StationList extends ListAct Line 482  public class StationList extends ListAct
482                                                    
483                                                    
484                          if (success) {                                                    if (success) {                          
485                                  if (stationProvider.getStations().size() == 0)                                  if (stationProvider.getStations().size() == 0) {
486                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                          showMessageAndClose(getString(stationlist_nostations));
487                                    }
488                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
489                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
490                                                                    
491                          } else { //communication or parse errors                          } else { //communication or parse errors
492                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
493                                  builder.setMessage("Error on finding nearby stations");                                  builder.setMessage(getString(stationlist_nearbyerror));                        
494                                  builder.setCancelable(true);                                  builder.setCancelable(true);
495                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
496                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
497                                                  dialog.dismiss();                                                  dialog.dismiss();
498                                                                                                    
# Line 542  public class StationList extends ListAct Line 504  public class StationList extends ListAct
504                                                  });                                                  });
505                                          }                                          }
506                                  });                                  });
507                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
508                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
509                                                  dialog.dismiss();                                                  dialog.dismiss();
510                                          }                                                                                                }                                                      
511                                  });                                                                                                                              });                    
512                                  builder.show();                                                          try {
513                                            builder.show();
514                                    } catch (android.view.WindowManager.BadTokenException e) {                                      
515                                            Log.i("StationList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running
516                                    }
517                          }                          }
518                  }                  }
519          }          }
# Line 568  public class StationList extends ListAct Line 534  public class StationList extends ListAct
534                          int stationID = stations.get(selectedPosition).getId();                          int stationID = stations.get(selectedPosition).getId();
535    
536                          if (!favorites.contains(stationID)) {                          if (!favorites.contains(stationID)) {
537                                  menu.add(0, FAVORITES_ADD, 0, "Add to favorites");                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
538                          } else {                          } else {
539                                  menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites");                                  menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
540                          }                          }
541                                                    
542                  }                  }
# Line 581  public class StationList extends ListAct Line 547  public class StationList extends ListAct
547                          int stationID = sb.getId();                          int stationID = sb.getId();
548                          if (item.getItemId() == FAVORITES_ADD) {                          if (item.getItemId() == FAVORITES_ADD) {
549                                  favorites.add(stationID);                                  favorites.add(stationID);
550                                  Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
551                          } else {                          } else {
552                                                                    
553                                  favorites.remove(stationID);                                  favorites.remove(stationID);
554                                  Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
555                                                                    
556                                                                    
557                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {

Legend:
Removed from v.482  
changed lines
  Added in v.630

  ViewVC Help
Powered by ViewVC 1.1.20