Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Unified Diff: components/location/android/java/src/org/chromium/components/location/LocationUtils.java

Issue 2038753004: Add a LocationUtils class to give all Chromium Android code access to location helpers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: Use LOCATION_MODE instead of isProviderEnabled. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/location/android/java/src/org/chromium/components/location/LocationUtils.java
diff --git a/components/location/android/java/src/org/chromium/components/location/LocationUtils.java b/components/location/android/java/src/org/chromium/components/location/LocationUtils.java
new file mode 100644
index 0000000000000000000000000000000000000000..0bd813ba23ae2d82b1d1b4fb2c6aa29c8681ab42
--- /dev/null
+++ b/components/location/android/java/src/org/chromium/components/location/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.components.location;
+
+import android.Manifest;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.os.Process;
+import android.provider.Settings;
+
+import org.chromium.base.ThreadUtils;
+import org.chromium.base.VisibleForTesting;
+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) {
+ return Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
+ Settings.Secure.LOCATION_MODE_OFF)
+ != Settings.Secure.LOCATION_MODE_OFF;
+ }
+
+ /**
+ * 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;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698