/[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

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

Legend:
Removed from v.331  
changed lines
  Added in v.578

  ViewVC Help
Powered by ViewVC 1.1.20