DO NOT MERGE Let isLocationEnabledForUser() return true location setting

* In API 28, LocationManager#isLocationEnabled() and
  LocationManager#isLocationEnabledForUser(UserHandle) always
  return false when a device has neither LocationManager#GPS_PROVIDER
  nor LocationManager#NETWORK_PROVIDER.
* Instead of letting setLocationEnabled() and isLocationEnabled()
  depend on whether device has GPS or NETWORK location provider, this
  CL changes these two APIs to directly use LOCATION_MODE setting to
  store the location enable state
* Hence when LOCATION_PROVIDERS_ALLOWED contains a provider, the provider
  may be used, but it is not guaranteed to exist
* Settings.java has two workarounds that actually convert:
   - putInt(LOCATION_MODE, LOCATION_MODE_HIGH_ACCURACY)
       => putString(LOCATION_PROVIDERS_ALLOWED, "+gps");
       => putString(LOCATION_PROVIDERS_ALLOWED, "+network");
   - getInt(LOCATION_MODE, LOCATION_MODE_HIGH_ACCURACY)
       => getString(LOCATION_PROVIDERS_ALLOWED):
         - "gps,network" - LOCATION_MODE_HIGH_ACCURACY
         - "gps"         - LOCATION_MODE_SENSORS_ONLY
         - "network"     - LOCATION_MODE_BATTERY_SAVING
	 - others        - LOCATION_MODE_OFF
* Hence this is NOT a new behavior

Bug: 121040693
Bug: 118242060
Bug: 127359153
Test: CTS tests,
android.bluetooth.cts.BluetoothLeScanTest#testBasicBleScan,
android.bluetooth.cts.BluetoothLeScanTest#testScanFilter,
android.location.cts.LocationManagerTest,
android.location2.cts.LocationManagerTest
Change-Id: I7972d8f97f4ca82c58c29641a081ef73fdcb106c
This commit is contained in:
Soonil Nagarkar
2019-03-04 17:17:22 -08:00
committed by Jack He
parent f96cc08066
commit 1841870f22

View File

@@ -2582,24 +2582,9 @@ public class LocationManagerService extends ILocationManager.Stub {
long identity = Binder.clearCallingIdentity();
try {
synchronized (mLock) {
final String allowedProviders = Settings.Secure.getStringForUser(
mContext.getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
userId);
if (allowedProviders == null) {
return false;
}
final List<String> providerList = Arrays.asList(allowedProviders.split(","));
for(String provider : mRealProviders.keySet()) {
if (provider.equals(LocationManager.PASSIVE_PROVIDER)
|| provider.equals(LocationManager.FUSED_PROVIDER)) {
continue;
}
if (providerList.contains(provider)) {
return true;
}
}
return false;
return Settings.Secure.getIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF, userId)
!= Settings.Secure.LOCATION_MODE_OFF;
}
} finally {
Binder.restoreCallingIdentity(identity);
@@ -2624,41 +2609,10 @@ public class LocationManagerService extends ILocationManager.Stub {
long identity = Binder.clearCallingIdentity();
try {
synchronized (mLock) {
final Set<String> allRealProviders = mRealProviders.keySet();
// Update all providers on device plus gps and network provider when disabling
// location
Set<String> allProvidersSet = new ArraySet<>(allRealProviders.size() + 2);
allProvidersSet.addAll(allRealProviders);
// When disabling location, disable gps and network provider that could have been
// enabled by location mode api.
if (enabled == false) {
allProvidersSet.add(LocationManager.GPS_PROVIDER);
allProvidersSet.add(LocationManager.NETWORK_PROVIDER);
}
if (allProvidersSet.isEmpty()) {
return;
}
// to ensure thread safety, we write the provider name with a '+' or '-'
// and let the SettingsProvider handle it rather than reading and modifying
// the list of enabled providers.
final String prefix = enabled ? "+" : "-";
StringBuilder locationProvidersAllowed = new StringBuilder();
for (String provider : allProvidersSet) {
if (provider.equals(LocationManager.PASSIVE_PROVIDER)
|| provider.equals(LocationManager.FUSED_PROVIDER)) {
continue;
}
locationProvidersAllowed.append(prefix);
locationProvidersAllowed.append(provider);
locationProvidersAllowed.append(",");
}
// Remove the trailing comma
locationProvidersAllowed.setLength(locationProvidersAllowed.length() - 1);
Settings.Secure.putStringForUser(
mContext.getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
locationProvidersAllowed.toString(),
userId);
int locationMode = enabled ? Settings.Secure.LOCATION_MODE_HIGH_ACCURACY
: Settings.Secure.LOCATION_MODE_OFF;
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, locationMode, userId);
}
} finally {
Binder.restoreCallingIdentity(identity);