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

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

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

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

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

  ViewVC Help
Powered by ViewVC 1.1.20