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

Commit 00af9a32 authored by Soonil Nagarkar's avatar Soonil Nagarkar
Browse files

Ask location provider for its package name

The package name from the service watcher may be inconsistent with the
package name of the currently bound service.

Test: none
Change-Id: Iede437d2e61a0da0b853fdbb133a29add04f6bf3
parent e4e09739
Loading
Loading
Loading
Loading
+1 −8
Original line number Diff line number Diff line
@@ -25,15 +25,8 @@ import com.android.internal.location.ProviderProperties;
 * @hide
 */
interface ILocationProviderManager {

    void onSetAttributionTag(String attributionTag);

    @UnsupportedAppUsage
    void onSetIdentity(@nullable String packageName, @nullable String attributionTag);
    void onSetAllowed(boolean allowed);

    @UnsupportedAppUsage
    void onSetProperties(in ProviderProperties properties);

    @UnsupportedAppUsage
    void onReportLocation(in Location location);
}
+6 −5
Original line number Diff line number Diff line
@@ -79,7 +79,8 @@ public abstract class LocationProviderBase {
    public static final String FUSED_PROVIDER = LocationManager.FUSED_PROVIDER;

    final String mTag;
    final String mAttributionTag;
    @Nullable final String mPackageName;
    @Nullable final String mAttributionTag;
    final IBinder mBinder;

    /**
@@ -93,8 +94,7 @@ public abstract class LocationProviderBase {
    protected final ILocationManager mLocationManager;

    // write locked on mBinder, read lock is optional depending on atomicity requirements
    @Nullable
    volatile ILocationProviderManager mManager;
    @Nullable volatile ILocationProviderManager mManager;
    volatile ProviderProperties mProperties;
    volatile boolean mAllowed;

@@ -116,6 +116,7 @@ public abstract class LocationProviderBase {
    public LocationProviderBase(Context context, String tag,
            ProviderPropertiesUnbundled properties) {
        mTag = tag;
        mPackageName = context != null ? context.getPackageName() : null;
        mAttributionTag = context != null ? context.getAttributionTag() : null;
        mBinder = new Service();

@@ -332,8 +333,8 @@ public abstract class LocationProviderBase {
        public void setLocationProviderManager(ILocationProviderManager manager) {
            synchronized (mBinder) {
                try {
                    if (mAttributionTag != null) {
                        manager.onSetAttributionTag(mAttributionTag);
                    if (mPackageName != null || mAttributionTag != null) {
                        manager.onSetIdentity(mPackageName, mAttributionTag);
                    }
                    manager.onSetProperties(mProperties);
                    manager.onSetAllowed(mAllowed);
+1 −1
Original line number Diff line number Diff line
@@ -199,7 +199,7 @@ public class FusedLocationServiceTest {
        }

        @Override
        public void onSetAttributionTag(String attributionTag) {
        public void onSetIdentity(String packageName, String attributionTag) {

        }

+15 −12
Original line number Diff line number Diff line
@@ -56,8 +56,7 @@ import java.util.Objects;

/**
 * Maintains a binding to the best service that matches the given intent information. Bind and
 * unbind callbacks, as well as all binder operations, will all be run on a single thread, but the
 * exact thread is left undefined.
 * unbind callbacks, as well as all binder operations, will all be run on a single thread.
 */
public class ServiceWatcher implements ServiceConnection {

@@ -73,7 +72,11 @@ public class ServiceWatcher implements ServiceConnection {
    public interface BinderRunner {
        /** Called to run client code with the binder. */
        void run(IBinder binder) throws RemoteException;
        /** Called if an error occurred and the function could not be run. */
        /**
         * Called if an error occurred and the function could not be run. This callback is only
         * intended for resource deallocation and cleanup in response to a single binder operation,
         * it should not be used to propagate errors further.
         */
        default void onError() {}
    }

@@ -189,8 +192,15 @@ public class ServiceWatcher implements ServiceConnection {
    public ServiceWatcher(Context context, String action,
            @Nullable BinderRunner onBind, @Nullable Runnable onUnbind,
            @BoolRes int enableOverlayResId, @StringRes int nonOverlayPackageResId) {
        this(context, FgThread.getHandler(), action, onBind, onUnbind, enableOverlayResId,
                nonOverlayPackageResId);
    }

    public ServiceWatcher(Context context, Handler handler, String action,
            @Nullable BinderRunner onBind, @Nullable Runnable onUnbind,
            @BoolRes int enableOverlayResId, @StringRes int nonOverlayPackageResId) {
        mContext = context;
        mHandler = FgThread.getHandler();
        mHandler = handler;
        mIntent = new Intent(Objects.requireNonNull(action));

        Resources resources = context.getResources();
@@ -278,13 +288,6 @@ public class ServiceWatcher implements ServiceConnection {
        return true;
    }

    /**
     * Returns information on the currently selected service.
     */
    public ServiceInfo getBoundService() {
        return mServiceInfo;
    }

    private void onBestServiceChanged(boolean forceRebind) {
        Preconditions.checkState(Looper.myLooper() == mHandler.getLooper());

@@ -380,7 +383,7 @@ public class ServiceWatcher implements ServiceConnection {
    }

    @Override
    public void onBindingDied(ComponentName component) {
    public final void onBindingDied(ComponentName component) {
        Preconditions.checkState(Looper.myLooper() == mHandler.getLooper());

        if (D) {
+22 −13
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.annotation.Nullable;
import android.content.Context;
import android.location.Location;
import android.location.util.identity.CallerIdentity;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
@@ -128,27 +129,38 @@ public class LocationProviderProxy extends AbstractLocationProvider {
        mServiceWatcher.dump(fd, pw, args);
    }

    private static String guessPackageName(Context context, int uid) {
        String[] packageNames = context.getPackageManager().getPackagesForUid(uid);
        if (packageNames == null || packageNames.length == 0) {
            // illegal state exception will propagate back through binders
            throw new IllegalStateException(
                    "location provider from uid " + uid + " has no package information");
        } else {
            return packageNames[0];
        }
    }

    private class Proxy extends ILocationProviderManager.Stub {

        Proxy() {}

        // executed on binder thread
        @Override
        public void onSetAttributionTag(String attributionTag) {
        public void onSetIdentity(@Nullable String packageName, @Nullable String attributionTag) {
            synchronized (mLock) {
                if (mProxy != this) {
                    return;
                }

                String packageName = mServiceWatcher.getBoundService().getPackageName();
                CallerIdentity identity;
                if (packageName == null) {
                    return;
                    packageName = guessPackageName(mContext, Binder.getCallingUid());
                    // unsafe is ok since the package is coming direct from the package manager here
                    identity = CallerIdentity.fromBinderUnsafe(packageName, attributionTag);
                } else {
                    identity = CallerIdentity.fromBinder(mContext, packageName, attributionTag);
                }

                // we don't need to verify the package name because we're getting it straight from
                // the service watcher
                CallerIdentity identity = CallerIdentity.fromBinderUnsafe(packageName,
                        attributionTag);
                setIdentity(identity);
            }
        }
@@ -163,13 +175,10 @@ public class LocationProviderProxy extends AbstractLocationProvider {

                // if no identity is set yet, set it now
                if (getIdentity() == null) {
                    String packageName = mServiceWatcher.getBoundService().getPackageName();
                    if (packageName != null) {
                        // we don't need to verify the package name because we're getting it
                        // straight from the service watcher
                    String packageName = guessPackageName(mContext, Binder.getCallingUid());
                    // unsafe is ok since the package is coming direct from the package manager here
                    setIdentity(CallerIdentity.fromBinderUnsafe(packageName, null));
                }
                }

                setProperties(properties);
            }