/[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 371 by torben, Wed Sep 30 19:09:59 2009 UTC revision 420 by torben, Thu Oct 8 11:50:46 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    
# Line 81  public class StationList extends ListAct Line 97  public class StationList extends ListAct
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_MAP, 0, "Show station map");                  MenuItem item;
108                  menu.add(0, OPTIONS_ABOUT, 0, "About");                  
109                    item = menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations");
110                    item.setIcon(android.R.drawable.ic_menu_mylocation);
111                    
112                    item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station");
113                    item.setIcon(android.R.drawable.ic_menu_search);
114                                    
115                    item = menu.add(0, OPTIONS_MAP, 0, "Show station map");
116                    item.setIcon(android.R.drawable.ic_menu_mapmode);
117                    
118                    item = menu.add(0, OPTIONS_ABOUT, 0, "About");
119                    item.setIcon(android.R.drawable.ic_menu_info_details);
120                  return true;                  return true;
121          }          }
122    
123          @Override          @Override
124          public boolean onOptionsItemSelected(MenuItem item) {          public boolean onOptionsItemSelected(MenuItem item) {
125                  boolean retval;                  boolean retval = true;
126    
127                                    
128                  switch (item.getItemId()) {                  switch (item.getItemId()) {
129                    case OPTIONS_RESCAN:
130                            startLookup();
131                            break;
132                    case OPTIONS_NAMESEARCH:
133                            showDialog(DLG_STATIONNAME);
134                            break;
135                  case OPTIONS_MAP:                  case OPTIONS_MAP:
136                                                    
137                          Intent intent = new Intent(this,StationMapView.class);                          Intent intent = new Intent(this,StationMapView.class);
138                          intent.putExtra("userlocation", GeoPair.fromLocation(locator.getLocation()) );                          intent.putExtra("userlocation", location );
139                                                    
140                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();                          ArrayList<GeoPair> stationPoints = new ArrayList<GeoPair>();
141                          for (StationBean st : stations ) {                          for (StationBean st : stations ) {
# Line 111  public class StationList extends ListAct Line 145  public class StationList extends ListAct
145                          intent.putExtra("stations", stationPoints);                          intent.putExtra("stations", stationPoints);
146                                                    
147                          startActivity(intent);                          startActivity(intent);
                         retval = true;  
148                          break;                          break;
149                  case OPTIONS_ABOUT:                  case OPTIONS_ABOUT:
150                          String ver = this.getResources().getString(R.string.app_version);                          String ver = this.getResources().getString(R.string.app_version);
151                                                    
152                            Location loc = locator.getLocation();
153                          StringBuffer message = new StringBuffer();                          StringBuffer message = new StringBuffer();
154                          message.append("TrainInfo DK v").append(ver).append("\n");                          message.append("TrainInfo DK v").append(ver).append("\n");
155                          message.append("By Torben Nielsen\n");                          message.append("By Torben Nielsen\n");
156                            message.append("\n");
157                            message.append("Location info:\n");
158                            message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n");
159                            message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n");
160    
161                          MessageBox.showMessage(this, message.toString());                          MessageBox.showMessage(this, message.toString());
                         retval = true;  
162                          break;                          break;
163                  default:                  default:
164                          retval = super.onOptionsItemSelected(item);                          retval = super.onOptionsItemSelected(item);
# Line 138  public class StationList extends ListAct Line 175  public class StationList extends ListAct
175                          dlg.setMessage("Wait for location fix");                          dlg.setMessage("Wait for location fix");
176                          dlg.setCancelable(false);                          dlg.setCancelable(false);
177                          return dlg;                                              return dlg;                    
178                    case DLG_STATIONNAME:
179                            LayoutInflater factory = LayoutInflater.from(this);
180                            final View rootView = factory.inflate(R.layout.textinput, null);
181                            
182                            
183                            AlertDialog.Builder builder = new AlertDialog.Builder(this);
184                            
185                            builder.setTitle("Station search");
186                            builder.setView(rootView);
187                            builder.setCancelable(true);
188                            builder.setPositiveButton("Search", new DialogInterface.OnClickListener() {
189                                    public void onClick(DialogInterface dialog, int which) {
190                                            EditText et = (EditText) rootView.findViewById(R.id.EditText);
191                                            dialog.dismiss();
192                                            if (et.getText().toString().length() >= 2) {
193                                                    startNameSearch(et.getText().toString());
194                                            } else {
195                                                    MessageBox.showMessage(StationList.this, "Two characters minimum" );
196                                            }
197                                    }
198                            });
199                            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
200                                    public void onClick(DialogInterface dialog, int which) {
201                                            dialog.dismiss();
202                                    }
203                            });                    
204                            return builder.create();
205                            
206                  default:                  default:
207                          return super.onCreateDialog(id);                                          return super.onCreateDialog(id);                
208                  }                  }
# Line 145  public class StationList extends ListAct Line 210  public class StationList extends ListAct
210          }          }
211                    
212                    
   
