Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/LocationProvider.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/LocationProvider.java b/content/public/android/java/src/org/chromium/content/browser/LocationProvider.java |
| index afcc17a68e9bf6b0978667fe5526f91e9e334755..ad2373b3389dae1a35ef431472a52f8f8e09436f 100644 |
| --- a/content/public/android/java/src/org/chromium/content/browser/LocationProvider.java |
| +++ b/content/public/android/java/src/org/chromium/content/browser/LocationProvider.java |
| @@ -10,8 +10,13 @@ import android.location.Location; |
| import android.location.LocationListener; |
| import android.location.LocationManager; |
| import android.os.Bundle; |
| +import android.os.Handler; |
| +import android.os.HandlerThread; |
| +import android.os.Message; |
| import android.util.Log; |
| +import com.google.common.annotations.VisibleForTesting; |
| + |
| import org.chromium.base.ActivityStatus; |
| import org.chromium.base.CalledByNative; |
| import org.chromium.base.ThreadUtils; |
| @@ -26,74 +31,118 @@ import java.util.concurrent.FutureTask; |
| * content/browser/geolocation/android_location_api_adapter.h. |
| * Based on android.webkit.GeolocationService.java |
| */ |
| -class LocationProvider { |
| +public class LocationProvider { |
| // Log tag |
| private static final String TAG = "LocationProvider"; |
| + private static boolean sUseMockProvider; |
| + |
| + @VisibleForTesting |
| + public static void enableMockLocationProvider() { |
|
mkosiba (inactive)
2013/12/09 16:26:08
nit: I think it would be a bit cleaner if the Loca
|
| + sUseMockProvider = true; |
| + } |
| + |
| + private interface ContentLocationProvider { |
| + public void start(boolean gpsEnabled); |
| + public void stop(); |
| + public boolean isRunning(); |
| + } |
| + |
| + private static class MockLocationProviderImpl implements ContentLocationProvider { |
| + private boolean mIsRunning; |
| + private Handler mHandler; |
| + private static final Object mLock = new Object(); |
| + |
| + private static final int UPDATE_LOCATION = 100; |
| + |
| + public MockLocationProviderImpl() { |
| + HandlerThread handlerThread = new HandlerThread("MockLocationProviderImpl"); |
| + handlerThread.start(); |
| + mHandler = new Handler(handlerThread.getLooper()) { |
| + @Override |
| + public void handleMessage(Message msg) { |
| + synchronized(mLock) { |
| + if (msg.what == UPDATE_LOCATION) { |
| + newLocation(); |
| + sendEmptyMessageDelayed(UPDATE_LOCATION, 1000); |
| + } |
| + } |
| + } |
| + }; |
| + } |
| + |
| + @Override |
| + public void start(boolean gpsEnabled) { |
| + if (mIsRunning) return; |
| + mIsRunning = true; |
| + synchronized (mLock) { |
| + mHandler.sendEmptyMessage(UPDATE_LOCATION); |
| + } |
| + } |
| + |
| + @Override |
| + public void stop() { |
| + if (!mIsRunning) return; |
| + mIsRunning = false; |
| + synchronized (mLock) { |
| + mHandler.removeMessages(UPDATE_LOCATION); |
| + } |
| + } |
| + |
| + @Override |
| + public boolean isRunning() { |
| + return mIsRunning; |
| + } |
| + |
| + private void newLocation() { |
| + nativeNewLocationAvailable(0, 0, System.currentTimeMillis() / 1000.0, |
| + false, 0, |
| + true, 0.5, |
| + false, 0, |
| + false, 0); |
| + } |
| + }; |
| + |
| /** |
| * This is the core of android location provider. It is a separate class for clarity |
| * so that it can manage all processing completely in the UI thread. The container class |
| * ensures that the start/stop calls into this class are done in the UI thread. |
| */ |
| private static class LocationProviderImpl |
| - implements LocationListener, ActivityStatus.StateListener { |
| + implements LocationListener, ContentLocationProvider { |
| private Context mContext; |
| private LocationManager mLocationManager; |
| private boolean mIsRunning; |
| - private boolean mShouldRunAfterActivityResume; |
| - private boolean mIsGpsEnabled; |
| LocationProviderImpl(Context context) { |
| mContext = context; |
| } |
| - @Override |
| - public void onActivityStateChange(int state) { |
| - if (state == ActivityStatus.PAUSED) { |
| - mShouldRunAfterActivityResume |= mIsRunning; |
| - unregisterFromLocationUpdates(); |
| - } else if (state == ActivityStatus.RESUMED) { |
| - assert !mIsRunning; |
| - if (mShouldRunAfterActivityResume) { |
| - registerForLocationUpdates(); |
| - } |
| - } |
| - } |
| - |
| /** |
| * Start listening for location updates. |
| * @param gpsEnabled Whether or not we're interested in high accuracy GPS. |
| */ |
| - private void start(boolean gpsEnabled) { |
| - if (!mIsRunning && !mShouldRunAfterActivityResume) { |
| - // Currently idle so start listening to activity status changes. |
| - ActivityStatus.registerStateListener(this); |
| - } |
| - mIsGpsEnabled = gpsEnabled; |
| - |
| - if (ActivityStatus.getState() != ActivityStatus.RESUMED) { |
| - mShouldRunAfterActivityResume = true; |
| - } else { |
| - unregisterFromLocationUpdates(); |
| - registerForLocationUpdates(); |
| - } |
| + @Override |
| + public void start(boolean gpsEnabled) { |
| + unregisterFromLocationUpdates(); |
| + registerForLocationUpdates(gpsEnabled); |
| } |
| /** |
| * Stop listening for location updates. |
| */ |
| - private void stop() { |
| + @Override |
| + public void stop() { |
| unregisterFromLocationUpdates(); |
| - ActivityStatus.unregisterStateListener(this); |
| - mShouldRunAfterActivityResume = false; |
| } |
| /** |
| * Returns true if we are currently listening for location updates, false if not. |
| */ |
| - private boolean isRunning() { |
| + @Override |
| + public boolean isRunning() { |
| return mIsRunning; |
| } |
| @@ -140,7 +189,7 @@ class LocationProvider { |
| /** |
| * Registers this object with the location service. |
| */ |
| - private void registerForLocationUpdates() { |
| + private void registerForLocationUpdates(boolean isGpsEnabled) { |
| ensureLocationManagerCreated(); |
| if (usePassiveOneShotLocation()) return; |
| @@ -153,7 +202,7 @@ class LocationProvider { |
| Criteria criteria = new Criteria(); |
| mLocationManager.requestLocationUpdates(0, 0, criteria, this, |
| ThreadUtils.getUiThreadLooper()); |
| - if (mIsGpsEnabled) { |
| + if (isGpsEnabled) { |
| criteria.setAccuracy(Criteria.ACCURACY_FINE); |
| mLocationManager.requestLocationUpdates(0, 0, criteria, this, |
| ThreadUtils.getUiThreadLooper()); |
| @@ -207,10 +256,14 @@ class LocationProvider { |
| } |
| // Delegate handling the real work in the main thread. |
| - private LocationProviderImpl mImpl; |
| + private ContentLocationProvider mImpl; |
| private LocationProvider(Context context) { |
| - mImpl = new LocationProviderImpl(context); |
| + if (sUseMockProvider) { |
| + mImpl = new MockLocationProviderImpl(); |
| + } else { |
| + mImpl = new LocationProviderImpl(context); |
| + } |
| } |
| @CalledByNative |