/[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 439 by torben, Sun Oct 11 07:07:29 2009 UTC revision 1143 by torben, Tue Sep 28 15:30:13 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
3    import static dk.thoerup.traininfo.R.string.app_name;
4    import static dk.thoerup.traininfo.R.string.generic_cancel;
5    import static dk.thoerup.traininfo.R.string.generic_retry;
6    import static dk.thoerup.traininfo.R.string.generic_search;
7    import static dk.thoerup.traininfo.R.string.stationlist_accuracy;
8    import static dk.thoerup.traininfo.R.string.stationlist_addfavorite;
9    import static dk.thoerup.traininfo.R.string.stationlist_favorites;
10    import static dk.thoerup.traininfo.R.string.stationlist_fetcherror;
11    import static dk.thoerup.traininfo.R.string.stationlist_findbyname;
12    import static dk.thoerup.traininfo.R.string.stationlist_findingnearby;
13    import static dk.thoerup.traininfo.R.string.stationlist_gpsinfo;
14    import static dk.thoerup.traininfo.R.string.stationlist_gpstimeout;
15    import static dk.thoerup.traininfo.R.string.stationlist_latitude;
16    import static dk.thoerup.traininfo.R.string.stationlist_loadfavorites;
17    import static dk.thoerup.traininfo.R.string.stationlist_locationinfo;
18    import static dk.thoerup.traininfo.R.string.stationlist_longitude;
19    import static dk.thoerup.traininfo.R.string.stationlist_nearbystations;
20    import static dk.thoerup.traininfo.R.string.stationlist_nofavorites;
21    import static dk.thoerup.traininfo.R.string.stationlist_nolocation;
22    import static dk.thoerup.traininfo.R.string.stationlist_nolocationprovider;
23    import static dk.thoerup.traininfo.R.string.stationlist_nostations;
24    import static dk.thoerup.traininfo.R.string.stationlist_obtainedby;
25    import static dk.thoerup.traininfo.R.string.stationlist_removefavorite;
26    import static dk.thoerup.traininfo.R.string.stationlist_search;
27    import static dk.thoerup.traininfo.R.string.stationlist_stationadded;
28    import static dk.thoerup.traininfo.R.string.stationlist_stationmap;
29    import static dk.thoerup.traininfo.R.string.stationlist_stationremoved;
30    import static dk.thoerup.traininfo.R.string.stationlist_stationsearch;
31    import static dk.thoerup.traininfo.R.string.stationlist_twocharmin;
32    import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation;
33    import static dk.thoerup.traininfo.R.string.stationlist_satellitecount;
34    
35  import java.util.ArrayList;  import java.util.ArrayList;
 import java.util.List;  
 import java.util.Locale;  
36    
37    import android.app.Activity;
38  import android.app.AlertDialog;  import android.app.AlertDialog;
39  import android.app.Dialog;  import android.app.Dialog;
40  import android.app.ListActivity;  import android.app.ListActivity;
# Line 12  import android.content.DialogInterface; Line 43  import android.content.DialogInterface;
43  import android.content.Intent;  import android.content.Intent;
44  import android.content.SharedPreferences;  import android.content.SharedPreferences;
45  import android.content.SharedPreferences.Editor;  import android.content.SharedPreferences.Editor;
 import android.location.Address;  
 import android.location.Geocoder;  
46  import android.location.Location;  import android.location.Location;
47  import android.os.AsyncTask;  import android.os.AsyncTask;
48  import android.os.Bundle;  import android.os.Bundle;
# Line 31  import android.widget.AdapterView; Line 60  import android.widget.AdapterView;
60  import android.widget.EditText;  import android.widget.EditText;
61  import android.widget.ListView;  import android.widget.ListView;
62  import android.widget.Toast;  import android.widget.Toast;
63    import dk.thoerup.android.traininfo.common.StationBean;
64    import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
65  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
66  import dk.thoerup.traininfo.provider.StationProvider;  import dk.thoerup.traininfo.provider.StationProvider;
67  import dk.thoerup.traininfo.stationmap.GeoPair;  import dk.thoerup.traininfo.stationmap.GeoPair;
# Line 39  import dk.thoerup.traininfo.util.IntSet; Line 70  import dk.thoerup.traininfo.util.IntSet;
70  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
71    
72  public class StationList extends ListActivity  {  public class StationList extends ListActivity  {
73          public static final int GOTLOCATION = 1001;  
         public static final int GOTSTATIONLIST = 1002;  
         public static final int NOPROVIDER = 1003;  
         public static final int LOCATIONFIXTIMEOUT = 1004;  
74                    
         public static final int OPTIONS_RESCAN = 2001;  
         public static final int OPTIONS_NAMESEARCH = 2002;  
         public static final int OPTIONS_MAP = 2003;  
         public static final int OPTIONS_ABOUT = 2004;  
         public static final int OPTIONS_FAVORITES = 2005;  
75                    
76            public static final int OPTIONS_MAP = 2003;
77            public static final int OPTIONS_GPSINFO = 2004;
78                    
79          public static final int DLG_PROGRESS = 3001;          public static final int DLG_PROGRESS = 3001;
80          public static final int DLG_STATIONNAME = 3002;          public static final int DLG_STATIONNAME = 3002;
81                    
82          /** Called when the activity is first created. */          
83            public static final int GPS_TIMEOUT_MS = 15000; //how long are we willing to wait for gps fix -in milliseconds
84            
85            
86            static enum LookupMethod {
87                    ByLocation,
88                    ByName,
89                    ByList,
90                    MethodNone
91            }
92            
93            
94          String dialogMessage = "";          String dialogMessage = "";
95          ProgressDialog dialog;          ProgressDialog dialog;
96          LocationLookup locator = null;          LocationLookup locationLookup = null;
97          LocatorTask locatorTask;          FindStationsTask findStationsTask;
98          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
99                    
100          GeoPair location = new GeoPair();          //GeoPair location = new GeoPair();
101            
102          boolean isRunning = false;          boolean isLaunchedforShortcut;
103          List<StationBean> stations = new ArrayList<StationBean>();  
104            StationBean stations = new StationBean();
105                    
106          StationProvider stationProvider = ProviderFactory.getStationProvider();          StationProvider stationProvider = ProviderFactory.getStationProvider();
107                                    
# Line 73  public class StationList extends ListAct Line 109  public class StationList extends ListAct
109                    
110          FavoritesMenu contextMenu = new FavoritesMenu();          FavoritesMenu contextMenu = new FavoritesMenu();
111          IntSet favorites = new IntSet();          IntSet favorites = new IntSet();
112            
113          static enum LookupMethod {          WelcomeScreen.ListType listType;
                 ByLocation,  
                 ByName,  
                 ByList,  
                 MethodNone  
         }  
           
114          SharedPreferences prefs;          SharedPreferences prefs;
115                                    
116          ///////////////////////////////////////////////////////////////////////////////////////////          ///////////////////////////////////////////////////////////////////////////////////////////
117          //Activity call backs          //Activity call backs
118                    
119          @SuppressWarnings("unchecked")  
120          @Override          @Override
121          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
122                  super.onCreate(savedInstanceState);                  super.onCreate(savedInstanceState);
123                  setContentView(R.layout.main);                  setContentView(R.layout.stationlist);
124                                    
125                                    
126                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this);
# Line 99  public class StationList extends ListAct Line 129  public class StationList extends ListAct
129                  ListView lv = getListView();                  ListView lv = getListView();
130                  lv.setOnCreateContextMenuListener(contextMenu);                  lv.setOnCreateContextMenuListener(contextMenu);
131                                    
132                  locator = new LocationLookup(this, stationsFetched);                  locationLookup = new LocationLookup(this);
133                                    
134    
135                  prefs = getSharedPreferences("TrainStation", 0);                  prefs = getSharedPreferences("TrainStation", 0);
# Line 108  public class StationList extends ListAct Line 138  public class StationList extends ListAct
138                          favorites.fromString(favoriteString);                          favorites.fromString(favoriteString);
139                  }                  }
140                                    
141                    listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
142                    setTitle();
143                    
144                    isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);
145                    
146                    ProviderFactory.purgeOldEntries(); //cleanup before fetching more data
147                    
148                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
149                          startLookup();  
150                            
151                            switch (listType) {
152                            case ListNearest:
153                                    startLookup();
154                                    break;
155                            case ListSearch:                                
156                                    showDialog(DLG_STATIONNAME);
157                                    break;
158                            case ListFavorites:
159                                    startFavoriteLookup();
160                                    break;
161                            default:
162                                    // Not possible !?!
163                            }
164                            
165                  } else {                  } else {
166                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");                          stations = (StationBean) savedInstanceState.getSerializable("stations");
167                          adapter.setStations(stations);                          adapter.setStations(stations);
                         location = (GeoPair) savedInstanceState.getSerializable("location");  
168                  }                  }
169                    
170            }
171            
172            
173    
174    
175    
176    
177            @Override
178            protected void onDestroy() {
179                    super.onDestroy();
180                    
181                    
182                    if (locationLookup != null) {
183                            locationLookup.stopSearch();
184                    }
185                    if (findStationsTask != null) {
186                            findStationsTask.cancel(true);
187                    }              
188          }          }
189    
190            
191            protected void setTitle() {
192                    String dialogTitle = getResources().getString(app_name);
193                    switch (listType) {
194                    case ListNearest:
195                            dialogTitle += " - " + getString(stationlist_nearbystations);
196                            break;
197                    case ListSearch:
198                            dialogTitle += " - " + getString(stationlist_search);
199                            break;
200                    case ListFavorites:
201                            dialogTitle += " - " + getString(stationlist_favorites);
202                            break;
203                    default:
204                            dialogTitle = "";//not possible                                
205                    }
206            
207                    setTitle(dialogTitle);
208                    
209            }
210            
211                    
212    
213      @Override      @Override
214      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
215      {      {
216          if (dialog != null && dialog.isShowing())          if (dialog != null && dialog.isShowing())
217                  dialog.dismiss();                  dialog.dismiss();
218          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (StationBean) stations);
219          outState.putSerializable("location", location);          
220      }      }
221                    
222                    
# Line 133  public class StationList extends ListAct Line 225  public class StationList extends ListAct
225          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
226                  MenuItem item;                  MenuItem item;
227                                    
228                  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");  
229                  item.setIcon(android.R.drawable.ic_menu_mapmode);                  item.setIcon(android.R.drawable.ic_menu_mapmode);
230                                    
231                  item = menu.add(0, OPTIONS_ABOUT, 0, "About");                  item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
232                  item.setIcon(android.R.drawable.ic_menu_info_details);                  item.setIcon(android.R.drawable.ic_menu_mapmode);              
233                    
234                  return true;                  return true;
235          }          }
236    
# Line 154  public class StationList extends ListAct Line 238  public class StationList extends ListAct
238          public boolean onOptionsItemSelected(MenuItem item) {          public boolean onOptionsItemSelected(MenuItem item) {
239                  boolean retval = true;                  boolean retval = true;
240    
241                                    //TODO: Cleanup
242                  switch (item.getItemId()) {                  switch (item.getItemId()) {
                 case OPTIONS_RESCAN:  
                         startLookup();  
                         break;  
                 case OPTIONS_NAMESEARCH:  
                         showDialog(DLG_STATIONNAME);  
                         break;  
                 case OPTIONS_FAVORITES:  
                         startFavoriteLookup();  
                         break;  
243                  case OPTIONS_MAP:                  case OPTIONS_MAP:
244                                                    
245                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
                         intent.putExtra("userlocation", location );  
246                                                    
247                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
248                          for (StationBean st : stations ) {                          for (StationEntry st : stations.entries ) {
249                                  stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );                                  stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
250                          }                          }
251                                                    
# Line 179  public class StationList extends ListAct Line 253  public class StationList extends ListAct
253                                                    
254                          startActivity(intent);                          startActivity(intent);
255                          break;                          break;
256                  case OPTIONS_ABOUT:                  case OPTIONS_GPSINFO:
257                          String ver = this.getResources().getString(R.string.app_version);                          Location loc = locationLookup.getLocation();
                           
                         Location loc = locator.getLocation();  
258                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
259                          message.append("TrainInfo DK v").append(ver).append("\n");                          message.append( getString(stationlist_locationinfo) ).append(":\n");
260                          message.append("By Torben Nielsen\n");                          if (loc != null) {
261                          message.append("\n");                                  message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
262                          message.append("Location info:\n");                                  message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
263                          message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n");                                  message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
264                          message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n");                                  message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
265                            } else {
266                          MessageBox.showMessage(this, message.toString());                                  message.append( getString(stationlist_nolocation) );
267                            }                      
268                            
269                            MessageBox.showMessage(this, message.toString(), false);
270                          break;                          break;
271                  default:                  default:
272                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 209  public class StationList extends ListAct Line 284  public class StationList extends ListAct
284    
285    
286          }          }
287            
288            public void showMessageAndClose(String message) {
289                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
290                    builder.setMessage(message)
291                    .setCancelable(false)
292                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
293                            public void onClick(DialogInterface dialog, int id) {
294                                    dialog.dismiss();
295                                    StationList.this.finish();
296                            }
297                    })
298                    .show();
299            }
300    
301    
302    
# Line 218  public class StationList extends ListAct Line 306  public class StationList extends ListAct
306                  switch (id) {                  switch (id) {
307                  case DLG_PROGRESS:                  case DLG_PROGRESS:
308                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
309                          dlg.setMessage("Wait for location fix");                          dlg.setMessage( getString(stationlist_waitforlocation) );
310                          dlg.setCancelable(false);                          dlg.setCancelable(false);
311                          return dlg;                                              return dlg;                    
312                  case DLG_STATIONNAME:                  case DLG_STATIONNAME:
# Line 228  public class StationList extends ListAct Line 316  public class StationList extends ListAct
316                                                    
317                          AlertDialog.Builder builder = new AlertDialog.Builder(this);                          AlertDialog.Builder builder = new AlertDialog.Builder(this);
318                                                    
319                          builder.setTitle("Station search");                          builder.setTitle( getString(stationlist_stationsearch) );
320                          builder.setView(rootView);                          builder.setView(rootView);
321                          builder.setCancelable(true);                          builder.setCancelable(true);
322                          builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {                          builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
323                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
324                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);                                          EditText et = (EditText) rootView.findViewById(R.id.EditText);
325                                          dialog.dismiss();                                          dialog.dismiss();
326                                          if (et.getText().toString().length() >= 2) {                                          String search = et.getText().toString().trim();
327                                                  startNameSearch(et.getText().toString());                                          if (search.length() >= 2) {
328                                                    startNameSearch(search);
329                                          } else {                                          } else {
330                                                  MessageBox.showMessage(StationList.this, "Two characters minimum" );                                                  showMessageAndClose( getString(stationlist_twocharmin) );
331                                          }                                          }
332                                  }                                  }
333                          });                          });
334                          builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                          builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
335                                  public void onClick(DialogInterface dialog, int which) {                                  public void onClick(DialogInterface dialog, int which) {
336                                          dialog.dismiss();                                          dialog.dismiss();
337                                            StationList.this.finish(); // Close this Activity
338                                  }                                  }
339                          });                                              });                    
340                          return builder.create();                          return builder.create();
# Line 274  public class StationList extends ListAct Line 364  public class StationList extends ListAct
364          protected void onListItemClick(ListView l, View v, int position, long id) {          protected void onListItemClick(ListView l, View v, int position, long id) {
365                  super.onListItemClick(l, v, position, id);                  super.onListItemClick(l, v, position, id);
366                                                                    
367                  StationBean station = stations.get(position);                  StationEntry station = stations.entries.get(position);
   
                 double latitude = station.getLatitude();  
                 double longitude = station.getLongitude();  
   
   
368                                    
369                  Intent intent = new Intent(this, DepartureList.class);                  if (isLaunchedforShortcut == true) {
370                  intent.putExtra("name", station.getName());                          Intent i = new Intent();
371                  intent.putExtra("distance", station.getDistance());                          i.putExtra("station", station);
372                  intent.putExtra("latitude", latitude);                          setResult(Activity.RESULT_OK, i);
373                  intent.putExtra("longitude", longitude);                          finish();
374                  intent.putExtra("stationid", station.getId());                  } else {                
375                  intent.putExtra("address", station.getAddress());                          Intent intent = new Intent(this, DepartureList.class);
376                  startActivity(intent);                          intent.putExtra("stationbean", station);
377                            startActivity(intent);
378                    }
379          }          }
380    
381          /////////////////////////////////////////////////////////////          /////////////////////////////////////////////////////////////
382          //          //
383    
384          public void startLookup() {          public void startLookup() {
385                  isRunning = true;                  dialogMessage = getString( stationlist_waitforlocation );
                 dialogMessage = "Wait for location fix";  
386                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
387                                    
388                  locator.locateStations();                  locationLookup.locateStations();
389                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(0, 500);
390          }          }
391                    
392          void startNameSearch(String name) {          void startNameSearch(String name) {
393                  dialogMessage = "Finding stations by name";                  dialogMessage = getString( stationlist_findbyname );
394                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
395    
396                  locatorTask = new LocatorTask();                  findStationsTask = new FindStationsTask();
397                  locatorTask.searchByName(name, locator.getLocation());                  findStationsTask.searchByName(name);
398                  locatorTask.execute();                  findStationsTask.execute();
399                                    
400          }          }
401                    
402          public void startFavoriteLookup() {          public void startFavoriteLookup() {
403                                    
404                  if (favorites.toString().length() > 0) {                  if (favorites.size() > 0) {
405                          dialogMessage = "Loading favorites";                          dialogMessage = getString( stationlist_loadfavorites );
406                          showDialog(DLG_PROGRESS);                          showDialog(DLG_PROGRESS);
407    
408                          locatorTask = new LocatorTask();                          findStationsTask = new FindStationsTask();
409                          locatorTask.searchByIds(favorites.toString(), locator.getLocation());                          findStationsTask.searchByIds( favorites.toString() );
410                          locatorTask.execute();                          findStationsTask.execute();
411                  } else {                  } else {
412                          MessageBox.showMessage(this, "Favorite list is empty");                          showMessageAndClose( getString( stationlist_nofavorites ) );
413                  }                  }
414          }          }
415    
# Line 331  public class StationList extends ListAct Line 417  public class StationList extends ListAct
417                    
418          void startLocatorTask()          void startLocatorTask()
419          {          {
420                  dialogMessage = "Finding nearby stations";                  dialogMessage = getString( stationlist_findingnearby );
421                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);              
422                                    
423                  locatorTask = new LocatorTask();                  findStationsTask = new FindStationsTask();
424                  locatorTask.searchByLocation( locator.getLocation() );                  findStationsTask.searchByLocation( locationLookup.getLocation() );
425                  locatorTask.execute();                    findStationsTask.execute();    
426          }          }
           
   
         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);  
                 }  
