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