diff options
Diffstat (limited to 'location')
17 files changed, 915 insertions, 71 deletions
diff --git a/location/java/android/location/Geocoder.java b/location/java/android/location/Geocoder.java index 2ce1273..c325b1b 100644 --- a/location/java/android/location/Geocoder.java +++ b/location/java/android/location/Geocoder.java @@ -45,10 +45,7 @@ import java.util.List; public final class Geocoder { private static final String TAG = "Geocoder"; - private String mLanguage; - private String mCountry; - private String mVariant; - private String mAppName; + private GeocoderParams mParams; private ILocationManager mService; /** @@ -64,11 +61,7 @@ public final class Geocoder { if (locale == null) { throw new NullPointerException("locale == null"); } - mLanguage = locale.getLanguage(); - mCountry = locale.getCountry(); - mVariant = locale.getVariant(); - mAppName = context.getPackageName(); - + mParams = new GeocoderParams(context, locale); IBinder b = ServiceManager.getService(Context.LOCATION_SERVICE); mService = ILocationManager.Stub.asInterface(b); } @@ -119,7 +112,7 @@ public final class Geocoder { try { List<Address> results = new ArrayList<Address>(); String ex = mService.getFromLocation(latitude, longitude, maxResults, - mLanguage, mCountry, mVariant, mAppName, results); + mParams, results); if (ex != null) { throw new IOException(ex); } else { @@ -161,7 +154,7 @@ public final class Geocoder { try { List<Address> results = new ArrayList<Address>(); String ex = mService.getFromLocationName(locationName, - 0, 0, 0, 0, maxResults, mLanguage, mCountry, mVariant, mAppName, results); + 0, 0, 0, 0, maxResults, mParams, results); if (ex != null) { throw new IOException(ex); } else { @@ -234,7 +227,7 @@ public final class Geocoder { ArrayList<Address> result = new ArrayList<Address>(); String ex = mService.getFromLocationName(locationName, lowerLeftLatitude, lowerLeftLongitude, upperRightLatitude, upperRightLongitude, - maxResults, mLanguage, mCountry, mVariant, mAppName, result); + maxResults, mParams, result); if (ex != null) { throw new IOException(ex); } else { diff --git a/location/java/android/location/GeocoderParams.aidl b/location/java/android/location/GeocoderParams.aidl new file mode 100644 index 0000000..2484e20 --- /dev/null +++ b/location/java/android/location/GeocoderParams.aidl @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2010, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.location; + +parcelable GeocoderParams; diff --git a/location/java/android/location/GeocoderParams.java b/location/java/android/location/GeocoderParams.java new file mode 100644 index 0000000..8b8e63b --- /dev/null +++ b/location/java/android/location/GeocoderParams.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.location; + +import android.content.Context; +import android.os.Parcel; +import android.os.Parcelable; + +import java.util.Locale; + +/** + * This class contains extra parameters to pass to an IGeocodeProvider + * implementation from the Geocoder class. Currently this contains the + * language, country and variant information from the Geocoder's locale + * as well as the Geocoder client's package name for geocoder server + * logging. This information is kept in a separate class to allow for + * future expansion of the IGeocodeProvider interface. + */ +public class GeocoderParams implements Parcelable { + private Locale mLocale; + private String mPackageName; + + // used only for parcelling + private GeocoderParams() { + } + + /** + * This object is only constructed by the Geocoder class + * + * @hide + */ + public GeocoderParams(Context context, Locale locale) { + mLocale = locale; + mPackageName = context.getPackageName(); + } + + /** + * returns the Geocoder's locale + */ + public Locale getLocale() { + return mLocale; + } + + /** + * returns the package name of the Geocoder's client + */ + public String getClientPackage() { + return mPackageName; + } + + public static final Parcelable.Creator<GeocoderParams> CREATOR = + new Parcelable.Creator<GeocoderParams>() { + public GeocoderParams createFromParcel(Parcel in) { + GeocoderParams gp = new GeocoderParams(); + String language = in.readString(); + String country = in.readString(); + String variant = in.readString(); + gp.mLocale = new Locale(language, country, variant); + gp.mPackageName = in.readString(); + return gp; + } + + public GeocoderParams[] newArray(int size) { + return new GeocoderParams[size]; + } + }; + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel parcel, int flags) { + parcel.writeString(mLocale.getLanguage()); + parcel.writeString(mLocale.getCountry()); + parcel.writeString(mLocale.getVariant()); + parcel.writeString(mPackageName); + } +} diff --git a/location/java/android/location/IGeocodeProvider.aidl b/location/java/android/location/IGeocodeProvider.aidl index e79e8d2..aaa70c7 100644 --- a/location/java/android/location/IGeocodeProvider.aidl +++ b/location/java/android/location/IGeocodeProvider.aidl @@ -17,6 +17,7 @@ package android.location; import android.location.Address; +import android.location.GeocoderParams; /** * An interface for location providers implementing the Geocoder services. @@ -26,10 +27,10 @@ import android.location.Address; interface IGeocodeProvider { String getFromLocation(double latitude, double longitude, int maxResults, - String language, String country, String variant, String appName, out List<Address> addrs); + in GeocoderParams params, out List<Address> addrs); String getFromLocationName(String locationName, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude, int maxResults, - String language, String country, String variant, String appName, out List<Address> addrs); + in GeocoderParams params, out List<Address> addrs); } diff --git a/location/java/android/location/ILocationManager.aidl b/location/java/android/location/ILocationManager.aidl index b6c59d6..1fac07c 100644 --- a/location/java/android/location/ILocationManager.aidl +++ b/location/java/android/location/ILocationManager.aidl @@ -18,6 +18,7 @@ package android.location; import android.app.PendingIntent; import android.location.Address; +import android.location.GeocoderParams; import android.location.IGeocodeProvider; import android.location.IGpsStatusListener; import android.location.ILocationListener; @@ -63,11 +64,11 @@ interface ILocationManager void reportLocation(in Location location); String getFromLocation(double latitude, double longitude, int maxResults, - String language, String country, String variant, String appName, out List<Address> addrs); + in GeocoderParams params, out List<Address> addrs); String getFromLocationName(String locationName, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude, int maxResults, - String language, String country, String variant, String appName, out List<Address> addrs); + in GeocoderParams params, out List<Address> addrs); void addTestProvider(String name, boolean requiresNetwork, boolean requiresSatellite, boolean requiresCell, boolean hasMonetaryCost, boolean supportsAltitude, diff --git a/location/java/android/location/ILocationProvider.aidl b/location/java/android/location/ILocationProvider.aidl index 7da16e4..9fe6ab4 100644 --- a/location/java/android/location/ILocationProvider.aidl +++ b/location/java/android/location/ILocationProvider.aidl @@ -37,7 +37,6 @@ interface ILocationProvider { int getAccuracy(); void enable(); void disable(); - boolean isEnabled(); int getStatus(out Bundle extras); long getStatusUpdateTime(); void enableLocationTracking(boolean enable); diff --git a/location/java/android/location/LocationManager.java b/location/java/android/location/LocationManager.java index 94ced22..6d7a23d 100644 --- a/location/java/android/location/LocationManager.java +++ b/location/java/android/location/LocationManager.java @@ -23,7 +23,6 @@ import android.os.Looper; import android.os.RemoteException; import android.os.Handler; import android.os.Message; -import android.util.Config; import android.util.Log; import com.android.internal.location.DummyLocationProvider; @@ -105,6 +104,48 @@ public class LocationManager { */ public static final String KEY_LOCATION_CHANGED = "location"; + public interface GeocodeProvider { + String getFromLocation(double latitude, double longitude, int maxResults, + GeocoderParams params, List<Address> addrs); + + String getFromLocationName(String locationName, + double lowerLeftLatitude, double lowerLeftLongitude, + double upperRightLatitude, double upperRightLongitude, int maxResults, + GeocoderParams params, List<Address> addrs); + } + + private static final class GeocodeProviderProxy extends IGeocodeProvider.Stub { + private GeocodeProvider mProvider; + + GeocodeProviderProxy(GeocodeProvider provider) { + mProvider = provider; + } + + /** + * This method is overridden to implement the + * {@link Geocoder#getFromLocation(double, double, int)} method. + * Classes implementing this method should not hold a reference to the params parameter. + */ + public String getFromLocation(double latitude, double longitude, int maxResults, + GeocoderParams params, List<Address> addrs) { + return mProvider.getFromLocation(latitude, longitude, maxResults, params, addrs); + } + + /** + * This method is overridden to implement the + * {@link Geocoder#getFromLocationName(String, int, double, double, double, double)} method. + * Classes implementing this method should not hold a reference to the params parameter. + */ + public String getFromLocationName(String locationName, + double lowerLeftLatitude, double lowerLeftLongitude, + double upperRightLatitude, double upperRightLongitude, int maxResults, + GeocoderParams params, List<Address> addrs) { + return mProvider.getFromLocationName(locationName, lowerLeftLatitude, + lowerLeftLongitude, upperRightLatitude, upperRightLongitude, + maxResults, params, addrs); + } + } + // Map from LocationListeners to their associated ListenerTransport objects private HashMap<LocationListener,ListenerTransport> mListeners = new HashMap<LocationListener,ListenerTransport>(); @@ -206,7 +247,7 @@ public class LocationManager { * factory Context.getSystemService. */ public LocationManager(ILocationManager service) { - if (Config.LOGD) { + if (false) { Log.d(TAG, "Constructor: service = " + service); } mService = service; @@ -235,7 +276,7 @@ public class LocationManager { * @return list of Strings containing names of the providers */ public List<String> getAllProviders() { - if (Config.LOGD) { + if (false) { Log.d(TAG, "getAllProviders"); } try { @@ -787,7 +828,7 @@ public class LocationManager { if (listener == null) { throw new IllegalArgumentException("listener==null"); } - if (Config.LOGD) { + if (false) { Log.d(TAG, "removeUpdates: listener = " + listener); } try { @@ -812,7 +853,7 @@ public class LocationManager { if (intent == null) { throw new IllegalArgumentException("intent==null"); } - if (Config.LOGD) { + if (false) { Log.d(TAG, "removeUpdates: intent = " + intent); } try { @@ -867,7 +908,7 @@ public class LocationManager { */ public void addProximityAlert(double latitude, double longitude, float radius, long expiration, PendingIntent intent) { - if (Config.LOGD) { + if (false) { Log.d(TAG, "addProximityAlert: latitude = " + latitude + ", longitude = " + longitude + ", radius = " + radius + ", expiration = " + expiration + @@ -888,7 +929,7 @@ public class LocationManager { * proximity alerts */ public void removeProximityAlert(PendingIntent intent) { - if (Config.LOGD) { + if (false) { Log.d(TAG, "removeProximityAlert: intent = " + intent); } try { @@ -1378,6 +1419,19 @@ public class LocationManager { } /** + * Installs a location provider. + * + * @param provider implementation of the location provider + * + * @return true if the command succeeds. + * + * Requires the android.permission.INSTALL_LOCATION_PROVIDER permission. + */ + public boolean installLocationProvider(LocationProviderImpl provider) { + return installLocationProvider(provider.getName(), provider.getInterface()); + } + + /** * Installs a geocoder server. * * @param provider Binder interface for the geocoder provider @@ -1385,12 +1439,10 @@ public class LocationManager { * @return true if the command succeeds. * * Requires the android.permission.INSTALL_LOCATION_PROVIDER permission. - * - * {@hide} */ - public boolean installGeocodeProvider(IGeocodeProvider provider) { + public boolean installGeocodeProvider(GeocodeProvider provider) { try { - mService.installGeocodeProvider(provider); + mService.installGeocodeProvider(new GeocodeProviderProxy(provider)); return true; } catch (RemoteException e) { Log.e(TAG, "RemoteException in setGeocodeProvider: ", e); @@ -1404,8 +1456,6 @@ public class LocationManager { * @param location new Location to report * * Requires the android.permission.INSTALL_LOCATION_PROVIDER permission. - * - * {@hide} */ public void reportLocation(Location location) { try { diff --git a/location/java/android/location/LocationProviderImpl.java b/location/java/android/location/LocationProviderImpl.java new file mode 100644 index 0000000..7148a02 --- /dev/null +++ b/location/java/android/location/LocationProviderImpl.java @@ -0,0 +1,226 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.location; + +import android.net.NetworkInfo; +import android.os.Bundle; + +/** + * An abstract superclass for location providers that are implemented + * outside of the core android platform. + * A LocationProviderImpl can be installed using the + * {@link LocationManager#installLocationProvider(LocationProviderImpl)} method. + * Installing a location provider requires the + * android.permission.INSTALL_LOCATION_PROVIDER permission. + */ +public abstract class LocationProviderImpl extends LocationProvider { + + private ILocationProvider.Stub mProvider = new ILocationProvider.Stub() { + + public boolean requiresNetwork() { + return LocationProviderImpl.this.requiresNetwork(); + } + + public boolean requiresSatellite() { + return LocationProviderImpl.this.requiresSatellite(); + } + + public boolean requiresCell() { + return LocationProviderImpl.this.requiresCell(); + } + + public boolean hasMonetaryCost() { + return LocationProviderImpl.this.hasMonetaryCost(); + } + + public boolean supportsAltitude() { + return LocationProviderImpl.this.supportsAltitude(); + } + + public boolean supportsSpeed() { + return LocationProviderImpl.this.supportsSpeed(); + } + + public boolean supportsBearing() { + return LocationProviderImpl.this.supportsBearing(); + } + + public int getPowerRequirement() { + return LocationProviderImpl.this.getPowerRequirement(); + } + + public int getAccuracy() { + return LocationProviderImpl.this.getAccuracy(); + } + + public void enable() { + LocationProviderImpl.this.enable(); + } + + public void disable() { + LocationProviderImpl.this.disable(); + } + + public int getStatus(Bundle extras) { + return LocationProviderImpl.this.getStatus(extras); + } + + public long getStatusUpdateTime() { + return LocationProviderImpl.this.getStatusUpdateTime(); + } + + public void enableLocationTracking(boolean enable) { + LocationProviderImpl.this.enableLocationTracking(enable); + } + + public void setMinTime(long minTime) { + LocationProviderImpl.this.setMinTime(minTime); + } + + public void updateNetworkState(int state, NetworkInfo info) { + LocationProviderImpl.this.updateNetworkState(state, info); + } + + public void updateLocation(Location location) { + LocationProviderImpl.this.updateLocation(location); + } + + public boolean sendExtraCommand(String command, Bundle extras) { + return LocationProviderImpl.this.sendExtraCommand(command, extras); + } + + public void addListener(int uid) { + LocationProviderImpl.this.addListener(uid); + } + + public void removeListener(int uid) { + LocationProviderImpl.this.removeListener(uid); + } + }; + + public LocationProviderImpl(String name) { + super(name); + } + + /** + * {@hide} + */ + /* package */ ILocationProvider getInterface() { + return mProvider; + } + + /** + * Enables the location provider + */ + public abstract void enable(); + + /** + * Disables the location provider + */ + public abstract void disable(); + + /** + * Returns a information on the status of this provider. + * {@link #OUT_OF_SERVICE} is returned if the provider is + * out of service, and this is not expected to change in the near + * future; {@link #TEMPORARILY_UNAVAILABLE} is returned if + * the provider is temporarily unavailable but is expected to be + * available shortly; and {@link #AVAILABLE} is returned + * if the provider is currently available. + * + * <p> If extras is non-null, additional status information may be + * added to it in the form of provider-specific key/value pairs. + */ + public abstract int getStatus(Bundle extras); + + /** + * Returns the time at which the status was last updated. It is the + * responsibility of the provider to appropriately set this value using + * {@link android.os.SystemClock#elapsedRealtime SystemClock.elapsedRealtime()}. + * there is a status update that it wishes to broadcast to all its + * listeners. The provider should be careful not to broadcast + * the same status again. + * + * @return time of last status update in millis since last reboot + */ + public abstract long getStatusUpdateTime(); + + /** + * Notifies the location provider that clients are listening for locations. + * Called with enable set to true when the first client is added and + * called with enable set to false when the last client is removed. + * This allows the provider to prepare for receiving locations, + * and to shut down when no clients are remaining. + * + * @param enable true if location tracking should be enabled. + */ + public abstract void enableLocationTracking(boolean enable); + + /** + * Notifies the location provider of the smallest minimum time between updates amongst + * all clients that are listening for locations. This allows the provider to reduce + * the frequency of updates to match the requested frequency. + * + * @param minTime the smallest minTime value over all listeners for this provider. + */ + public abstract void setMinTime(long minTime); + + /** + * Updates the network state for the given provider. This function must + * be overwritten if {@link #requiresNetwork} returns true. The state is + * {@link #TEMPORARILY_UNAVAILABLE} (disconnected), OR {@link #AVAILABLE} + * (connected or connecting). + * + * @param state data state + */ + public abstract void updateNetworkState(int state, NetworkInfo info); + + /** + * Informs the provider when a new location has been computed by a different + * location provider. This is intended to be used as aiding data for the + * receiving provider. + * + * @param location new location from other location provider + */ + public abstract void updateLocation(Location location); + + /** + * Implements addditional location provider specific additional commands. + * + * @param command name of the command to send to the provider. + * @param extras optional arguments for the command (or null). + * The provider may optionally fill the extras Bundle with results from the command. + * + * @return true if the command succeeds. + */ + public abstract boolean sendExtraCommand(String command, Bundle extras); + + /** + * Notifies the location provider when a new client is listening for locations. + * + * @param uid user ID of the new client. + */ + public abstract void addListener(int uid); + + /** + * Notifies the location provider when a client is no longer listening for locations. + * + * @param uid user ID of the client no longer listening. + */ + public abstract void removeListener(int uid); +} + diff --git a/location/java/com/android/internal/location/GpsLocationProvider.java b/location/java/com/android/internal/location/GpsLocationProvider.java index add2404..2b4dab9 100755 --- a/location/java/com/android/internal/location/GpsLocationProvider.java +++ b/location/java/com/android/internal/location/GpsLocationProvider.java @@ -41,7 +41,6 @@ import android.os.RemoteException; import android.os.ServiceManager; import android.os.SystemClock; import android.provider.Settings; -import android.util.Config; import android.util.Log; import android.util.SparseIntArray; @@ -70,7 +69,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { private static final String TAG = "GpsLocationProvider"; - private static final boolean DEBUG = true; + private static final boolean DEBUG = false; private static final boolean VERBOSE = false; /** @@ -385,7 +384,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { public void updateNetworkState(int state, NetworkInfo info) { mNetworkAvailable = (state == LocationProvider.AVAILABLE); - if (Config.LOGD) { + if (DEBUG) { Log.d(TAG, "updateNetworkState " + (mNetworkAvailable ? "available" : "unavailable") + " info: " + info); } @@ -506,7 +505,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { * when the provider is enabled. */ public synchronized void enable() { - if (Config.LOGD) Log.d(TAG, "enable"); + if (DEBUG) Log.d(TAG, "enable"); if (mEnabled) return; mEnabled = native_init(); @@ -542,7 +541,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { * down while the provider is disabled. */ public synchronized void disable() { - if (Config.LOGD) Log.d(TAG, "disable"); + if (DEBUG) Log.d(TAG, "disable"); if (!mEnabled) return; mEnabled = false; @@ -577,10 +576,6 @@ public class GpsLocationProvider extends ILocationProvider.Stub { } } - public boolean isEnabled() { - return mEnabled; - } - public int getStatus(Bundle extras) { if (extras != null) { extras.putInt("satellites", mSvCount); @@ -614,7 +609,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { } public void setMinTime(long minTime) { - if (Config.LOGD) Log.d(TAG, "setMinTime " + minTime); + if (DEBUG) Log.d(TAG, "setMinTime " + minTime); if (minTime >= 0) { int interval = (int)(minTime/1000); @@ -635,7 +630,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { } public void binderDied() { - if (Config.LOGD) Log.d(TAG, "GPS status listener died"); + if (DEBUG) Log.d(TAG, "GPS status listener died"); synchronized(mListeners) { mListeners.remove(this); @@ -733,7 +728,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { } private boolean forceTimeInjection() { - if (Config.LOGD) Log.d(TAG, "forceTimeInjection"); + if (DEBUG) Log.d(TAG, "forceTimeInjection"); if (mNetworkThread != null) { mNetworkThread.timeInjectRequest(); return true; @@ -809,7 +804,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { // report time to first fix if (mTTFF == 0 && (flags & LOCATION_HAS_LAT_LONG) == LOCATION_HAS_LAT_LONG) { mTTFF = (int)(mLastFixTime - mFixRequestTime); - if (Config.LOGD) Log.d(TAG, "TTFF: " + mTTFF); + if (DEBUG) Log.d(TAG, "TTFF: " + mTTFF); // notify status listeners synchronized(mListeners) { @@ -1074,7 +1069,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { } private void xtraDownloadRequest() { - if (Config.LOGD) Log.d(TAG, "xtraDownloadRequest"); + if (DEBUG) Log.d(TAG, "xtraDownloadRequest"); if (mNetworkThread != null) { mNetworkThread.xtraDownloadRequest(); } @@ -1091,7 +1086,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { StringBuilder extrasBuf = new StringBuilder(); - if (Config.LOGD) Log.d(TAG, "sendNiResponse, notifId: " + notificationId + + if (DEBUG) Log.d(TAG, "sendNiResponse, notifId: " + notificationId + ", response: " + userResponse); native_send_ni_response(notificationId, userResponse); @@ -1177,14 +1172,14 @@ public class GpsLocationProvider extends ILocationProvider.Stub { } public void run() { - if (Config.LOGD) Log.d(TAG, "GpsEventThread starting"); + if (DEBUG) Log.d(TAG, "GpsEventThread starting"); // Exit as soon as disable() is called instead of waiting for the GPS to stop. while (mEnabled) { // this will wait for an event from the GPS, // which will be reported via reportLocation or reportStatus native_wait_for_event(); } - if (Config.LOGD) Log.d(TAG, "GpsEventThread exiting"); + if (DEBUG) Log.d(TAG, "GpsEventThread exiting"); } } @@ -1209,7 +1204,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { } public void runLocked() { - if (Config.LOGD) Log.d(TAG, "NetworkThread starting"); + if (DEBUG) Log.d(TAG, "NetworkThread starting"); SntpClient client = new SntpClient(); GpsXtraDownloader xtraDownloader = null; @@ -1225,18 +1220,17 @@ public class GpsLocationProvider extends ILocationProvider.Stub { synchronized (this) { try { if (!mNetworkAvailable) { - if (Config.LOGD) Log.d(TAG, - "NetworkThread wait for network"); + if (DEBUG) Log.d(TAG, "NetworkThread wait for network"); wait(); } else if (waitTime > 0) { - if (Config.LOGD) { + if (DEBUG) { Log.d(TAG, "NetworkThread wait for " + waitTime + "ms"); } wait(waitTime); } } catch (InterruptedException e) { - if (Config.LOGD) { + if (DEBUG) { Log.d(TAG, "InterruptedException in GpsNetworkThread"); } } @@ -1245,12 +1239,11 @@ public class GpsLocationProvider extends ILocationProvider.Stub { } while (!mDone && ((!mXtraDownloadRequested && !mTimeInjectRequested && waitTime > 0) || !mNetworkAvailable)); - if (Config.LOGD) Log.d(TAG, "NetworkThread out of wake loop"); - + if (DEBUG) Log.d(TAG, "NetworkThread out of wake loop"); if (!mDone) { if (mNtpServer != null && (mTimeInjectRequested || mNextNtpTime <= System.currentTimeMillis())) { - if (Config.LOGD) { + if (DEBUG) { Log.d(TAG, "Requesting time from NTP server " + mNtpServer); } mTimeInjectRequested = false; @@ -1279,7 +1272,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { } mNextNtpTime = now + NTP_INTERVAL; } else { - if (Config.LOGD) Log.d(TAG, "requestTime failed"); + if (DEBUG) Log.d(TAG, "requestTime failed"); mNextNtpTime = System.currentTimeMillis() + RETRY_INTERVAL; } } @@ -1290,7 +1283,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { mXtraDownloadRequested = false; byte[] data = xtraDownloader.downloadXtraData(); if (data != null) { - if (Config.LOGD) { + if (DEBUG) { Log.d(TAG, "calling native_inject_xtra_data"); } native_inject_xtra_data(data, data.length); @@ -1301,7 +1294,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { } } } - if (Config.LOGD) Log.d(TAG, "NetworkThread exiting"); + if (DEBUG) Log.d(TAG, "NetworkThread exiting"); } synchronized void xtraDownloadRequest() { @@ -1319,7 +1312,7 @@ public class GpsLocationProvider extends ILocationProvider.Stub { } synchronized void setDone() { - if (Config.LOGD) Log.d(TAG, "stopping NetworkThread"); + if (DEBUG) Log.d(TAG, "stopping NetworkThread"); mDone = true; notify(); } diff --git a/location/java/com/android/internal/location/GpsXtraDownloader.java b/location/java/com/android/internal/location/GpsXtraDownloader.java index 33ebce7..02a9f48 100644 --- a/location/java/com/android/internal/location/GpsXtraDownloader.java +++ b/location/java/com/android/internal/location/GpsXtraDownloader.java @@ -32,10 +32,12 @@ import java.util.Random; import android.content.Context; import android.net.Proxy; -import android.net.http.AndroidHttpClient; import android.util.Config; import android.util.Log; +import com.android.common.AndroidHttpClient; + + /** * A class for downloading GPS XTRA data. * @@ -169,4 +171,3 @@ public class GpsXtraDownloader { } } - diff --git a/location/java/com/android/internal/location/LocationProviderProxy.java b/location/java/com/android/internal/location/LocationProviderProxy.java index 89337b3..2e0be89 100644 --- a/location/java/com/android/internal/location/LocationProviderProxy.java +++ b/location/java/com/android/internal/location/LocationProviderProxy.java @@ -40,6 +40,7 @@ public class LocationProviderProxy implements IBinder.DeathRecipient { private final String mName; private final ILocationProvider mProvider; private boolean mLocationTracking = false; + private boolean mEnabled = false; private long mMinTime = 0; private boolean mDead; @@ -152,6 +153,7 @@ public class LocationProviderProxy implements IBinder.DeathRecipient { public void enable() { try { mProvider.enable(); + mEnabled = true; } catch (RemoteException e) { Log.e(TAG, "enable failed", e); } @@ -160,18 +162,14 @@ public class LocationProviderProxy implements IBinder.DeathRecipient { public void disable() { try { mProvider.disable(); + mEnabled = false; } catch (RemoteException e) { Log.e(TAG, "disable failed", e); } } public boolean isEnabled() { - try { - return mProvider.isEnabled(); - } catch (RemoteException e) { - Log.e(TAG, "isEnabled failed", e); - return false; - } + return mEnabled; } public int getStatus(Bundle extras) { diff --git a/location/java/com/android/internal/location/MockProvider.java b/location/java/com/android/internal/location/MockProvider.java index 2614f82..7d9e86c 100644 --- a/location/java/com/android/internal/location/MockProvider.java +++ b/location/java/com/android/internal/location/MockProvider.java @@ -95,10 +95,6 @@ public class MockProvider extends ILocationProvider.Stub { return mStatusUpdateTime; } - public boolean isEnabled() { - return mEnabled; - } - public int getAccuracy() { return mAccuracy; } diff --git a/location/tests/locationtests/Android.mk b/location/tests/locationtests/Android.mk new file mode 100644 index 0000000..902cd96 --- /dev/null +++ b/location/tests/locationtests/Android.mk @@ -0,0 +1,14 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +# We only want this apk build for tests. +LOCAL_MODULE_TAGS := tests + +# Include all test java files. +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_JAVA_LIBRARIES := android.test.runner +LOCAL_PACKAGE_NAME := FrameworksLocationTests + +include $(BUILD_PACKAGE) + diff --git a/location/tests/locationtests/AndroidManifest.xml b/location/tests/locationtests/AndroidManifest.xml new file mode 100644 index 0000000..1d9df0f --- /dev/null +++ b/location/tests/locationtests/AndroidManifest.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2008 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.frameworks.locationtests"> + + <!-- location test permissions --> + <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> + <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/> + <uses-permission android:name="android.permission.WRITE_SETTINGS"/> + <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/> + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> + + <application> + <uses-library android:name="android.test.runner" /> + </application> + + <instrumentation + android:name="android.test.InstrumentationTestRunner" + android:targetPackage="com.android.frameworks.locationtests" + android:label="Frameworks Location Tests" /> +</manifest> diff --git a/location/tests/locationtests/src/android/location/GeocoderTest.java b/location/tests/locationtests/src/android/location/GeocoderTest.java new file mode 100644 index 0000000..8a13a24 --- /dev/null +++ b/location/tests/locationtests/src/android/location/GeocoderTest.java @@ -0,0 +1,64 @@ +package android.location; + +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import android.location.Address; +import android.location.Geocoder; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.Suppress; +import android.util.Log; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Locale; +import java.util.Set; +import java.util.List; + +@Suppress +public class GeocoderTest extends AndroidTestCase { + + public void testGeocoder() throws Exception { + Locale locale = new Locale("en", "us"); + Geocoder g = new Geocoder(mContext, locale); + + List<Address> addresses1 = g.getFromLocation(37.435067, -122.166767, 2); + assertNotNull(addresses1); + assertEquals(1, addresses1.size()); + + Address addr = addresses1.get(0); + assertEquals("94305", addr.getFeatureName()); + assertEquals("Palo Alto, CA 94305", addr.getAddressLine(0)); + assertEquals("USA", addr.getAddressLine(1)); + assertEquals("94305", addr.getPostalCode()); + assertFalse(Math.abs(addr.getLatitude() - 37.4240385) > 0.1); + + List<Address> addresses2 = g.getFromLocationName("San Francisco, CA", 1); + assertNotNull(addresses2); + assertEquals(1, addresses2.size()); + + addr = addresses2.get(0); + assertEquals("San Francisco", addr.getFeatureName()); + assertEquals("San Francisco, CA", addr.getAddressLine(0)); + assertEquals("United States", addr.getAddressLine(1)); + assertEquals("San Francisco", addr.getLocality()); + assertEquals("CA", addr.getAdminArea()); + assertEquals(null, addr.getPostalCode()); + + assertFalse(Math.abs(addr.getLatitude() - 37.77916) > 0.1); + + } +} diff --git a/location/tests/locationtests/src/android/location/LocationManagerTest.java b/location/tests/locationtests/src/android/location/LocationManagerTest.java new file mode 100644 index 0000000..0b8e61d --- /dev/null +++ b/location/tests/locationtests/src/android/location/LocationManagerTest.java @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.location; + +import android.content.Context; +import android.location.Criteria; +import android.location.Location; +import android.location.LocationManager; +import android.location.LocationProvider; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.Suppress; +import android.util.Log; + +@Suppress +public class LocationManagerTest extends AndroidTestCase { + private static final String LOG_TAG = "LocationManagerTest"; + + private LocationManager manager; + + @Override + public void setUp() throws Exception { + super.setUp(); + manager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); + assertNotNull(manager); + } + + public void testGetBogusProvider() { + LocationProvider p = manager.getProvider("bogus"); + assertNull(p); + } + + public void testGetNetworkProvider() { + LocationProvider p = manager.getProvider("network"); + assertNotNull(p); + } + + public void testGetGpsProvider() { + LocationProvider p = manager.getProvider("gps"); + assertNotNull(p); + } + + public void testGetBestProviderEmptyCriteria() { + String p = manager.getBestProvider(new Criteria(), true); + assertNotNull(p); + } + + public void testGetBestProviderPowerCriteria() { + Criteria c = new Criteria(); + c.setPowerRequirement(Criteria.POWER_HIGH); + String p = manager.getBestProvider(c, true); + assertNotNull(p); + + c.setPowerRequirement(Criteria.POWER_MEDIUM); + p = manager.getBestProvider(c, true); + assertNotNull(p); + + c.setPowerRequirement(Criteria.POWER_LOW); + p = manager.getBestProvider(c, true); + assertNotNull(p); + + c.setPowerRequirement(Criteria.NO_REQUIREMENT); + p = manager.getBestProvider(c, true); + assertNotNull(p); + } + + public void testGpsTracklog() { + LocationProvider p = manager.getProvider("gps"); + assertNotNull(p); + + // TODO: test requestUpdates method + } + + public void testLocationConversions() { + String loc1 = Location.convert(-80.075, Location.FORMAT_DEGREES); + Log.i(LOG_TAG, "Input = " + (-80.075) + ", output = " + loc1); + assertEquals("-80.075", loc1); + + String loc1b = Location.convert(-80.0, Location.FORMAT_DEGREES); + Log.i(LOG_TAG, "Input = " + (-80.0) + ", output = " + loc1b); + assertEquals("-80", loc1b); + + String loc2 = Location.convert(-80.085, Location.FORMAT_DEGREES); + Log.i(LOG_TAG, "Input = " + (-80.085) + ", output = " + loc2); + assertEquals("-80.085", loc2); + + String loc3 = Location.convert(-80.085, Location.FORMAT_MINUTES); + Log.i(LOG_TAG, "Input = " + (-80.085) + ", output = " + loc3); + assertEquals("-80:5.1", loc3); + + String loc4 = Location.convert(-80.085, Location.FORMAT_SECONDS); + Log.i(LOG_TAG, "Input = " + (-80.085) + ", output = " + loc4); + assertEquals("-80:5:6", loc4); + + String loc5 = Location.convert(5 + 0.5f / 60.0f, Location.FORMAT_MINUTES); + Log.i(LOG_TAG, "Input = 5:0.5, output = " + loc5); + int index = loc5.indexOf(':'); + String loc5a = loc5.substring(0, index); + Log.i(LOG_TAG, "loc5a = " + loc5a); + assertTrue(loc5a.equals("5")); + String loc5b = loc5.substring(index + 1); + Log.i(LOG_TAG, "loc5b = " + loc5b); + double minutes = Double.parseDouble(loc5b); + Log.i(LOG_TAG, "minutes = " + minutes); + assertTrue(Math.abs(minutes - 0.5) < 0.0001); + + String loc6 = Location.convert(0.1, Location.FORMAT_DEGREES); + Log.i(LOG_TAG, "loc6 = " + loc6); + assertEquals(loc6, "0.1"); + + String loc7 = Location.convert(0.1, Location.FORMAT_MINUTES); + Log.i(LOG_TAG, "loc7 = " + loc7); + assertEquals(loc7, "0:6"); + + String loc8 = Location.convert(0.1, Location.FORMAT_SECONDS); + Log.i(LOG_TAG, "loc8 = " + loc8); + assertEquals(loc8, "0:6:0"); + } +} diff --git a/location/tests/locationtests/src/android/location/LocationTest.java b/location/tests/locationtests/src/android/location/LocationTest.java new file mode 100644 index 0000000..847ac7a --- /dev/null +++ b/location/tests/locationtests/src/android/location/LocationTest.java @@ -0,0 +1,230 @@ +/* + * Copyright (C) 2007 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.location; + +import android.test.suitebuilder.annotation.SmallTest; + +import junit.framework.TestCase; + +/** + * Unit tests for android.location.Location + */ +@SmallTest +public class LocationTest extends TestCase { + + // ***** Tests for Location.convert + public void testConvert_DegreesToDouble(){ + String testDegreesCoord = "-80.075"; + String message; + double result; + + result = Location.convert(testDegreesCoord); + message = "degreesToDoubleTest: Double should be -80.075, actual value is " + + String.valueOf(result); + assertEquals(message, -80.075, result); + } + + public void testConvert_MinutesToDouble(){ + String testMinutesCoord = "-80:05.10000"; + String message; + double result; + + result = Location.convert(testMinutesCoord); + message = "minutesToDoubleTest: Double should be -80.085, actual value is " + + String.valueOf(result); + assertEquals(message, -80.085, result); + } + + public void testConvert_SecondsToDouble(){ + String testSecondsCoord = "-80:04:03.00000"; + String message; + double result; + + result = Location.convert(testSecondsCoord); + message = "secondsToDoubleTest: Double should be -80.0675, actual value is " + + String.valueOf(result); + assertEquals(message, -80.0675, result); + } + + public void testConvert_SecondsToDouble2(){ + String testSecondsCoord = "-80:4:3"; + String message; + double result; + + result = Location.convert(testSecondsCoord); + message = "secondsToDouble2Test: Double should be -80.0675, actual value is " + + String.valueOf(result); + assertEquals(message, -80.0675, result); + } + + // Testing the Convert(Double, Int) + public void testConvert_CoordinateToDegrees(){ + String message; + String result; + + result = Location.convert(-80.075, Location.FORMAT_DEGREES); + message = "coordinateToDegreesTest: Should return a string -80.075, but returned " + result; + assertEquals(message, "-80.075", result); + } + + public void testConvert_CoordinateToDegrees2(){ + String message; + String result; + result = Location.convert(-80.0, Location.FORMAT_DEGREES); + message = "coordinateToDegrees2Test: Should return a string -80, but returned " + result; + assertEquals(message, "-80", result); + } + + public void testConvert_CoordinateToMinutes(){ + String message; + String result; + double input = -80.085; + result = Location.convert(input, Location.FORMAT_MINUTES); + message = "coordinateToMinuteTest: Should return a string -80:5.1, but returned " + + result; + assertEquals(message, "-80:5.1", result); + } + + public void testConvert_CoordinateToMinutes2(){ + String message; + String result; + double input = -80; + result = Location.convert(input, Location.FORMAT_MINUTES); + message = "coordinateToMinute2Test: Should return a string -80:0, but returned " + + result; + assertEquals(message, "-80:0", result); + } + + public void testConvert_CoordinateToSeconds(){ + String message; + String result; + + result = Location.convert(-80.075, Location.FORMAT_SECONDS); + message = "coordinateToSecondsTest: Should return a string -80:4:30, but returned " + + result; + assertEquals(message, "-80:4:30", result); + } + // **** end tests for Location.convert + + + public void testBearingTo(){ + String message; + float bearing; + Location zeroLocation = new Location(""); + zeroLocation.setLatitude(0); + zeroLocation.setLongitude(0); + + Location testLocation = new Location(""); + testLocation.setLatitude(1000000); + testLocation.setLongitude(0); + + bearing = zeroLocation.bearingTo(zeroLocation); + message = "bearingToTest: Bearing should be 0, actual value is " + String.valueOf(bearing); + assertEquals(message, 0, bearing, 0); + + bearing = zeroLocation.bearingTo(testLocation); + message = "bearingToTest: Bearing should be 180, actual value is " + + String.valueOf(bearing); + assertEquals(message, 180, bearing, 0); + + testLocation.setLatitude(0); + testLocation.setLongitude(1000000); + bearing = zeroLocation.bearingTo(testLocation); + message = "bearingToTest: Bearing should be -90, actual value is " + + String.valueOf(bearing); + assertEquals(message, -90, bearing, 0); + + //TODO: Test a Random Middle Value + } + + public void testDistanceTo() { + String message; + boolean result = true; + float distance; + Location zeroLocation = new Location(""); + zeroLocation.setLatitude(0); + zeroLocation.setLongitude(0); + + Location testLocation = new Location(""); + testLocation.setLatitude(1000000); + testLocation.setLongitude(0); + + distance = zeroLocation.distanceTo(zeroLocation); + message = "distanceToTest: Distance should be 0, actual value is " + + String.valueOf(distance); + assertEquals(message, distance, 0, 0); + + distance = zeroLocation.distanceTo(testLocation); + message = "distanceToTest: Distance should be 8885140, actual value is " + + String.valueOf(distance); + assertEquals(message, distance, 8885140.0, 1); + } + + public void testAltitude() { + String message; + Location loc = new Location(""); + + loc.setAltitude(1); + message = "altitudeTest: set/getAltitude to 1 didn't work."; + assertEquals(message, loc.getAltitude(), 1, 0); + message = "altitudeTest: hasAltitude (a) didn't work."; + assertTrue(message, loc.hasAltitude()); + + loc.removeAltitude(); + message = "altitudeTest: hasAltitude (b) didn't work."; + assertFalse(message, loc.hasAltitude()); + message = "altitudeTest: getAltitude didn't return 0 when there was no altitude."; + assertEquals(message, loc.getAltitude(), 0, 0); + } + + public void testSpeed() { + String message; + Location loc = new Location(""); + + loc.setSpeed(1); + message = "speedTest: set/getSpeed to 1 didn't work."; + assertEquals(message, loc.getSpeed(), 1, 0); + message = "speedTest: hasSpeed (a) didn't work."; + assertTrue(message, loc.hasSpeed()); + + loc.removeSpeed(); + message = "speedTest: hasSpeed (b) didn't work."; + assertFalse(message, loc.hasSpeed()); + message = "speedTest: getSpeed didn't return 0 when there was no speed."; + assertEquals(message, loc.getSpeed(), 0, 0); + } + + public void testBearing() { + String message; + Location loc = new Location(""); + + loc.setBearing(1); + message = "bearingTest: set/getBearing to 1 didn't work."; + assertEquals(message, loc.getBearing(), 1, 0); + message = "bearingTest: hasBearing (a) didn't work."; + assertTrue(message, loc.hasBearing()); + + loc.removeBearing(); + message = "bearingTest: hasBearing (b) didn't work."; + assertFalse(message, loc.hasBearing()); + message = "bearingTest: getBearing didn't return 0 when there was no bearing."; + assertEquals(message, loc.getBearing(), 0, 0); + } + +} + + |