/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 240 by torben, Sun Aug 9 09:20:45 2009 UTC revision 253 by torben, Mon Aug 10 16:58:22 2009 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo;  package dk.thoerup.traininfo;
2    
 import java.io.BufferedReader;  
 import java.io.InputStreamReader;  
 import java.net.URL;  
 import java.net.URLConnection;  
3  import java.util.ArrayList;  import java.util.ArrayList;
4  import java.util.List;  import java.util.List;
5    
6  import org.json.JSONArray;  import org.json.JSONArray;
7  import org.json.JSONObject;  import org.json.JSONObject;
8    
 import dk.thoerup.traininfo.util.DownloadUtil;  
   
9  import android.content.Context;  import android.content.Context;
10  import android.location.Criteria;  import android.location.Criteria;
11  import android.location.Location;  import android.location.Location;
12  import android.location.LocationListener;  import android.location.LocationListener;
13  import android.location.LocationManager;  import android.location.LocationManager;
14    import android.location.LocationProvider;
15  import android.os.Bundle;  import android.os.Bundle;
16  import android.os.Handler;  import android.os.Handler;
17  import android.util.Log;  import android.util.Log;
18    import dk.thoerup.traininfo.provider.ProviderFactory;
19    import dk.thoerup.traininfo.provider.StationProvider;
20    import dk.thoerup.traininfo.util.DownloadUtil;
21    
22  public class StationLocator implements LocationListener{  public class StationLocator implements LocationListener{
23    
# Line 27  public class StationLocator implements L Line 25  public class StationLocator implements L
25          Context cntx;          Context cntx;
26          Handler hndl;          Handler hndl;
27    
28          ArrayList<StationBean> stationList = new ArrayList<StationBean>();          List<StationBean> stationList = new ArrayList<StationBean>();
29          List<StationBean> safeStationList = java.util.Collections.unmodifiableList(stationList);  
30            
31            Location savedLocation = null;
32    
33          public StationLocator(Context c, Handler h) {          public StationLocator(Context c, Handler h) {
34                  cntx = c;                  cntx = c;
# Line 36  public class StationLocator implements L Line 36  public class StationLocator implements L
36          }          }
37                    
38          public List<StationBean> getStations() {          public List<StationBean> getStations() {
39                  return safeStationList;                  return stationList;
40          }          }
41                    
42          public void abortLocationListener() {          public void abortLocationListener() {
# Line 60  public class StationLocator implements L Line 60  public class StationLocator implements L
60                  Criteria c = new Criteria();                  Criteria c = new Criteria();
61                  c.setAccuracy(Criteria.ACCURACY_FINE);                  c.setAccuracy(Criteria.ACCURACY_FINE);
62                  String bestProv = locManager.getBestProvider(c, true);                  String bestProv = locManager.getBestProvider(c, true);
63                  Log.e("BestProvider", bestProv);                  Log.i("BestProvider", ""+bestProv);
64                    
65    
66                  if (bestProv != null) {                  if (bestProv != null) {
67                            savedLocation = locManager.getLastKnownLocation(bestProv);
68                          locManager.requestLocationUpdates(bestProv, 0, 0, this);                          locManager.requestLocationUpdates(bestProv, 0, 0, this);
69                  } else {                  } else {
70                          // message that no suitable provider was found                          // message that no suitable provider was found
# Line 73  public class StationLocator implements L Line 75  public class StationLocator implements L
75          public void onLocationChanged(Location location) {          public void onLocationChanged(Location location) {
76                  Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());                  Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider());
77    
78                    savedLocation = new Location(location); //save a copy
79                    
80                  if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix                  if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix
81                          return;                          return;
82    
83                  locManager.removeUpdates(this);                  locManager.removeUpdates(this);
84                  hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);                  hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION);
85            }
86            
87            public void findNearestStations() {
88                    findNearestStations(savedLocation);
89            }
90            
91            public void findNearestStations(Location location) {
92                    StationProvider prov = ProviderFactory.getStationProvider();
93                    prov.lookupStations(location);
94                    this.stationList = prov.getStations();
95                                    
96                  //ToDo: to be nice put the api key into the request                  if (stationList.size() > 0)
                 String urlSource = "http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&q=Train%20station&near=" + location.getLatitude() + "%2C" + location.getLongitude() + "&v=1.0";  
                 //String urlSource = "http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&q=Train%20station&near=56.2%2C9.0&v=1.0";  
   
                 try {  
                         String data = DownloadUtil.getContent(urlSource, 30000, "UTF-8");  
                         StringBuilder builder = new StringBuilder(data);  
   
                         while (builder.charAt(0) != '{')  
                                 builder.deleteCharAt(0);  
                         while (builder.charAt(builder.length()-1) != '}')  
                                 builder.deleteCharAt(builder.length()-1);  
   
                         JSONObject json = new JSONObject(builder.toString());  
                         // now have some fun with the results...  
                         JSONArray res = json.getJSONArray("results");  
   
                         Location tmpLocation = new Location("gps");  
                         stationList.clear();  
                           
                         for (int i=0; i<res.length(); i++) {  
                                 JSONObject jsonStation = res.getJSONObject(i);  
   
                                 double lat = jsonStation.getDouble("lat");  
                                 double lng = jsonStation.getDouble("lng");  
                                 String title = jsonStation.getString("title");  
                                 String addr = jsonStation.getString("streetAddress");  
                                 tmpLocation.setLatitude(lat);  
                                 tmpLocation.setLongitude(lng);  
   
                                 float distance = location.distanceTo(tmpLocation);  
   
                                 stationList.add( new StationBean(title,lat,lng,(int)distance,addr) );  
   
                                 Log.e("Location", title + " " + lat +"," + lng);  
                         }  
97                          hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);                          hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST);
98                    else
                 } catch (Exception e) {  
                         Log.e("Location","Location",e);  
99                          hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);                          hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED);
                 }  
                   
100          }          }
101    
102    
# Line 144  public class StationLocator implements L Line 119  public class StationLocator implements L
119                  // TODO Auto-generated method stub                  // TODO Auto-generated method stub
120    
121          }          }
122            
123    
124            public static void injectMockLocation(Context cntx) {
125                    Location odder = new Location("gps2");
126                    odder.setLatitude(55.976632);
127                    odder.setLongitude(10.16407);
128                    
129                    Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573
130                    kbh.setLatitude(55.675092);
131                    kbh.setLongitude(12.578573);
132                    
133                    Location bjbro = new Location("gps2");
134                    bjbro.setLatitude(56.380745);
135                    bjbro.setLongitude(9.655609);
136                    
137                    
138                    LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
139                    if (lm.getProvider("gps2") == null)
140                            lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE );
141                    lm.setTestProviderEnabled("gps2", true);
142                    lm.setTestProviderLocation("gps2", kbh);
143            }
144            
145            public static void removeMockLocation(Context cntx) {
146                    LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE);
147                    if (lm.getProvider("gps2") != null)
148                            lm.removeTestProvider("gps2");
149            }
150  }  }

Legend:
Removed from v.240  
changed lines
  Added in v.253

  ViewVC Help
Powered by ViewVC 1.1.20