213          @Override          @Override
214          protected void onPrepareDialog(int id, Dialog dialog) {          protected void onPrepareDialog(int id, Dialog dialog) {
215                  super.onPrepareDialog(id, dialog);                  super.onPrepareDialog(id, dialog);
# Line 159  public class StationList extends ListAct Line 223  public class StationList extends ListAct
223                          break;                          break;
224                  }                  }
225          }          }
226            
227            @Override
228            protected void onListItemClick(ListView l, View v, int position, long id) {
229                    super.onListItemClick(l, v, position, id);
230                                    
231                    StationBean station = stations.get(position);
232    
233                    double latitude = station.getLatitude();
234                    double longitude = station.getLongitude();
235    
236    
237                    
238                    Intent intent = new Intent(this, DepartureList.class);
239                    intent.putExtra("name", station.getName());
240                    intent.putExtra("distance", station.getDistance());
241                    intent.putExtra("latitude", latitude);
242                    intent.putExtra("longitude", longitude);
243                    intent.putExtra("stationid", station.getId());
244                    intent.putExtra("address", station.getAddress());
245                    startActivity(intent);
246            }
247    
248            /////////////////////////////////////////////////////////////
249            //
250    
251          public void startLookup() {          public void startLookup() {
252                  isRunning = true;                  isRunning = true;
253                    dialogMessage = "Wait for location fix";
254                  showDialog(DLG_PROGRESS);                  showDialog(DLG_PROGRESS);
255                                    
256                  locator.locateStations();                  locator.locateStations();
257                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);                  stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000);
258          }          }
259            
260            void startNameSearch(String name) {
261                    dialogMessage = "Finding stations by name";
262                    showDialog(DLG_PROGRESS);
263    
264                    locatorTask = new LocatorTask();
265                    locatorTask.searchByName(name, locator.getLocation());
266                    locatorTask.execute();
267                    
268            }
269    
270    
271            
272            void startLocatorTask()
273            {
274                    dialogMessage = "Finding nearby stations";
275                    showDialog(DLG_PROGRESS);
276                    
277                    locatorTask = new LocatorTask();
278                    locatorTask.searchByLocation( locator.getLocation() );
279                    locatorTask.execute();  
280            }
281            
282    
283            String lookupAddress(double latitude, double longitude) {
284                    
285                    Geocoder coder = new Geocoder(this, new Locale("da"));
286                    StringBuilder sb = new StringBuilder();
287                    Log.i("lookupaddr", "" + latitude + "/" + longitude);
288                    try {
289                            List<Address> addressList = coder.getFromLocation(latitude, longitude, 1);
290                            Address addr = addressList.get(0);
291                            
292                            
293                            int max = addr.getMaxAddressLineIndex();
294                            for (int i=0; i<max; i++) {
295                                    if (i>0)
296                                            sb.append(", ");
297                                    
298                                    sb.append(addr.getAddressLine(i));
299                            }
300                            
301                            
302                    } catch (Exception e) {
303                            Log.e("DepartureList", "geocoder failed", e);
304                    }
305                    
306                    return sb.toString();
307            }
308            
309            
310            ////////////////////////////////////////////////////////////////////////////
311            // Inner classes
312    
313          Handler stationsFetched = new Handler() {          class StationsFetchedHandler extends Handler {
314                  @Override                  @Override
315                  public void handleMessage(Message msg) {                  public void handleMessage(Message msg) {
316    
# Line 178  public class StationList extends ListAct Line 319  public class StationList extends ListAct
319                                  dismissDialog(DLG_PROGRESS);                                  dismissDialog(DLG_PROGRESS);
320                                                                    
321                                  startLocatorTask();                                  startLocatorTask();
322                                    location = GeoPair.fromLocation( locator.getLocation() );
323                                                                    
324                                  break;                                  break;
325    
# Line 194  public class StationList extends ListAct Line 336  public class StationList extends ListAct
336                                                  dismissDialog(DLG_PROGRESS);                                                  dismissDialog(DLG_PROGRESS);
337                                                                                                    
338                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                                                                  AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this);                                                
339                                                  builder.setMessage("GPS fix timed out");                                                  builder.setMessage("Location fix timed out");
340                                                  builder.setCancelable(true);                                                  builder.setCancelable(true);
341                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {                                                  builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
342                                                          public void onClick(DialogInterface dialog, int id) {                                                          public void onClick(DialogInterface dialog, int id) {
# Line 217  public class StationList extends ListAct Line 359  public class StationList extends ListAct
359                          isRunning = false;                          isRunning = false;
360                  }                  }
361          };          };
           
         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();  
   
362    
363            
364            class LocatorTask extends AsyncTask<Void,Void,Void> {
365                                    
366                  Intent intent = new Intent(this, DepartureList.class);                  LookupMethod method = LookupMethod.MethodNone;
367                  intent.putExtra("name", station.getName());                  boolean success;
368                  intent.putExtra("distance", station.getDistance());                  String name;
369                  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) {  
370                                    
371                  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));  
                         }  
