/[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 368 by torben, Wed Sep 30 13:32:20 2009 UTC revision 1027 by torben, Wed Sep 8 06:03:45 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 = 15000; //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            
145    
146            @Override
147            protected void onStart() {
148                    super.onStart();
149                    ProviderFactory.purgeOldEntries();
150            }
151    
152    
153    
154    
155            @Override
156            protected void onDestroy() {
157                    super.onDestroy();
158                    
159                    isRunning = false;
160                    
161                    if (locationLookup != null) {
162                            locationLookup.stopSearch();
163                    }
164                    if (findStationsTask != null) {
165                            findStationsTask.cancel(true);
166                    }              
167            }
168    
169            
170            protected void setTitle() {
171                    String dialogTitle = getResources().getString(app_name);
172                    switch (listType) {
173                    case ListNearest:
174                            dialogTitle += " - " + getString(stationlist_nearbystations);
175                            break;
176                    case ListSearch:
177                            dialogTitle += " - " + getString(stationlist_search);
178                            break;
179                    case ListFavorites:
180                            dialogTitle += " - " + getString(stationlist_favorites);
181                            break;
182                    default:
183                            dialogTitle = "";//not possible                                
184                    }
185            
186                    setTitle(dialogTitle);
187                    
188            }
189            
190                    
191    
192      @Override      @Override
193      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
194      {      {
195          if (dialog != null && dialog.isShowing())          if (dialog != null && dialog.isShowing())
196                  dialog.dismiss();                  dialog.dismiss();
197          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (ArrayList<StationBean>) stations);
198            outState.putSerializable("location", location);
199            
200      }      }
201                    
202                    
203    
204          @Override          @Override
205          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
206                  menu.add(0, OPTIONS_MAP, 0, "Show station map");                  MenuItem item;
207                  menu.add(0, OPTIONS_ABOUT, 0, "About");                  
208                    item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap));
209                    item.setIcon(android.R.drawable.ic_menu_mapmode);
210                    
211                    item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo));
212                    item.setIcon(android.R.drawable.ic_menu_mapmode);              
213                                    
214                  return true;                  return true;
215          }          }
216    
217          @Override          @Override
218          public boolean onOptionsItemSelected(MenuItem item) {          public boolean onOptionsItemSelected(MenuItem item) {
219                  boolean retval;                  boolean retval = true;
220                    
221                    //TODO: Cleanup
222                  switch (item.getItemId()) {                  switch (item.getItemId()) {
223                  case OPTIONS_MAP:                  case OPTIONS_MAP:
224                                                    
225                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
                         intent.putExtra("userlocation", GeoPair.fromLocation(locator.getLocation()) );  
226                                                    
227                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
228                          for (StationBean st : stations ) {                          for (StationBean st : stations ) {
229                                  stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude()) );                                  stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
230                          }                          }
231                                                    
232                          intent.putExtra("stations", stationPoints);                          intent.putExtra("stations", stationPoints);
233                                                    
234                          startActivity(intent);                          startActivity(intent);
235                          retval = true;                          break;
236                    case OPTIONS_GPSINFO:
237                            Location loc = locationLookup.getLocation();
238                            StringBuffer message = new StringBuffer();
239                            message.append( getString(stationlist_locationinfo) ).append(":\n");
240                            if (loc != null) {
241                                    message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
242                                    message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
243                                    message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
244                                    message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
245                            } else {
246                                    message.append( getString(stationlist_nolocation) );
247                            }                      
248                            
249                            MessageBox.showMessage(this, message.toString(), false);
250                          break;                          break;
251                  default:                  default:
252                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 118  public class StationList extends ListAct Line 254  public class StationList extends ListAct
254                                    
255                  return retval;                  return retval;
256          }          }
257            
258            
259    
260            @Override
261            public boolean onContextItemSelected(MenuItem item) {
262                    contextMenu.onContextItemSelected(item);
263                    return true;
264    
265    
266            }
267            
268            public void showMessageAndClose(String message) {
269                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
270                    builder.setMessage(message)
271                    .setCancelable(false)
272                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
273                            public void onClick(DialogInterface dialog, int id) {
274                                    dialog.dismiss();
275                                    StationList.this.finish();
276                            }
277                    })
278                    .show();
279            }
280    
281    
282    
283    
284          @Override          @Override
285          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
286                  switch (id) {                  switch (id) {
287                  case DLG_PROGRESS:                  case DLG_PROGRESS:
288                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
289                          dlg.setMessage("Wait for location fix");                          dlg.setMessage( getString(stationlist_waitforlocation) );
290                          dlg.setCancelable(false);                          dlg.setCancelable(false);
291                          return dlg;                                              return dlg;                    
292                    case DLG_STATIONNAME:
293                            LayoutInflater factory = LayoutInflater.from(this);
294                            final View rootView = factory.inflate(R.layout.textinput, null);
295                            
296                            
297                            AlertDialog.Builder builder = new AlertDialog.Builder(this);
298                            
299                            builder.setTitle( getString(stationlist_stationsearch) );
300                            builder.setView(rootView);
301                            builder.setCancelable(true);
302                            builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
303                                    public void onClick(DialogInterface dialog, int which) {
304                                            EditText et = (EditText) rootView.findViewById(R.id.EditText);
305                                            dialog.dismiss();
306                                            String search = et.getText().toString().trim();
307                                            if (search.length() >= 2) {
308                                                    startNameSearch(search);
309                                            } else {
310                                                    showMessageAndClose( getString(stationlist_twocharmin) );
311                                            }
312                                    }
313                            });
314                            builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
315                                    public void onClick(DialogInterface dialog, int which) {
316                                            dialog.dismiss();
317                                            StationList.this.finish(); // Close this Activity
318                                    }
319                            });                    
320                            return builder.create();
321                            
322                  default:                  default:
323                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
324                  }                  }
# Line 134  public class StationList extends ListAct Line 326  public class StationList extends ListAct
326          }          }
327                    
328                    
   