427                                    
                 return sb.toString();  
         }  
           
428                    
429          ////////////////////////////////////////////////////////////////////////////          ////////////////////////////////////////////////////////////////////////////
430          // Inner classes          // Inner classes
# Line 373  public class StationList extends ListAct Line 432  public class StationList extends ListAct
432          class StationsFetchedHandler extends Handler {          class StationsFetchedHandler extends Handler {
433                  @Override                  @Override
434                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
435                            Log.e("Handler", "WHAT:" + msg.what);
436    
437                            LocationLookup.LookupStates state = locationLookup.getState();
438    
439                          switch (msg.what) {  
440                            switch (state) {
441                          case GOTLOCATION:                          case GOTLOCATION:
442                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
443                                    
444                                  startLocatorTask();                                  startLocatorTask();
445                                  location = GeoPair.fromLocation( locator.getLocation() );  
446                                                                    return;
                                 break;  
447    
448                          case NOPROVIDER:                          case NOPROVIDER:
449                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
450                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
451                                  break;                                  //StationList.this.finish();
452                          case LOCATIONFIXTIMEOUT:                                                                  return;
453                                  if (isRunning) {                          }
454                                          locator.stopSearch();  
455                                          if (locator.hasLocation()) {  
456                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );  
457                                          } else {                                                                          if (locationLookup.elapsedTime() >=  GPS_TIMEOUT_MS) {
458                                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
459                                                    
460                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  locationLookup.stopSearch();
461                                                  builder.setMessage("Location fix timed out");  
462                                                  builder.setCancelable(true);                                  if (locationLookup.hasLocation()) {
463                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                          startLocatorTask();
464                                                          public void onClick(DialogInterface dialog, int id) {                                  } else {                                                
465                                                                  dialog.dismiss();                                          AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
466                                                                  startLookup();                                          builder.setMessage(  getString( stationlist_gpstimeout) );
467                                                                                                            builder.setCancelable(true);
468                                                          }                                          builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
469                                                  });                                                  public void onClick(DialogInterface dialog, int id) {
470                                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                                          dialog.dismiss();
471                                                          public void onClick(DialogInterface dialog, int id) {                                                          startLookup();
472                                                                  dialog.dismiss();  
473                                                          }                                                                                                        }
474                                                  });                                                                                                                                      });
475                                                  builder.show();                                          builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
476                                                    public void onClick(DialogInterface dialog, int id) {
477                                                            dialog.dismiss();
478                                                    }                                                      
479                                            });
480                                            builder.show();
481    
                                         }  
