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

Commit 0ac8fd7a authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add ICompanionDeviceManager.getAssociation overload with explicit userId" into oc-dev

parents e9ec87d0 adce09b9
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -167,7 +167,7 @@ public final class CompanionDeviceManager {
            return Collections.emptyList();
            return Collections.emptyList();
        }
        }
        try {
        try {
            return mService.getAssociations(mContext.getPackageName());
            return mService.getAssociations(mContext.getPackageName(), mContext.getUserId());
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
            throw e.rethrowFromSystemServer();
        }
        }
+1 −1
Original line number Original line Diff line number Diff line
@@ -29,7 +29,7 @@ interface ICompanionDeviceManager {
        in IFindDeviceCallback callback,
        in IFindDeviceCallback callback,
        in String callingPackage);
        in String callingPackage);


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


    //TODO add these
    //TODO add these
+20 −0
Original line number Original line Diff line number Diff line
@@ -113,6 +113,26 @@ public class Preconditions {
        return reference;
        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
     * Ensures the truth of an expression involving the state of the calling
     * instance, but not involving any parameters to the calling method.
     * instance, but not involving any parameters to the calling method.
+35 −9
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@


package com.android.server.print;
package com.android.server.print;


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


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


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


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


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




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


        @Override
        @Override
        public void disassociate(String deviceMacAddress, String callingPackage) {
        public void disassociate(String deviceMacAddress, String callingPackage)
            updateAssociations((associations) -> ArrayUtils.remove(associations,
                throws RemoteException {
                    new Association(getUserId(), checkNotNull(deviceMacAddress), callingPackage)));
            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());
        return UserHandle.getUserId(Binder.getCallingUid());
    }
    }


@@ -320,11 +346,11 @@ public class CompanionDeviceManagerService extends SystemService implements Bind


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


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


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