LocationProviderAdapter.java revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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
5package org.chromium.content.browser;
6
7import android.content.Context;
8
9import com.google.common.annotations.VisibleForTesting;
10
11import org.chromium.base.CalledByNative;
12import org.chromium.base.ThreadUtils;
13
14import java.util.concurrent.FutureTask;
15
16/**
17 * Implements the Java side of LocationProviderAndroid.
18 * Delegates all real functionality to the implementation
19 * returned from LocationProviderFactory.
20 * See detailed documentation on
21 * content/browser/geolocation/android_location_api_adapter.h.
22 * Based on android.webkit.GeolocationService.java
23 */
24@VisibleForTesting
25public class LocationProviderAdapter {
26
27    // Delegate handling the real work in the main thread.
28    private LocationProviderFactory.LocationProvider mImpl;
29
30    private LocationProviderAdapter(Context context) {
31        mImpl = LocationProviderFactory.get(context);
32    }
33
34    @CalledByNative
35    static LocationProviderAdapter create(Context context) {
36        return new LocationProviderAdapter(context);
37    }
38
39    /**
40     * Start listening for location updates until we're told to quit. May be
41     * called in any thread.
42     * @param gpsEnabled Whether or not we're interested in high accuracy GPS.
43     */
44    @CalledByNative
45    public boolean start(final boolean gpsEnabled) {
46        FutureTask<Void> task = new FutureTask<Void>(new Runnable() {
47            @Override
48            public void run() {
49                mImpl.start(gpsEnabled);
50            }
51        }, null);
52        ThreadUtils.runOnUiThread(task);
53        return true;
54    }
55
56    /**
57     * Stop listening for location updates. May be called in any thread.
58     */
59    @CalledByNative
60    public void stop() {
61        FutureTask<Void> task = new FutureTask<Void>(new Runnable() {
62            @Override
63            public void run() {
64                mImpl.stop();
65            }
66        }, null);
67        ThreadUtils.runOnUiThread(task);
68    }
69
70    /**
71     * Returns true if we are currently listening for location updates, false if not.
72     * Must be called only in the UI thread.
73     */
74    public boolean isRunning() {
75        assert ThreadUtils.runningOnUiThread();
76        return mImpl.isRunning();
77    }
78
79    public static void newLocationAvailable(double latitude, double longitude, double timestamp,
80            boolean hasAltitude, double altitude,
81            boolean hasAccuracy, double accuracy,
82            boolean hasHeading, double heading,
83            boolean hasSpeed, double speed) {
84        nativeNewLocationAvailable(latitude, longitude, timestamp, hasAltitude, altitude,
85                hasAccuracy, accuracy, hasHeading, heading, hasSpeed, speed);
86    }
87
88    public static void newErrorAvailable(String message) {
89        nativeNewErrorAvailable(message);
90    }
91
92    // Native functions
93    private static native void nativeNewLocationAvailable(
94            double latitude, double longitude, double timeStamp,
95            boolean hasAltitude, double altitude,
96            boolean hasAccuracy, double accuracy,
97            boolean hasHeading, double heading,
98            boolean hasSpeed, double speed);
99    private static native void nativeNewErrorAvailable(String message);
100}
101