482                                  }                                  }
483                                  break;                          } else {
484                                    if (locationLookup.hasGps()) {
485                                            int count = locationLookup.getSatCount();
486                                            String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count;
487                                            dialog.setMessage( dialogMessage );
488                                    }
489                                    this.sendEmptyMessageDelayed(0, 500);
490                          }                          }
491                          isRunning = false;                          
492                  }                  }
493          };          }
494    
495    
496    
497                    
498          class LocatorTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
499                                    
500                  LookupMethod method = LookupMethod.MethodNone;                  LookupMethod method = LookupMethod.MethodNone;
                 boolean success;  
501                  String name;                  String name;
502                  Location loc;                  Location loc;
503                  String ids;                  String ids;
504                                    
505                  public void searchByName(String n, Location l) {                  public void searchByName(String n) {
506                                                    
507                          method = LookupMethod.ByName;                          method = LookupMethod.ByName;
                         loc = l;  
508                          name = n;                          name = n;
509                  }                  }
510                                    
# Line 441  public class StationList extends ListAct Line 513  public class StationList extends ListAct
513                          loc = l;                          loc = l;
514                  }                  }
515                                    
516                  public void searchByIds(String id, Location l) {                  public void searchByIds(String id) {
517                                                    
518                          method = LookupMethod.ByList;                          method = LookupMethod.ByList;
                         loc = l;  
519                          ids = id;                          ids = id;
520                  }                  }
521                                    
# Line 458  public class StationList extends ListAct Line 529  public class StationList extends ListAct
529                                    
530                  @Override                  @Override
531                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
532                    
533                          if (method.equals(LookupMethod.ByLocation))                          switch (method) {
534                                  success = stationProvider.lookupStations(loc);                          case ByLocation:
535                                                            stations = stationProvider.lookupStations(loc);
536                          if (method.equals(LookupMethod.ByName))                                  break;
537                                  success = stationProvider.lookupStationsByName(name);                          case ByName:
538                                                            stations = stationProvider.lookupStationsByName(name);
539                          if (method.equals(LookupMethod.ByList))                                  break;
540                                  success = stationProvider.lookupStationsByIds(ids);                          case ByList:
541                                                            stations = stationProvider.lookupStationsByIds(ids);
542                          Location dummy = new Location("gps");                                  break;
543                          List<StationBean> stations = stationProvider.getStations();                          default:
544                                    stations = null; // not possible        
545                            }
546                                                    
                         for (StationBean station : stations) {  
                                 String addr = lookupAddress(station.getLatitude(), station.getLongitude());  
                                 station.setAddress(addr);  
                                   
                                 if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) {  
                                         dummy.setLatitude(station.getLatitude());  
                                         dummy.setLongitude(station.getLongitude());  
                                         station.setDistance( (int)loc.distanceTo(dummy) );  
                                 }  
                         }                                                
