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

Legend:
Removed from v.258  
changed lines
  Added in v.1244

  ViewVC Help
Powered by ViewVC 1.1.20