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

Commit 544d7146 authored by Eugene Susla's avatar Eugene Susla
Browse files

Add ICompanionDeviceManager.getAssociation overload with explicit userId

Bug: 30932767
Test: ensure getAssociations works as before
Change-Id: I58dc4dac5bf7aae83fe3611890116a928e290a43
parent c07e4ae2
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -167,7 +167,7 @@ public final class CompanionDeviceManager {
            return Collections.emptyList();
        }
        try {
            return mService.getAssociations(mContext.getPackageName());
            return mService.getAssociations(mContext.getPackageName(), mContext.getUserId());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ interface ICompanionDeviceManager {
        in IFindDeviceCallback callback,
        in String callingPackage);

    List<String> getAssociations(String callingPackage);
    List<String> getAssociations(String callingPackage, int userId);
    void disassociate(String deviceMacAddress, String callingPackage);

    //TODO add these
+20 −0
Original line number Diff line number Diff line
@@ -113,6 +113,26 @@ public class Preconditions {
        return reference;
    }

    /**
     * Ensures that an object reference passed as a parameter to the calling
     * method is not null.
     *
     * @param reference an object reference
     * @param messageTemplate a printf-style message template to use if the check fails; will
     *     be converted to a string using {@link String#format(String, Object...)}
     * @param messageArgs arguments for {@code messageTemplate}
     * @return the non-null reference that was validated
     * @throws NullPointerException if {@code reference} is null
     */
    public static @NonNull <T> T checkNotNull(final T reference,
            final String messageTemplate,
            final Object... messageArgs) {
        if (reference == null) {
            throw new NullPointerException(String.format(messageTemplate, messageArgs));
        }
        return reference;
    }

    /**
     * Ensures the truth of an expression involving the state of the calling
     * instance, but not involving any parameters to the calling method.
+35 −9
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@

package com.android.server.print;

import static com.android.internal.util.Preconditions.checkArgument;
import static com.android.internal.util.Preconditions.checkNotNull;

import android.Manifest;
@@ -50,6 +51,7 @@ import android.util.ExceptionUtils;
import android.util.Slog;
import android.util.Xml;

import com.android.internal.app.IAppOpsService;
import com.android.internal.content.PackageMonitor;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.CollectionUtils;
@@ -98,12 +100,15 @@ public class CompanionDeviceManagerService extends SystemService implements Bind
    private IDeviceIdleController mIdleController;
    private IFindDeviceCallback mFindDeviceCallback;
    private ServiceConnection mServiceConnection;
    private IAppOpsService mAppOpsManager;

    public CompanionDeviceManagerService(Context context) {
        super(context);
        mImpl = new CompanionDeviceManagerImpl();
        mIdleController = IDeviceIdleController.Stub.asInterface(
                ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER));
        mAppOpsManager = IAppOpsService.Stub.asInterface(
                ServiceManager.getService(Context.APP_OPS_SERVICE));
        registerPackageMonitor();
    }

@@ -182,13 +187,14 @@ public class CompanionDeviceManagerService extends SystemService implements Bind
        public void associate(
                AssociationRequest request,
                IFindDeviceCallback callback,
                String callingPackage) {
                String callingPackage) throws RemoteException {
            if (DEBUG) {
                Slog.i(LOG_TAG, "associate(request = " + request + ", callback = " + callback
                        + ", callingPackage = " + callingPackage + ")");
            }
            checkNotNull(request, "Request cannot be null");
            checkNotNull(callback, "Callback cannot be null");
            checkCallerIsSystemOr(callingPackage);
            final long callingIdentity = Binder.clearCallingIdentity();
            try {
                //TODO bindServiceAsUser
@@ -202,20 +208,40 @@ public class CompanionDeviceManagerService extends SystemService implements Bind
        }

        @Override
        public List<String> getAssociations(String callingPackage) {
        public List<String> getAssociations(String callingPackage, int userId)
                throws RemoteException {
            checkCallerIsSystemOr(callingPackage, userId);
            return CollectionUtils.map(
                    readAllAssociations(getUserId(), callingPackage),
                    readAllAssociations(userId, callingPackage),
                    a -> a.deviceAddress);
        }

        @Override
        public void disassociate(String deviceMacAddress, String callingPackage) {
            updateAssociations((associations) -> ArrayUtils.remove(associations,
                    new Association(getUserId(), checkNotNull(deviceMacAddress), callingPackage)));
        public void disassociate(String deviceMacAddress, String callingPackage)
                throws RemoteException {
            checkNotNull(deviceMacAddress);
            checkCallerIsSystemOr(callingPackage);
            updateAssociations(associations -> ArrayUtils.remove(associations,
                    new Association(getCallingUserId(), deviceMacAddress, callingPackage)));
        }

        private void checkCallerIsSystemOr(String pkg) throws RemoteException {
            checkCallerIsSystemOr(pkg, getCallingUserId());
        }

        private void checkCallerIsSystemOr(String pkg, int userId) throws RemoteException {
            if (getCallingUserId() == UserHandle.USER_SYSTEM) {
                return;
            }

            checkArgument(getCallingUserId() == userId,
                    "Must be called by either same user or system");

            mAppOpsManager.checkPackage(Binder.getCallingUid(), pkg);
        }
    }

    private int getUserId() {
    private int getCallingUserId() {
        return UserHandle.getUserId(Binder.getCallingUid());
    }

@@ -325,11 +351,11 @@ public class CompanionDeviceManagerService extends SystemService implements Bind

    private void recordAssociation(String priviledgedPackage, String deviceAddress) {
        updateAssociations((associations) -> ArrayUtils.add(associations,
                new Association(getUserId(), deviceAddress, priviledgedPackage)));
                new Association(getCallingUserId(), deviceAddress, priviledgedPackage)));
    }

    private void updateAssociations(Function<ArrayList<Association>, List<Association>> update) {
        updateAssociations(update, getUserId());
        updateAssociations(update, getCallingUserId());
    }

    private void updateAssociations(Function<ArrayList<Association>, List<Association>> update,