/[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 480 by torben, Wed Oct 28 12:52:52 2009 UTC revision 984 by torben, Sun Jul 11 15:29:13 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 = 17500; //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;
         String dialogTitle;  
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); //TODO: this.showDialogSafe(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");
                         dialogTitle = savedInstanceState.getString("title");  
                         setTitle(dialogTitle);  
139                  }                  }
140                    
141            }
142            
143    
144            @Override
145            protected void onDestroy() {
146                    super.onDestroy();
147                    
148                    if (findStationsTask != null) {
149                            findStationsTask.cancel(true);
150                    }
151                    if (locationLookup != null) {
152                            locationLookup.stopSearch();
153                    }
154                    isRunning = false;
155          }          }
156    
157            
158            protected void setTitle() {
159                    String dialogTitle = getResources().getString(app_name);
160                    switch (listType) {
161                    case ListNearest:
162                            dialogTitle += " - " + getString(stationlist_nearbystations);
163                            break;
164                    case ListSearch:
165                            dialogTitle += " - " + getString(stationlist_search);
166                            break;
167                    case ListFavorites:
168                            dialogTitle += " - " + getString(stationlist_favorites);
169                            break;
170                    default:
171                            dialogTitle = "";//not possible                                
172                    }
173            
174                    setTitle(dialogTitle);
175                    
176            }
177            
178            
179            /* these 3 dialogs helper functions are very rude and ugly hack
180             * to remove these auto-reported exceptions
181             * - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running?
182             * - java.lang.IllegalArgumentException: View not attached to window manager
183             */
184    
185            /*
186            public void showDialogSafe(int id) {
187                    try {
188                            showDialog(id);
189                    } catch (Exception e) {
190                            Log.e("StationList", "showDialog failed", e);
191                    }
192            }
193            
194            public void dismissDialogSafe(int id) {
195                    try {
196                            dismissDialog(id);
197                    } catch (Exception e) {
198                            Log.e("StationList", "dismissDialog failed", e);
199                    }
200            }
201            public void dismissDialogSafe(Dialog dlg) {
202                    try {
203                            dlg.dismiss();
204                    } catch (Exception e) {
205                            Log.e("StationList", "dismissDialog failed", e);
206                    }
207            }
208            
209            public void builderShowSafe(AlertDialog.Builder builder) {
210                    try {
211                            builder.show();
212                    } catch (Exception e) {
213                            Log.e("StationList", "builder.show() failed", e);
214                    }
215                    
216            }*/
217            
218            /* EOF rude and ugly dialog hack */
219            
220    
221    
222      @Override      @Override
223      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
# Line 129  public class StationList extends ListAct Line 226  public class StationList extends ListAct
226                  dialog.dismiss();                  dialog.dismiss();
227          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (ArrayList<StationBean>) stations);
228          outState.putSerializable("location", location);          outState.putSerializable("location", location);
229          outState.putString("title", dialogTitle);          
230      }      }
231                    
232                    
# Line 138  public class StationList extends ListAct Line 235  public class StationList extends ListAct
235          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
236                  MenuItem item;                  MenuItem item;
237                                    
238                  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");  
239                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
240                                    
241                  item = menu.add(0, OPTIONS_ABOUT, 0, "About");                  item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
242                  item.setIcon(android.R.drawable.ic_menu_info_details);                  item.setIcon(android.R.drawable.ic_menu_mapmode);              
243                    
244                  return true;                  return true;
245          }          }
246    
# Line 159  public class StationList extends ListAct Line 248  public class StationList extends ListAct
248          public boolean onOptionsItemSelected(MenuItem item) {          public boolean onOptionsItemSelected(MenuItem item) {
249                  boolean retval = true;                  boolean retval = true;
250    
251                                    //TODO: Cleanup
252                  switch (item.getItemId()) {                  switch (item.getItemId()) {
                 case OPTIONS_RESCAN:  
                         startLookup();  
                         break;  
                 case OPTIONS_NAMESEARCH:  
                         showDialog(DLG_STATIONNAME);  
                         break;  
                 case OPTIONS_FAVORITES:  
                         startFavoriteLookup();  
                         break;  
253                  case OPTIONS_MAP:                  case OPTIONS_MAP:
254                                                    
255                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
                         intent.putExtra("userlocation", location );  
256                                                    
257                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
258                          for (StationBean st : stations ) {                          for (StationBean st : stations ) {
# Line 184  public class StationList extends ListAct Line 263  public class StationList extends ListAct
263                                                    
264                          startActivity(intent);                          startActivity(intent);
265                          break;                          break;
266                  case OPTIONS_ABOUT:                  case OPTIONS_GPSINFO:
267                          String ver = this.getResources().getString(R.string.app_version);                          Location loc = locationLookup.getLocation();
                           
                         Location loc = locator.getLocation();  
268                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
269                          message.append("TrainInfo DK v").append(ver).append("\n");                          message.append( getString(stationlist_locationinfo) ).append(":\n");
270                          message.append("By Torben Nielsen\n");                          if (loc != null) {
271                          message.append("\n");                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
272                          message.append("Location info:\n");                                  message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
273                          message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n");                                  message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
274                          message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n");                                  message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
275                            } else {
276                          MessageBox.showMessage(this, message.toString());                                  message.append( getString(stationlist_nolocation) );
277                            }                      
278                            
279                            MessageBox.showMessage(this, message.toString(), false);
280                          break;                          break;
281                  default:                  default:
282                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 214  public class StationList extends ListAct Line 294  public class StationList extends ListAct
294    
295    
296          }          }
297            
298            public void showMessageAndClose(String message) {
299                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
300                    builder.setMessage(message)
301                    .setCancelable(false)
302                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
303                            public void onClick(DialogInterface dialog, int id) {
304                                    dialog.dismiss();
305                                    StationList.this.finish();
306                            }
307                    })
308                    .show();
309            }
310    
311    
312    
# Line 223  public class StationList extends ListAct Line 316  public class StationList extends ListAct
316                  switch (id) {                  switch (id) {
317                  case DLG_PROGRESS:                  case DLG_PROGRESS:
318                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
319                          dlg.setMessage("Wait for location fix");                          dlg.setMessage( getString(stationlist_waitforlocation) );
320                          dlg.setCancelable(false);                          dlg.setCancelable(false);
321                          return dlg;                                              return dlg;                    
322                  case DLG_STATIONNAME:                  case DLG_STATIONNAME:
# Line 233  public class StationList extends ListAct Line 326  public class StationList extends ListAct
326                                                    
327                          AlertDialog.Builder builder = new AlertDialog.Builder(this);                          AlertDialog.Builder builder = new AlertDialog.Builder(this);
328                                                    
329                          builder.setTitle("Station search");                          builder.setTitle( getString(stationlist_stationsearch) );
330                          builder.setView(rootView);                          builder.setView(rootView);
331                          builder.setCancelable(true);                          builder.setCancelable(true);
332                          builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {                          builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
333                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
334                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
335                                          dialog.dismiss();                                          dialog.dismiss();
336                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
337                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
338                                                    startNameSearch(search);
339                                          } else {                                          } else {
340                                                  MessageBox.showMessage(StationList.this, "Two characters minimum" );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
341                                          }                                          }
342                                  }                                  }
343                          });                          });
344                          builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                          builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
345                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
346                                          dialog.dismiss();                                          dialog.dismiss();
347                                            StationList.this.finish(); // Close this Activity
348                                  }                                  }
349                          });                                              });                    
350                          return builder.create();                          return builder.create();
# Line 280  public class StationList extends ListAct Line 375  public class StationList extends ListAct
375                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
376                                                                    
377                  StationBean station = stations.get(position);                  StationBean station = stations.get(position);
   
                 double latitude = station.getLatitude();  
                 double longitude = station.getLongitude();  
   
   
