/[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 369 by torben, Wed Sep 30 16:40:48 2009 UTC revision 401 by torben, Tue Oct 6 19:13:34 2009 UTC
# Line 18  import android.os.Bundle; Line 18  import android.os.Bundle;
18  import android.os.Handler;  import android.os.Handler;
19  import android.os.Message;  import android.os.Message;
20  import android.util.Log;  import android.util.Log;
21    import android.view.LayoutInflater;
22  import android.view.Menu;  import android.view.Menu;
23  import android.view.MenuItem;  import android.view.MenuItem;
24  import android.view.View;  import android.view.View;
25    import android.widget.EditText;
26  import android.widget.ListView;  import android.widget.ListView;
   
   
27  import dk.thoerup.traininfo.provider.ProviderFactory;  import dk.thoerup.traininfo.provider.ProviderFactory;
28  import dk.thoerup.traininfo.provider.StationProvider;  import dk.thoerup.traininfo.provider.StationProvider;
29  import dk.thoerup.traininfo.stationmap.GeoPair;  import dk.thoerup.traininfo.stationmap.GeoPair;
# Line 36  public class StationList extends ListAct Line 36  public class StationList extends ListAct
36          public static final int NOPROVIDER = 1003;          public static final int NOPROVIDER = 1003;
37          public static final int LOCATIONFIXTIMEOUT = 1004;          public static final int LOCATIONFIXTIMEOUT = 1004;
38                    
39          public static final int OPTIONS_MAP = 2001;          public static final int OPTIONS_RESCAN = 2001;
40          public static final int OPTIONS_ABOUT = 2002;          public static final int OPTIONS_NAMESEARCH = 2002;
41            public static final int OPTIONS_MAP = 2003;
42            public static final int OPTIONS_ABOUT = 2004;
43    
44                    
45          public static final int DLG_PROGRESS = 1001;          public static final int DLG_PROGRESS = 3001;
46            public static final int DLG_STATIONNAME = 3002;
47                    
48          /** Called when the activity is first created. */          /** Called when the activity is first created. */
49          String dialogMessage = "";          String dialogMessage = "";
50          ProgressDialog dialog;          ProgressDialog dialog;
51          LocationLookup locator = null;          LocationLookup locator = null;
52          LocatorTask locatorTask;          LocatorTask locatorTask;
53            StationsFetchedHandler stationsFetched = new StationsFetchedHandler();
54            
55            GeoPair location = new GeoPair();
56                    
57          boolean isRunning = false;          boolean isRunning = false;
58          List<StationBean> stations = new ArrayList<StationBean>();          List<StationBean> stations = new ArrayList<StationBean>();
59                    
60          StationProvider stationProvider = ProviderFactory.getStationProvider();          StationProvider stationProvider = ProviderFactory.getStationProvider();
61                    
62            StationListAdapter adapter = null;
63                    
64            static enum LookupMethod {
65                    ByLocation,
66                    ByName,
67                    MethodNone
68            }
69            
70            ///////////////////////////////////////////////////////////////////////////////////////////
71            //Activity call backs
72                    
         StationListAdapter adapter = null;  
73          @SuppressWarnings("unchecked")          @SuppressWarnings("unchecked")
74          @Override          @Override
75          public void onCreate(Bundle savedInstanceState) {          public void onCreate(Bundle savedInstanceState) {
# Line 71  public class StationList extends ListAct Line 86  public class StationList extends ListAct
86                  } else {                  } else {
87                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");                          stations = (ArrayList<StationBean>) savedInstanceState.getSerializable("stations");
88                          adapter.setStations(stations);                          adapter.setStations(stations);
89                            location = (GeoPair) savedInstanceState.getSerializable("location");
90                  }                  }
91          }          }
92            
93    
94      @Override      @Override
95      public void onSaveInstanceState(Bundle outState)      public void onSaveInstanceState(Bundle outState)
96      {      {
97          if (dialog != null && dialog.isShowing())          if (dialog != null && dialog.isShowing())
98                  dialog.dismiss();                  dialog.dismiss();
99          outState.putSerializable("stations", (ArrayList<StationBean>) stations);          outState.putSerializable("stations", (ArrayList<StationBean>) stations);
100            outState.putSerializable("location", location);
101      }      }
102                    
103                    
104    
105          @Override          @Override
106          public boolean onCreateOptionsMenu(Menu menu) {          public boolean onCreateOptionsMenu(Menu menu) {
107                    menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations");
108                    menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station");
109                  menu.add(0, OPTIONS_MAP, 0, "Show station map");                  menu.add(0, OPTIONS_MAP, 0, "Show station map");
110                  menu.add(0, OPTIONS_ABOUT, 0, "About");                  menu.add(0, OPTIONS_ABOUT, 0, "About");
                   
111                  return true;                  return true;
112          }          }
113    
114          @Override          @Override
115          public boolean onOptionsItemSelected(MenuItem item) {          public boolean onOptionsItemSelected(MenuItem item) {
116                  boolean retval;                  boolean retval = true;
117    
118                                    
119                  switch (item.getItemId()) {                  switch (item.getItemId()) {
120                    case OPTIONS_RESCAN:
121                            startLookup();
122                            break;
123                    case OPTIONS_NAMESEARCH:
124                            showDialog(DLG_STATIONNAME);
125                            break;
126                  case OPTIONS_MAP:                  case OPTIONS_MAP:
127                                                    
128                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
129                          intent.putExtra("userlocation", GeoPair.fromLocation(locator.getLocation()) );                          intent.putExtra("userlocation", location );
130                                                    
131                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
132                          for (StationBean st : stations ) {                          for (StationBean st : stations ) {
# Line 110  public class StationList extends ListAct Line 136  public class StationList extends ListAct
136                          intent.putExtra("stations", stationPoints);                          intent.putExtra("stations", stationPoints);
137                                                    
138                          startActivity(intent);                          startActivity(intent);
139                          retval = true;                          break;
140                    case OPTIONS_ABOUT:
141                            String ver = this.getResources().getString(R.string.app_version);
142                            
143                            Location loc = locator.getLocation();
144                            StringBuffer message = new StringBuffer();
145                            message.append("TrainInfo DK v").append(ver).append("\n");
146                            message.append("By Torben Nielsen\n");
147                            message.append("\n");
148                            message.append("Location info:\n");
149                            message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n");
150                            message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n");
151    
152                            MessageBox.showMessage(this, message.toString());
153                          break;                          break;
154                  default:                  default:
155                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 127  public class StationList extends ListAct Line 166  public class StationList extends ListAct
166                          dlg.setMessage("Wait for location fix");                          dlg.setMessage("Wait for location fix");
167                          dlg.setCancelable(false);                          dlg.setCancelable(false);
168                          return dlg;                                              return dlg;                    
169                    case DLG_STATIONNAME:
170                            LayoutInflater factory = LayoutInflater.from(this);
171                            final View rootView = factory.inflate(R.layout.textinput, null);
172                            
173                            
174                            AlertDialog.Builder builder = new AlertDialog.Builder(this);
175                            
176                            builder.setTitle("Station search");
177                            builder.setView(rootView);
178                            builder.setCancelable(true);
179                            builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {
180                                    public void onClick(DialogInterface dialog, int which) {
181                                            EditText et = (EditText) rootView.findViewById(R.id.EditText);
182                                            dialog.dismiss();
183                                            if (et.getText().toString().length() >= 2) {
184                                                    startNameSearch(et.getText().toString());
185                                            } else {
186                                                    MessageBox.showMessage(StationList.this, "Two characters minimum" );
187                                            }
188                                    }
189                            });
190                            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
191                                    public void onClick(DialogInterface dialog, int which) {
192                                            dialog.dismiss();
193                                    }
194                            });                    
195                            return builder.create();
196                            
197                  default:                  default:
198                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
199                  }                  }
# Line 134  public class StationList extends ListAct Line 201  public class StationList extends ListAct
201          }          }
202                    
203                    
   
