2 * Copyright (C) 2009 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package android.webkit;
19 import android.app.ActivityThread;
20 import android.content.Context;
21 import android.location.Location;
22 import android.location.LocationListener;
23 import android.location.LocationManager;
24 import android.location.LocationProvider;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.webkit.WebView;
28 import android.webkit.WebViewCore;
32 * Implements the Java side of GeolocationServiceAndroid.
34 final class GeolocationService implements LocationListener {
37 private static final String TAG = "geolocationService";
39 private long mNativeObject;
40 private LocationManager mLocationManager;
41 private boolean mIsGpsEnabled;
42 private boolean mIsRunning;
43 private boolean mIsNetworkProviderAvailable;
44 private boolean mIsGpsProviderAvailable;
48 * @param nativeObject The native object to which this object will report position updates and
51 public GeolocationService(long nativeObject) {
52 mNativeObject = nativeObject;
53 // Register newLocationAvailable with platform service.
54 ActivityThread thread = ActivityThread.systemMain();
55 Context context = thread.getApplication();
56 mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
57 if (mLocationManager == null) {
58 Log.e(TAG, "Could not get location manager.");
63 * Start listening for location updates.
66 registerForLocationUpdates();
71 * Stop listening for location updates.
74 unregisterFromLocationUpdates();
79 * Sets whether to use the GPS.
80 * @param enable Whether to use the GPS.
82 public void setEnableGps(boolean enable) {
83 if (mIsGpsEnabled != enable) {
84 mIsGpsEnabled = enable;
86 // There's no way to unregister from a single provider, so we can
87 // only unregister from all, then reregister with all but the GPS.
88 unregisterFromLocationUpdates();
89 registerForLocationUpdates();
95 * LocationListener implementation.
96 * Called when the location has changed.
97 * @param location The new location, as a Location object.
99 public void onLocationChanged(Location location) {
100 // Callbacks from the system location sevice are queued to this thread, so it's possible
101 // that we receive callbacks after unregistering. At this point, the native object will no
104 nativeNewLocationAvailable(mNativeObject, location);
109 * LocationListener implementation.
110 * Called when the provider status changes.
111 * @param provider The name of the provider.
112 * @param status The new status of the provider.
113 * @param extras an optional Bundle with provider specific data.
115 public void onStatusChanged(String providerName, int status, Bundle extras) {
116 boolean isAvailable = (status == LocationProvider.AVAILABLE);
117 if (LocationManager.NETWORK_PROVIDER.equals(providerName)) {
118 mIsNetworkProviderAvailable = isAvailable;
119 } else if (LocationManager.GPS_PROVIDER.equals(providerName)) {
120 mIsGpsProviderAvailable = isAvailable;
122 maybeReportError("The last location provider is no longer available");
126 * LocationListener implementation.
127 * Called when the provider is enabled.
128 * @param provider The name of the location provider that is now enabled.
130 public void onProviderEnabled(String providerName) {
131 // No need to notify the native side. It's enough to start sending
132 // valid position fixes again.
133 if (LocationManager.NETWORK_PROVIDER.equals(providerName)) {
134 mIsNetworkProviderAvailable = true;
135 } else if (LocationManager.GPS_PROVIDER.equals(providerName)) {
136 mIsGpsProviderAvailable = true;
141 * LocationListener implementation.
142 * Called when the provider is disabled.
143 * @param provider The name of the location provider that is now disabled.
145 public void onProviderDisabled(String providerName) {
146 if (LocationManager.NETWORK_PROVIDER.equals(providerName)) {
147 mIsNetworkProviderAvailable = false;
148 } else if (LocationManager.GPS_PROVIDER.equals(providerName)) {
149 mIsGpsProviderAvailable = false;
151 maybeReportError("The last location provider was disabled");
155 * Registers this object with the location service.
157 private void registerForLocationUpdates() {
159 mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
160 mIsNetworkProviderAvailable = true;
162 mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
163 mIsGpsProviderAvailable = true;
165 } catch(SecurityException e) {
166 Log.e(TAG, "Caught security exception registering for location updates from system. " +
167 "This should only happen in DumpRenderTree.");
172 * Unregisters this object from the location service.
174 private void unregisterFromLocationUpdates() {
175 mLocationManager.removeUpdates(this);
179 * Reports an error if neither the network nor the GPS provider is available.
181 private void maybeReportError(String message) {
182 // Callbacks from the system location sevice are queued to this thread, so it's possible
183 // that we receive callbacks after unregistering. At this point, the native object will no
185 if (mIsRunning && !mIsNetworkProviderAvailable && !mIsGpsProviderAvailable) {
186 nativeNewErrorAvailable(mNativeObject, message);
191 private static native void nativeNewLocationAvailable(long nativeObject, Location location);
192 private static native void nativeNewErrorAvailable(long nativeObject, String message);