329          @Override          @Override
330          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
331                  super.onPrepareDialog(id, dialog);                  super.onPrepareDialog(id, dialog);
# Line 148  public class StationList extends ListAct Line 339  public class StationList extends ListAct
339                          break;                          break;
340                  }                  }
341          }          }
342            
343            @Override
344            protected void onListItemClick(ListView l, View v, int position, long id) {
345                    super.onListItemClick(l, v, position, id);
346                                    
347                    StationBean station = stations.get(position);
348                    
349                    if (isLaunchedforShortcut == true) {
350                            Intent i = new Intent();
351                            i.putExtra("station", station);
352                            setResult(Activity.RESULT_OK, i);
353                            finish();
354                    } else {                
355                            Intent intent = new Intent(this, DepartureList.class);
356                            intent.putExtra("stationbean", station);
357                            startActivity(intent);
358                    }
359            }
360    
361            /////////////////////////////////////////////////////////////
362            //
363    
364          public void startLookup() {          public void startLookup() {
365                  isRunning = true;                  isRunning = true;              
366                    dialogMessage = getString( stationlist_waitforlocation );
367                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
368                                    
369                  locator.locateStations();                  locationLookup.locateStations();
370                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS);
371            }
372            
373            void startNameSearch(String name) {
374                    dialogMessage = getString( stationlist_findbyname );
375                    showDialog(DLG_PROGRESS);
376    
377                    findStationsTask = new FindStationsTask();
378                    findStationsTask.searchByName(name);
379                    findStationsTask.execute();
380                    
381            }
382            
383            public void startFavoriteLookup() {
384                    
385                    if (favorites.size() > 0) {
386                            dialogMessage = getString( stationlist_loadfavorites );
387                            showDialog(DLG_PROGRESS);
388    
389                            findStationsTask = new FindStationsTask();
390                            findStationsTask.searchByIds( favorites.toString() );
391                            findStationsTask.execute();
392                    } else {
393                            showMessageAndClose( getString( stationlist_nofavorites ) );
394                    }
395          }          }
396    
397    
398          Handler stationsFetched = new Handler() {          
399            void startLocatorTask()
400            {
401                    dialogMessage = getString( stationlist_findingnearby );
402                    showDialog(DLG_PROGRESS);
403                    
404                    findStationsTask = new FindStationsTask();
405                    findStationsTask.searchByLocation( locationLookup.getLocation() );
406                    findStationsTask.execute();    
407            }
408                    
409            
410            ////////////////////////////////////////////////////////////////////////////
411            // Inner classes
412    
413            class StationsFetchedHandler extends Handler {
414                  @Override                  @Override
415                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
416    
# Line 167  public class StationList extends ListAct Line 419  public class StationList extends ListAct
419                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
420                                                                    
421                                  startLocatorTask();                                  startLocatorTask();
422                                    location = GeoPair.fromLocation( locationLookup.getLocation() );
423                                                                    
424                                  break;                                  break;
425    
426                          case NOPROVIDER:                          case NOPROVIDER:
427                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
428                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
429                                    //StationList.this.finish();
430                                  break;                                  break;
431                          case LOCATIONFIXTIMEOUT:                                                          case LOCATIONFIXTIMEOUT:                                
432                                  if (isRunning) {                                  if (isRunning) {
433                                          locator.stopSearch();                                          locationLookup.stopSearch();
434                                          if (locator.hasLocation()) {                                          if (locationLookup.hasLocation()) {
435                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );
436                                          } else {                                                                                          } else {                                                
437                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialog(DLG_PROGRESS);
438                                                                                                    
439                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
440                                                  builder.setMessage("GPS fix timed out");                                                  builder.setMessage(  getString( stationlist_gpstimeout) );
441                                                  builder.setCancelable(true);                                                  builder.setCancelable(true);
442                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
443                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
444                                                                  dialog.dismiss();                                                                  dialog.dismiss();
445                                                                  startLookup();                                                                  startLookup();
446                                                                                                                                    
447                                                          }                                                          }
448                                                  });                                                  });
449                                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                                  builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
450                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
451                                                                  dialog.dismiss();                                                                  dialog.dismiss();
452                                                          }                                                                                                                }                                                      
453                                                  });                                                                                                                                              });
454                                                  builder.show();                                                  builder.show();
455    
456                                          }                                          }
457                                  }                                  }
# Line 206  public class StationList extends ListAct Line 460  public class StationList extends ListAct
460                          isRunning = false;                          isRunning = false;
461                  }                  }
462          };          };
           
         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();  
   
