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 LocationProviderAdapter(Context context)29 private LocationProviderAdapter(Context context) { 30 mImpl = LocationProviderFactory.get(context); 31 } 32 33 @CalledByNative create(Context context)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 start(final boolean gpsEnabled)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 stop()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 */ isRunning()73 public boolean isRunning() { 74 assert ThreadUtils.runningOnUiThread(); 75 return mImpl.isRunning(); 76 } 77 newLocationAvailable(double latitude, double longitude, double timestamp, boolean hasAltitude, double altitude, boolean hasAccuracy, double accuracy, boolean hasHeading, double heading, boolean hasSpeed, double speed)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 newErrorAvailable(String message)87 public static void newErrorAvailable(String message) { 88 nativeNewErrorAvailable(message); 89 } 90 91 // Native functions nativeNewLocationAvailable( double latitude, double longitude, double timeStamp, boolean hasAltitude, double altitude, boolean hasAccuracy, double accuracy, boolean hasHeading, double heading, boolean hasSpeed, double speed)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); nativeNewErrorAvailable(String message)98 private static native void nativeNewErrorAvailable(String message); 99 } 100