/[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 380 by torben, Wed Sep 30 20:38:47 2009 UTC revision 381 by torben, Fri Oct 2 10:39:09 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();          GeoPair location = new GeoPair();
56                    
# Line 54  public class StationList extends ListAct Line 58  public class StationList extends ListAct
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 91  public class StationList extends ListAct Line 104  public class StationList extends ListAct
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);
# Line 115  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);
                         retval = true;  
139                          break;                          break;
140                  case OPTIONS_ABOUT:                  case OPTIONS_ABOUT:
141                          String ver = this.getResources().getString(R.string.app_version);                          String ver = this.getResources().getString(R.string.app_version);
# Line 125  public class StationList extends ListAct Line 145  public class StationList extends ListAct
145                          message.append("By Torben Nielsen\n");                          message.append("By Torben Nielsen\n");
146    
147                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString());
                         retval = true;  
148                          break;                          break;
149                  default:                  default:
150                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 142  public class StationList extends ListAct Line 161  public class StationList extends ListAct
161                          dlg.setMessage("Wait for location fix");                          dlg.setMessage("Wait for location fix");
162                          dlg.setCancelable(false);                          dlg.setCancelable(false);
163                          return dlg;                                              return dlg;                    
164                    case DLG_STATIONNAME:
165                            LayoutInflater factory = LayoutInflater.from(this);
166                            final View rootView = factory.inflate(R.layout.textinput, null);
167                            
168                            
169                            AlertDialog.Builder builder = new AlertDialog.Builder(this);
170                            
171                            builder.setTitle("Station search");
172                            builder.setView(rootView);
173                            builder.setCancelable(true);
174                            builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {
175                                    public void onClick(DialogInterface dialog, int which) {
176                                            EditText et = (EditText) rootView.findViewById(R.id.EditText);
177                                            dialog.dismiss();
178                                            if (et.getText().toString().length() >= 2) {
179                                                    startNameSearch(et.getText().toString());
180                                            } else {
181                                                    MessageBox.showMessage(StationList.this, "To characters, minimum" );
182                                            }
183                                    }
184                            });
185                            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
186                                    public void onClick(DialogInterface dialog, int which) {
187                                            dialog.dismiss();
188                                    }
189                            });                    
190                            return builder.create();
191                            
192                  default:                  default:
193                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
194                  }                  }
# Line 149  public class StationList extends ListAct Line 196  public class StationList extends ListAct
196          }          }
197                    
198                    
   
199          @Override          @Override
200          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
201                  super.onPrepareDialog(id, dialog);                  super.onPrepareDialog(id, dialog);
# Line 163  public class StationList extends ListAct Line 209  public class StationList extends ListAct
209                          break;                          break;
210                  }                  }
211          }          }
212            
213            @Override
214            protected void onListItemClick(ListView l, View v, int position, long id) {
215                    super.onListItemClick(l, v, position, id);
216                                    
217                    StationBean station = stations.get(position);
218    
219                    double latitude = station.getLatitude();
220                    double longitude = station.getLongitude();
221    
222    
223                    
224                    Intent intent = new Intent(this, DepartureList.class);
225                    intent.putExtra("name", station.getName());
226                    intent.putExtra("distance", station.getDistance());
227                    intent.putExtra("latitude", latitude);
228                    intent.putExtra("longitude", longitude);
229                    intent.putExtra("stationid", station.getId());
230                    intent.putExtra("address", station.getAddress());
231                    startActivity(intent);
232            }
233    
234            /////////////////////////////////////////////////////////////
235            //
236    
237          public void startLookup() {          public void startLookup() {
238                  isRunning = true;                  isRunning = true;
239                    dialogMessage = "Wait for location fix";
240                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
241                                    
242                  locator.locateStations();                  locator.locateStations();
243                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
244          }          }
245            
246            void startNameSearch(String name) {
247                    dialogMessage = "Finding stations by name";
248                    showDialog(DLG_PROGRESS);
249    
250                    locatorTask = new LocatorTask();
251                    locatorTask.searchByName(name, locator.getLocation());
252                    locatorTask.execute();
253                    
254            }
255    
256    
257          Handler stationsFetched = new Handler() {          
258            void startLocatorTask()
259            {
260                    dialogMessage = "Finding nearby stations";
261                    showDialog(DLG_PROGRESS);
262                    
263                    locatorTask = new LocatorTask();
264                    locatorTask.searchByLocation( locator.getLocation() );
265                    locatorTask.execute();  
266            }
267            
268    
269            String lookupAddress(double latitude, double longitude) {
270                    
271                    Geocoder coder = new Geocoder(this, new Locale("da"));
272                    StringBuilder sb = new StringBuilder();
273                    Log.i("lookupaddr", "" + latitude + "/" + longitude);
274                    try {
275                            List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);
276                            Address addr = addressList.get(0);
277                            
278                            
279                            int max = addr.getMaxAddressLineIndex();
280                            for (int i=0; i<max; i++) {
281                                    if (i>0)
282                                            sb.append(", ");
283                                    
284                                    sb.append(addr.getAddressLine(i));
285                            }
286                            
287                            
288                    } catch (Exception e) {
289                            Log.e("DepartureList", "geocoder failed", e);
290                    }
291                    
292                    return sb.toString();
293            }
294            
295            
296            ////////////////////////////////////////////////////////////////////////////
297            // Inner classes
298    
299            class StationsFetchedHandler extends Handler {
300                  @Override                  @Override
301                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
302    
# Line 222  public class StationList extends ListAct Line 345  public class StationList extends ListAct
345                          isRunning = false;                          isRunning = false;
346                  }                  }
347          };          };
           
         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();  
   
