/[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 354 by torben, Tue Sep 29 18:24:23 2009 UTC revision 433 by torben, Sat Oct 10 11:30:08 2009 UTC
# Line 3  package dk.thoerup.traininfo; Line 3  package dk.thoerup.traininfo;
3  import java.util.ArrayList;  import java.util.ArrayList;
4  import java.util.List;  import java.util.List;
5  import java.util.Locale;  import java.util.Locale;
6    import java.util.prefs.Preferences;
7    
8  import android.app.AlertDialog;  import android.app.AlertDialog;
9  import android.app.Dialog;  import android.app.Dialog;
# Line 10  import android.app.ListActivity; Line 11  import android.app.ListActivity;
11  import android.app.ProgressDialog;  import android.app.ProgressDialog;
12  import android.content.DialogInterface;  import android.content.DialogInterface;
13  import android.content.Intent;  import android.content.Intent;
14    import android.content.SharedPreferences;
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;
25    import android.view.Menu;
26    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;
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;
37    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 29  public class StationList extends ListAct Line 43  public class StationList extends ListAct
43          public static final int GOTSTATIONLIST = 1002;          public static final int GOTSTATIONLIST = 1002;
44          public static final int NOPROVIDER = 1003;          public static final int NOPROVIDER = 1003;
45          public static final int LOCATIONFIXTIMEOUT = 1004;          public static final int LOCATIONFIXTIMEOUT = 1004;
46            
47            public static final int OPTIONS_RESCAN = 2001;
48            public static final int OPTIONS_NAMESEARCH = 2002;
49            public static final int OPTIONS_MAP = 2003;
50            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 = 1001;          public static final int DLG_PROGRESS = 3001;
56            public static final int DLG_STATIONNAME = 3002;
57                    
58          /** Called when the activity is first created. */          /** Called when the activity is first created. */
59          String dialogMessage = "";          String dialogMessage = "";
60          ProgressDialog dialog;          ProgressDialog dialog;
61          LocationLookup locator = null;          LocationLookup locator = null;
62          LocatorTask locatorTask;          LocatorTask locatorTask;
63            StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
64            
65            GeoPair location = new GeoPair();
66                    
67          boolean isRunning = false;          boolean isRunning = false;
68          List<StationBean> stations = new ArrayList<StationBean>();          List<StationBean> stations = new ArrayList<StationBean>();
69                    
70          StationProvider stationProvider = ProviderFactory.getStationProvider();          StationProvider stationProvider = ProviderFactory.getStationProvider();
71                    
72            StationListAdapter adapter = null;
73                    
74            FavoritesMenu contextMenu = new FavoritesMenu();
75            IntSet favorites = new IntSet();
76            
77            static enum LookupMethod {
78                    ByLocation,
79                    ByName,
80                    ByList,
81                    MethodNone
82            }
83            
84            SharedPreferences prefs;
85            
86            ///////////////////////////////////////////////////////////////////////////////////////////
87            //Activity call backs
88                    
         StationListAdapter adapter = null;  
89          @SuppressWarnings("unchecked")          @SuppressWarnings("unchecked")
90          @Override          @Override
91          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
# Line 56  public class StationList extends ListAct Line 96  public class StationList extends ListAct
96                  adapter = new StationListAdapter(this);                  adapter = new StationListAdapter(this);
97                  setListAdapter(adapter);                  setListAdapter(adapter);
98                                    
99                    ListView lv = getListView();
100                    lv.setOnCreateContextMenuListener(contextMenu);
101                    
102                  locator = new LocationLookup(this, stationsFetched);                  locator = new LocationLookup(this, stationsFetched);
103                    
104    
105                    prefs = getSharedPreferences("TrainStation", 0);
106                    String favoriteString = prefs.getString("favorites", "");
107                    if (! favoriteString.equals("") ) {
108                            favorites.fromString(favoriteString);
109                    }
110                    
111                  if (savedInstanceState == null) {                  if (savedInstanceState == null) {
112                          startLookup();                          startLookup();
113                  } else {                  } else {
114                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
115                          adapter.setStations(stations);                          adapter.setStations(stations);
116                            location = (GeoPair) savedInstanceState.getSerializable("location");
117                  }                  }
118          }          }
119            
120    
121      @Override      @Override
122      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
123      {      {
124          if (dialog != null && dialog.isShowing())          if (dialog != null && dialog.isShowing())
125                  dialog.dismiss();                  dialog.dismiss();
126          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (ArrayList<StationBean>) stations);
127            outState.putSerializable("location", location);
128      }      }
129                    
130                    
131    
132          @Override          @Override
133            public boolean onCreateOptionsMenu(Menu menu) {
134                    MenuItem item;
135                    
136                    item = menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations");
137                    item.setIcon(android.R.drawable.ic_menu_mylocation);
138                    
139                    item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station");
140                    item.setIcon(android.R.drawable.ic_menu_search);
141                    
142                    item = menu.add(0, OPTIONS_FAVORITES, 0, "Favorites");
143                    item.setIcon(android.R.drawable.ic_menu_agenda);
144                    
145                    item = menu.add(0, OPTIONS_MAP, 0, "Show station map");
146                    item.setIcon(android.R.drawable.ic_menu_mapmode);
147                    
148                    item = menu.add(0, OPTIONS_ABOUT, 0, "About");
149                    item.setIcon(android.R.drawable.ic_menu_info_details);
150                    return true;
151            }
152    
153            @Override
154            public boolean onOptionsItemSelected(MenuItem item) {
155                    boolean retval = true;
156    
157                    
158                    switch (item.getItemId()) {
159                    case OPTIONS_RESCAN:
160                            startLookup();
161                            break;
162                    case OPTIONS_NAMESEARCH:
163                            showDialog(DLG_STATIONNAME);
164                            break;
165                    case OPTIONS_FAVORITES:
166                            startFavoriteLookup();
167                            break;
168                    case OPTIONS_MAP:
169                            
170                            Intent intent = new Intent(this,StationMapView.class);
171                            intent.putExtra("userlocation", location );
172                            
173                            ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
174                            for (StationBean st : stations ) {
175                                    stationPoints.add( new GeoPair(st.getLatitude(), st.getLongitude(), st.getName()) );
176                            }
177                            
178                            intent.putExtra("stations", stationPoints);
179                            
180                            startActivity(intent);
181                            break;
182                    case OPTIONS_ABOUT:
183                            String ver = this.getResources().getString(R.string.app_version);
184                            
185                            Location loc = locator.getLocation();
186                            StringBuffer message = new StringBuffer();
187                            message.append("TrainInfo DK v").append(ver).append("\n");
188                            message.append("By Torben Nielsen\n");
189                            message.append("\n");
190                            message.append("Location info:\n");
191                            message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n");
192                            message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n");
193    
194                            MessageBox.showMessage(this, message.toString());
195                            break;
196                    default:
197                            retval = super.onOptionsItemSelected(item);
198                    }
199                    
200                    return retval;
201            }
202            
203            
204    
205            @Override
206            public boolean onContextItemSelected(MenuItem item) {
207                    contextMenu.onContextItemSelected(item);
208                    return true;
209    
210    
211            }
212    
213    
214    
215    
216            @Override
217          protected Dialog onCreateDialog(int id) {          protected Dialog onCreateDialog(int id) {
218                  switch (id) {                  switch (id) {
219                  case DLG_PROGRESS:                  case DLG_PROGRESS:
# Line 83  public class StationList extends ListAct Line 221  public class StationList extends ListAct
221                          dlg.setMessage("Wait for location fix");                          dlg.setMessage("Wait for location fix");
222                          dlg.setCancelable(false);                          dlg.setCancelable(false);
223                          return dlg;                                              return dlg;                    
224                    case DLG_STATIONNAME:
225                            LayoutInflater factory = LayoutInflater.from(this);
226                            final View rootView = factory.inflate(R.layout.textinput, null);
227                            
228                            
229                            AlertDialog.Builder builder = new AlertDialog.Builder(this);
230                            
231                            builder.setTitle("Station search");
232                            builder.setView(rootView);
233                            builder.setCancelable(true);
234                            builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {
235                                    public void onClick(DialogInterface dialog, int which) {
236                                            EditText et = (EditText) rootView.findViewById(R.id.EditText);
237                                            dialog.dismiss();
238                                            if (et.getText().toString().length() >= 2) {
239                                                    startNameSearch(et.getText().toString());
240                                            } else {
241                                                    MessageBox.showMessage(StationList.this, "Two characters minimum" );
242                                            }
243                                    }
244                            });
245                            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
246                                    public void onClick(DialogInterface dialog, int which) {
247                                            dialog.dismiss();
248                                    }
249                            });                    
250                            return builder.create();
251                            
252                  default:                  default:
253                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
254                  }                  }
# Line 90  public class StationList extends ListAct Line 256  public class StationList extends ListAct
256          }          }
257                    
258                    
   
