Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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.LocationProvider; | |
| 12 import org.chromium.content.browser.LocationProviderFactory; | |
| 13 | |
| 14 public class MockLocationProvider implements LocationProviderFactory.LocationPro viderImpl { | |
| 15 private boolean mIsRunning; | |
| 16 private Handler mHandler; | |
| 17 private HandlerThread mHandlerThread; | |
| 18 private static final Object mLock = new Object(); | |
| 19 | |
| 20 private static final int UPDATE_LOCATION = 100; | |
| 21 | |
| 22 public MockLocationProvider() { | |
| 23 } | |
| 24 | |
| 25 public void stopUpdates() { | |
| 26 if (mHandlerThread != null) { | |
| 27 mHandlerThread.quit(); | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 @Override | |
| 32 public void start(boolean gpsEnabled) { | |
| 33 if (mHandlerThread == null) { | |
| 34 startMockLocationProviderThread(); | |
| 35 } | |
| 36 | |
| 37 if (mIsRunning) return; | |
|
bulach
2013/12/11 15:48:39
nit: would be clearer to move this check first.
benm (inactive)
2013/12/11 20:08:51
Done.
| |
| 38 mIsRunning = true; | |
| 39 synchronized (mLock) { | |
| 40 mHandler.sendEmptyMessage(UPDATE_LOCATION); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 @Override | |
| 45 public void stop() { | |
| 46 if (!mIsRunning) return; | |
| 47 mIsRunning = false; | |
| 48 synchronized (mLock) { | |
| 49 mHandler.removeMessages(UPDATE_LOCATION); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 @Override | |
| 54 public boolean isRunning() { | |
| 55 return mIsRunning; | |
| 56 } | |
| 57 | |
| 58 private void startMockLocationProviderThread() { | |
| 59 assert mHandlerThread == null; | |
| 60 assert mHandler == null; | |
| 61 | |
| 62 mHandlerThread = new HandlerThread("MockLocationProviderImpl"); | |
| 63 mHandlerThread.start(); | |
| 64 mHandler = new Handler(mHandlerThread.getLooper()) { | |
| 65 @Override | |
| 66 public void handleMessage(Message msg) { | |
| 67 synchronized(mLock) { | |
| 68 if (msg.what == UPDATE_LOCATION) { | |
| 69 newLocation(); | |
| 70 sendEmptyMessageDelayed(UPDATE_LOCATION, 250); | |
| 71 } | |
| 72 } | |
| 73 } | |
| 74 }; | |
| 75 } | |
| 76 | |
| 77 private void newLocation() { | |
| 78 LocationProvider.nativeNewLocationAvailable( | |
| 79 0, 0, System.currentTimeMillis() / 1000.0, | |
| 80 false, 0, | |
| 81 true, 0.5, | |
| 82 false, 0, | |
| 83 false, 0); | |
| 84 } | |
| 85 }; | |
| 86 | |
| OLD | NEW |