/[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 483 by torben, Thu Oct 29 10:49:14 2009 UTC revision 564 by torben, Thu Jan 28 09:16:33 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  import android.util.Log;  
21  import android.view.ContextMenu;  import android.view.ContextMenu;
22  import android.view.LayoutInflater;  import android.view.LayoutInflater;
23  import android.view.Menu;  import android.view.Menu;
# Line 38  import dk.thoerup.traininfo.stationmap.S Line 36  import dk.thoerup.traininfo.stationmap.S
36  import dk.thoerup.traininfo.util.IntSet;  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;
# Line 135  public class StationList extends ListAct Line 135  public class StationList extends ListAct
135                                    
136          }          }
137          protected void setTitle() {          protected void setTitle() {
138                  String dialogTitle;                  String dialogTitle = getResources().getString(app_name);
139                  switch (listType) {                  switch (listType) {
140                  case ListNearest:                  case ListNearest:
141                          dialogTitle = "TrainInfo DK - Nearby stations";                          dialogTitle += " - " + getString(stationlist_nearbystations);
142                          break;                          break;
143                  case ListSearch:                  case ListSearch:
144                          dialogTitle = "TrainInfo DK - Search";                          dialogTitle += " - " + getString(stationlist_search);
145                          break;                          break;
146                  case ListFavorites:                  case ListFavorites:
147                          dialogTitle = "TrainInfo DK - Favorites";                          dialogTitle += " - " + getString(stationlist_favorites);
148                          break;                          break;
149                  default:                  default:
150                          dialogTitle = "";//not possible                                                          dialogTitle = "";//not possible                                
# Line 171  public class StationList extends ListAct Line 171  public class StationList extends ListAct
171          @Override          @Override
172          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
173                  MenuItem item;                  MenuItem item;
174                                                    
175                  item = menu.add(0, OPTIONS_MAP, 0, "Station map");                  item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
176                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
177                                    
178                  item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info");                  item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
179                  item.setIcon(android.R.drawable.ic_menu_mapmode);                                item.setIcon(android.R.drawable.ic_menu_mapmode);              
180                                    
181                  return true;                  return true;
# Line 190  public class StationList extends ListAct Line 190  public class StationList extends ListAct
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 204  public class StationList extends ListAct Line 203  public class StationList extends ListAct
203                  case OPTIONS_GPSINFO:                  case OPTIONS_GPSINFO:
204                          Location loc = locationLookup.getLocation();                          Location loc = locationLookup.getLocation();
205                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
206                          message.append("Location info:\n");                          message.append( getString(stationlist_locationinfo) ).append(":\n");
207                          message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n");                          if (loc != null) {
208                          message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n");                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
209                                    message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
210                                    message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
211                                    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 226  public class StationList extends ListAct Line 231  public class StationList extends ListAct
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    
# Line 235  public class StationList extends ListAct Line 253  public class StationList extends ListAct
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 245  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 292  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 312  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                  locationLookup.locateStations();                  locationLookup.locateStations();
# Line 321  public class StationList extends ListAct Line 330  public class StationList extends ListAct
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                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
# Line 333  public class StationList extends ListAct Line 342  public class StationList extends ListAct
342          public void startFavoriteLookup() {          public void startFavoriteLookup() {
343                                    
344                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
345                          dialogMessage = "Loading favorites";                          dialogMessage = getString( stationlist_loadfavorites );
346                          showDialog(DLG_PROGRESS);                          showDialog(DLG_PROGRESS);
347    
348                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
349                          findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation());                          findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation());
350                          findStationsTask.execute();                          findStationsTask.execute();
351                  } else {                  } else {
352                          MessageBox.showMessage(this, "Favorite list is empty");                          showMessageAndClose( getString( stationlist_nofavorites ) );
353                  }                  }
354          }          }
355    
# Line 348  public class StationList extends ListAct Line 357  public class StationList extends ListAct
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                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
# Line 357  public class StationList extends ListAct Line 366  public class StationList extends ListAct
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 381  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 402  public class StationList extends ListAct Line 412  public class StationList extends ListAct
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) {
# Line 413  public class StationList extends ListAct Line 423  public class StationList extends ListAct
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("Location 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 495  public class StationList extends ListAct Line 505  public class StationList extends ListAct
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());                                                                  
                                 station.setAddress(addr);  
                                   
                                   
509                                  if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) {                                  if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) {
510                                          if (loc != null) { //only do the distance calc if we have a location                                          if (loc != null) { //only do the distance calc if we have a location
511                                                  dummy.setLatitude(station.getLatitude());                                                  dummy.setLatitude(station.getLatitude());
# Line 522  public class StationList extends ListAct Line 529  public class StationList extends ListAct
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 !?!                                          MessageBox.showMessage(StationList.this, getString(stationlist_nostations) ); // this should not be possible !?!
533                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
534                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
535                                                                    
536                          } else { //communication or parse errors                          } else { //communication or parse errors
537                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
538                                  builder.setMessage("Error on finding nearby stations");                                  builder.setMessage(getString(stationlist_nearbyerror));                        
539                                  builder.setCancelable(true);                                  builder.setCancelable(true);
540                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
541                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
542                                                  dialog.dismiss();                                                  dialog.dismiss();
543                                                                                                    
# Line 542  public class StationList extends ListAct Line 549  public class StationList extends ListAct
549                                                  });                                                  });
550                                          }                                          }
551                                  });                                  });
552                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
553                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
554                                                  dialog.dismiss();                                                  dialog.dismiss();
555                                          }                                                                                                }                                                      
# Line 568  public class StationList extends ListAct Line 575  public class StationList extends ListAct
575                          int stationID = stations.get(selectedPosition).getId();                          int stationID = stations.get(selectedPosition).getId();
576    
577                          if (!favorites.contains(stationID)) {                          if (!favorites.contains(stationID)) {
578                                  menu.add(0, FAVORITES_ADD, 0, "Add to favorites");                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
579                          } else {                          } else {
580                                  menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites");                                  menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
581                          }                          }
582                                                    
583                  }                  }
# Line 581  public class StationList extends ListAct Line 588  public class StationList extends ListAct
588                          int stationID = sb.getId();                          int stationID = sb.getId();
589                          if (item.getItemId() == FAVORITES_ADD) {                          if (item.getItemId() == FAVORITES_ADD) {
590                                  favorites.add(stationID);                                  favorites.add(stationID);
591                                  Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
592                          } else {                          } else {
593                                                                    
594                                  favorites.remove(stationID);                                  favorites.remove(stationID);
595                                  Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
596                                                                    
597                                                                    
598                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {

Legend:
Removed from v.483  
changed lines
  Added in v.564

  ViewVC Help
Powered by ViewVC 1.1.20