378                                    
379                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
380                  intent.putExtra("name", station.getName());                          Intent i = new Intent();
381                  intent.putExtra("distance", station.getDistance());                          i.putExtra("station", station);
382                  intent.putExtra("latitude", latitude);                          setResult(Activity.RESULT_OK, i);
383                  intent.putExtra("longitude", longitude);                          finish();
384                  intent.putExtra("stationid", station.getId());                  } else {                
385                  intent.putExtra("address", station.getAddress());                          Intent intent = new Intent(this, DepartureList.class);
386                  startActivity(intent);                          intent.putExtra("stationbean", station);
387                            startActivity(intent);
388                    }
389          }          }
390    
391          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
392          //          //
393    
394          public void startLookup() {          public void startLookup() {
395                  isRunning = true;                  isRunning = true;              
396                  dialogMessage = "Wait for location fix";                  dialogMessage = getString( stationlist_waitforlocation );
397                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS);
398                                    
399                  locator.locateStations();                  locationLookup.locateStations();
400                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS);
401          }          }
402                    
403          void startNameSearch(String name) {          void startNameSearch(String name) {
404                  dialogMessage = "Finding stations by name";                  dialogMessage = getString( stationlist_findbyname );
405                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS);
406    
407                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
408                  findStationsTask.searchByName(name, locator.getLocation());                  findStationsTask.searchByName(name);
409                  findStationsTask.execute();                  findStationsTask.execute();
410                                    
411          }          }
# Line 321  public class StationList extends ListAct Line 413  public class StationList extends ListAct
413          public void startFavoriteLookup() {          public void startFavoriteLookup() {
414                                    
415                  if (favorites.size() > 0) {                  if (favorites.size() > 0) {
416                          dialogMessage = "Loading favorites";                          dialogMessage = getString( stationlist_loadfavorites );
417                          showDialog(DLG_PROGRESS);                          showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS);
418    
419                          findStationsTask = new FindStationsTask();                          findStationsTask = new FindStationsTask();
420                          findStationsTask.searchByIds(favorites.toString(), locator.getLocation());                          findStationsTask.searchByIds( favorites.toString() );
421                          findStationsTask.execute();                          findStationsTask.execute();
422                  } else {                  } else {
423                          MessageBox.showMessage(this, "Favorite list is empty");                          showMessageAndClose( getString( stationlist_nofavorites ) );
424                  }                  }
425          }          }
426    
# Line 336  public class StationList extends ListAct Line 428  public class StationList extends ListAct
428                    
429          void startLocatorTask()          void startLocatorTask()
430          {          {
431                  dialogMessage = "Finding nearby stations";                  dialogMessage = getString( stationlist_findingnearby );
432                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS);
433                                    
434                  findStationsTask = new FindStationsTask();                  findStationsTask = new FindStationsTask();
435                  findStationsTask.searchByLocation( locator.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
436                  findStationsTask.execute();                      findStationsTask.execute();    
437          }          }
           
   
         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);  
                 }  
