/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/StationList.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/StationList.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 336 by torben, Wed Sep 23 12:51:49 2009 UTC revision 1448 by torben, Thu May 5 05:08:00 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;  
 import java.util.Locale;  
36    
37    import android.app.Activity;
38  import android.app.AlertDialog;  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;  import android.content.DialogInterface;
43  import android.content.Intent;  import android.content.Intent;
44  import android.location.Address;  import android.content.SharedPreferences;
45  import android.location.Geocoder;  import android.content.SharedPreferences.Editor;
46  import android.location.Location;  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;  import android.util.Log;
52    import android.view.ContextMenu;
53    import android.view.ContextMenu.ContextMenuInfo;
54    import android.view.LayoutInflater;
55    import android.view.Menu;
56    import android.view.MenuItem;
57  import android.view.View;  import android.view.View;
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.StationEntry;
65  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
66  import dk.thoerup.traininfo.provider.StationProvider;  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 StationList extends ListActivity  {  public class StationList extends ListActivity  {
         public static final int GOTLOCATION = 1001;  
         public static final int GOTSTATIONLIST = 1002;  
         public static final int NOPROVIDER = 1003;  
         public static final int LOCATIONFIXTIMEOUT = 1004;  
73    
74                    
         public static final int DLG_PROGRESS = 1001;  
75                    
76          /** Called when the activity is first created. */          public static final int OPTIONS_MAP = 103;
77            public static final int OPTIONS_GPSINFO = 104;
78            
79            public static final int DLG_PROGRESS = 3001;
80            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 = "";          String dialogMessage = "";
95          ProgressDialog dialog;          ProgressDialog dialog;
96          LocationLookup locator = null;          LocationLookup locationLookup = null;
97          LocatorTask locatorTask;          FindStationsTask findStationsTask;
98            StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
99                    
100          boolean isRunning = false;          //GeoPair location = new GeoPair();
101          List<StationBean> stations = new ArrayList<StationBean>();  
102            boolean isLaunchedforShortcut;
103    
104            StationBean stations = new StationBean();
105                    
106          StationProvider stationProvider = ProviderFactory.getStationProvider();          StationProvider stationProvider = ProviderFactory.getStationProvider();
107                    
108            StationListAdapter adapter = null;
109                    
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          StationListAdapter adapter = null;  
         @SuppressWarnings("unchecked")  
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                                    
                 LocationLookup.removeMockLocation(this);  
                 //LocationLookup.injectMockLocation(this);  
125                                    
126                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this);
127                  setListAdapter(adapter);                  setListAdapter(adapter);
128                                    
129                  locator = new LocationLookup(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(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_info_details);
235                    boolean hasLoc = (locationLookup.getLocation() != null);
236                    item.setEnabled(hasLoc);
237                    
238                    return true;
239            }
240            
241    
242    
243    
244            @Override
245            public boolean onOptionsItemSelected(MenuItem item) {
246                    boolean retval = true;
247    
248                    //TODO: Cleanup
249                    switch (item.getItemId()) {
250                    case OPTIONS_MAP:
251                            
252                            Intent intent = new Intent(this,StationMapView.class);
253                            
254                            ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
255                            for (StationEntry st : stations.entries ) {
256                                    stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
257                            }
258                            
259                            intent.putExtra("stations", stationPoints);
260                            
261                            startActivity(intent);
262                            break;
263                    case OPTIONS_GPSINFO:
264                            Location loc = locationLookup.getLocation();
265                            StringBuffer message = new StringBuffer();
266                            message.append( getString(stationlist_locationinfo) ).append(":\n");
267                            
268                            message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n");
269                            message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n");
270                            message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n");
271                            message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n");
272                                                    
273                            
274                            MessageBox.showMessage(this, message.toString(), false);
275                            break;
276                    default:
277                            retval = super.onOptionsItemSelected(item);
278                    }
279                    
280                    return retval;
281            }
282            
283            
284    
285            @Override
286            public boolean onContextItemSelected(MenuItem item) {
287                    contextMenu.onContextItemSelected(item);
288                    return true;
289    
290    
291            }
292            
293            public void showMessageAndClose(String message) {
294                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
295                    builder.setMessage(message)
296                    .setCancelable(false)
297                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
298                            public void onClick(DialogInterface dialog, int id) {
299                                    dialog.dismiss();
300                                    StationList.this.finish();
301                            }
302                    })
303                    .show();
304            }
305    
306    
307    
308    
309            @Override
310          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
311                  switch (id) {                  switch (id) {
312                  case DLG_PROGRESS:                  case DLG_PROGRESS:
313                          ProgressDialog dlg = new ProgressDialog(this);                          ProgressDialog dlg = new ProgressDialog(this);
314                          dlg.setMessage("Wait for location fix");                          dlg.setMessage( getString(stationlist_waitforlocation) );
315                          dlg.setCancelable(false);                          dlg.setCancelable(false);
316                          return dlg;                                              return dlg;                    
317                    case DLG_STATIONNAME:
318                            LayoutInflater factory = LayoutInflater.from(this);
319                            final View rootView = factory.inflate(R.layout.textinput, null);
320                            
321                            
322                            AlertDialog.Builder builder = new AlertDialog.Builder(this);
323                            
324                            builder.setTitle( getString(stationlist_stationsearch) );
325                            builder.setView(rootView);
326                            builder.setCancelable(true);
327                            builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() {
328                                    public void onClick(DialogInterface dialog, int which) {
329                                            EditText et = (EditText) rootView.findViewById(R.id.EditText);
330                                            dialog.dismiss();
331                                            String search = et.getText().toString().trim();
332                                            if (search.length() >= 2) {
333                                                    startNameLookup(search);
334                                            } else {
335                                                    showMessageAndClose( getString(stationlist_twocharmin) );
336                                            }
337                                    }
338                            });
339                            builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
340                                    public void onClick(DialogInterface dialog, int which) {
341                                            dialog.dismiss();
342                                            StationList.this.finish(); // Close this Activity
343                                    }
344                            });                    
345                            return builder.create();
346                            
347                  default:                  default:
348                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
349                  }                  }
# Line 92  public class StationList extends ListAct Line 351  public class StationList extends ListAct
351          }          }
352                    
353                    
   
