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