Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8dbb6341 authored by Victoria Lease's avatar Victoria Lease
Browse files

Allow apps to getProviders() without location permissions.

This restores MR0's behavior in this regard - apps calling
LocationManager.getProviders() or LocationManager.getBestProvider()
will no longer receive a SecurityException if they do not have
any location permissions. Instead, as was the behavior in MR0, they
only receive providers that their permissions grant them access to,
including an empty list if they have no permission whatsoever.

Bug: 7207864
Change-Id: I027df425e258d436c4821c34a25bc46a2a292824
parent 537d47f5
Loading
Loading
Loading
Loading
+29 −13
Original line number Original line Diff line number Diff line
@@ -601,6 +601,23 @@ public class LocationManagerService extends ILocationManager.Stub implements Run
        }
        }
    }
    }


    private boolean isAllowedProviderSafe(String provider) {
        if (LocationManager.GPS_PROVIDER.equals(provider) ||
                LocationManager.PASSIVE_PROVIDER.equals(provider)) {
            // gps and passive providers require FINE permission
            return mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED;
        } else if (LocationManager.NETWORK_PROVIDER.equals(provider) ||
                LocationManager.FUSED_PROVIDER.equals(provider)) {
            // network and fused providers are ok with COARSE or FINE
            return (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) ||
                    (mContext.checkCallingOrSelfPermission(ACCESS_COARSE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED);
        }
        return false;
    }

    /**
    /**
     * Returns all providers by name, including passive, but excluding
     * Returns all providers by name, including passive, but excluding
     * fused.
     * fused.
@@ -632,8 +649,6 @@ public class LocationManagerService extends ILocationManager.Stub implements Run
     */
     */
    @Override
    @Override
    public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
    public List<String> getProviders(Criteria criteria, boolean enabledOnly) {
        checkPermission();

        ArrayList<String> out;
        ArrayList<String> out;
        synchronized (mLock) {
        synchronized (mLock) {
            out = new ArrayList<String>(mProviders.size());
            out = new ArrayList<String>(mProviders.size());
@@ -642,6 +657,7 @@ public class LocationManagerService extends ILocationManager.Stub implements Run
                if (LocationManager.FUSED_PROVIDER.equals(name)) {
                if (LocationManager.FUSED_PROVIDER.equals(name)) {
                    continue;
                    continue;
                }
                }
                if (isAllowedProviderSafe(name)) {
                    if (enabledOnly && !isAllowedBySettingsLocked(name)) {
                    if (enabledOnly && !isAllowedBySettingsLocked(name)) {
                        continue;
                        continue;
                    }
                    }
@@ -652,6 +668,7 @@ public class LocationManagerService extends ILocationManager.Stub implements Run
                    out.add(name);
                    out.add(name);
                }
                }
            }
            }
        }


        if (D) Log.d(TAG, "getProviders()=" + out);
        if (D) Log.d(TAG, "getProviders()=" + out);
        return out;
        return out;
@@ -660,23 +677,22 @@ public class LocationManagerService extends ILocationManager.Stub implements Run
    /**
    /**
     * Return the name of the best provider given a Criteria object.
     * Return the name of the best provider given a Criteria object.
     * This method has been deprecated from the public API,
     * This method has been deprecated from the public API,
     * and the whole LoactionProvider (including #meetsCriteria)
     * and the whole LocationProvider (including #meetsCriteria)
     * has been deprecated as well. So this method now uses
     * has been deprecated as well. So this method now uses
     * some simplified logic.
     * some simplified logic.
     */
     */
    @Override
    @Override
    public String getBestProvider(Criteria criteria, boolean enabledOnly) {
    public String getBestProvider(Criteria criteria, boolean enabledOnly) {
        String result = null;
        String result = null;
        checkPermission();


        List<String> providers = getProviders(criteria, enabledOnly);
        List<String> providers = getProviders(criteria, enabledOnly);
        if (providers.size() < 1) {
        if (!providers.isEmpty()) {
            result = pickBest(providers);
            result = pickBest(providers);
            if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
            if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
            return result;
            return result;
        }
        }
        providers = getProviders(null, enabledOnly);
        providers = getProviders(null, enabledOnly);
        if (providers.size() >= 1) {
        if (!providers.isEmpty()) {
            result = pickBest(providers);
            result = pickBest(providers);
            if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
            if (D) Log.d(TAG, "getBestProvider(" + criteria + ", " + enabledOnly + ")=" + result);
            return result;
            return result;