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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/preferences/LocationSettings.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: Adjust DEPS. 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
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;
14 import org.chromium.base.ThreadUtils; 11 import org.chromium.base.ThreadUtils;
15 import org.chromium.base.VisibleForTesting; 12 import org.chromium.base.VisibleForTesting;
16 import org.chromium.base.annotations.CalledByNative; 13 import org.chromium.base.annotations.CalledByNative;
17 import org.chromium.base.annotations.SuppressFBWarnings; 14 import org.chromium.base.annotations.SuppressFBWarnings;
18 import org.chromium.chrome.browser.ChromeApplication; 15 import org.chromium.chrome.browser.ChromeApplication;
16 import org.chromium.components.location.LocationUtils;
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 {
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 /**
(...skipping 13 matching lines...) Expand all
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.hasAndroidLocationPermission(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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698