diff options
4 files changed, 57 insertions, 11 deletions
diff --git a/core/java/android/companion/CompanionDeviceManager.java b/core/java/android/companion/CompanionDeviceManager.java index 7b38863c9cc6..4d788e783f34 100644 --- a/core/java/android/companion/CompanionDeviceManager.java +++ b/core/java/android/companion/CompanionDeviceManager.java @@ -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(); } diff --git a/core/java/android/companion/ICompanionDeviceManager.aidl b/core/java/android/companion/ICompanionDeviceManager.aidl index 495141d75a44..7798406c35e5 100644 --- a/core/java/android/companion/ICompanionDeviceManager.aidl +++ b/core/java/android/companion/ICompanionDeviceManager.aidl @@ -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 diff --git a/core/java/com/android/internal/util/Preconditions.java b/core/java/com/android/internal/util/Preconditions.java index 1ead5b3de1de..4e6857a72b43 100644 --- a/core/java/com/android/internal/util/Preconditions.java +++ b/core/java/com/android/internal/util/Preconditions.java @@ -114,6 +114,26 @@ public class Preconditions { } /** + * 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. * diff --git a/services/print/java/com/android/server/print/CompanionDeviceManagerService.java b/services/print/java/com/android/server/print/CompanionDeviceManagerService.java index 7790698d02e9..d0dfc6cc4fe8 100644 --- a/services/print/java/com/android/server/print/CompanionDeviceManagerService.java +++ b/services/print/java/com/android/server/print/CompanionDeviceManagerService.java @@ -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 @@ -203,20 +209,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()); } @@ -320,11 +346,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, |