463    
464            
465            class FindStationsTask extends AsyncTask<Void,Void,Void> {
466                                    
467                  Intent intent = new Intent(this, DepartureList.class);                  LookupMethod method = LookupMethod.MethodNone;
468                  intent.putExtra("name", station.getName());                  String name;
469                  intent.putExtra("distance", station.getDistance());                  Location loc;
470                  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) {  
471                                    
472                  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));  
                         }  
473                                                    
474                            method = LookupMethod.ByName;
475                            name = n;
476                    }
477                    
478                    public void searchByLocation(Location l) {
479                            method = LookupMethod.ByLocation;
480                            loc = l;
481                    }
482                    
483                    public void searchByIds(String id) {
484                                                    
485                  } catch (Exception e) {                          method = LookupMethod.ByList;
486                          Log.e("DepartureList", "geocoder failed", e);                          ids = id;
487                  }                  }
488                                    
                 return sb.toString();  
         }  
           
         class LocatorTask extends AsyncTask<Void,Void,Void> {  
                 boolean success;  
489                  @Override                  @Override
490                  protected void onPreExecute() {                  protected void onPreExecute() {
491    
492                            if (method.equals(LookupMethod.MethodNone))
493                                    throw new RuntimeException("Method not set");
494                          super.onPreExecute();                          super.onPreExecute();
495                  }                  }
496                                    
497                  @Override                  @Override
498                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
499                          Location loc = locator.getLocation();  
500                          success = stationProvider.lookupStations(loc);                          switch (method) {
501                                                    case ByLocation:
502                                                            stations = stationProvider.lookupStations(loc);
503                          List<StationBean> stations = stationProvider.getStations();                                  break;
504                          for (StationBean station : stations) {                          case ByName:
505                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());                                  stations = stationProvider.lookupStationsByName(name);
506                                  station.setAddress(addr);                                  break;
507                            case ByList:
508                                    stations = stationProvider.lookupStationsByIds(ids);
509                                    break;
510                            default:
511                                    stations = null; // not possible        
512                          }                          }
513                                                    
514                            
515                          return null;                          return null;
516                  }                  }
517    
# Line 291  public class StationList extends ListAct Line 520  public class StationList extends ListAct
520                          super.onPostExecute(result);                          super.onPostExecute(result);
521                          dialog.dismiss();                          dialog.dismiss();
522                                                    
523                          if (success) {                                                    
524                                  if (stationProvider.getStations().size() == 0)                          if (stations != null) {                        
525                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                  if (stations.size() == 0) {
526                                  stations = stationProvider.getStations();                                          showMessageAndClose(getString(stationlist_nostations));
527                                    }
528    
529                                    StationList.this.getListView().invalidateViews();
530                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
531                                                                    
532                                    
533                          } else { //communication or parse errors                          } else { //communication or parse errors
534                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
535                                  builder.setMessage("Error on finding nearby stations");                                  builder.setMessage(getString(stationlist_fetcherror));                          
536                                  builder.setCancelable(true);                                  builder.setCancelable(true);
537                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
538                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
539                                                  dialog.dismiss();                                                  dialog.dismiss();
540                                                                                                    
541                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
542                                                          @Override                                                  switch (method) {
543                                                          public void run() {                                                  case ByLocation:
544                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
545                                                          }                                                                  @Override
546                                                  });                                                                  public void run() {
547                                                                            startLocatorTask();                                                            
548                                                                    }
549                                                            };
550                                                            break;
551                                                    case ByName:
552                                                            runner = new Runnable() {
553                                                                    @Override
554                                                                    public void run() {
555                                                                            startNameSearch( FindStationsTask.this.name );
556                                                                    }
557                                                            };
558                                                            break;
559                                                    case ByList:
560                                                            runner = new Runnable() {
561                                                                    @Override
562                                                                    public void run() {
563                                                                            startFavoriteLookup();
564                                                                    }
565                                                            };
566                                                            break;
567                                                    }
568                                                    
569                                                    stationsFetched.post( runner );
570                                          }                                          }
571                                  });                                  });
572                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
573                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
574                                                  dialog.dismiss();                                                  dialog.dismiss();
575                                                    StationList.this.finish();
576                                          }                                                                                                }                                                      
577                                  });                                                                                                                              });
578                                  builder.show();                                                          
579                                    builder.show();
580                            }
581                    }
582            }
583            
584            
585            class FavoritesMenu implements OnCreateContextMenuListener {
586                    private final static int FAVORITES_ADD = 9001;
587                    private final static int FAVORITES_REMOVE = 9002;
588                    
589                    private int selectedPosition;
590                    
591                    
592                    @Override
593                    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
594                                                    
595                            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
596                            selectedPosition = info.position;
597                            int stationID = stations.get(selectedPosition).getId();
598    
599                            if (!favorites.contains(stationID)) {
600                                    menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
601                            } else {
602                                    menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
603                            }
604                            
605                    }
606                    
607                    public void onContextItemSelected(MenuItem item) {
608                            StationBean sb = stations.get(selectedPosition);
609                            
610                            int stationID = sb.getId();
611                            if (item.getItemId() == FAVORITES_ADD) {
612                                    favorites.add(stationID);
613                                    Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
614                            } else {
615                                    
616                                    favorites.remove(stationID);
617                                    Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
618                                    
619                                    
620                                    if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
621                                            stations.remove(selectedPosition);
622                                            adapter.notifyDataSetChanged();
623                                    }
624                          }                          }
625                            Editor ed = prefs.edit();
626                            ed.putString("favorites", favorites.toString());
627                            ed.commit();
628                  }                  }
629          }          }
630  }  }

Legend:
Removed from v.368  
changed lines
  Added in v.1027

  ViewVC Help
Powered by ViewVC 1.1.20