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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.components.location;
6
7 import android.Manifest;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.pm.PackageManager;
11 import android.os.Process;
12 import android.provider.Settings;
13
14 import org.chromium.base.ThreadUtils;
15 import org.chromium.base.VisibleForTesting;
16 import org.chromium.base.annotations.SuppressFBWarnings;
17
18 /**
19 * Provides methods for querying Chrome's ability to use Android's location serv ices.
20 *
21 * This class should be used only on the UI thread.
22 */
23 public class LocationUtils {
24 private static LocationUtils sInstance;
25
26 protected LocationUtils() {}
27
28 /**
29 * Returns the singleton instance of LocationSettings, creating it if needed .
30 */
31 @SuppressFBWarnings("LI_LAZY_INIT_STATIC")
32 public static LocationUtils getInstance() {
33 ThreadUtils.assertOnUiThread();
34 if (sInstance == null) {
35 sInstance = new LocationUtils();
36 }
37 return sInstance;
38 }
39
40 private boolean hasPermission(Context context, String name) {
41 return context.checkPermission(name, Process.myPid(), Process.myUid())
42 == PackageManager.PERMISSION_GRANTED;
43 }
44
45 /**
46 * Returns true if Chromium has permission to access location.
47 *
48 * Check both chromeHasLocationPermission() and isSystemLocationSettingEnabl ed() to determine if
49 * Chromium's location requests will return results.
50 */
51 public boolean chromiumHasLocationPermission(Context context) {
52 return hasPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION )
53 || hasPermission(context, Manifest.permission.ACCESS_FINE_LOCATI ON);
54 }
55
56 /**
57 * Returns whether location services are enabled system-wide, i.e. whether a ny application is
58 * able to access location.
59 */
60 public boolean isSystemLocationSettingEnabled(Context context) {
61 return Settings.Secure.getInt(context.getContentResolver(), Settings.Sec ure.LOCATION_MODE,
62 Settings.Secure.LOCATION_MODE_OFF)
63 != Settings.Secure.LOCATION_MODE_OFF;
64 }
65
66 /**
67 * Returns an intent to launch Android Location Settings.
68 */
69 public Intent getSystemLocationSettingsIntent() {
70 Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
71 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
72 return i;
73 }
74
75 @VisibleForTesting
76 public static void setInstanceForTesting(LocationUtils instance) {
77 sInstance = instance;
78 }
79 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698