354          @Override          @Override
355          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
356                  super.onPrepareDialog(id, dialog);                  super.onPrepareDialog(id, dialog);
# Line 106  public class StationList extends ListAct Line 364  public class StationList extends ListAct
364                          break;                          break;
365                  }                  }
366          }          }
367            
368            @Override
369            protected void onListItemClick(ListView l, View v, int position, long id) {
370                    super.onListItemClick(l, v, position, id);
371                                    
372                    StationEntry station = stations.entries.get(position);
373                    
374                    if (isLaunchedforShortcut == true) {
375                            Intent i = new Intent();
376                            i.putExtra("station", station);
377                            setResult(Activity.RESULT_OK, i);
378                            finish();
379                    } else {                
380                            Intent intent = new Intent(this, DepartureList.class);
381                            intent.putExtra("stationbean", station);
382                            startActivity(intent);
383                    }
384            }
385    
386          public void startLookup() {          /////////////////////////////////////////////////////////////
387                  isRunning = true;          //
388    
389            public void startNearestLookup() {
390                    dialogMessage = getString( stationlist_waitforlocation );
391                    showDialog(DLG_PROGRESS);
392                    
393                    locationLookup.locateStations();
394                    stationsFetched.sendEmptyMessageDelayed(0, 500);
395            }
396            
397            void startNameLookup(String name) {
398                    dialogMessage = getString( stationlist_findbyname );
399                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
400    
401                    findStationsTask = new FindStationsTask();
402                    findStationsTask.searchByName(name);
403                    findStationsTask.execute();
404                                    
                 locator.locateStations();  
                 stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);  
405          }          }
406            
407            public void startFavoriteLookup() {
408                    
409                    if (favorites.size() > 0) {
410                            dialogMessage = getString( stationlist_loadfavorites );
411                            showDialog(DLG_PROGRESS);
412    
413                            findStationsTask = new FindStationsTask();
414                            findStationsTask.searchByIds( favorites.toString() );
415                            findStationsTask.execute();
416                    } else {
417                            showMessageAndClose( getString( stationlist_nofavorites ) );
418                    }
419            }
420    
421    
422            
423            void startLocatorTask()
424            {
425                    dialogMessage = getString( stationlist_findingnearby );
426                    showDialog(DLG_PROGRESS);              
427                    
428                    findStationsTask = new FindStationsTask();
429                    findStationsTask.searchByLocation( locationLookup.getLocation() );
430                    findStationsTask.execute();    
431            }
432                    
433            
434            ////////////////////////////////////////////////////////////////////////////
435            // Inner classes
436    
437          Handler stationsFetched = new Handler() {          class StationsFetchedHandler extends Handler {
438                  @Override                  @Override
439                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
440    
441                          switch (msg.what) {                          LocationLookup.LookupStates state = locationLookup.getState();
442    
443    
444                            switch (state) {
445                          case GOTLOCATION:                          case GOTLOCATION:
446                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
447                                    
448                                  startLocatorTask();                                  startLocatorTask();
449                                    
450                                  break;                                  return;
451    
452                          case NOPROVIDER:                          case NOPROVIDER:
453                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
454                                  MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps.");                                  MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true );
455                                  break;                                  //StationList.this.finish();
456                          case LOCATIONFIXTIMEOUT:                                                                  return;
457                                  if (isRunning) {                          case IDLE:
458                                          locator.stopSearch();                                  Log.e("TrainInfo", "How did this happen ???");
459                                          if (locator.hasLocation()) {                                  dismissDialog(DLG_PROGRESS); // how did we get here ??
460                                                  stationsFetched.sendEmptyMessage( GOTLOCATION );                                  return;
461                                          } else {                                                                                  
462                                                  dismissDialog(DLG_PROGRESS);                          }
463                                                    
464                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                  
465                                                  builder.setMessage("GPS fix timed out");  
466                                                  builder.setCancelable(true);                          if (locationLookup.elapsedTime() >=  GPS_TIMEOUT_MS) {
467                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  dismissDialog(DLG_PROGRESS);
468                                                          public void onClick(DialogInterface dialog, int id) {  
469                                                                  dialog.dismiss();  
470                                                                  startLookup();                                  locationLookup.stopSearch();
471                                                                    
472                                                          }                                  if (locationLookup.hasLocation()) {
473                                                  });                                          startLocatorTask();
474                                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  } else {                                                
475                                                          public void onClick(DialogInterface dialog, int id) {                                          AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
476                                                                  dialog.dismiss();                                          builder.setMessage(  getString( stationlist_gpstimeout) );
477                                                          }                                                                                                builder.setCancelable(true);
478                                                  });                                                                                                                                      builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
479                                                  builder.show();                                                  public void onClick(DialogInterface dialog, int id) {
480                                                            dialog.dismiss();
481                                                            startNearestLookup();
482    
483                                                    }
484                                            });
485                                            builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() {
486                                                    public void onClick(DialogInterface dialog, int id) {
487                                                            dialog.dismiss();
488                                                            StationList.this.finish(); // Close this Activity
489                                                    }                                                      
490                                            });
491                                            builder.show();
492    
                                         }  
