/* * Copyright (C) 2011 The Rexsee Open Source Project * * You may obtain a copy of the License at * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * limitations under the License. */ package rexsee.location; import java.util.List; import rexsee.core.browser.JavascriptInterface; import rexsee.core.browser.RexseeBrowser; import rexsee.core.browser.ActivityResult.ActivityResultListener; mport android.os.Bundle; import android.provider.Settings; public class RexseeGps implements JavascriptInterface { private static final String INTERFACE_NAME = "Gps"; @Override public String getInterfaceName() { return mBrowser.application.resources.prefix + INTERFACE_NAME; } @Override public static final String EVENT_ONGPSLOCATIONCHANGED = "onGpsLocationChanged"; public static String address2Json(Address address) { if (address == null) return "{}"; String rtn = ""; rtn += "{"; rtn += "\"Premises\":\"" + address.getPremises() + "\""; //Type of the location. Cross, road or POI. //rtn += ",\"SubLocality\":\"" + address.getSubLocality() + "\"";Not support SDK3 rtn += ",\"Thoroughfare\":\"" + address.getThoroughfare() + "\""; rtn += "}"; return rtn; } public static String addresses2Json(List<Address> addresses) { String rtn = "["; for (int i = 0; i < addresses.size(); i++) { if (i != 0) rtn += ","; } private int minUpdateDuration = 60; public RexseeGps(final RexseeBrowser browser) { mContext = browser.getContext(); mBrowser = browser; browser.eventList.add(EVENT_ONGPSSETTINGSUCCESSED); public void run() { stop(); } }); mListener = new LocationListener() { @Override public void onLocationChanged(Location location) { if (browser != null) { browser.eventList.run(EVENT_ONGPSLOCATIONCHANGED, new String[]{ } @Override public void onProviderDisabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } } }; } //JavaScript Interface public boolean isReady() { return (((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).isProviderEnabled(LocationManager.GPS_PROVIDER)) ? true : false; } locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minUpdateDuration * 1000, minUpdateDistance, mListener); } else { mBrowser.exception(getInterfaceName(), "GPS is not available."); } } public void stop() { } public void setMinimumUpdateDuration(int seconds) { minUpdateDuration = seconds; } public void setMinimumUpdateDistance(int meter) { minUpdateDistance = meter; } } public String getLastKnownLocation() { if (!isReady()) return "{}"; try { Location location = ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location == null) return ""; return rtn; } catch (Exception e) { mBrowser.exception(getInterfaceName(), e); return "{}"; } } public String getLastKnownGeo(int maxNumber) { if (!isReady()) return "[]"; try { Location location = ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).getLastKnownLocation(LocationManager.GPS_PROVIDER); (), location.getLongitude(), maxNumber); return addresses2Json(addresses); } catch (IOException e) { mBrowser.exception(getInterfaceName(), e); return "[]"; } } public float getDistanceBetween(String startLongitude, String startLatitude, String endLongitude, String endLatitude) { Location.distanceBetween(sLatitude, sLongitude, eLatitude, eLongitude, results); return results[0]; } }