/[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 371 by torben, Wed Sep 30 19:09:59 2009 UTC revision 1008 by torben, Tue Aug 3 06:36:29 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;
11  import android.app.ProgressDialog;  import android.app.ProgressDialog;
12  import android.content.DialogInterface;  import android.content.DialogInterface;
13  import android.content.Intent;  import android.content.Intent;
14  import android.location.Address;  import android.content.SharedPreferences;
15  import android.location.Geocoder;  import android.content.SharedPreferences.Editor;
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;
23    import android.view.LayoutInflater;
24  import android.view.Menu;  import android.view.Menu;
25  import android.view.MenuItem;  import android.view.MenuItem;
26  import android.view.View;  import android.view.View;
27    import android.view.ContextMenu.ContextMenuInfo;
28    import android.view.View.OnCreateContextMenuListener;
29    import android.widget.AdapterView;
30    import android.widget.EditText;
31  import android.widget.ListView;  import android.widget.ListView;
32    import android.widget.Toast;
   
33  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
34  import dk.thoerup.traininfo.provider.StationProvider;  import dk.thoerup.traininfo.provider.StationProvider;
35  import dk.thoerup.traininfo.stationmap.GeoPair;  import dk.thoerup.traininfo.stationmap.GeoPair;
36  import dk.thoerup.traininfo.stationmap.StationMapView;  import dk.thoerup.traininfo.stationmap.StationMapView;
37    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                    
48          public static final int OPTIONS_MAP = 2001;          public static final int OPTIONS_MAP = 2003;
49          public static final int OPTIONS_ABOUT = 2002;          public static final int OPTIONS_GPSINFO = 2004;
50            
51            public static final int DLG_PROGRESS = 3001;
52            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 {
59                    ByLocation,
60                    ByName,
61                    ByList,
62                    MethodNone
63            }
64                    
         public static final int DLG_PROGRESS = 1001;  
65                    
         /** Called when the activity is first created. */  
66          String dialogMessage = "";          String dialogMessage = "";
67          ProgressDialog dialog;          ProgressDialog dialog;
68          LocationLookup locator = null;          LocationLookup locationLookup = null;
69          LocatorTask locatorTask;          FindStationsTask findStationsTask;
70            StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
71                    
72            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                    
78          StationProvider stationProvider = ProviderFactory.getStationProvider();          StationProvider stationProvider = ProviderFactory.getStationProvider();
79                    
80            StationListAdapter adapter = null;
81                    
82            FavoritesMenu contextMenu = new FavoritesMenu();
83            IntSet favorites = new IntSet();
84    
85            WelcomeScreen.ListType listType;
86            SharedPreferences prefs;
87            
88            ///////////////////////////////////////////////////////////////////////////////////////////
89            //Activity call backs
90                    
         StationListAdapter adapter = null;  
91          @SuppressWarnings("unchecked")          @SuppressWarnings("unchecked")
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);
99                  setListAdapter(adapter);                  setListAdapter(adapter);
100                                    
101                  locator = new LocationLookup(this, stationsFetched);                  ListView lv = getListView();
102                    lv.setOnCreateContextMenuListener(contextMenu);
103                    
104                    locationLookup = new LocationLookup(this, stationsFetched);
105                    
106    
107                    prefs = getSharedPreferences("TrainStation", 0);
108                    String favoriteString = prefs.getString("favorites", "");
109                    if (! favoriteString.equals("") ) {
110                            favorites.fromString(favoriteString);
111                    }
112                    
113                    listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type");
114                    setTitle();
115                    
116                    isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false);
117                    
118                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
119                          startLookup();  
120                            
121                            switch (listType) {
122                            case ListNearest:
123                                    startLookup();
124                                    break;
125                            case ListSearch:                                
126                                    showDialog(DLG_STATIONNAME);
127                                    break;
128                            case ListFavorites:
129                                    startFavoriteLookup();
130                                    break;
131                            default:
132                                    // Not possible !?!
133                            }
134                            
135                  } else {                  } else {
136                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
137                          adapter.setStations(stations);                          adapter.setStations(stations);
138                            location = (GeoPair) savedInstanceState.getSerializable("location");
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    
180      @Override      @Override
181      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
# Line 81  public class StationList extends ListAct Line 183  public class StationList extends ListAct
183          if (dialog != null && dialog.isShowing())          if (dialog != null && dialog.isShowing())
184                  dialog.dismiss();                  dialog.dismiss();
185          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (ArrayList<StationBean>) stations);
186            outState.putSerializable("location", location);
187            
188      }      }
189                    
190                    
191    
192          @Override          @Override
193          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
194                  menu.add(0, OPTIONS_MAP, 0, "Show station map");                  MenuItem item;
195                  menu.add(0, OPTIONS_ABOUT, 0, "About");                  
196                    item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
197                    item.setIcon(android.R.drawable.ic_menu_mapmode);
198                    
199                    item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
200                    item.setIcon(android.R.drawable.ic_menu_mapmode);              
201                                    
202                  return true;                  return true;
203          }          }
204    
205          @Override          @Override
206          public boolean onOptionsItemSelected(MenuItem item) {          public boolean onOptionsItemSelected(MenuItem item) {
207                  boolean retval;                  boolean retval = true;
208                    
209                    //TODO: Cleanup
210                  switch (item.getItemId()) {                  switch (item.getItemId()) {
211                  case OPTIONS_MAP:                  case OPTIONS_MAP:
212                                                    
213                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
                         intent.putExtra("userlocation", GeoPair.fromLocation(locator.getLocation()) );  
214                                                    
215                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
216                          for (StationBean st : stations ) {                          for (StationBean st : stations ) {
# Line 111  public class StationList extends ListAct Line 220  public class StationList extends ListAct
220                          intent.putExtra("stations", stationPoints);                          intent.putExtra("stations", stationPoints);
221                                                    
222                          startActivity(intent);                          startActivity(intent);
                         retval = true;  
223                          break;                          break;
224                  case OPTIONS_ABOUT:                  case OPTIONS_GPSINFO:
225                          String ver = this.getResources().getString(R.string.app_version);                          Location loc = locationLookup.getLocation();
                           
226                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
227                          message.append("TrainInfo DK v").append(ver).append("\n");                          message.append( getString(stationlist_locationinfo) ).append(":\n");
228                          message.append("By Torben Nielsen\n");                          if (loc != null) {
229                                    message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
230                          MessageBox.showMessage(this, message.toString());                                  message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
231                          retval = true;                                  message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
232                                    message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
233                            } else {
234                                    message.append( getString(stationlist_nolocation) );
235                            }                      
236                            
237                            MessageBox.showMessage(this, message.toString(), false);
238                          break;                          break;
239                  default:                  default:
240                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 129  public class StationList extends ListAct Line 242  public class StationList extends ListAct
242                                    
243                  return retval;                  return retval;
244          }          }
245            
246            
247    
248            @Override
249            public boolean onContextItemSelected(MenuItem item) {
250                    contextMenu.onContextItemSelected(item);
251                    return true;
252    
253    
254            }
255            
256            public void showMessageAndClose(String message) {
257                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
258                    builder.setMessage(message)
259                    .setCancelable(false)
260                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
261                            public void onClick(DialogInterface dialog, int id) {
262                                    dialog.dismiss();
263                                    StationList.this.finish();
264                            }
265                    })
266                    .show();
267            }
268    
269    
270    
271    
272          @Override          @Override
273          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
274                  switch (id) {                  switch (id) {
275                  case DLG_PROGRESS:                  case DLG_PROGRESS:
276                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
277                          dlg.setMessage("Wait for location fix");                          dlg.setMessage( getString(stationlist_waitforlocation) );
278                          dlg.setCancelable(false);                          dlg.setCancelable(false);
279                          return dlg;                                              return dlg;                    
280                    case DLG_STATIONNAME:
281                            LayoutInflater factory = LayoutInflater.from(this);
282                            final View rootView = factory.inflate(R.layout.textinput, null);
283                            
284                            
285                            AlertDialog.Builder builder = new AlertDialog.Builder(this);
286                            
287                            builder.setTitle( getString(stationlist_stationsearch) );
288                            builder.setView(rootView);
289                            builder.setCancelable(true);
290                            builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
291                                    public void onClick(DialogInterface dialog, int which) {
292                                            EditText et = (EditText) rootView.findViewById(R.id.EditText);
293                                            dialog.dismiss();
294                                            String search = et.getText().toString().trim();
295                                            if (search.length() >= 2) {
296                                                    startNameSearch(search);
297                                            } else {
298                                                    showMessageAndClose( getString(stationlist_twocharmin) );
299                                            }
300                                    }
301                            });
302                            builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
303                                    public void onClick(DialogInterface dialog, int which) {
304                                            dialog.dismiss();
305                                            StationList.this.finish(); // Close this Activity
306                                    }
307                            });                    
308                            return builder.create();
309                            
310                  default:                  default:
311                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
312                  }                  }
# Line 145  public class StationList extends ListAct Line 314  public class StationList extends ListAct
314          }          }
315                    
316                    
   