204          @Override          @Override
205          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
206                  super.onPrepareDialog(id, dialog);                  super.onPrepareDialog(id, dialog);
# Line 148  public class StationList extends ListAct Line 214  public class StationList extends ListAct
214                          break;                          break;
215                  }                  }
216          }          }
217            
218            @Override
219            protected void onListItemClick(ListView l, View v, int position, long id) {
220                    super.onListItemClick(l, v, position, id);
221                                    
222                    StationBean station = stations.get(position);
223    
224                    double latitude = station.getLatitude();
225                    double longitude = station.getLongitude();
226    
227    
228                    
229                    Intent intent = new Intent(this, DepartureList.class);
230                    intent.putExtra("name", station.getName());
231                    intent.putExtra("distance", station.getDistance());
232                    intent.putExtra("latitude", latitude);
233                    intent.putExtra("longitude", longitude);
234                    intent.putExtra("stationid", station.getId());
235                    intent.putExtra("address", station.getAddress());
236                    startActivity(intent);
237            }
238    
239            /////////////////////////////////////////////////////////////
240            //
241    
242          public void startLookup() {          public void startLookup() {
243                  isRunning = true;                  isRunning = true;
244                    dialogMessage = "Wait for location fix";
245                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
246                                    
247                  locator.locateStations();                  locator.locateStations();
248                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
249          }          }
250            
251            void startNameSearch(String name) {
252                    dialogMessage = "Finding stations by name";
253                    showDialog(DLG_PROGRESS);
254    
255                    locatorTask = new LocatorTask();
256                    locatorTask.searchByName(name, locator.getLocation());
257                    locatorTask.execute();
258                    
259            }
260    
261    
262            
263            void startLocatorTask()
264            {
265                    dialogMessage = "Finding nearby stations";
266                    showDialog(DLG_PROGRESS);
267                    
268                    locatorTask = new LocatorTask();
269                    locatorTask.searchByLocation( locator.getLocation() );
270                    locatorTask.execute();  
271            }
272            
273    
274            String lookupAddress(double latitude, double longitude) {
275                    
276                    Geocoder coder = new Geocoder(this, new Locale("da"));
277                    StringBuilder sb = new StringBuilder();
278                    Log.i("lookupaddr", "" + latitude + "/" + longitude);
279                    try {
280                            List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);
281                            Address addr = addressList.get(0);
282                            
283                            
284                            int max = addr.getMaxAddressLineIndex();
285                            for (int i=0; i<max; i++) {
286                                    if (i>0)
287                                            sb.append(", ");
288                                    
289                                    sb.append(addr.getAddressLine(i));
290                            }
291                            
292                            
293                    } catch (Exception e) {
294                            Log.e("DepartureList", "geocoder failed", e);
295                    }
296                    
297                    return sb.toString();
298            }
299            
300            
301            ////////////////////////////////////////////////////////////////////////////
302            // Inner classes
303    
304          Handler stationsFetched = new Handler() {          class StationsFetchedHandler extends Handler {
305                  @Override                  @Override
306                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
307    
# Line 167  public class StationList extends ListAct Line 310  public class StationList extends ListAct
310                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
311                                                                    
312                                  startLocatorTask();                                  startLocatorTask();
313                                    location = GeoPair.fromLocation( locator.getLocation() );
314                                                                    
315                                  break;                                  break;
316    
# Line 206  public class StationList extends ListAct Line 350  public class StationList extends ListAct
350                          isRunning = false;                          isRunning = false;
351                  }                  }
352          };          };
           
         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();  
   
