| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.device.geolocation; | |
| 6 | |
| 7 import android.os.Handler; | |
| 8 import android.os.HandlerThread; | |
| 9 import android.os.Message; | |
| 10 | |
| 11 /** | |
| 12 * A mock location provider. When started, runs a background thread that periodi
cally | |
| 13 * posts location updates. This does not involve any system Location APIs and th
us | |
| 14 * does not require any special permissions in the test app or on the device. | |
| 15 */ | |
| 16 public class MockLocationProvider implements LocationProviderFactory.LocationPro
vider { | |
| 17 private boolean mIsRunning; | |
| 18 private Handler mHandler; | |
| 19 private HandlerThread mHandlerThread; | |
| 20 private final Object mLock = new Object(); | |
| 21 | |
| 22 private static final int UPDATE_LOCATION_MSG = 100; | |
| 23 | |
| 24 public MockLocationProvider() { | |
| 25 } | |
| 26 | |
| 27 public void stopUpdates() { | |
| 28 if (mHandlerThread != null) { | |
| 29 mHandlerThread.quit(); | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 @Override | |
| 34 public void start(boolean enableHighAccuracy) { | |
| 35 if (mIsRunning) return; | |
| 36 | |
| 37 if (mHandlerThread == null) { | |
| 38 startMockLocationProviderThread(); | |
| 39 } | |
| 40 | |
| 41 mIsRunning = true; | |
| 42 synchronized (mLock) { | |
| 43 mHandler.sendEmptyMessage(UPDATE_LOCATION_MSG); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 @Override | |
| 48 public void stop() { | |
| 49 if (!mIsRunning) return; | |
| 50 mIsRunning = false; | |
| 51 synchronized (mLock) { | |
| 52 mHandler.removeMessages(UPDATE_LOCATION_MSG); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 @Override | |
| 57 public boolean isRunning() { | |
| 58 return mIsRunning; | |
| 59 } | |
| 60 | |
| 61 private void startMockLocationProviderThread() { | |
| 62 assert mHandlerThread == null; | |
| 63 assert mHandler == null; | |
| 64 | |
| 65 mHandlerThread = new HandlerThread("MockLocationProviderImpl"); | |
| 66 mHandlerThread.start(); | |
| 67 mHandler = new Handler(mHandlerThread.getLooper()) { | |
| 68 @Override | |
| 69 public void handleMessage(Message msg) { | |
| 70 synchronized (mLock) { | |
| 71 if (msg.what == UPDATE_LOCATION_MSG) { | |
| 72 newLocation(); | |
| 73 sendEmptyMessageDelayed(UPDATE_LOCATION_MSG, 250); | |
| 74 } | |
| 75 } | |
| 76 } | |
| 77 }; | |
| 78 } | |
| 79 | |
| 80 private void newLocation() { | |
| 81 LocationProviderAdapter.newLocationAvailable( | |
| 82 0, 0, System.currentTimeMillis() / 1000.0, | |
| 83 false, 0, | |
| 84 true, 0.5, | |
| 85 false, 0, | |
| 86 false, 0); | |
| 87 } | |
| 88 }; | |
| 89 | |
| OLD | NEW |