372                                                    
373                                                    method = LookupMethod.ByName;
374                  } catch (Exception e) {                          loc = l;
375                          Log.e("DepartureList", "geocoder failed", e);                          name = n;
376                    }
377                    
378                    public void searchByLocation(Location l) {
379                            method = LookupMethod.ByLocation;
380                            loc = l;
381                  }                  }
382                                    
                 return sb.toString();  
         }  
           
         class LocatorTask extends AsyncTask<Void,Void,Void> {  
                 boolean success;  
383                  @Override                  @Override
384                  protected void onPreExecute() {                  protected void onPreExecute() {
385    
386                            if (method.equals(LookupMethod.MethodNone))
387                                    throw new RuntimeException("Method not set");
388                          super.onPreExecute();                          super.onPreExecute();
389                  }                  }
390                                    
391                  @Override                  @Override
392                  protected Void doInBackground(Void... params) {                  protected Void doInBackground(Void... params) {
393                          Location loc = locator.getLocation();                  
394                          success = stationProvider.lookupStations(loc);                          if (method.equals(LookupMethod.ByLocation))
395                                    success = stationProvider.lookupStations(loc);
396                                                    
397                            if (method.equals(LookupMethod.ByName))
398                                    success = stationProvider.lookupStations(name);
399                                                    
400                            Location dummy = new Location("gps");
401                          List<StationBean> stations = stationProvider.getStations();                          List<StationBean> stations = stationProvider.getStations();
402                          for (StationBean station : stations) {                          
403                            for (StationBean station : stations) {
404                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());                                  String addr = lookupAddress(station.getLatitude(), station.getLongitude());
405                                  station.setAddress(addr);                                  station.setAddress(addr);
406                          }                                  
407                                    if (method.equals(LookupMethod.ByName) ) {
408                                            dummy.setLatitude(station.getLatitude());
409                                            dummy.setLongitude(station.getLongitude());
410                                            station.setDistance( (int)loc.distanceTo(dummy) );
411                                    }
412                            }                                              
413                                                    
414                          return null;                          return null;
415                  }                  }

Legend:
Removed from v.371  
changed lines
  Added in v.420

  ViewVC Help
Powered by ViewVC 1.1.20