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

Commit e9b92247 authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

feat:2398: whitelist app from fakelocation, improve codestyle

parent b142ab4d
Loading
Loading
Loading
Loading
+33 −25
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@

package com.android.server.location;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.location.Location;
import android.location.LocationResult;
@@ -38,63 +40,69 @@ public class FakeLocationResolver {
    private static final String PARAM_LATITUDE = "latitude";
    private static final String PARAM_LONGITUDE = "longitude";

    public static LocationResult fakeLocations(Context context, LocationResult baseLocations, CallerIdentity identity) {
        int uid = identity.getUid();
        String packageName = identity.getPackageName();
    public static @Nullable LocationResult fakeLocations(
            @NonNull Context context,
            @Nullable LocationResult baseLocations,
            @NonNull CallerIdentity identity) {
        final int uid = identity.getUid();
        final String packageName = identity.getPackageName();

        if (baseLocations == null || context == null || packageName == null || uid < 0) {
            Log.w(TAG, "FakeLocationResolver::fakeLocations invalid parameters");
            return baseLocations;
        }
        FakeLocation latLon = getFakeLocation(context, packageName, uid);
        final FakeLocation latLon = getFakeLocation(context, packageName, uid);
        if (latLon == null) return baseLocations;

        return LocationResult.wrap(overrideLatLons(baseLocations.asList(), latLon));
        return LocationResult.wrap(overrideLatLon(baseLocations.asList(), latLon));
    }

    public static Location fakeLocation(Context context, Location baseLocation, CallerIdentity identity) {
        int uid = identity.getUid();
        String packageName = identity.getPackageName();
    public static @Nullable Location fakeLocation(
            @NonNull Context context,
            @Nullable Location baseLocation,
            @NonNull CallerIdentity identity) {
        final int uid = identity.getUid();
        final String packageName = identity.getPackageName();

        if (baseLocation == null || context == null || packageName == null || uid < 0) {
            Log.w(TAG, "FakeLocationResolver::fakeLocation invalid parameters");
            return baseLocation;
        }
        FakeLocation latLon = getFakeLocation(context, packageName, uid);
        final FakeLocation latLon = getFakeLocation(context, packageName, uid);
        if (latLon == null) return baseLocation;

        return overrideLatLons(baseLocation, latLon);
        return overrideLatLon(baseLocation, latLon);
    }

    public static boolean hasFakeLocation(Context context, CallerIdentity identity) {
        int uid = identity.getUid();
        String packageName = identity.getPackageName();
    public static boolean hasFakeLocation(@NonNull Context context, @NonNull CallerIdentity identity) {
        final int uid = identity.getUid();
        final String packageName = identity.getPackageName();

        if (context == null || packageName == null || uid < 0) {
            Log.w(TAG, "FakeLocationResolver::hasFakeLocation invalid parameters");
            return false;
        }
        FakeLocation latLon = getFakeLocation(context, packageName, uid);
        final FakeLocation latLon = getFakeLocation(context, packageName, uid);
        if (latLon == null) return false;

        return true;
    }

    private static class FakeLocation {
        Double latitude;
        Double longitude;
        double latitude;
        double longitude;

        public FakeLocation(Double latitude, Double longitude) {
        public FakeLocation(double latitude, double longitude) {
            this.latitude = latitude;
            this.longitude = longitude;
        }
    }

    private static FakeLocation getFakeLocation(Context context, String packageName, int uid) {
    private static @Nullable FakeLocation getFakeLocation(@NonNull Context context, @NonNull String packageName, int uid) {
        try {
            Bundle extra = new Bundle();
            final Bundle extra = new Bundle();
            extra.putInt(PARAM_UID, uid);
            Bundle result = context.getContentResolver().call(
            final Bundle result = context.getContentResolver().call(
                    Uri.parse(FAKE_LOCATIONS_URI),
                    "",
                    packageName,
@@ -110,18 +118,18 @@ public class FakeLocationResolver {
        return null;
    }

    private static List<Location> overrideLatLons(List<Location> baseLocations, FakeLocation latLon) {
        ArrayList<Location> fakedLocations = new ArrayList<Location>(baseLocations.size());
    private static @NonNull List<Location> overrideLatLon(@NonNull List<Location> baseLocations, @NonNull FakeLocation latLon) {
        final ArrayList<Location> fakedLocations = new ArrayList<Location>(baseLocations.size());
        for (Location location: baseLocations) {
            Location fakedLocation = overrideLatLons(location, latLon);
            Location fakedLocation = overrideLatLon(location, latLon);

            fakedLocations.add(fakedLocation);
        }
        return fakedLocations;
    }

    private static Location overrideLatLons(Location baseLocation, FakeLocation latLon) {
        Location fakedLocation = new Location(baseLocation);
    private static @NonNull Location overrideLatLon(@NonNull Location baseLocation, @NonNull FakeLocation latLon) {
        final Location fakedLocation = new Location(baseLocation);
        fakedLocation.setLatitude(latLon.latitude);
        fakedLocation.setLongitude(latLon.longitude);
        fakedLocation.setAltitude(3.0);
+9 −8
Original line number Diff line number Diff line
@@ -966,17 +966,17 @@ public class LocationProviderManager extends

                @Override
                public void operate(LocationTransport listener) throws Exception {
                    LocationResult fakedLocationResult = FakeLocationResolver.fakeLocations(mContext, locationResult, getIdentity());

                    // if delivering to the same process, make a copy of the location first (since
                    // location is mutable)
                    LocationResult deliverLocationResult;
                    if (getIdentity().getPid() == Process.myPid()) {
                        deliverLocationResult = fakedLocationResult.deepCopy();
                        deliverLocationResult = locationResult.deepCopy();
                    } else {
                        deliverLocationResult = fakedLocationResult;
                        deliverLocationResult = locationResult;
                    }

                    deliverLocationResult = FakeLocationResolver.fakeLocations(mContext, deliverLocationResult, getIdentity());

                    listener.deliverOnLocationChanged(deliverLocationResult,
                            mUseWakeLock ? mWakeLockReleaser : null);
                    EVENT_LOG.logProviderDeliveredLocations(mName, locationResult.size(),
@@ -1271,16 +1271,17 @@ public class LocationProviderManager extends
            return new ListenerOperation<LocationTransport>() {
                @Override
                public void operate(LocationTransport listener) throws Exception {
                    LocationResult fakedLocationResult = FakeLocationResolver.fakeLocations(mContext, locationResult, getIdentity());
                    // if delivering to the same process, make a copy of the location first (since
                    // location is mutable)
                    LocationResult deliverLocationResult;
                    if (getIdentity().getPid() == Process.myPid() && fakedLocationResult != null) {
                        deliverLocationResult = fakedLocationResult.deepCopy();
                    if (getIdentity().getPid() == Process.myPid() && locationResult != null) {
                        deliverLocationResult = locationResult.deepCopy();
                    } else {
                        deliverLocationResult = fakedLocationResult;
                        deliverLocationResult = locationResult;
                    }

                    deliverLocationResult = FakeLocationResolver.fakeLocations(mContext, deliverLocationResult, getIdentity());

                    // we currently don't hold a wakelock for getCurrentLocation deliveries
                    listener.deliverOnLocationChanged(deliverLocationResult, null);
                    EVENT_LOG.logProviderDeliveredLocations(mName,