259          @Override          @Override
260          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
261                  super.onPrepareDialog(id, dialog);                  super.onPrepareDialog(id, dialog);
# Line 104  public class StationList extends ListAct Line 269  public class StationList extends ListAct
269                          break;                          break;
270                  }                  }
271          }          }
272            
273            @Override
274            protected void onListItemClick(ListView l, View v, int position, long id) {
275                    super.onListItemClick(l, v, position, id);
276                                    
277                    StationBean station = stations.get(position);
278    
279                    double latitude = station.getLatitude();
280                    double longitude = station.getLongitude();
281    
282    
283                    
284                    Intent intent = new Intent(this, DepartureList.class);
285                    intent.putExtra("name", station.getName());
286                    intent.putExtra("distance", station.getDistance());
287                    intent.putExtra("latitude", latitude);
288                    intent.putExtra("longitude", longitude);
289                    intent.putExtra("stationid", station.getId());
290                    intent.putExtra("address", station.getAddress());
291                    startActivity(intent);
292            }
293    
294            /////////////////////////////////////////////////////////////
295            //
296    
297          public void startLookup() {          public void startLookup() {
298                  isRunning = true;                  isRunning = true;
299                    dialogMessage = "Wait for location fix";
300                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
301                                    
302                  locator.locateStations();                  locator.locateStations();
303                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
304          }          }
305            
306            void startNameSearch(String name) {
307                    dialogMessage = "Finding stations by name";
308                    showDialog(DLG_PROGRESS);
309    
310                    locatorTask = new LocatorTask();
311                    locatorTask.searchByName(name, locator.getLocation());
312                    locatorTask.execute();
313                    
314            }
315            
316            public void startFavoriteLookup() {
317                    
318                    if (favorites.toString().length() > 0) {
319                            dialogMessage = "Loading favorites";
320                            showDialog(DLG_PROGRESS);
321    
322                            locatorTask = new LocatorTask();
323                            locatorTask.searchByIds(favorites.toString(), locator.getLocation());
324                            locatorTask.execute();
325                    } else {
326                            MessageBox.showMessage(this, "Favorite list is empty");
327                    }
328            }
329    
330    
331            
332            void startLocatorTask()
333            {
334                    dialogMessage = "Finding nearby stations";
335                    showDialog(DLG_PROGRESS);
336                    
337                    locatorTask = new LocatorTask();
338                    locatorTask.searchByLocation( locator.getLocation() );
339                    locatorTask.execute();  
340            }
341            
342    
343            String lookupAddress(double latitude, double longitude) {
344                    
345                    Geocoder coder = new Geocoder(this, new Locale("da"));
346                    StringBuilder sb = new StringBuilder();
347                    Log.i("lookupaddr", "" + latitude + "/" + longitude);
348                    try {
349                            List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);
350                            Address addr = addressList.get(0);
351                            
352                            
353                            int max = addr.getMaxAddressLineIndex();
354                            for (int i=0; i<max; i++) {
355                                    if (i>0)
356                                            sb.append(", ");
357                                    
358                                    sb.append(addr.getAddressLine(i));
359                            }
360                            
361                            
362                    } catch (Exception e) {
363                            Log.e("DepartureList", "geocoder failed", e);
364                    }
365                    
366                    return sb.toString();
367            }
368            
369            
370            ////////////////////////////////////////////////////////////////////////////
371            // Inner classes
372    
373          Handler stationsFetched = new Handler() {          class StationsFetchedHandler extends Handler {
374                  @Override                  @Override
375                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
376    
# Line 123  public class StationList extends ListAct Line 379  public class StationList extends ListAct
379                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
380                                                                    
381                                  startLocatorTask();                                  startLocatorTask();
382                                    location = GeoPair.fromLocation( locator.getLocation() );
383                                                                    
384                                  break;                                  break;
385    
# Line 139  public class StationList extends ListAct Line 396  public class StationList extends ListAct
396                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialog(DLG_PROGRESS);
397                                                                                                    
398                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
399                                                  builder.setMessage("GPS fix timed out");                                                  builder.setMessage("Location fix timed out");
400                                                  builder.setCancelable(true);                                                  builder.setCancelable(true);
401                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
402                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
# Line 162  public class StationList extends ListAct Line 419  public class StationList extends ListAct
419                          isRunning = false;                          isRunning = false;
420                  }                  }
421          };          };
           
         void startLocatorTask()  
         {  
                 dialogMessage = "Finding nearby stations";  
                 showDialog(DLG_PROGRESS);  
                   
                 locatorTask = new LocatorTask();  
                 locatorTask.execute();    
         }  
           
         @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();  
   
