Index: base/android/java/src/org/chromium/base/LocationUtils.java |
diff --git a/base/android/java/src/org/chromium/base/LocationUtils.java b/base/android/java/src/org/chromium/base/LocationUtils.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f332dee9aec8af8e519941fc1d4fd03de730fe62 |
--- /dev/null |
+++ b/base/android/java/src/org/chromium/base/LocationUtils.java |
@@ -0,0 +1,79 @@ |
+// Copyright 2016 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.base; |
+ |
+import android.Manifest; |
+import android.content.Context; |
+import android.content.Intent; |
+import android.content.pm.PackageManager; |
+import android.location.LocationManager; |
+import android.os.Process; |
+import android.provider.Settings; |
+ |
+import org.chromium.base.annotations.SuppressFBWarnings; |
+ |
+/** |
+ * Provides methods for querying Chrome's ability to use Android's location services. |
+ * |
+ * This class should be used only on the UI thread. |
+ */ |
+public class LocationUtils { |
+ private static LocationUtils sInstance; |
+ |
+ protected LocationUtils() {} |
+ |
+ /** |
+ * Returns the singleton instance of LocationSettings, creating it if needed. |
+ */ |
+ @SuppressFBWarnings("LI_LAZY_INIT_STATIC") |
+ public static LocationUtils getInstance() { |
+ ThreadUtils.assertOnUiThread(); |
+ if (sInstance == null) { |
+ sInstance = new LocationUtils(); |
+ } |
+ return sInstance; |
+ } |
+ |
+ private boolean hasPermission(Context context, String name) { |
+ return context.checkPermission(name, Process.myPid(), Process.myUid()) |
+ == PackageManager.PERMISSION_GRANTED; |
+ } |
+ |
+ /** |
+ * Returns true if Chromium has permission to access location. |
+ * |
+ * Check both chromeHasLocationPermission() and isSystemLocationSettingEnabled() to determine if |
+ * Chromium's location requests will return results. |
+ */ |
+ public boolean chromiumHasLocationPermission(Context context) { |
+ return hasPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) |
+ || hasPermission(context, Manifest.permission.ACCESS_FINE_LOCATION); |
+ } |
+ |
+ /** |
+ * Returns whether location services are enabled system-wide, i.e. whether any application is |
+ * able to access location. |
+ */ |
+ public boolean isSystemLocationSettingEnabled(Context context) { |
+ LocationManager locationManager = |
+ (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); |
+ return (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) |
Jeffrey Yasskin
2016/06/06 15:53:48
https://developer.android.com/reference/android/lo
Torne
2016/06/07 11:30:43
Should probably do what the documentation says.
Jeffrey Yasskin
2016/06/09 22:08:11
Done.
|
+ || locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)); |
+ } |
+ |
+ /** |
+ * Returns an intent to launch Android Location Settings. |
+ */ |
+ public Intent getSystemLocationSettingsIntent() { |
+ Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); |
+ i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
+ return i; |
+ } |
+ |
+ @VisibleForTesting |
+ public static void setInstanceForTesting(LocationUtils instance) { |
+ sInstance = instance; |
+ } |
+} |