Chromium Code Reviews| 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; | |
| 12 | 9 |
| 13 import org.chromium.base.ContextUtils; | 10 import org.chromium.base.ContextUtils; |
| 11 import org.chromium.base.LocationUtils; | |
| 14 import org.chromium.base.ThreadUtils; | 12 import org.chromium.base.ThreadUtils; |
| 15 import org.chromium.base.VisibleForTesting; | 13 import org.chromium.base.VisibleForTesting; |
| 16 import org.chromium.base.annotations.CalledByNative; | 14 import org.chromium.base.annotations.CalledByNative; |
| 17 import org.chromium.base.annotations.SuppressFBWarnings; | 15 import org.chromium.base.annotations.SuppressFBWarnings; |
| 18 import org.chromium.chrome.browser.ChromeApplication; | 16 import org.chromium.chrome.browser.ChromeApplication; |
| 19 import org.chromium.content.browser.ContentViewCore; | 17 import org.chromium.content.browser.ContentViewCore; |
| 20 import org.chromium.content_public.browser.WebContents; | 18 import org.chromium.content_public.browser.WebContents; |
| 21 import org.chromium.ui.base.WindowAndroid; | 19 import org.chromium.ui.base.WindowAndroid; |
| 22 | 20 |
| 23 /** | 21 /** |
| 24 * Provides methods for querying Android system-wide location settings as well a s Chrome's internal | 22 * Provides methods for querying Chrome's internal location setting and |
| 25 * location setting. | 23 * combining that with the system-wide setting and permissions. |
| 26 * | 24 * |
| 27 * This class should be used only on the UI thread. | 25 * This class should be used only on the UI thread. |
| 28 */ | 26 */ |
| 29 public class LocationSettings { | 27 public class LocationSettings { |
|
Jeffrey Yasskin
2016/06/07 15:56:10
Ted, I'm having trouble finding the internal versi
| |
| 30 | 28 |
| 31 private static LocationSettings sInstance; | 29 private static LocationSettings sInstance; |
| 32 | 30 |
| 33 protected final Context mContext; | 31 protected final Context mContext; |
| 34 | 32 |
| 35 /** | 33 /** |
| 36 * Don't use this; use getInstance() instead. This should be used only by th e Application inside | 34 * Don't use this; use getInstance() instead. This should be used only by th e Application inside |
| 37 * of createLocationSettings(). | 35 * of createLocationSettings(). |
| 38 */ | 36 */ |
| 39 protected LocationSettings(Context context) { | 37 protected LocationSettings(Context context) { |
| 40 mContext = context; | 38 mContext = context; |
| 41 } | 39 } |
| 42 | 40 |
| 43 /** | 41 /** |
| 44 * Returns the singleton instance of LocationSettings, creating it if needed . | 42 * Returns the singleton instance of LocationSettings, creating it if needed . |
| 45 */ | 43 */ |
| 46 @SuppressFBWarnings("LI_LAZY_INIT_STATIC") | 44 @SuppressFBWarnings("LI_LAZY_INIT_STATIC") |
| 47 public static LocationSettings getInstance() { | 45 public static LocationSettings getInstance() { |
| 48 ThreadUtils.assertOnUiThread(); | 46 ThreadUtils.assertOnUiThread(); |
| 49 if (sInstance == null) { | 47 if (sInstance == null) { |
| 50 ChromeApplication application = | 48 ChromeApplication application = |
| 51 (ChromeApplication) ContextUtils.getApplicationContext(); | 49 (ChromeApplication) ContextUtils.getApplicationContext(); |
| 52 sInstance = application.createLocationSettings(); | 50 sInstance = application.createLocationSettings(); |
| 53 } | 51 } |
| 54 return sInstance; | 52 return sInstance; |
| 55 } | 53 } |
| 56 | 54 |
| 57 @CalledByNative | 55 @CalledByNative |
| 58 private static boolean canSitesRequestLocationPermission(WebContents webCont ents) { | 56 private static boolean canSitesRequestLocationPermission(WebContents webCont ents) { |
| 59 if (!LocationSettings.getInstance().isSystemLocationSettingEnabled()) re turn false; | |
| 60 | |
| 61 ContentViewCore cvc = ContentViewCore.fromWebContents(webContents); | 57 ContentViewCore cvc = ContentViewCore.fromWebContents(webContents); |
| 62 if (cvc == null) return false; | 58 if (cvc == null) return false; |
| 63 WindowAndroid windowAndroid = cvc.getWindowAndroid(); | 59 WindowAndroid windowAndroid = cvc.getWindowAndroid(); |
| 64 if (windowAndroid == null) return false; | 60 if (windowAndroid == null) return false; |
| 61 Context context = windowAndroid.getApplicationContext(); | |
| 65 | 62 |
| 66 return windowAndroid.hasPermission(Manifest.permission.ACCESS_COARSE_LOC ATION) | 63 LocationUtils locationUtils = LocationUtils.getInstance(); |
| 67 || windowAndroid.hasPermission(Manifest.permission.ACCESS_FINE_L OCATION) | 64 if (!locationUtils.isSystemLocationSettingEnabled(context)) return false ; |
| 65 | |
| 66 return locationUtils.chromiumHasLocationPermission(context) | |
| 68 || windowAndroid.canRequestPermission(Manifest.permission.ACCESS _FINE_LOCATION); | 67 || windowAndroid.canRequestPermission(Manifest.permission.ACCESS _FINE_LOCATION); |
| 69 } | 68 } |
| 70 | 69 |
| 71 /** | 70 /** |
| 72 * Returns true if location is enabled system-wide and the Chrome location s etting is enabled. | 71 * Returns true if location is enabled system-wide and the Chrome location s etting is enabled. |
| 73 */ | 72 */ |
| 74 public boolean areAllLocationSettingsEnabled() { | 73 public boolean areAllLocationSettingsEnabled() { |
| 75 return isChromeLocationSettingEnabled() && isSystemLocationSettingEnable d(); | 74 return isChromeLocationSettingEnabled() |
| 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 | |
| 105 @VisibleForTesting | 85 @VisibleForTesting |
| 106 public static void setInstanceForTesting(LocationSettings instance) { | 86 public static void setInstanceForTesting(LocationSettings instance) { |
| 107 sInstance = instance; | 87 sInstance = instance; |
| 108 } | 88 } |
| 109 } | 89 } |
| OLD | NEW |