317          @Override          @Override
318          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
319                  super.onPrepareDialog(id, dialog);                  super.onPrepareDialog(id, dialog);
# Line 159  public class StationList extends ListAct Line 327  public class StationList extends ListAct
327                          break;                          break;
328                  }                  }
329          }          }
330            
331            @Override
332            protected void onListItemClick(ListView l, View v, int position, long id) {
333                    super.onListItemClick(l, v, position, id);
334                                    
335                    StationBean station = stations.get(position);
336                    
337                    if (isLaunchedforShortcut == true) {
338                            Intent i = new Intent();
339                            i.putExtra("station", station);
340                            setResult(Activity.RESULT_OK, i);
341                            finish();
342                    } else {                
343                            Intent intent = new Intent(this, DepartureList.class);
344                            intent.putExtra("stationbean", station);
345                            startActivity(intent);
346                    }
347            }
348    
349            /////////////////////////////////////////////////////////////
350            //
351    
352          public void startLookup() {          public void startLookup() {
353                  isRunning = true;                  isRunning = true;              
354                    dialogMessage = getString( stationlist_waitforlocation );
355                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
356                                    
357                  locator.locateStations();                  locationLookup.locateStations();
358                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS);
359            }
360            
361            void startNameSearch(String name) {
362                    dialogMessage = getString( stationlist_findbyname );
363                    showDialog(DLG_PROGRESS);
364    
365                    findStationsTask = new FindStationsTask();
366                    findStationsTask.searchByName(name);
367                    findStationsTask.execute();
368                    
369            }
370            
371            public void startFavoriteLookup() {
372                    
373                    if (favorites.size() > 0) {
374                            dialogMessage = getString( stationlist_loadfavorites );
375                            showDialog(DLG_PROGRESS);
376    
377                            findStationsTask = new FindStationsTask();
378                            findStationsTask.searchByIds( favorites.toString() );
379                            findStationsTask.execute();
380                    } else {
381                            showMessageAndClose( getString( stationlist_nofavorites ) );
382                    }
383          }          }
384    
385    
386          Handler stationsFetched = new Handler() {          
387            void startLocatorTask()
388            {
389                    dialogMessage = getString( stationlist_findingnearby );
390                    showDialog(DLG_PROGRESS);
391                    
392                    findStationsTask = new FindStationsTask();
393                    findStationsTask.searchByLocation( locationLookup.getLocation() );
394                    findStationsTask.execute();    
395            }
396                    
397            
398            ////////////////////////////////////////////////////////////////////////////
399            // Inner classes
400    
401            class StationsFetchedHandler extends Handler {
402                  @Override                  @Override
403                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
404    
# Line 178  public class StationList extends ListAct Line 407  public class StationList extends ListAct
407                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
408                                                                    
409                                  startLocatorTask();                                  startLocatorTask();
410                                    location = GeoPair.fromLocation( locationLookup.getLocation() );
411                                                                    
412                                  break;                                  break;
413    
414                          case NOPROVIDER:                          case NOPROVIDER:
415                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
416                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
417                                    //StationList.this.finish();
418                                  break;                                  break;
419                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
420                                  if (isRunning) {                                  if (isRunning) {
421                                          locator.stopSearch();                                          locationLookup.stopSearch();
422                                          if (locator.hasLocation()) {                                          if (locationLookup.hasLocation()) {
423                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
424                                          } else {                                                                                          } else {                                                
425                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialog(DLG_PROGRESS);
426                                                                                                    
427                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
428                                                  builder.setMessage("GPS fix timed out");                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
429                                                  builder.setCancelable(true);                                                  builder.setCancelable(true);
430                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
431                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
432                                                                  dialog.dismiss();                                                                  dialog.dismiss();
433                                                                  startLookup();                                                                  startLookup();
434                                                                                                                                    
435                                                          }                                                          }
436                                                  });                                                  });
437                                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                                  builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
438                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
439                                                                  dialog.dismiss();                                                                  dialog.dismiss();
440                                                          }                                                                                                                }                                                      
441                                                  });                                                                                                                                              });
442                                                  builder.show();                                                  builder.show();
443    
444                                          }                                          }
445                                  }                                  }
# Line 217  public class StationList extends ListAct Line 448  public class StationList extends ListAct
448                          isRunning = false;                          isRunning = false;
449                  }                  }
450          };          };
           
         void startLocatorTask()  
         {  
                 dialogMessage = "Finding nearby stations";  
                 showDialog(DLG_PROGRESS);  
                   
                 locatorTask = new LocatorTask();  
                 locatorTask.execute();    
         }  
           
         @Override  
         protected void onListItemClick(ListView l, View v, int position, long id) {  
                 super.onListItemClick(l, v, position, id);  
                                   
                 StationBean station = stations.get(position);  
   
                 double latitude = station.getLatitude();  
                 double longitude = station.getLongitude();  
   
451    
452            
453            class FindStationsTask extends AsyncTask<Void,Void,Void> {
454                                    
455                  Intent intent = new Intent(this, DepartureList.class);                  LookupMethod method = LookupMethod.MethodNone;
456                  intent.putExtra("name", station.getName());                  String name;
457                  intent.putExtra("distance", station.getDistance());                  Location loc;
458                  intent.putExtra("latitude", latitude);                  String ids;
                 intent.putExtra("longitude", longitude);  
                 intent.putExtra("stationid", station.getId());  
                 intent.putExtra("address", station.getAddress());  
                 startActivity(intent);  
         }  
   
         String lookupAddress(double latitude, double longitude) {  
459                                    
460                  Geocoder coder = new Geocoder(this, new Locale("da"));                  public void searchByName(String n) {
                 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));  
                         }  