422    
423            
424            class LocatorTask extends AsyncTask<Void,Void,Void> {
425                                    
426                  Intent intent = new Intent(this, DepartureList.class);                  LookupMethod method = LookupMethod.MethodNone;
427                  intent.putExtra("name", station.getName());                  boolean success;
428                  intent.putExtra("distance", station.getDistance());                  String name;
429                  intent.putExtra("latitude", latitude);                  Location loc;
430                  intent.putExtra("longitude", longitude);                  String ids;
                 intent.putExtra("stationid", station.getId());  
                 intent.putExtra("address", station.getAddress());  
                 startActivity(intent);  
         }  
   
         String lookupAddress(double latitude, double longitude) {  
431                                    
432                  Geocoder coder = new Geocoder(this, new Locale("da"));                  public void searchByName(String n, Location l) {
                 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));  
                         }  
433                                                    
434                            method = LookupMethod.ByName;
435                            loc = l;
436                            name = n;
437                    }
438                    
439                    public void searchByLocation(Location l) {
440                            method = LookupMethod.ByLocation;
441                            loc = l;
442                    }
443                    
444                    public void searchByIds(String id, Location l) {
445                                                    
446                  } catch (Exception e) {                          method = LookupMethod.ByList;
447                          Log.e("DepartureList", "geocoder failed", e);                          loc = l;
448                            ids = id;
449                  }                  }
450                                    
                 return sb.toString();  
         }  
           
         class LocatorTask extends AsyncTask<Void,Void,Void> {  
                 boolean success;  
451                  @Override                  @Override
452                  protected void onPreExecute() {                  protected void onPreExecute() {
453    
454                            if (method.equals(LookupMethod.MethodNone))
455                                    throw new RuntimeException("Method not set");
456                          super.onPreExecute();                          super.onPreExecute();
457                  }                  }
458                                    
459                  @Override                  @Override
460                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
461                          Location loc = locator.getLocation();                  
462                          success = stationProvider.lookupStations(loc);                          if (method.equals(LookupMethod.ByLocation))
463                                    success = stationProvider.lookupStations(loc);
464                            
465                            if (method.equals(LookupMethod.ByName))
466                                    success = stationProvider.lookupStationsByName(name);
467                                                    
468                            if (method.equals(LookupMethod.ByList))
469                                    success = stationProvider.lookupStationsByIds(ids);
470                                                    
471                            Location dummy = new Location("gps");
472                          List<StationBean> stations = stationProvider.getStations();                          List<StationBean> stations = stationProvider.getStations();
473                          for (StationBean station : stations) {                          
474                            for (StationBean station : stations) {
475                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());
476                                  station.setAddress(addr);                                  station.setAddress(addr);
477                          }                                  
478                                    if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) {
479                                            dummy.setLatitude(station.getLatitude());
480                                            dummy.setLongitude(station.getLongitude());
481                                            station.setDistance( (int)loc.distanceTo(dummy) );
482                                    }
483                            }                                              
484                                                    
485                          return null;                          return null;
486                  }                  }
# Line 278  public class StationList extends ListAct Line 521  public class StationList extends ListAct
521                          }                          }
522                  }                  }
523          }          }
524            
525            
526            class FavoritesMenu implements OnCreateContextMenuListener {
527                    private final static int FAVORITES_ADD = 9001;
528                    private final static int FAVORITES_REMOVE = 9002;
529                    
530                    private int selectedPosition;
531                    
532                    
533                    @Override
534                    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
535                                                    
536                            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
537                            selectedPosition = info.position;
538                            int stationID = stations.get(selectedPosition).getId();
539    
540                            if (!favorites.hasInt(stationID)) {
541                                    menu.add(0, FAVORITES_ADD, 0, "Add to favorites");
542                            } else {
543                                    menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites");
544                            }
545                            
546                    }
547                    
548                    public void onContextItemSelected(MenuItem item) {
549                            StationBean sb = stations.get(selectedPosition);
550                            
551                            int stationID = sb.getId();
552                            if (item.getItemId() == FAVORITES_ADD) {
553                                    favorites.add(stationID);
554                                    Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show();
555                            } else {
556                                    favorites.remove(stationID);
557                                    Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show();
558                            }
559                            
560                            prefs.edit().putString("favorites", favorites.toString());
561                    }
562            }
563  }  }

Legend:
Removed from v.354  
changed lines
  Added in v.433

  ViewVC Help
Powered by ViewVC 1.1.20