353    
354            
355            class LocatorTask extends AsyncTask<Void,Void,Void> {
356                                    
357                  Intent intent = new Intent(this, DepartureList.class);                  LookupMethod method = LookupMethod.MethodNone;
358                  intent.putExtra("name", station.getName());                  boolean success;
359                  intent.putExtra("distance", station.getDistance());                  String name;
360                  intent.putExtra("latitude", latitude);                  Location loc;
                 intent.putExtra("longitude", longitude);  
                 intent.putExtra("stationid", station.getId());  
                 intent.putExtra("address", station.getAddress());  
                 startActivity(intent);  
         }  
   
         String lookupAddress(double latitude, double longitude) {  
361                                    
362                  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);  
                           
363                                                    
364                          int max = addr.getMaxAddressLineIndex();                          method = LookupMethod.ByName;
365                          for (int i=0; i<max; i++) {                          loc = l;
366                                  if (i>0)                          name = n;
367                                          sb.append(", ");                  }
368                                                    
369                                  sb.append(addr.getAddressLine(i));                  public void searchByLocation(Location l) {
370                          }                          method = LookupMethod.ByLocation;
371                                                    loc = l;
                           
                 } catch (Exception e) {  
                         Log.e("DepartureList", "geocoder failed", e);  
372                  }                  }
373                                    
                 return sb.toString();  
         }  
           
         class LocatorTask extends AsyncTask<Void,Void,Void> {  
                 boolean success;  
374                  @Override                  @Override
375                  protected void onPreExecute() {                  protected void onPreExecute() {
376    
377                            if (method.equals(LookupMethod.MethodNone))
378                                    throw new RuntimeException("Method not set");
379                          super.onPreExecute();                          super.onPreExecute();
380                  }                  }
381                                    
382                  @Override                  @Override
383                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
384                          Location loc = locator.getLocation();                  
385                          success = stationProvider.lookupStations(loc);                          if (method.equals(LookupMethod.ByLocation))
386                                    success = stationProvider.lookupStations(loc);
387                                                    
388                            if (method.equals(LookupMethod.ByName))
389                                    success = stationProvider.lookupStations(name);
390                                                    
391                            Location dummy = new Location("gps");
392                          List<StationBean> stations = stationProvider.getStations();                          List<StationBean> stations = stationProvider.getStations();
393                          for (StationBean station : stations) {                          
394                            for (StationBean station : stations) {
395                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());
396                                  station.setAddress(addr);                                  station.setAddress(addr);
397                          }                                  
398                                    if (method.equals(LookupMethod.ByName) ) {
399                                            dummy.setLatitude(station.getLatitude());
400                                            dummy.setLongitude(station.getLongitude());
401                                            station.setDistance( (int)loc.distanceTo(dummy) );
402                                    }
403                            }                                              
404                                                    
405                          return null;                          return null;
406                  }                  }

Legend:
Removed from v.369  
changed lines
  Added in v.401

  ViewVC Help
Powered by ViewVC 1.1.20