493                                  }                                  }
494                                  break;                          } else {
495                                    if (locationLookup.hasGps()) {
496                                            int count = locationLookup.getSatCount();
497                                            String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count;
498                                            dialog.setMessage( dialogMessage );
499                                    }
500                                    this.sendEmptyMessageDelayed(0, 500);
501                          }                          }
502                          isRunning = false;                          
503                  }                  }
         };  
           
         void startLocatorTask()  
         {  
                 dialogMessage = "Finding nearby stations";  
                 showDialog(DLG_PROGRESS);  
                   
                 locatorTask = new LocatorTask();  
                 locatorTask.execute();    
504          }          }
           
         @Override  
         protected void onListItemClick(ListView l, View v, int position, long id) {  
                 super.onListItemClick(l, v, position, id);  
                                   
                 StationBean station = stations.get(position);  
505    
                 double latitude = station.getLatitude();  
                 double longitude = station.getLongitude();  
506    
507    
508            
509            class FindStationsTask extends AsyncTask<Void,Void,Void> {
510                                    
511                  Intent intent = new Intent(this, DepartureList.class);                  LookupMethod method = LookupMethod.MethodNone;
512                  intent.putExtra("name", station.getName());                  String name;
513                  intent.putExtra("distance", station.getDistance());                  Location loc;
514                  intent.putExtra("latitude", latitude);                  String ids;
                 intent.putExtra("longitude", longitude);  
                 intent.putExtra("stationid", station.getId());  
                 intent.putExtra("address", station.getAddress());  
                 startActivity(intent);  
         }  
   
         String lookupAddress(double latitude, double longitude) {  
515                                    
516                  Geocoder coder = new Geocoder(this, new Locale("da"));                  public void searchByName(String n) {
                 StringBuilder sb = new StringBuilder();  
                 Log.i("lookupaddr", "" + latitude + "/" + longitude);  
                 try {  
                         List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);  
                         Address addr = addressList.get(0);  
                           
                           
                         int max = addr.getMaxAddressLineIndex();  
                         for (int i=0; i<max; i++) {  
                                 if (i>0)  
                                         sb.append(", ");  
                                   
                                 sb.append(addr.getAddressLine(i));  
                         }  
517                                                    
518                            method = LookupMethod.ByName;
519                            name = n;
520                    }
521                    
522                    public void searchByLocation(Location l) {
523                            method = LookupMethod.ByLocation;
524                            loc = l;
525                    }
526                    
527                    public void searchByIds(String id) {
528                                                    
529                  } catch (Exception e) {                          method = LookupMethod.ByList;
530                          Log.e("DepartureList", "geocoder failed", e);                          ids = id;
531                  }                  }
532                                    
                 return sb.toString();  
         }  
           
         class LocatorTask extends AsyncTask<Void,Void,Void> {  
                 boolean success;  
533                  @Override                  @Override
534                  protected void onPreExecute() {                  protected void onPreExecute() {
535    
536                            if (method.equals(LookupMethod.MethodNone))
537                                    throw new RuntimeException("Method not set");
538                          super.onPreExecute();                          super.onPreExecute();
539                  }                  }
540                                    
541                  @Override                  @Override
542                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
543                          Location loc = locator.getLocation();  
544                          success = stationProvider.lookupStations(loc);                          switch (method) {
545                                                    case ByLocation:
546                                                            stations = stationProvider.lookupStationsByLocation(loc);
547                          List<StationBean> stations = stationProvider.getStations();                                  break;
548                          for (StationBean station : stations) {                          case ByName:
549                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());                                  stations = stationProvider.lookupStationsByName(name);
550                                  station.setAddress(addr);                                  break;
551                            case ByList:
552                                    stations = stationProvider.lookupStationsByIds(ids);
553                                    break;
554                            default:
555                                    stations = null; // not possible        
556                          }                          }
557                                                    
558                            
559                          return null;                          return null;
560                  }                  }
561    
# Line 249  public class StationList extends ListAct Line 564  public class StationList extends ListAct
564                          super.onPostExecute(result);                          super.onPostExecute(result);
565                          dialog.dismiss();                          dialog.dismiss();
566                                                    
567                          if (success) {                                                    
568                                  if (stationProvider.getStations().size() == 0)                          if (stations != null) {                        
569                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                  if (stations.entries.size() == 0) {
570                                  stations = stationProvider.getStations();                                          showMessageAndClose(getString(stationlist_nostations));
571                                    }
572    
573                                    StationList.this.getListView().invalidateViews();
574                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );                                
575                                                                    
576                                    
577                          } else { //communication or parse errors                          } else { //communication or parse errors
578                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
579                                  builder.setMessage("Error on finding nearby stations");                                  builder.setMessage(getString(stationlist_fetcherror));                          
580                                  builder.setCancelable(true);                                  builder.setCancelable(true);
581                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                  builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() {
582                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
583                                                  dialog.dismiss();                                                  dialog.dismiss();
584                                                                                                    
585                                                  stationsFetched.post( new Runnable() {                                                  Runnable runner = null;
586                                                          @Override                                                  switch (method) {
587                                                          public void run() {                                                  case ByLocation:
588                                                                  startLocatorTask();                                                                                                                      runner = new Runnable() {
589                                                          }                                                                  @Override
590                                                  });                                                                  public void run() {
591                                                                            startLocatorTask();                                                            
592                                                                    }
593                                                            };
594                                                            break;
595                                                    case ByName:
596                                                            runner = new Runnable() {
597                                                                    @Override
598                                                                    public void run() {
599                                                                            startNameLookup( FindStationsTask.this.name );
600                                                                    }
601                                                            };
602                                                            break;
603                                                    case ByList:
604                                                            runner = new Runnable() {
605                                                                    @Override
606                                                                    public void run() {
607                                                                            startFavoriteLookup();
608                                                                    }
609                                                            };
610                                                            break;
611                                                    }
612                                                    
613                                                    stationsFetched.post( runner );
614                                          }                                          }
615                                  });                                  });
616                                  builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                                  builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() {
617                                          public void onClick(DialogInterface dialog, int id) {                                          public void onClick(DialogInterface dialog, int id) {
618                                                  dialog.dismiss();                                                  dialog.dismiss();
619                                                    StationList.this.finish();
620                                          }                                                                                                }                                                      
621                                  });                                                                                                                              });
622                                  builder.show();                                                          
623                                    builder.show();
624                            }
625                    }
626            }
627            
628            
629            class FavoritesMenu implements OnCreateContextMenuListener {
630                    private final static int FAVORITES_ADD = 9001;
631                    private final static int FAVORITES_REMOVE = 9002;
632                    
633                    private int selectedPosition;
634                    
635                    
636                    @Override
637                    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
638                                                    
639                            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
640                            selectedPosition = info.position;
641                            int stationID = stations.entries.get(selectedPosition).getId();
642    
643                            if (!favorites.contains(stationID)) {
644                                    menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) );
645                            } else {
646                                    menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) );
647                            }
648                            
649                    }
650                    
651                    public void onContextItemSelected(MenuItem item) {
652                            StationEntry sb = stations.entries.get(selectedPosition);
653                            
654                            int stationID = sb.getId();
655                            if (item.getItemId() == FAVORITES_ADD) {
656                                    favorites.add(stationID);
657                                    Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show();
658                            } else {
659                                    
660                                    favorites.remove(stationID);
661                                    Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show();
662                                    
663                                    
664                                    if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {
665                                            stations.entries.remove(selectedPosition);
666                                            adapter.notifyDataSetChanged();
667                                    }
668                          }                          }
669                            Editor ed = prefs.edit();
670                            ed.putString("favorites", favorites.toString());
671                            ed.commit();
672                  }                  }
673          }          }
674  }  }

Legend:
Removed from v.336  
changed lines
  Added in v.1448

  ViewVC Help
Powered by ViewVC 1.1.20