summaryrefslogtreecommitdiffstats
path: root/content/public/android/java/src/org/chromium/content/browser/LocationProviderAdapter.java
blob: 3fcc675116dca911f8a60d619d9e32f4a0645355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.content.browser;

import android.content.Context;

import org.chromium.base.ThreadUtils;
import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.MainDex;

import java.util.concurrent.FutureTask;

/**
 * Implements the Java side of LocationProviderAndroid.
 * Delegates all real functionality to the implementation
 * returned from LocationProviderFactory.
 * See detailed documentation on
 * content/browser/geolocation/android_location_api_adapter.h.
 * Based on android.webkit.GeolocationService.java
 */
@MainDex
@VisibleForTesting
public class LocationProviderAdapter {

    // Delegate handling the real work in the main thread.
    private LocationProviderFactory.LocationProvider mImpl;

    private LocationProviderAdapter(Context context) {
        mImpl = LocationProviderFactory.get(context);
    }

    @CalledByNative
    static LocationProviderAdapter create(Context context) {
        return new LocationProviderAdapter(context);
    }

    /**
     * Start listening for location updates until we're told to quit. May be
     * called in any thread.
     * @param gpsEnabled Whether or not we're interested in high accuracy GPS.
     */
    @CalledByNative
    public boolean start(final boolean gpsEnabled) {
        FutureTask<Void> task = new FutureTask<Void>(new Runnable() {
            @Override
            public void run() {
                mImpl.start(gpsEnabled);
            }
        }, null);
        ThreadUtils.runOnUiThread(task);
        return true;
    }

    /**
     * Stop listening for location updates. May be called in any thread.
     */
    @CalledByNative
    public void stop() {
        FutureTask<Void> task = new FutureTask<Void>(new Runnable() {
            @Override
            public void run() {
                mImpl.stop();
            }
        }, null);
        ThreadUtils.runOnUiThread(task);
    }

    /**
     * Returns true if we are currently listening for location updates, false if not.
     * Must be called only in the UI thread.
     */
    public boolean isRunning() {
        assert ThreadUtils.runningOnUiThread();
        return mImpl.isRunning();
    }

    public static void newLocationAvailable(double latitude, double longitude, double timestamp,
            boolean hasAltitude, double altitude,
            boolean hasAccuracy, double accuracy,
            boolean hasHeading, double heading,
            boolean hasSpeed, double speed) {
        nativeNewLocationAvailable(latitude, longitude, timestamp, hasAltitude, altitude,
                hasAccuracy, accuracy, hasHeading, heading, hasSpeed, speed);
    }

    public static void newErrorAvailable(String message) {
        nativeNewErrorAvailable(message);
    }

    // Native functions
    private static native void nativeNewLocationAvailable(
            double latitude, double longitude, double timeStamp,
            boolean hasAltitude, double altitude,
            boolean hasAccuracy, double accuracy,
            boolean hasHeading, double heading,
            boolean hasSpeed, double speed);
    private static native void nativeNewErrorAvailable(String message);
}