438                                    
                 return sb.toString();  
         }  
           
439                    
440          ////////////////////////////////////////////////////////////////////////////          ////////////////////////////////////////////////////////////////////////////
441          // Inner classes          // Inner classes
# Line 381  public class StationList extends ListAct Line 446  public class StationList extends ListAct
446    
447                          switch (msg.what) {                          switch (msg.what) {
448                          case GOTLOCATION:                          case GOTLOCATION:
449                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS);
450                                                                    
451                                  startLocatorTask();                                  startLocatorTask();
452                                  location = GeoPair.fromLocation( locator.getLocation() );                                  location = GeoPair.fromLocation( locationLookup.getLocation() );
453                                                                    
454                                  break;                                  break;
455    
456                          case NOPROVIDER:                          case NOPROVIDER:
457                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS);
458                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
459                                    //StationList.this.finish();
460                                  break;                                  break;
461                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
462                                  if (isRunning) {                                  if (isRunning) {
463                                          locator.stopSearch();                                          locationLookup.stopSearch();
464                                          if (locator.hasLocation()) {                                          if (locationLookup.hasLocation()) {
465                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
466                                          } else {                                                                                          } else {                                                
467                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS);
468                                                                                                    
469                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
470                                                  builder.setMessage("Location fix timed out");                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
471                                                  builder.setCancelable(true);                                                  builder.setCancelable(true);
472                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
473                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
474                                                                  dialog.dismiss();                                                                  dialog.dismiss();
475                                                                  startLookup();                                                                  startLookup();
476                                                                                                                                    
477                                                          }                                                          }
478                                                  });                                                  });
479                                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                                  builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
480                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
481                                                                  dialog.dismiss();                                                                  dialog.dismiss();
482                                                          }                                                                                                                }                                                      
483                                                  });                                                                                                                                              });
484                                                  builder.show();                                                  builder.show();//TODO:builderShowSafe(builder);
485    
486                                          }                                          }
487                                  }                                  }
# Line 434  public class StationList extends ListAct Line 500  public class StationList extends ListAct
500                  Location loc;                  Location loc;
501                  String ids;                  String ids;
502                                    
503                  public void searchByName(String n, Location l) {                  public void searchByName(String n) {
504                                                    
505                          method = LookupMethod.ByName;                          method = LookupMethod.ByName;
                         loc = l;  
506                          name = n;                          name = n;
507                  }                  }
508                                    
# Line 446  public class StationList extends ListAct Line 511  public class StationList extends ListAct
511                          loc = l;                          loc = l;
512                  }                  }
513                                    
514                  public void searchByIds(String id, Location l) {                  public void searchByIds(String id) {
515                                                    
516                          method = LookupMethod.ByList;                          method = LookupMethod.ByList;
                         loc = l;  
517                          ids = id;                          ids = id;
518                  }                  }
519                                    
# Line 479  public class StationList extends ListAct Line 543  public class StationList extends ListAct
543                          }                          }
544                                                    
545                                                    
                         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);  
                                         }  
                                 }  
   
                         }                                                
                           
