Loading core/java/android/companion/CompanionDeviceManager.java +1 −1 Original line number Diff line number Diff line Loading @@ -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(); } Loading core/java/android/companion/ICompanionDeviceManager.aidl +1 −1 Original line number Diff line number Diff line Loading @@ -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 Loading core/java/com/android/internal/util/Preconditions.java +20 −0 Original line number Diff line number Diff line Loading @@ -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. Loading services/print/java/com/android/server/print/CompanionDeviceManagerService.java +35 −9 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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(); } Loading Loading @@ -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 Loading @@ -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()); } Loading Loading @@ -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, Loading Loading
core/java/android/companion/CompanionDeviceManager.java +1 −1 Original line number Diff line number Diff line Loading @@ -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(); } Loading
core/java/android/companion/ICompanionDeviceManager.aidl +1 −1 Original line number Diff line number Diff line Loading @@ -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 Loading
core/java/com/android/internal/util/Preconditions.java +20 −0 Original line number Diff line number Diff line Loading @@ -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. Loading
services/print/java/com/android/server/print/CompanionDeviceManagerService.java +35 −9 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading Loading @@ -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(); } Loading Loading @@ -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 Loading @@ -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()); } Loading Loading @@ -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, Loading