461                                                    
462                            method = LookupMethod.ByName;
463                            name = n;
464                    }
465                    
466                    public void searchByLocation(Location l) {
467                            method = LookupMethod.ByLocation;
468                            loc = l;
469                    }
470                    
471                    public void searchByIds(String id) {
472                                                    
473                  } catch (Exception e) {                          method = LookupMethod.ByList;
474                          Log.e("DepartureList", "geocoder failed", e);                          ids = id;
475                  }                  }
476                                    
                 return sb.toString();  
         }  
           
         class LocatorTask extends AsyncTask<Void,Void,Void> {  
                 boolean success;  
477                  @Override                  @Override
478                  protected void onPreExecute() {                  protected void onPreExecute() {
479    
480                            if (method.equals(LookupMethod.MethodNone))
481                                    throw new RuntimeException("Method not set");
482                          super.onPreExecute();                          super.onPreExecute();
483                  }                  }
484                                    
485                  @Override                  @Override
486                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
487                          Location loc = locator.getLocation();  
488                          success = stationProvider.lookupStations(loc);                          switch (method) {
489                                                    case ByLocation:
490                                                            stations = stationProvider.lookupStations(loc);
491                          List<StationBean> stations = stationProvider.getStations();                                  break;
492                          for (StationBean station : stations) {                          case ByName:
493                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());                                  stations = stationProvider.lookupStationsByName(name);
494                                  station.setAddress(addr);                                  break;
495                            case ByList:
496                                    stations = stationProvider.lookupStationsByIds(ids);
497                                    break;
498                            default:
499                                    stations = null; // not possible        
500                          }                          }
501                                                    
502                            
503                          return null;                          return null;
504                  }                  }
505    
# Line 302  public class StationList extends ListAct Line 508  public class StationList extends ListAct
508                          super.onPostExecute(result);                          super.onPostExecute(result);
509                          dialog.dismiss();                          dialog.dismiss();
510                                                    
511                          if (success) {                                                    
512                                  if (stationProvider.getStations().size() == 0)                          if (stations != null) {                        
513                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                  if (stations.size() == 0) {
514                                  stations = stationProvider.getStations();                                          showMessageAndClose(getString(stationlist_nostations));
515                                    }
516    
517                                    StationList.this.getListView().invalidateViews();
518                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
519                                                                    
520                                    
521                          } else { //communication or parse errors                          } else { //communication or parse errors
522                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
523                                  builder.setMessage("Error on finding nearby stations");                                  builder.setMessage(getString(stationlist_fetcherror));                          
524                                  builder.setCancelable(true);                                  builder.setCancelable(true);
525                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
526                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
527                                                  dialog.dismiss();                                                  dialog.dismiss();
528                                                                                                    
529                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
530                                                          @Override                                                  switch (method) {
531                                                          public void run() {                                                  case ByLocation:
532                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
533                                                          }                                                                  @Override
534                                                  });                                                                  public void run() {
535                                                                            startLocatorTask();                                                            
536                                                                    }
537                                                            };
538                                                            break;
539                                                    case ByName:
540                                                            runner = new Runnable() {
541                                                                    @Override
542                                                                    public void run() {
543                                                                            startNameSearch( FindStationsTask.this.name );
544                                                                    }
545                                                            };
546                                                            break;
547                                                    case ByList:
548                                                            runner = new Runnable() {
549                                                                    @Override
550                                                                    public void run() {
551                                                                            startFavoriteLookup();
552                                                                    }
553                                                            };
554                                                            break;
555                                                    }
556                                                    
557                                                    stationsFetched.post( runner );
558                                          }                                          }
559                                  });                                  });
560                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
561                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
562                                                  dialog.dismiss();                                                  dialog.dismiss();
563                                                    StationList.this.finish();
564                                          }                                                                                                }                                                      
565                                  });                                                                                                                              });
566                                  builder.show();                                                          
567                                    builder.show();
568                            }
569                    }
570            }
571            
572            
573            class FavoritesMenu implements OnCreateContextMenuListener {
574                    private final static int FAVORITES_ADD = 9001;
575                    private final static int FAVORITES_REMOVE = 9002;
576                    
577                    private int selectedPosition;
578                    
579                    
580                    @Override
581                    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
582                                                    
583                            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
584                            selectedPosition = info.position;
585                            int stationID = stations.get(selectedPosition).getId();
586    
587                            if (!favorites.contains(stationID)) {
588                                    menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
589                            } else {
590                                    menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
591                            }
592                            
593                    }
594                    
595                    public void onContextItemSelected(MenuItem item) {
596                            StationBean sb = stations.get(selectedPosition);
597                            
598                            int stationID = sb.getId();
599                            if (item.getItemId() == FAVORITES_ADD) {
600                                    favorites.add(stationID);
601                                    Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
602                            } else {
603                                    
604                                    favorites.remove(stationID);
605                                    Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
606                                    
607                                    
608                                    if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
609                                            stations.remove(selectedPosition);
610                                            adapter.notifyDataSetChanged();
611                                    }
612                          }                          }
613                            Editor ed = prefs.edit();
614                            ed.putString("favorites", favorites.toString());
615                            ed.commit();
616                  }                  }
617          }          }
618  }  }

Legend:
Removed from v.371  
changed lines
  Added in v.1008

  ViewVC Help
Powered by ViewVC 1.1.20