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

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

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

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

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

  ViewVC Help
Powered by ViewVC 1.1.20