Chromium Code Reviews| Index: device/geolocation/android/java/src/org/chromium/device/geolocation/LocationProviderAndroid.java |
| diff --git a/device/geolocation/android/java/src/org/chromium/device/geolocation/LocationProviderAndroid.java b/device/geolocation/android/java/src/org/chromium/device/geolocation/LocationProviderAndroid.java |
| index 261ce9dfd3c147140266a3e244010aec30152027..6c45be9b48a5f36d8e2dcc959e6ddd233b3e070c 100644 |
| --- a/device/geolocation/android/java/src/org/chromium/device/geolocation/LocationProviderAndroid.java |
| +++ b/device/geolocation/android/java/src/org/chromium/device/geolocation/LocationProviderAndroid.java |
| @@ -13,6 +13,7 @@ import android.os.Bundle; |
| import org.chromium.base.Log; |
| import org.chromium.base.ThreadUtils; |
| +import org.chromium.base.VisibleForTesting; |
| import java.util.List; |
| @@ -37,17 +38,20 @@ public class LocationProviderAndroid |
| @Override |
| public void start(boolean enableHighAccuracy) { |
| + ThreadUtils.assertOnUiThread(); |
| unregisterFromLocationUpdates(); |
| registerForLocationUpdates(enableHighAccuracy); |
| } |
| @Override |
| public void stop() { |
| + ThreadUtils.assertOnUiThread(); |
| unregisterFromLocationUpdates(); |
| } |
| @Override |
| public boolean isRunning() { |
| + assert ThreadUtils.runningOnUiThread(); |
|
Reilly Grant (use Gerrit)
2017/04/20 22:35:37
ThreadUtils.assertOnUiThread()?
mcasas
2017/04/20 22:55:35
Done.
|
| return mIsRunning; |
| } |
| @@ -57,7 +61,7 @@ public class LocationProviderAndroid |
| // possible that we receive callbacks after unregistering. At this point, the |
| // native object will no longer exist. |
| if (mIsRunning) { |
| - updateNewLocation(location); |
| + LocationProviderAdapter.onNewLocationAvailable(location); |
| } |
| } |
| @@ -70,7 +74,12 @@ public class LocationProviderAndroid |
| @Override |
| public void onProviderDisabled(String provider) {} |
| - private void ensureLocationManagerCreated() { |
| + @VisibleForTesting |
| + public void setLocationManagerForTesting(LocationManager manager) { |
| + mLocationManager = manager; |
| + } |
| + |
| + private void createLocationManagerIfNeeded() { |
| if (mLocationManager != null) return; |
| mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
| if (mLocationManager == null) { |
| @@ -82,7 +91,7 @@ public class LocationProviderAndroid |
| * Registers this object with the location service. |
| */ |
| private void registerForLocationUpdates(boolean enableHighAccuracy) { |
| - ensureLocationManagerCreated(); |
| + createLocationManagerIfNeeded(); |
| if (usePassiveOneShotLocation()) return; |
| assert !mIsRunning; |
| @@ -103,8 +112,8 @@ public class LocationProviderAndroid |
| unregisterFromLocationUpdates(); |
| // Propagate an error to JavaScript, this can happen in case of WebView |
| // when the embedding app does not have sufficient permissions. |
| - LocationProviderAdapter.newErrorAvailable("application does not have sufficient " |
| - + "geolocation permissions."); |
| + LocationProviderAdapter.newErrorAvailable( |
| + "application does not have sufficient geolocation permissions."); |
| } catch (IllegalArgumentException e) { |
| Log.e(TAG, "Caught IllegalArgumentException registering for location updates."); |
| unregisterFromLocationUpdates(); |
| @@ -116,33 +125,26 @@ public class LocationProviderAndroid |
| * Unregisters this object from the location service. |
| */ |
| private void unregisterFromLocationUpdates() { |
| - if (mIsRunning) { |
| - mIsRunning = false; |
| - mLocationManager.removeUpdates(this); |
| - } |
| - } |
| - |
| - private void updateNewLocation(Location location) { |
| - LocationProviderAdapter.newLocationAvailable(location.getLatitude(), |
| - location.getLongitude(), location.getTime() / 1000.0, location.hasAltitude(), |
| - location.getAltitude(), location.hasAccuracy(), location.getAccuracy(), |
| - location.hasBearing(), location.getBearing(), location.hasSpeed(), |
| - location.getSpeed()); |
| + if (!mIsRunning) return; |
| + mIsRunning = false; |
| + mLocationManager.removeUpdates(this); |
| } |
| private boolean usePassiveOneShotLocation() { |
| - if (!isOnlyPassiveLocationProviderEnabled()) return false; |
| + if (!isOnlyPassiveLocationProviderEnabled()) { |
| + return false; |
| + } |
| // Do not request a location update if the only available location provider is |
| // the passive one. Make use of the last known location and call |
| - // onLocationChanged directly. |
| + // onNewLocationAvailable directly. |
| final Location location = |
| mLocationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); |
| if (location != null) { |
| ThreadUtils.runOnUiThread(new Runnable() { |
| @Override |
| public void run() { |
| - updateNewLocation(location); |
| + LocationProviderAdapter.onNewLocationAvailable(location); |
| } |
| }); |
| } |
| @@ -154,7 +156,7 @@ public class LocationProviderAndroid |
| * in the system. |
| */ |
| private boolean isOnlyPassiveLocationProviderEnabled() { |
| - List<String> providers = mLocationManager.getProviders(true); |
| + final List<String> providers = mLocationManager.getProviders(true); |
| return providers != null && providers.size() == 1 |
| && providers.get(0).equals(LocationManager.PASSIVE_PROVIDER); |
| } |