Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Alternate location proxy implementation
Browse files Browse the repository at this point in the history
Refs #2105
  • Loading branch information
M66B committed Jan 6, 2015
1 parent 348de98 commit e9b1f23
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions src/biz/bokhorst/xprivacy/XLocationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.app.PendingIntent;
import android.location.Location;
import android.os.Binder;
import android.os.Bundle;
import android.os.IInterface;
import android.util.Log;
import android.location.GpsSatellite;
Expand Down Expand Up @@ -326,19 +327,25 @@ private void proxyLocationListener(XParam param, int arg, Class<?> interfaze) th
param.setResult(null);

else if (param.args[arg] != null && param.thisObject != null) {
// Create proxy
ClassLoader cl = param.thisObject.getClass().getClassLoader();
InvocationHandler ih = new OnLocationChangedHandler(Binder.getCallingUid(), param.args[arg]);
Object proxy = Proxy.newProxyInstance(cl, new Class<?>[] { interfaze }, ih);

Object key = param.args[arg];
if (key instanceof IInterface)
key = ((IInterface) key).asBinder();
synchronized (mMapProxy) {
// Reuse existing proxy
if (mMapProxy.containsKey(key)) {
param.args[arg] = mMapProxy.get(key);
return;
}

// Already proxied
if (mMapProxy.containsValue(key))
return;
}

// Create proxy
Object proxy = new ProxyLocationListener(Binder.getCallingUid(), (LocationListener) param.args[arg]);

// Use proxy
synchronized (mMapProxy) {
mMapProxy.put(key, proxy);
Util.log(this, Log.INFO, "proxyLocationListener uid=" + Binder.getCallingUid());
}
param.args[arg] = proxy;
}
Expand All @@ -351,31 +358,42 @@ private void unproxyLocationListener(XParam param, int arg) {

else if (param.args[arg] != null) {
Object key = param.args[arg];
if (key instanceof IInterface)
key = ((IInterface) key).asBinder();

synchronized (mMapProxy) {
if (mMapProxy.containsKey(key)) {
param.args[arg] = mMapProxy.get(key);
Util.log(this, Log.INFO, "unproxyLocationListener uid=" + Binder.getCallingUid());
}
}
}
}

private class OnLocationChangedHandler implements InvocationHandler {
private static class ProxyLocationListener implements LocationListener {
private int mUid;
private Object mTarget;
private LocationListener mListener;

public OnLocationChangedHandler(int uid, Object target) {
public ProxyLocationListener(int uid, LocationListener listener) {
mUid = uid;
mTarget = target;
mListener = listener;
}

@Override
public void onLocationChanged(Location location) {
Location fakeLocation = PrivacyManager.getDefacedLocation(mUid, location);
mListener.onLocationChanged(fakeLocation);
}

@Override
public void onProviderDisabled(String provider) {
mListener.onProviderDisabled(provider);
}

@Override
public void onProviderEnabled(String provider) {
mListener.onProviderEnabled(provider);
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("onLocationChanged".equals(method.getName()))
args[0] = PrivacyManager.getDefacedLocation(mUid, (Location) args[0]);
return method.invoke(mTarget, args);
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
mListener.onStatusChanged(provider, status, extras);
}
}
}

0 comments on commit e9b1f23

Please sign in to comment.