348    
349            
350            class LocatorTask extends AsyncTask<Void,Void,Void> {
351                                    
352                  Intent intent = new Intent(this, DepartureList.class);                  LookupMethod method = LookupMethod.MethodNone;
353                  intent.putExtra("name", station.getName());                  boolean success;
354                  intent.putExtra("distance", station.getDistance());                  String name;
355                  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) {  
356                                    
357                  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));  
                         }  
358                                                    
359                                                    method = LookupMethod.ByName;
360                  } catch (Exception e) {                          loc = l;
361                          Log.e("DepartureList", "geocoder failed", e);                          name = n;
362                    }
363                    
364                    public void searchByLocation(Location l) {
365                            method = LookupMethod.ByLocation;
366                            loc = l;
367                  }                  }
368                                    
                 return sb.toString();  
         }  
           
         class LocatorTask extends AsyncTask<Void,Void,Void> {  
                 boolean success;  
369                  @Override                  @Override
370                  protected void onPreExecute() {                  protected void onPreExecute() {
371    
372                            if (method.equals(LookupMethod.MethodNone))
373                                    throw new RuntimeException("Method not set");
374                          super.onPreExecute();                          super.onPreExecute();
375                  }                  }
376                                    
377                  @Override                  @Override
378                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
379                          Location loc = locator.getLocation();                  
380                          success = stationProvider.lookupStations(loc);                          if (method.equals(LookupMethod.ByLocation))
381                                    success = stationProvider.lookupStations(loc);
382                                                    
383                            if (method.equals(LookupMethod.ByName))
384                                    success = stationProvider.lookupStations(name);
385                                                    
386                            Location dummy = new Location("gps");
387                          List<StationBean> stations = stationProvider.getStations();                          List<StationBean> stations = stationProvider.getStations();
388                          for (StationBean station : stations) {                          
389                            for (StationBean station : stations) {
390                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());
391                                  station.setAddress(addr);                                  station.setAddress(addr);
392                          }                                  
393                                    if (method.equals(LookupMethod.ByName) ) {
394                                            dummy.setLatitude(station.getLatitude());
395                                            dummy.setLongitude(station.getLongitude());
396                                            station.setDistance( (int)loc.distanceTo(dummy) );
397                                    }
398                            }                                              
399                                                    
400                          return null;                          return null;
401                  }                  }

Legend:
Removed from v.380  
changed lines
  Added in v.381

  ViewVC Help
Powered by ViewVC 1.1.20