Commit e3c66d54 authored by khushalsagar's avatar khushalsagar Committed by Commit bot
Browse files

Revert of Add a LocationUtils class to give all Chromium Android code access...

Revert of Add a LocationUtils class to give all Chromium Android code access to location helpers. (patchset #9 id:160001 of https://codereview.chromium.org/2038753004/ )

Reason for revert:
Breaks the downstream bots.

Original issue's description:
> Add a components.location.LocationUtils class to give all Chromium Android code access to location helpers.
>
> This class currently helps code check if Chrome has permission to access
> location and if location is turned on for the device.
>
> Committed: https://crrev.com/a082f486985a4631ce3db3329874b9834e369e4d
> Cr-Commit-Position: refs/heads/master@{#402319}

TBR=ortuno@chromium.org,blundell@chromium.org,finnur@chromium.org,scheib@chromium.org,tedchoc@chromium.org,jyasskin@chromium.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true

Review-Url: https://codereview.chromium.org/2105573002
Cr-Commit-Position: refs/heads/master@{#402344}
parent 36b58361
......@@ -198,7 +198,6 @@ android_library("chrome_java") {
"//components/gcm_driver/android:gcm_driver_java",
"//components/gcm_driver/instance_id/android:instance_id_driver_java",
"//components/invalidation/impl:java",
"//components/location/android:location_java",
"//components/navigation_interception/android:navigation_interception_java",
"//components/policy/android:policy_java",
"//components/precache/android:precache_java",
......@@ -331,7 +330,6 @@ android_library("chrome_test_java") {
"//components/gcm_driver/android:gcm_driver_java",
"//components/invalidation/impl:java",
"//components/invalidation/impl:javatests",
"//components/location/android:location_java",
"//components/navigation_interception/android:navigation_interception_java",
"//components/policy/android:policy_java",
"//components/precache/android:precache_java",
......
......@@ -3,7 +3,6 @@ include_rules = [
"+components/dom_distiller/android/java/src/org/chromium/components/dom_distiller/content",
"+components/dom_distiller/android/java/src/org/chromium/components/dom_distiller/core",
"+components/gcm_driver/android/java/src/org/chromium/components/gcm_driver",
"+components/location/android/java",
"+components/navigation_interception",
"+components/precache/android/java",
"+components/safe_json/android/java",
......
......@@ -18,7 +18,6 @@ import org.chromium.base.annotations.CalledByNative;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.omnibox.OmniboxUrlEmphasizer;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.components.location.LocationUtils;
import org.chromium.ui.base.WindowAndroid;
import org.chromium.ui.text.NoUnderlineClickableSpan;
import org.chromium.ui.text.SpanApplier;
......@@ -162,7 +161,8 @@ public class BluetoothChooserDialog
}
private void checkLocationPermission() {
if (LocationUtils.getInstance().hasAndroidLocationPermission(mActivity)) {
if (mWindowAndroid.hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
|| mWindowAndroid.hasPermission(Manifest.permission.ACCESS_FINE_LOCATION)) {
return;
}
......@@ -255,8 +255,8 @@ public class BluetoothChooserDialog
@CalledByNative
private static BluetoothChooserDialog create(WindowAndroid windowAndroid, String origin,
int securityLevel, long nativeBluetoothChooserDialogPtr) {
if (!LocationUtils.getInstance().hasAndroidLocationPermission(
windowAndroid.getActivity().get())
if (!windowAndroid.hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
&& !windowAndroid.hasPermission(Manifest.permission.ACCESS_FINE_LOCATION)
&& !windowAndroid.canRequestPermission(
Manifest.permission.ACCESS_COARSE_LOCATION)) {
// If we can't even ask for enough permission to scan for Bluetooth devices, don't open
......
......@@ -64,7 +64,6 @@ import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.ssl.SecurityStateModel;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.util.UrlUtilities;
import org.chromium.components.location.LocationUtils;
import org.chromium.components.security_state.ConnectionSecurityLevel;
import org.chromium.content.browser.ContentViewCore;
import org.chromium.content_public.browser.WebContents;
......@@ -593,6 +592,15 @@ public class WebsiteSettingsPopup implements OnClickListener {
== PackageManager.PERMISSION_GRANTED);
}
private boolean isAndroidLocationDisabled() {
try {
return Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE) == Settings.Secure.LOCATION_MODE_OFF;
} catch (Settings.SettingNotFoundException e) {
return false;
}
}
/**
* Adds a new row for the given permission.
*
......@@ -632,12 +640,11 @@ public class WebsiteSettingsPopup implements OnClickListener {
// If warningTextResource is non-zero, then the view must be tagged with either
// permission_intent_override or permission_type.
LocationUtils locationUtils = LocationUtils.getInstance();
if (permission.type == ContentSettingsType.CONTENT_SETTINGS_TYPE_GEOLOCATION
&& !locationUtils.isSystemLocationSettingEnabled(mContext)) {
&& isAndroidLocationDisabled()) {
warningTextResource = R.string.page_info_android_location_blocked;
permissionRow.setTag(R.id.permission_intent_override,
locationUtils.getSystemLocationSettingsIntent());
new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
} else if (!hasAndroidPermission(permission.type)) {
warningTextResource = R.string.page_info_android_permission_blocked;
permissionRow.setTag(R.id.permission_type,
......
......@@ -19,7 +19,6 @@ import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.NativePage;
import org.chromium.chrome.browser.UrlConstants;
import org.chromium.components.location.LocationUtils;
import java.util.HashSet;
import java.util.Set;
......@@ -101,9 +100,8 @@ public class PhysicalWebDiagnosticsPage implements NativePage {
boolean isSdkVersionCorrect = isSdkVersionCorrect();
boolean isDataConnectionActive = Utils.isDataConnectionActive(mContext);
int bluetoothStatus = Utils.getBluetoothEnabledStatus(mContext);
LocationUtils locationUtils = LocationUtils.getInstance();
boolean isLocationServicesEnabled = locationUtils.isSystemLocationSettingEnabled(mContext);
boolean isLocationPermissionGranted = locationUtils.hasAndroidLocationPermission(mContext);
boolean isLocationServicesEnabled = Utils.isLocationServicesEnabled(mContext);
boolean isLocationPermissionGranted = Utils.isLocationPermissionGranted(mContext);
boolean isPreferenceEnabled = PhysicalWeb.isPhysicalWebPreferenceEnabled(mContext);
boolean isOnboarding = PhysicalWeb.isOnboarding(mContext);
......
......@@ -12,7 +12,6 @@ import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.metrics.RecordUserAction;
import org.chromium.components.location.LocationUtils;
import org.json.JSONArray;
import org.json.JSONException;
......@@ -227,11 +226,10 @@ public class PhysicalWebUma {
* - The Physical Web preference status
*/
public static void recordPhysicalWebState(Context context, String actionName) {
LocationUtils locationUtils = LocationUtils.getInstance();
handleEnum(context, createStateString(LOCATION_SERVICES, actionName),
locationUtils.isSystemLocationSettingEnabled(context) ? 1 : 0, BOOLEAN_BOUNDARY);
Utils.isLocationServicesEnabled(context) ? 1 : 0, BOOLEAN_BOUNDARY);
handleEnum(context, createStateString(LOCATION_PERMISSION, actionName),
locationUtils.hasAndroidLocationPermission(context) ? 1 : 0, BOOLEAN_BOUNDARY);
Utils.isLocationPermissionGranted(context) ? 1 : 0, BOOLEAN_BOUNDARY);
handleEnum(context, createStateString(BLUETOOTH, actionName),
Utils.getBluetoothEnabledStatus(context), TRISTATE_BOUNDARY);
handleEnum(context, createStateString(DATA_CONNECTION, actionName),
......
......@@ -8,7 +8,9 @@ import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.os.Build;
import android.support.v4.content.PermissionChecker;
......@@ -40,4 +42,20 @@ class Utils {
}
return statusResult;
}
public static boolean isLocationServicesEnabled(Context context) {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isGpsProviderEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkProviderEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
return isGpsProviderEnabled || isNetworkProviderEnabled;
}
public static boolean isLocationPermissionGranted(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
return PermissionChecker.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
}
......@@ -6,6 +6,9 @@ package org.chromium.chrome.browser.preferences;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import org.chromium.base.ContextUtils;
import org.chromium.base.ThreadUtils;
......@@ -13,14 +16,13 @@ import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.SuppressFBWarnings;
import org.chromium.chrome.browser.ChromeApplication;
import org.chromium.components.location.LocationUtils;
import org.chromium.content.browser.ContentViewCore;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.WindowAndroid;
/**
* Provides methods for querying Chrome's internal location setting and
* combining that with the system-wide setting and permissions.
* Provides methods for querying Android system-wide location settings as well as Chrome's internal
* location setting.
*
* This class should be used only on the UI thread.
*/
......@@ -54,16 +56,15 @@ public class LocationSettings {
@CalledByNative
private static boolean canSitesRequestLocationPermission(WebContents webContents) {
if (!LocationSettings.getInstance().isSystemLocationSettingEnabled()) return false;
ContentViewCore cvc = ContentViewCore.fromWebContents(webContents);
if (cvc == null) return false;
WindowAndroid windowAndroid = cvc.getWindowAndroid();
if (windowAndroid == null) return false;
Context context = windowAndroid.getApplicationContext();
LocationUtils locationUtils = LocationUtils.getInstance();
if (!locationUtils.isSystemLocationSettingEnabled(context)) return false;
return locationUtils.hasAndroidLocationPermission(context)
return windowAndroid.hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
|| windowAndroid.hasPermission(Manifest.permission.ACCESS_FINE_LOCATION)
|| windowAndroid.canRequestPermission(Manifest.permission.ACCESS_FINE_LOCATION);
}
......@@ -71,8 +72,7 @@ public class LocationSettings {
* Returns true if location is enabled system-wide and the Chrome location setting is enabled.
*/
public boolean areAllLocationSettingsEnabled() {
return isChromeLocationSettingEnabled()
&& LocationUtils.getInstance().isSystemLocationSettingEnabled(mContext);
return isChromeLocationSettingEnabled() && isSystemLocationSettingEnabled();
}
/**
......@@ -82,6 +82,26 @@ public class LocationSettings {
return PrefServiceBridge.getInstance().isAllowLocationEnabled();
}
/**
* Returns whether location is enabled system-wide, i.e. whether Chrome itself is able to access
* location.
*/
public boolean isSystemLocationSettingEnabled() {
LocationManager locationManager =
(LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
return (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
|| locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER));
}
/**
* 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(LocationSettings instance) {
sInstance = instance;
......
......@@ -34,7 +34,6 @@ import org.chromium.chrome.browser.preferences.website.WebsitePreferenceBridge;
import org.chromium.chrome.browser.search_engines.TemplateUrlService;
import org.chromium.chrome.browser.search_engines.TemplateUrlService.LoadListener;
import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrl;
import org.chromium.components.location.LocationUtils;
import org.chromium.ui.text.SpanApplier;
import org.chromium.ui.text.SpanApplier.SpanInfo;
......@@ -205,7 +204,7 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On
if (selected) {
ForegroundColorSpan linkSpan = new ForegroundColorSpan(
ApiCompatibilityUtils.getColor(resources, R.color.pref_accent_color));
if (LocationUtils.getInstance().isSystemLocationSettingEnabled(mContext)) {
if (LocationSettings.getInstance().isSystemLocationSettingEnabled()) {
String message = mContext.getString(
locationEnabled(position, true)
? R.string.search_engine_location_allowed
......@@ -271,8 +270,9 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On
}
private void onLocationLinkClicked() {
if (!LocationUtils.getInstance().isSystemLocationSettingEnabled(mContext)) {
mContext.startActivity(LocationUtils.getInstance().getSystemLocationSettingsIntent());
if (!LocationSettings.getInstance().isSystemLocationSettingEnabled()) {
mContext.startActivity(
LocationSettings.getInstance().getSystemLocationSettingsIntent());
} else {
Intent settingsIntent = PreferencesLauncher.createIntentForSettingsPage(
mContext, SingleWebsitePreferences.class.getName());
......
......@@ -9,12 +9,10 @@ import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import org.chromium.base.ContextUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ContentSettingsType;
import org.chromium.chrome.browser.preferences.LocationSettings;
import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.components.location.LocationUtils;
/**
* A class for dealing with the Geolocation category.
......@@ -28,8 +26,7 @@ public class LocationCategory extends SiteSettingsCategory {
@Override
protected boolean enabledGlobally() {
return LocationUtils.getInstance().isSystemLocationSettingEnabled(
ContextUtils.getApplicationContext());
return LocationSettings.getInstance().isSystemLocationSettingEnabled();
}
@Override
......@@ -53,7 +50,7 @@ public class LocationCategory extends SiteSettingsCategory {
@Override
protected Intent getIntentToEnableOsGlobalPermission(Context context) {
if (enabledGlobally()) return null;
return LocationUtils.getInstance().getSystemLocationSettingsIntent();
return LocationSettings.getInstance().getSystemLocationSettingsIntent();
}
@Override
......
......@@ -2,7 +2,6 @@ include_rules = [
"+components/bookmarks/common/android/java/src/org/chromium/components/bookmarks",
"+components/dom_distiller/android/java/src/org/chromium/components/dom_distiller/core",
"+components/gcm_driver/android/java/src/org/chromium/components/gcm_driver",
"+components/location/android/java",
"+components/navigation_interception",
"+components/precache/android/javatests",
"+components/web_restrictions",
......
......@@ -6,7 +6,6 @@ package org.chromium.chrome.browser;
import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.test.MoreAsserts;
import android.test.suitebuilder.annotation.SmallTest;
......@@ -17,7 +16,6 @@ import android.widget.ListView;
import org.chromium.base.ThreadUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.test.ChromeActivityTestCaseBase;
import org.chromium.components.location.LocationUtils;
import org.chromium.components.security_state.ConnectionSecurityLevel;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
......@@ -70,7 +68,6 @@ public class BluetoothChooserDialogTest extends ChromeActivityTestCaseBase<Chrom
}
private ActivityWindowAndroid mWindowAndroid;
private FakeLocationUtils mLocationUtils;
private BluetoothChooserDialogWithFakeNatives mChooserDialog;
public BluetoothChooserDialogTest() {
......@@ -82,22 +79,9 @@ public class BluetoothChooserDialogTest extends ChromeActivityTestCaseBase<Chrom
@Override
protected void setUp() throws Exception {
super.setUp();
mLocationUtils = new FakeLocationUtils();
LocationUtils.setFactory(new LocationUtils.Factory() {
@Override
public LocationUtils create() {
return mLocationUtils;
}
});
mChooserDialog = createDialog();
}
@Override
protected void tearDown() throws Exception {
LocationUtils.setFactory(null);
super.tearDown();
}
@Override
public void startMainActivity() throws InterruptedException {
startMainActivityOnBlankPage();
......@@ -281,7 +265,7 @@ public class BluetoothChooserDialogTest extends ChromeActivityTestCaseBase<Chrom
new String[] {Manifest.permission.ACCESS_COARSE_LOCATION});
assertNotNull(permissionDelegate.mCallback);
// Grant permission.
mLocationUtils.mLocationGranted = true;
permissionDelegate.mLocationGranted = true;
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
......@@ -300,14 +284,15 @@ public class BluetoothChooserDialogTest extends ChromeActivityTestCaseBase<Chrom
// TODO(jyasskin): Test when the user denies Chrome the ability to ask for permission.
private static class TestAndroidPermissionDelegate implements AndroidPermissionDelegate {
boolean mLocationGranted = false;
PermissionCallback mCallback = null;
String[] mPermissionsRequested = null;
@Override
public boolean hasPermission(String permission) {
return false;
return permission.equals(Manifest.permission.ACCESS_COARSE_LOCATION)
&& mLocationGranted;
}
@Override
public boolean canRequestPermission(String permission) {
return true;
......@@ -325,13 +310,4 @@ public class BluetoothChooserDialogTest extends ChromeActivityTestCaseBase<Chrom
}
}
}
private static class FakeLocationUtils extends LocationUtils {
public boolean mLocationGranted = false;
@Override
public boolean hasAndroidLocationPermission(Context context) {
return mLocationGranted;
}
}
}
......@@ -62,7 +62,6 @@ android_library("chrome_java_test_support") {
"//chrome/android:chrome_java",
"//components/bookmarks/common/android:bookmarks_java",
"//components/invalidation/impl:java",
"//components/location/android:location_java",
"//components/policy/android:policy_java",
"//components/policy/android:policy_java_test_support",
"//components/web_contents_delegate_android:web_contents_delegate_android_java",
......
......@@ -4,10 +4,8 @@
package org.chromium.chrome.test.util.browser;
import android.content.Context;
import org.chromium.base.ThreadUtils;
import org.chromium.components.location.LocationUtils;
import org.chromium.chrome.browser.preferences.LocationSettings;
/**
* Methods for testing location-related features.
......@@ -21,15 +19,10 @@ public class LocationSettingsTestUtil {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
LocationUtils.setFactory(new LocationUtils.Factory() {
LocationSettings.setInstanceForTesting(new LocationSettings(null) {
@Override
public LocationUtils create() {
return new LocationUtils() {
@Override
public boolean isSystemLocationSettingEnabled(Context context) {
return enabled;
}
};
public boolean isSystemLocationSettingEnabled() {
return enabled;
}
});
}
......
jyasskin@chromium.org
tedchoc@chromium.org
# 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.
import("//build/config/android/rules.gni")
# GYP: //components/location.gypi:location_java
android_library("location_java") {
deps = [
"//base:base_java",
]
java_files =
[ "java/src/org/chromium/components/location/LocationUtils.java" ]
}
// 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.Build;
import android.os.Process;
import android.provider.Settings;
import android.text.TextUtils;
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 {
// Used to construct sInstance if that's null.
private static Factory sFactory;
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) {
if (sFactory == null) {
sInstance = new LocationUtils();
} else {
sInstance = sFactory.create();
}
}
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 hasAndroidLocationPermission() and isSystemLocationSettingEnabled() to determine
* if Chromium's location requests will return results.
*/
public boolean hasAndroidLocationPermission(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) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF)
!= Settings.Secure.LOCATION_MODE_OFF;
} else {
return !TextUtils.isEmpty(Settings.Secure.getString(
context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED));
}
}
/**
* 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;
}
/**
* Instantiate this to explain how to create a LocationUtils instance in
* LocationUtils.getInstance().
*/
public interface Factory { public LocationUtils create(); }
/**
* Call this to use a different subclass of LocationUtils throughout the program.
* This can be used by embedders in addition to tests.
*/
@VisibleForTesting
public static void setFactory(Factory factory) {
sFactory = factory;
sInstance = null;
}
}
......@@ -397,7 +397,6 @@ if (is_android) {
java_files = java_sources_needing_jni
deps = [
"//base:base_java",
"//components/location/android:location_java",
]
}
}