547                                                    
548                          return null;                          return null;
549                  }                  }
# Line 490  public class StationList extends ListAct Line 553  public class StationList extends ListAct
553                          super.onPostExecute(result);                          super.onPostExecute(result);
554                          dialog.dismiss();                          dialog.dismiss();
555                                                    
556                          if (success) {                                                    
557                                  if (stationProvider.getStations().size() == 0)                          if (stations != null) {                        
558                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                  if (stations.entries.size() == 0) {
559                                  stations = stationProvider.getStations();                                          showMessageAndClose(getString(stationlist_nostations));
560                                    }
561    
562                                    StationList.this.getListView().invalidateViews();
563                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
564                                                                    
565                                    
566                          } else { //communication or parse errors                          } else { //communication or parse errors
567                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
568                                  builder.setMessage("Error on finding nearby stations");                                  builder.setMessage(getString(stationlist_fetcherror));                          
569                                  builder.setCancelable(true);                                  builder.setCancelable(true);
570                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
571                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
572                                                  dialog.dismiss();                                                  dialog.dismiss();
573                                                                                                    
574                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
575                                                          @Override                                                  switch (method) {
576                                                          public void run() {                                                  case ByLocation:
577                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
578                                                          }                                                                  @Override
579                                                  });                                                                  public void run() {
580                                                                            startLocatorTask();                                                            
581                                                                    }
582                                                            };
583                                                            break;
584                                                    case ByName:
585                                                            runner = new Runnable() {
586                                                                    @Override
587                                                                    public void run() {
588                                                                            startNameSearch( FindStationsTask.this.name );
589                                                                    }
590                                                            };
591                                                            break;
592                                                    case ByList:
593                                                            runner = new Runnable() {
594                                                                    @Override
595                                                                    public void run() {
596                                                                            startFavoriteLookup();
597                                                                    }
598                                                            };
599                                                            break;
600                                                    }
601                                                    
602                                                    stationsFetched.post( runner );
603                                          }                                          }
604                                  });                                  });
605                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
606                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
607                                                  dialog.dismiss();                                                  dialog.dismiss();
608                                                    StationList.this.finish();
609                                          }                                                                                                }                                                      
610                                  });                                                                                                                              });
611                                  builder.show();                                                          
612                                    builder.show();
613                          }                          }
614                  }                  }
615          }          }
# Line 535  public class StationList extends ListAct Line 627  public class StationList extends ListAct
627                                                                                                    
628                          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;                          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
629                          selectedPosition = info.position;                          selectedPosition = info.position;
630                          int stationID = stations.get(selectedPosition).getId();                          int stationID = stations.entries.get(selectedPosition).getId();
631    
632                          if (!favorites.hasInt(stationID)) {                          if (!favorites.contains(stationID)) {
633                                  menu.add(0, FAVORITES_ADD, 0, "Add to favorites");                                  menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
634                          } else {                          } else {
635                                  menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites");                                  menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
636                          }                          }
637                                                    
638                  }                  }
639                                    
640                  public void onContextItemSelected(MenuItem item) {                  public void onContextItemSelected(MenuItem item) {
641                          StationBean sb = stations.get(selectedPosition);                          StationEntry sb = stations.entries.get(selectedPosition);
642                                                    
643                          int stationID = sb.getId();                          int stationID = sb.getId();
644                          if (item.getItemId() == FAVORITES_ADD) {                          if (item.getItemId() == FAVORITES_ADD) {
645                                  favorites.add(stationID);                                  favorites.add(stationID);
646                                  Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
647                          } else {                          } else {
648                                    
649                                  favorites.remove(stationID);                                  favorites.remove(stationID);
650                                  Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show();                                  Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
651                                    
652                                    
653                                    if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
654                                            stations.entries.remove(selectedPosition);
655                                            adapter.notifyDataSetChanged();
656                                    }
657                          }                          }
658                          Editor ed = prefs.edit();                          Editor ed = prefs.edit();
659                          ed.putString("favorites", favorites.toString());                          ed.putString("favorites", favorites.toString());

Legend:
Removed from v.439  
changed lines
  Added in v.1143

  ViewVC Help
Powered by ViewVC 1.1.20