/[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 382 by torben, Fri Oct 2 10:49:00 2009 UTC revision 473 by torben, Tue Oct 27 13:39:28 2009 UTC
# Line 10  import android.app.ListActivity; Line 10  import android.app.ListActivity;
10  import android.app.ProgressDialog;  import android.app.ProgressDialog;
11  import android.content.DialogInterface;  import android.content.DialogInterface;
12  import android.content.Intent;  import android.content.Intent;
13    import android.content.SharedPreferences;
14    import android.content.SharedPreferences.Editor;
15  import android.location.Address;  import android.location.Address;
16  import android.location.Geocoder;  import android.location.Geocoder;
17  import android.location.Location;  import android.location.Location;
# Line 18  import android.os.Bundle; Line 20  import android.os.Bundle;
20  import android.os.Handler;  import android.os.Handler;
21  import android.os.Message;  import android.os.Message;
22  import android.util.Log;  import android.util.Log;
23    import android.view.ContextMenu;
24  import android.view.LayoutInflater;  import android.view.LayoutInflater;
25  import android.view.Menu;  import android.view.Menu;
26  import android.view.MenuItem;  import android.view.MenuItem;
27  import android.view.View;  import android.view.View;
28    import android.view.ContextMenu.ContextMenuInfo;
29    import android.view.View.OnCreateContextMenuListener;
30    import android.widget.AdapterView;
31  import android.widget.EditText;  import android.widget.EditText;
32  import android.widget.ListView;  import android.widget.ListView;
33    import android.widget.Toast;
34  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
35  import dk.thoerup.traininfo.provider.StationProvider;  import dk.thoerup.traininfo.provider.StationProvider;
36  import dk.thoerup.traininfo.stationmap.GeoPair;  import dk.thoerup.traininfo.stationmap.GeoPair;
37  import dk.thoerup.traininfo.stationmap.StationMapView;  import dk.thoerup.traininfo.stationmap.StationMapView;
38    import dk.thoerup.traininfo.util.IntSet;
39  import dk.thoerup.traininfo.util.MessageBox;  import dk.thoerup.traininfo.util.MessageBox;
40    
41  public class StationList extends ListActivity  {  public class StationList extends ListActivity  {
# Line 40  public class StationList extends ListAct Line 48  public class StationList extends ListAct
48          public static final int OPTIONS_NAMESEARCH = 2002;          public static final int OPTIONS_NAMESEARCH = 2002;
49          public static final int OPTIONS_MAP = 2003;          public static final int OPTIONS_MAP = 2003;
50          public static final int OPTIONS_ABOUT = 2004;          public static final int OPTIONS_ABOUT = 2004;
51            public static final int OPTIONS_FAVORITES = 2005;
52            
53    
54                    
55          public static final int DLG_PROGRESS = 3001;          public static final int DLG_PROGRESS = 3001;
56          public static final int DLG_STATIONNAME = 3002;          public static final int DLG_STATIONNAME = 3002;
57                    
58          /** Called when the activity is first created. */          static enum LookupMethod {
59                    ByLocation,
60                    ByName,
61                    ByList,
62                    MethodNone
63            }
64            
65            
66          String dialogMessage = "";          String dialogMessage = "";
67          ProgressDialog dialog;          ProgressDialog dialog;
68          LocationLookup locator = null;          LocationLookup locator = null;
69          LocatorTask locatorTask;          FindStationsTask findStationsTask;
70          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();          StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
71                    
72          GeoPair location = new GeoPair();          GeoPair location = new GeoPair();
# Line 61  public class StationList extends ListAct Line 78  public class StationList extends ListAct
78                                    
79          StationListAdapter adapter = null;          StationListAdapter adapter = null;
80                    
81          static enum LookupMethod {          FavoritesMenu contextMenu = new FavoritesMenu();
82                  ByLocation,          IntSet favorites = new IntSet();
83                  ByName,          
84                  MethodNone          boolean showingFavorites = false;
85          }          
86            SharedPreferences prefs;
87                    
88          ///////////////////////////////////////////////////////////////////////////////////////////          ///////////////////////////////////////////////////////////////////////////////////////////
89          //Activity call backs          //Activity call backs
# Line 80  public class StationList extends ListAct Line 98  public class StationList extends ListAct
98                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this);
99                  setListAdapter(adapter);                  setListAdapter(adapter);
100                                    
101                    ListView lv = getListView();
102                    lv.setOnCreateContextMenuListener(contextMenu);
103                    
104                  locator = new LocationLookup(this, stationsFetched);                  locator = new LocationLookup(this, stationsFetched);
105                    
106    
107                    prefs = getSharedPreferences("TrainStation", 0);
108                    String favoriteString = prefs.getString("favorites", "");
109                    if (! favoriteString.equals("") ) {
110                            favorites.fromString(favoriteString);
111                    }
112                    
113                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
114                          startLookup();                          startLookup();
115                  } else {                  } else {
# Line 104  public class StationList extends ListAct Line 133  public class StationList extends ListAct
133    
134          @Override          @Override
135          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
136                  menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations");                  MenuItem item;
137                  menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station");                  
138                  menu.add(0, OPTIONS_MAP, 0, "Show station map");                  item = menu.add(0, OPTIONS_RESCAN, 0, "Nearest stations");
139                  menu.add(0, OPTIONS_ABOUT, 0, "About");                  item.setIcon(android.R.drawable.ic_menu_mylocation);
140                    
141                    item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station");
142                    item.setIcon(android.R.drawable.ic_menu_search);
143                    
144                    item = menu.add(0, OPTIONS_FAVORITES, 0, "Favorites");
145                    item.setIcon(android.R.drawable.ic_menu_agenda);
146                    
147                    item = menu.add(0, OPTIONS_MAP, 0, "Station map");
148                    item.setIcon(android.R.drawable.ic_menu_mapmode);
149                    
150                    item = menu.add(0, OPTIONS_ABOUT, 0, "About");
151                    item.setIcon(android.R.drawable.ic_menu_info_details);
152                  return true;                  return true;
153          }          }
154    
# Line 123  public class StationList extends ListAct Line 164  public class StationList extends ListAct
164                  case OPTIONS_NAMESEARCH:                  case OPTIONS_NAMESEARCH:
165                          showDialog(DLG_STATIONNAME);                          showDialog(DLG_STATIONNAME);
166                          break;                          break;
167                    case OPTIONS_FAVORITES:
168                            startFavoriteLookup();
169                            break;
170                  case OPTIONS_MAP:                  case OPTIONS_MAP:
171                                                    
172                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
# Line 140  public class StationList extends ListAct Line 184  public class StationList extends ListAct
184                  case OPTIONS_ABOUT:                  case OPTIONS_ABOUT:
185                          String ver = this.getResources().getString(R.string.app_version);                          String ver = this.getResources().getString(R.string.app_version);
186                                                    
187                            Location loc = locator.getLocation();
188                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
189                          message.append("TrainInfo DK v").append(ver).append("\n");                          message.append("TrainInfo DK v").append(ver).append("\n");
190                          message.append("By Torben Nielsen\n");                          message.append("By Torben Nielsen\n");
191                            message.append("\n");
192                            message.append("Location info:\n");
193                            message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n");
194                            message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n");
195    
196                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString());
197                          break;                          break;
# Line 152  public class StationList extends ListAct Line 201  public class StationList extends ListAct
201                                    
202                  return retval;                  return retval;
203          }          }
204            
205            
206    
207            @Override
208            public boolean onContextItemSelected(MenuItem item) {
209                    contextMenu.onContextItemSelected(item);
210                    return true;
211    
212    
213            }
214    
215    
216    
217    
218          @Override          @Override
219          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
# Line 247  public class StationList extends ListAct Line 309  public class StationList extends ListAct
309                  dialogMessage = "Finding stations by name";                  dialogMessage = "Finding stations by name";
310                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
311    
312                  locatorTask = new LocatorTask();                  findStationsTask = new FindStationsTask();
313                  locatorTask.searchByName(name, locator.getLocation());                  findStationsTask.searchByName(name, locator.getLocation());
314                  locatorTask.execute();                  findStationsTask.execute();
315                                    
316          }          }
317            
318            public void startFavoriteLookup() {
319                    
320                    if (favorites.size() > 0) {
321                            dialogMessage = "Loading favorites";
322                            showDialog(DLG_PROGRESS);
323    
324                            findStationsTask = new FindStationsTask();
325                            findStationsTask.searchByIds(favorites.toString(), locator.getLocation());
326                            findStationsTask.execute();
327                    } else {
328                            MessageBox.showMessage(this, "Favorite list is empty");
329                    }
330            }
331    
332    
333                    
# Line 260  public class StationList extends ListAct Line 336  public class StationList extends ListAct
336                  dialogMessage = "Finding nearby stations";                  dialogMessage = "Finding nearby stations";
337                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
338                                    
339                  locatorTask = new LocatorTask();                  findStationsTask = new FindStationsTask();
340                  locatorTask.searchByLocation( locator.getLocation() );                  findStationsTask.searchByLocation( locator.getLocation() );
341                  locatorTask.execute();                    findStationsTask.execute();    
342          }          }
343                    
344    
# Line 322  public class StationList extends ListAct Line 398  public class StationList extends ListAct
398                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialog(DLG_PROGRESS);
399                                                                                                    
400                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
401                                                  builder.setMessage("GPS fix timed out");                                                  builder.setMessage("Location fix timed out");
402                                                  builder.setCancelable(true);                                                  builder.setCancelable(true);
403                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
404                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
# Line 347  public class StationList extends ListAct Line 423  public class StationList extends ListAct
423          };          };
424    
425                    
426          class LocatorTask extends AsyncTask<Void,Void,Void> {          class FindStationsTask extends AsyncTask<Void,Void,Void> {
427                                    
428                  LookupMethod method = LookupMethod.MethodNone;                  LookupMethod method = LookupMethod.MethodNone;
429                  boolean success;                  boolean success;
430                  String name;                  String name;
431                  Location loc;                  Location loc;
432                    String ids;
433                                    
434                  public void searchByName(String n, Location l) {                  public void searchByName(String n, Location l) {
435                                                    
# Line 366  public class StationList extends ListAct Line 443  public class StationList extends ListAct
443                          loc = l;                          loc = l;
444                  }                  }
445                                    
446                    public void searchByIds(String id, Location l) {
447                            
448                            method = LookupMethod.ByList;
449                            loc = l;
450                            ids = id;
451                    }
452                    
453                  @Override                  @Override
454                  protected void onPreExecute() {                  protected void onPreExecute() {
455    
# Line 376  public class StationList extends ListAct Line 460  public class StationList extends ListAct
460                                    
461                  @Override                  @Override
462                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
463                    
464                          if (method.equals(LookupMethod.ByLocation))                          switch (method) {
465                            case ByLocation:
466                                  success = stationProvider.lookupStations(loc);                                  success = stationProvider.lookupStations(loc);
467                                    break;
468                            case ByName:
469                                    success = stationProvider.lookupStationsByName(name);
470                                    break;
471                            case ByList:
472                                    success = stationProvider.lookupStationsByIds(ids);
473                                    break;
474                            default:
475                                    success = false; // not possible        
476                            }
477                                                    
                         if (method.equals(LookupMethod.ByName))  
                                 success = stationProvider.lookupStations(name);  
478                                                    
479                          Location dummy = new Location("gps");                          Location dummy = new Location("gps");
480                          List<StationBean> stations = stationProvider.getStations();                          List<StationBean> stations = stationProvider.getStations();
# Line 390  public class StationList extends ListAct Line 483  public class StationList extends ListAct
483                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());
484                                  station.setAddress(addr);                                  station.setAddress(addr);
485                                                                    
486                                  if (method.equals(LookupMethod.ByName) ) {                                  if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) {
487                                          dummy.setLatitude(station.getLatitude());                                          dummy.setLatitude(station.getLatitude());
488                                          dummy.setLongitude(station.getLongitude());                                          dummy.setLongitude(station.getLongitude());
489                                          station.setDistance( (int)loc.distanceTo(dummy) );                                          station.setDistance( (int)loc.distanceTo(dummy) );
# Line 405  public class StationList extends ListAct Line 498  public class StationList extends ListAct
498                          super.onPostExecute(result);                          super.onPostExecute(result);
499                          dialog.dismiss();                          dialog.dismiss();
500                                                    
501                            //set title
502                            String title;
503                            switch (method) {
504                            case ByLocation:
505                                    title = "Traininfo DK - Nearby stations";
506                                    break;
507                            case ByName:
508                                    title = "Traininfo DK - Search";
509                                    break;
510                            case ByList:
511                                    title = "Traininfo DK - Favorites";
512                                    break;
513                            default:
514                                    title = "";//not possible                                      
515                            }                              
516                            
517                            StationList.this.setTitle(title);
518                            //set title end
519                            
520                          if (success) {                                                    if (success) {                          
521                                  if (stationProvider.getStations().size() == 0)                                  if (stationProvider.getStations().size() == 0)
522                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!                                          MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?!
523                                  stations = stationProvider.getStations();                                  stations = stationProvider.getStations();
524                                  adapter.setStations( stations );                                                                  adapter.setStations( stations );
525                                    
526                                    showingFavorites = method.equals(LookupMethod.ByList);                          
527                                                                    
528                          } else { //communication or parse errors                          } else { //communication or parse errors
529                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
# Line 436  public class StationList extends ListAct Line 550  public class StationList extends ListAct
550                          }                          }
551                  }                  }
552          }          }
553            
554            
555            class FavoritesMenu implements OnCreateContextMenuListener {
556                    private final static int FAVORITES_ADD = 9001;
557                    private final static int FAVORITES_REMOVE = 9002;
558                    
559                    private int selectedPosition;
560                    
561                    
562                    @Override
563                    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
564                                                    
565                            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
566                            selectedPosition = info.position;
567                            int stationID = stations.get(selectedPosition).getId();
568    
569                            if (!favorites.contains(stationID)) {
570                                    menu.add(0, FAVORITES_ADD, 0, "Add to favorites");
571                            } else {
572                                    menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites");
573                            }
574                            
575                    }
576                    
577                    public void onContextItemSelected(MenuItem item) {
578                            StationBean sb = stations.get(selectedPosition);
579                            
580                            int stationID = sb.getId();
581                            if (item.getItemId() == FAVORITES_ADD) {
582                                    favorites.add(stationID);
583                                    Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show();
584                            } else {
585                                    
586                                    favorites.remove(stationID);
587                                    Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show();
588                                    
589                                    if (showingFavorites) {
590                                            stations.remove(selectedPosition);
591                                            adapter.notifyDataSetChanged();
592                                    }
593                            }
594                            Editor ed = prefs.edit();
595                            ed.putString("favorites", favorites.toString());
596                            ed.commit();
597                    }
598            }
599  }  }

Legend:
Removed from v.382  
changed lines
  Added in v.473

  ViewVC Help
Powered by ViewVC 1.1.20