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

Legend:
Removed from v.285  
changed lines
  Added in v.1144

  ViewVC Help
Powered by ViewVC 1.1.20