OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.preferences; | 5 package org.chromium.chrome.browser.preferences; |
6 | 6 |
7 import android.Manifest; | 7 import android.Manifest; |
8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.content.Intent; |
| 10 import android.location.LocationManager; |
| 11 import android.provider.Settings; |
9 | 12 |
10 import org.chromium.base.ContextUtils; | 13 import org.chromium.base.ContextUtils; |
11 import org.chromium.base.ThreadUtils; | 14 import org.chromium.base.ThreadUtils; |
12 import org.chromium.base.VisibleForTesting; | 15 import org.chromium.base.VisibleForTesting; |
13 import org.chromium.base.annotations.CalledByNative; | 16 import org.chromium.base.annotations.CalledByNative; |
14 import org.chromium.base.annotations.SuppressFBWarnings; | 17 import org.chromium.base.annotations.SuppressFBWarnings; |
15 import org.chromium.chrome.browser.ChromeApplication; | 18 import org.chromium.chrome.browser.ChromeApplication; |
16 import org.chromium.components.location.LocationUtils; | |
17 import org.chromium.content.browser.ContentViewCore; | 19 import org.chromium.content.browser.ContentViewCore; |
18 import org.chromium.content_public.browser.WebContents; | 20 import org.chromium.content_public.browser.WebContents; |
19 import org.chromium.ui.base.WindowAndroid; | 21 import org.chromium.ui.base.WindowAndroid; |
20 | 22 |
21 /** | 23 /** |
22 * Provides methods for querying Chrome's internal location setting and | 24 * Provides methods for querying Android system-wide location settings as well a
s Chrome's internal |
23 * combining that with the system-wide setting and permissions. | 25 * location setting. |
24 * | 26 * |
25 * This class should be used only on the UI thread. | 27 * This class should be used only on the UI thread. |
26 */ | 28 */ |
27 public class LocationSettings { | 29 public class LocationSettings { |
28 | 30 |
29 private static LocationSettings sInstance; | 31 private static LocationSettings sInstance; |
30 | 32 |
31 protected final Context mContext; | 33 protected final Context mContext; |
32 | 34 |
33 /** | 35 /** |
(...skipping 13 matching lines...) Expand all Loading... |
47 if (sInstance == null) { | 49 if (sInstance == null) { |
48 ChromeApplication application = | 50 ChromeApplication application = |
49 (ChromeApplication) ContextUtils.getApplicationContext(); | 51 (ChromeApplication) ContextUtils.getApplicationContext(); |
50 sInstance = application.createLocationSettings(); | 52 sInstance = application.createLocationSettings(); |
51 } | 53 } |
52 return sInstance; | 54 return sInstance; |
53 } | 55 } |
54 | 56 |
55 @CalledByNative | 57 @CalledByNative |
56 private static boolean canSitesRequestLocationPermission(WebContents webCont
ents) { | 58 private static boolean canSitesRequestLocationPermission(WebContents webCont
ents) { |
| 59 if (!LocationSettings.getInstance().isSystemLocationSettingEnabled()) re
turn false; |
| 60 |
57 ContentViewCore cvc = ContentViewCore.fromWebContents(webContents); | 61 ContentViewCore cvc = ContentViewCore.fromWebContents(webContents); |
58 if (cvc == null) return false; | 62 if (cvc == null) return false; |
59 WindowAndroid windowAndroid = cvc.getWindowAndroid(); | 63 WindowAndroid windowAndroid = cvc.getWindowAndroid(); |
60 if (windowAndroid == null) return false; | 64 if (windowAndroid == null) return false; |
61 Context context = windowAndroid.getApplicationContext(); | |
62 | 65 |
63 LocationUtils locationUtils = LocationUtils.getInstance(); | 66 return windowAndroid.hasPermission(Manifest.permission.ACCESS_COARSE_LOC
ATION) |
64 if (!locationUtils.isSystemLocationSettingEnabled(context)) return false
; | 67 || windowAndroid.hasPermission(Manifest.permission.ACCESS_FINE_L
OCATION) |
65 | |
66 return locationUtils.hasAndroidLocationPermission(context) | |
67 || windowAndroid.canRequestPermission(Manifest.permission.ACCESS
_FINE_LOCATION); | 68 || windowAndroid.canRequestPermission(Manifest.permission.ACCESS
_FINE_LOCATION); |
68 } | 69 } |
69 | 70 |
70 /** | 71 /** |
71 * Returns true if location is enabled system-wide and the Chrome location s
etting is enabled. | 72 * Returns true if location is enabled system-wide and the Chrome location s
etting is enabled. |
72 */ | 73 */ |
73 public boolean areAllLocationSettingsEnabled() { | 74 public boolean areAllLocationSettingsEnabled() { |
74 return isChromeLocationSettingEnabled() | 75 return isChromeLocationSettingEnabled() && isSystemLocationSettingEnable
d(); |
75 && LocationUtils.getInstance().isSystemLocationSettingEnabled(mC
ontext); | |
76 } | 76 } |
77 | 77 |
78 /** | 78 /** |
79 * Returns whether Chrome's user-configurable location setting is enabled. | 79 * Returns whether Chrome's user-configurable location setting is enabled. |
80 */ | 80 */ |
81 public boolean isChromeLocationSettingEnabled() { | 81 public boolean isChromeLocationSettingEnabled() { |
82 return PrefServiceBridge.getInstance().isAllowLocationEnabled(); | 82 return PrefServiceBridge.getInstance().isAllowLocationEnabled(); |
83 } | 83 } |
84 | 84 |
| 85 /** |
| 86 * Returns whether location is enabled system-wide, i.e. whether Chrome itse
lf is able to access |
| 87 * location. |
| 88 */ |
| 89 public boolean isSystemLocationSettingEnabled() { |
| 90 LocationManager locationManager = |
| 91 (LocationManager) mContext.getSystemService(Context.LOCATION_SER
VICE); |
| 92 return (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVID
ER) |
| 93 || locationManager.isProviderEnabled(LocationManager.GPS_PROVIDE
R)); |
| 94 } |
| 95 |
| 96 /** |
| 97 * Returns an intent to launch Android Location Settings. |
| 98 */ |
| 99 public Intent getSystemLocationSettingsIntent() { |
| 100 Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); |
| 101 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 102 return i; |
| 103 } |
| 104 |
85 @VisibleForTesting | 105 @VisibleForTesting |
86 public static void setInstanceForTesting(LocationSettings instance) { | 106 public static void setInstanceForTesting(LocationSettings instance) { |
87 sInstance = instance; | 107 sInstance = instance; |
88 } | 108 } |
89 } | 109 } |
OLD | NEW |