546                          return null;                          return null;
547                  }                  }
548    
549                  @Override                  @Override
550                  protected void onPostExecute(Void result) {                  protected void onPostExecute(Void result) {
551                          super.onPostExecute(result);                          super.onPostExecute(result);
552                          dialog.dismiss();                          dialog.dismiss();//TODO:dismissDialogSafe(dialog);
553                                                    
                         //set title  
                         switch (method) {  
                         case ByLocation:  
                                 dialogTitle = "Traininfo DK - Nearby stations";  
                                 break;  
                         case ByName:  
                                 dialogTitle = "Traininfo DK - Search";  
                                 break;  
                         case ByList:  
                                 dialogTitle = "Traininfo DK - Favorites";  
                                 break;  
                         default:  
                                 dialogTitle = "";//not possible                                  
                         }                                
                           
                         StationList.this.setTitle(dialogTitle);  
                         //set title end  
554                                                    
555                          if (success) {                                                    if (success) {                          
556                                  if (stationProvider.getStations().size() == 0)                                  if (stationProvider.getStations().size() == 0) {
557                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                          showMessageAndClose(getString(stationlist_nostations));
558                                    }
559                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
560                                  adapter.setStations( stations );  
561                                    StationList.this.getListView().invalidateViews();
562                                    adapter.setStations( stations );                                
563                                                                    
                                 showingFavorites = method.equals(LookupMethod.ByList);                            
564                                                                    
565                          } else { //communication or parse errors                          } else { //communication or parse errors
566                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
567                                  builder.setMessage("Error on finding nearby stations");                                  builder.setMessage(getString(stationlist_fetcherror));                          
568                                  builder.setCancelable(true);                                  builder.setCancelable(true);
569                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
570                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
571                                                  dialog.dismiss();                                                  dialog.dismiss();
572                                                                                                    
573                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
574                                                          @Override                                                  switch (method) {
575                                                          public void run() {                                                  case ByLocation:
576                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
577                                                          }                                                                  @Override
578                                                  });                                                                  public void run() {
579                                                                            startLocatorTask();                                                            
580                                                                    }
581                                                            };
582                                                            break;
583                                                    case ByName:
584                                                            runner = new Runnable() {
585                                                                    @Override
586                                                                    public void run() {
587                                                                            startNameSearch( FindStationsTask.this.name );
588                                                                    }
589                                                            };
590                                                            break;
591                                                    case ByList:
592                                                            runner = new Runnable() {
593                                                                    @Override
594                                                                    public void run() {
595                                                                            startFavoriteLookup();
596                                                                    }
597                                                            };
598                                                            break;
599                                                    }
600                                                    
601                                                    stationsFetched.post( runner );
602                                          }                                          }
603                                  });                                  });
604                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
605                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
606                                                  dialog.dismiss();                                                  dialog.dismiss();
607                                                    StationList.this.finish();
608                                          }                                                                                                }                                                      
609                                  });                                                                                                                              });
610                                  builder.show();                                                          
611                                    builder.show();//TODO:builderShowSafe(builder);
612                          }                          }
613                  }                  }
614          }          }
# Line 575  public class StationList extends ListAct Line 629  public class StationList extends ListAct
629                          int stationID = stations.get(selectedPosition).getId();                          int stationID = stations.get(selectedPosition).getId();
630    
631                          if (!favorites.contains(stationID)) {                          if (!favorites.contains(stationID)) {
632                                  menu.add(0, FAVORITES_ADD, 0, "Add to favorites");                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
633                          } else {                          } else {
634                                  menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites");                                  menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
635                          }                          }
636                                                    
637                  }                  }
# Line 588  public class StationList extends ListAct Line 642  public class StationList extends ListAct
642                          int stationID = sb.getId();                          int stationID = sb.getId();
643                          if (item.getItemId() == FAVORITES_ADD) {                          if (item.getItemId() == FAVORITES_ADD) {
644                                  favorites.add(stationID);                                  favorites.add(stationID);
645                                  Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
646                          } else {                          } else {
647                                                                    
648                                  favorites.remove(stationID);                                  favorites.remove(stationID);
649                                  Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
650                                    
651                                                                    
652                                  if (showingFavorites) {                                  if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
653                                          stations.remove(selectedPosition);                                          stations.remove(selectedPosition);
654                                          adapter.notifyDataSetChanged();                                          adapter.notifyDataSetChanged();
655                                  }                                  }

Legend:
Removed from v.480  
changed lines
  Added in v.984

  ViewVC Help
Powered by ViewVC 1.1.20