Loading services/core/java/com/android/server/location/FakeLocationResolver.java +33 −25 Original line number Original line Diff line number Diff line Loading @@ -17,6 +17,8 @@ package com.android.server.location; package com.android.server.location; import android.annotation.NonNull; import android.annotation.Nullable; import android.content.Context; import android.content.Context; import android.location.Location; import android.location.Location; import android.location.LocationResult; import android.location.LocationResult; Loading @@ -38,63 +40,69 @@ public class FakeLocationResolver { private static final String PARAM_LATITUDE = "latitude"; private static final String PARAM_LATITUDE = "latitude"; private static final String PARAM_LONGITUDE = "longitude"; private static final String PARAM_LONGITUDE = "longitude"; public static LocationResult fakeLocations(Context context, LocationResult baseLocations, CallerIdentity identity) { public static @Nullable LocationResult fakeLocations( int uid = identity.getUid(); @NonNull Context context, String packageName = identity.getPackageName(); @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) { if (baseLocations == null || context == null || packageName == null || uid < 0) { Log.w(TAG, "FakeLocationResolver::fakeLocations invalid parameters"); Log.w(TAG, "FakeLocationResolver::fakeLocations invalid parameters"); return baseLocations; return baseLocations; } } FakeLocation latLon = getFakeLocation(context, packageName, uid); final FakeLocation latLon = getFakeLocation(context, packageName, uid); if (latLon == null) return baseLocations; 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) { public static @Nullable Location fakeLocation( int uid = identity.getUid(); @NonNull Context context, String packageName = identity.getPackageName(); @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) { if (baseLocation == null || context == null || packageName == null || uid < 0) { Log.w(TAG, "FakeLocationResolver::fakeLocation invalid parameters"); Log.w(TAG, "FakeLocationResolver::fakeLocation invalid parameters"); return baseLocation; return baseLocation; } } FakeLocation latLon = getFakeLocation(context, packageName, uid); final FakeLocation latLon = getFakeLocation(context, packageName, uid); if (latLon == null) return baseLocation; if (latLon == null) return baseLocation; return overrideLatLons(baseLocation, latLon); return overrideLatLon(baseLocation, latLon); } } public static boolean hasFakeLocation(Context context, CallerIdentity identity) { public static boolean hasFakeLocation(@NonNull Context context, @NonNull CallerIdentity identity) { int uid = identity.getUid(); final int uid = identity.getUid(); String packageName = identity.getPackageName(); final String packageName = identity.getPackageName(); if (context == null || packageName == null || uid < 0) { if (context == null || packageName == null || uid < 0) { Log.w(TAG, "FakeLocationResolver::hasFakeLocation invalid parameters"); Log.w(TAG, "FakeLocationResolver::hasFakeLocation invalid parameters"); return false; return false; } } FakeLocation latLon = getFakeLocation(context, packageName, uid); final FakeLocation latLon = getFakeLocation(context, packageName, uid); if (latLon == null) return false; if (latLon == null) return false; return true; return true; } } private static class FakeLocation { private static class FakeLocation { Double latitude; double latitude; Double longitude; double longitude; public FakeLocation(Double latitude, Double longitude) { public FakeLocation(double latitude, double longitude) { this.latitude = latitude; this.latitude = latitude; this.longitude = longitude; 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 { try { Bundle extra = new Bundle(); final Bundle extra = new Bundle(); extra.putInt(PARAM_UID, uid); extra.putInt(PARAM_UID, uid); Bundle result = context.getContentResolver().call( final Bundle result = context.getContentResolver().call( Uri.parse(FAKE_LOCATIONS_URI), Uri.parse(FAKE_LOCATIONS_URI), "", "", packageName, packageName, Loading @@ -110,18 +118,18 @@ public class FakeLocationResolver { return null; return null; } } private static List<Location> overrideLatLons(List<Location> baseLocations, FakeLocation latLon) { private static @NonNull List<Location> overrideLatLon(@NonNull List<Location> baseLocations, @NonNull FakeLocation latLon) { ArrayList<Location> fakedLocations = new ArrayList<Location>(baseLocations.size()); final ArrayList<Location> fakedLocations = new ArrayList<Location>(baseLocations.size()); for (Location location: baseLocations) { for (Location location: baseLocations) { Location fakedLocation = overrideLatLons(location, latLon); Location fakedLocation = overrideLatLon(location, latLon); fakedLocations.add(fakedLocation); fakedLocations.add(fakedLocation); } } return fakedLocations; return fakedLocations; } } private static Location overrideLatLons(Location baseLocation, FakeLocation latLon) { private static @NonNull Location overrideLatLon(@NonNull Location baseLocation, @NonNull FakeLocation latLon) { Location fakedLocation = new Location(baseLocation); final Location fakedLocation = new Location(baseLocation); fakedLocation.setLatitude(latLon.latitude); fakedLocation.setLatitude(latLon.latitude); fakedLocation.setLongitude(latLon.longitude); fakedLocation.setLongitude(latLon.longitude); fakedLocation.setAltitude(3.0); fakedLocation.setAltitude(3.0); Loading services/core/java/com/android/server/location/provider/LocationProviderManager.java +9 −8 Original line number Original line Diff line number Diff line Loading @@ -966,17 +966,17 @@ public class LocationProviderManager extends @Override @Override public void operate(LocationTransport listener) throws Exception { 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 // if delivering to the same process, make a copy of the location first (since // location is mutable) // location is mutable) LocationResult deliverLocationResult; LocationResult deliverLocationResult; if (getIdentity().getPid() == Process.myPid()) { if (getIdentity().getPid() == Process.myPid()) { deliverLocationResult = fakedLocationResult.deepCopy(); deliverLocationResult = locationResult.deepCopy(); } else { } else { deliverLocationResult = fakedLocationResult; deliverLocationResult = locationResult; } } deliverLocationResult = FakeLocationResolver.fakeLocations(mContext, deliverLocationResult, getIdentity()); listener.deliverOnLocationChanged(deliverLocationResult, listener.deliverOnLocationChanged(deliverLocationResult, mUseWakeLock ? mWakeLockReleaser : null); mUseWakeLock ? mWakeLockReleaser : null); EVENT_LOG.logProviderDeliveredLocations(mName, locationResult.size(), EVENT_LOG.logProviderDeliveredLocations(mName, locationResult.size(), Loading Loading @@ -1271,16 +1271,17 @@ public class LocationProviderManager extends return new ListenerOperation<LocationTransport>() { return new ListenerOperation<LocationTransport>() { @Override @Override public void operate(LocationTransport listener) throws Exception { 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 // if delivering to the same process, make a copy of the location first (since // location is mutable) // location is mutable) LocationResult deliverLocationResult; LocationResult deliverLocationResult; if (getIdentity().getPid() == Process.myPid() && fakedLocationResult != null) { if (getIdentity().getPid() == Process.myPid() && locationResult != null) { deliverLocationResult = fakedLocationResult.deepCopy(); deliverLocationResult = locationResult.deepCopy(); } else { } else { deliverLocationResult = fakedLocationResult; deliverLocationResult = locationResult; } } deliverLocationResult = FakeLocationResolver.fakeLocations(mContext, deliverLocationResult, getIdentity()); // we currently don't hold a wakelock for getCurrentLocation deliveries // we currently don't hold a wakelock for getCurrentLocation deliveries listener.deliverOnLocationChanged(deliverLocationResult, null); listener.deliverOnLocationChanged(deliverLocationResult, null); EVENT_LOG.logProviderDeliveredLocations(mName, EVENT_LOG.logProviderDeliveredLocations(mName, Loading Loading
services/core/java/com/android/server/location/FakeLocationResolver.java +33 −25 Original line number Original line Diff line number Diff line Loading @@ -17,6 +17,8 @@ package com.android.server.location; package com.android.server.location; import android.annotation.NonNull; import android.annotation.Nullable; import android.content.Context; import android.content.Context; import android.location.Location; import android.location.Location; import android.location.LocationResult; import android.location.LocationResult; Loading @@ -38,63 +40,69 @@ public class FakeLocationResolver { private static final String PARAM_LATITUDE = "latitude"; private static final String PARAM_LATITUDE = "latitude"; private static final String PARAM_LONGITUDE = "longitude"; private static final String PARAM_LONGITUDE = "longitude"; public static LocationResult fakeLocations(Context context, LocationResult baseLocations, CallerIdentity identity) { public static @Nullable LocationResult fakeLocations( int uid = identity.getUid(); @NonNull Context context, String packageName = identity.getPackageName(); @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) { if (baseLocations == null || context == null || packageName == null || uid < 0) { Log.w(TAG, "FakeLocationResolver::fakeLocations invalid parameters"); Log.w(TAG, "FakeLocationResolver::fakeLocations invalid parameters"); return baseLocations; return baseLocations; } } FakeLocation latLon = getFakeLocation(context, packageName, uid); final FakeLocation latLon = getFakeLocation(context, packageName, uid); if (latLon == null) return baseLocations; 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) { public static @Nullable Location fakeLocation( int uid = identity.getUid(); @NonNull Context context, String packageName = identity.getPackageName(); @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) { if (baseLocation == null || context == null || packageName == null || uid < 0) { Log.w(TAG, "FakeLocationResolver::fakeLocation invalid parameters"); Log.w(TAG, "FakeLocationResolver::fakeLocation invalid parameters"); return baseLocation; return baseLocation; } } FakeLocation latLon = getFakeLocation(context, packageName, uid); final FakeLocation latLon = getFakeLocation(context, packageName, uid); if (latLon == null) return baseLocation; if (latLon == null) return baseLocation; return overrideLatLons(baseLocation, latLon); return overrideLatLon(baseLocation, latLon); } } public static boolean hasFakeLocation(Context context, CallerIdentity identity) { public static boolean hasFakeLocation(@NonNull Context context, @NonNull CallerIdentity identity) { int uid = identity.getUid(); final int uid = identity.getUid(); String packageName = identity.getPackageName(); final String packageName = identity.getPackageName(); if (context == null || packageName == null || uid < 0) { if (context == null || packageName == null || uid < 0) { Log.w(TAG, "FakeLocationResolver::hasFakeLocation invalid parameters"); Log.w(TAG, "FakeLocationResolver::hasFakeLocation invalid parameters"); return false; return false; } } FakeLocation latLon = getFakeLocation(context, packageName, uid); final FakeLocation latLon = getFakeLocation(context, packageName, uid); if (latLon == null) return false; if (latLon == null) return false; return true; return true; } } private static class FakeLocation { private static class FakeLocation { Double latitude; double latitude; Double longitude; double longitude; public FakeLocation(Double latitude, Double longitude) { public FakeLocation(double latitude, double longitude) { this.latitude = latitude; this.latitude = latitude; this.longitude = longitude; 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 { try { Bundle extra = new Bundle(); final Bundle extra = new Bundle(); extra.putInt(PARAM_UID, uid); extra.putInt(PARAM_UID, uid); Bundle result = context.getContentResolver().call( final Bundle result = context.getContentResolver().call( Uri.parse(FAKE_LOCATIONS_URI), Uri.parse(FAKE_LOCATIONS_URI), "", "", packageName, packageName, Loading @@ -110,18 +118,18 @@ public class FakeLocationResolver { return null; return null; } } private static List<Location> overrideLatLons(List<Location> baseLocations, FakeLocation latLon) { private static @NonNull List<Location> overrideLatLon(@NonNull List<Location> baseLocations, @NonNull FakeLocation latLon) { ArrayList<Location> fakedLocations = new ArrayList<Location>(baseLocations.size()); final ArrayList<Location> fakedLocations = new ArrayList<Location>(baseLocations.size()); for (Location location: baseLocations) { for (Location location: baseLocations) { Location fakedLocation = overrideLatLons(location, latLon); Location fakedLocation = overrideLatLon(location, latLon); fakedLocations.add(fakedLocation); fakedLocations.add(fakedLocation); } } return fakedLocations; return fakedLocations; } } private static Location overrideLatLons(Location baseLocation, FakeLocation latLon) { private static @NonNull Location overrideLatLon(@NonNull Location baseLocation, @NonNull FakeLocation latLon) { Location fakedLocation = new Location(baseLocation); final Location fakedLocation = new Location(baseLocation); fakedLocation.setLatitude(latLon.latitude); fakedLocation.setLatitude(latLon.latitude); fakedLocation.setLongitude(latLon.longitude); fakedLocation.setLongitude(latLon.longitude); fakedLocation.setAltitude(3.0); fakedLocation.setAltitude(3.0); Loading
services/core/java/com/android/server/location/provider/LocationProviderManager.java +9 −8 Original line number Original line Diff line number Diff line Loading @@ -966,17 +966,17 @@ public class LocationProviderManager extends @Override @Override public void operate(LocationTransport listener) throws Exception { 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 // if delivering to the same process, make a copy of the location first (since // location is mutable) // location is mutable) LocationResult deliverLocationResult; LocationResult deliverLocationResult; if (getIdentity().getPid() == Process.myPid()) { if (getIdentity().getPid() == Process.myPid()) { deliverLocationResult = fakedLocationResult.deepCopy(); deliverLocationResult = locationResult.deepCopy(); } else { } else { deliverLocationResult = fakedLocationResult; deliverLocationResult = locationResult; } } deliverLocationResult = FakeLocationResolver.fakeLocations(mContext, deliverLocationResult, getIdentity()); listener.deliverOnLocationChanged(deliverLocationResult, listener.deliverOnLocationChanged(deliverLocationResult, mUseWakeLock ? mWakeLockReleaser : null); mUseWakeLock ? mWakeLockReleaser : null); EVENT_LOG.logProviderDeliveredLocations(mName, locationResult.size(), EVENT_LOG.logProviderDeliveredLocations(mName, locationResult.size(), Loading Loading @@ -1271,16 +1271,17 @@ public class LocationProviderManager extends return new ListenerOperation<LocationTransport>() { return new ListenerOperation<LocationTransport>() { @Override @Override public void operate(LocationTransport listener) throws Exception { 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 // if delivering to the same process, make a copy of the location first (since // location is mutable) // location is mutable) LocationResult deliverLocationResult; LocationResult deliverLocationResult; if (getIdentity().getPid() == Process.myPid() && fakedLocationResult != null) { if (getIdentity().getPid() == Process.myPid() && locationResult != null) { deliverLocationResult = fakedLocationResult.deepCopy(); deliverLocationResult = locationResult.deepCopy(); } else { } else { deliverLocationResult = fakedLocationResult; deliverLocationResult = locationResult; } } deliverLocationResult = FakeLocationResolver.fakeLocations(mContext, deliverLocationResult, getIdentity()); // we currently don't hold a wakelock for getCurrentLocation deliveries // we currently don't hold a wakelock for getCurrentLocation deliveries listener.deliverOnLocationChanged(deliverLocationResult, null); listener.deliverOnLocationChanged(deliverLocationResult, null); EVENT_LOG.logProviderDeliveredLocations(mName, EVENT_LOG.logProviderDeliveredLocations(mName, Loading