diff --git a/core/java/android/app/admin/DelegatedAdminReceiver.java b/core/java/android/app/admin/DelegatedAdminReceiver.java index 36097c928bb08..c74ddb285644f 100644 --- a/core/java/android/app/admin/DelegatedAdminReceiver.java +++ b/core/java/android/app/admin/DelegatedAdminReceiver.java @@ -104,6 +104,10 @@ public class DelegatedAdminReceiver extends BroadcastReceiver { * receiver's manifest in order to receive this callback. The default implementation * simply throws {@link UnsupportedOperationException}. * + *

+ * This callback is triggered by a foreground broadcast and the app should ensure that any + * long-running work is not executed synchronously inside the callback. + * * @param context The running context as per {@link #onReceive}. * @param intent The received intent as per {@link #onReceive}. * @param batchToken The token representing the current batch of network logs. @@ -130,6 +134,10 @@ public class DelegatedAdminReceiver extends BroadcastReceiver { * the receiver's manifest in order to receive this callback. The default implementation * simply throws {@link UnsupportedOperationException}. * + *

+ * This callback is triggered by a foreground broadcast and the app should ensure that any + * long-running work is not executed synchronously inside the callback. + * * @param context The running context as per {@link #onReceive}. * @param intent The received intent as per {@link #onReceive}. * @see DevicePolicyManager#retrieveSecurityLogs diff --git a/core/java/android/app/admin/DeviceAdminReceiver.java b/core/java/android/app/admin/DeviceAdminReceiver.java index da64dcd5b5f52..27e8c46ca548d 100644 --- a/core/java/android/app/admin/DeviceAdminReceiver.java +++ b/core/java/android/app/admin/DeviceAdminReceiver.java @@ -942,7 +942,12 @@ public class DeviceAdminReceiver extends BroadcastReceiver { * *

This callback will be re-triggered if the logs are not retrieved. * - *

This callback is only applicable to device owners. + *

This callback is only applicable to device owners and profile owners of + * organization-owned managed profiles. + * + *

+ * This callback is triggered by a foreground broadcast and the app should ensure that any + * long-running work is not executed synchronously inside the callback. * * @param context The running context as per {@link #onReceive}. * @param intent The received intent as per {@link #onReceive}. @@ -961,7 +966,11 @@ public class DeviceAdminReceiver extends BroadcastReceiver { * possible to retrieve the network logs batch with the most recent {@code batchToken} provided * by this callback. See {@link DevicePolicyManager#setAffiliationIds}. * - *

This callback is only applicable to device owners. + *

This callback is only applicable to device owners and profile owners. + * + *

+ * This callback is triggered by a foreground broadcast and the app should ensure that any + * long-running work is not executed synchronously inside the callback. * * @param context The running context as per {@link #onReceive}. * @param intent The received intent as per {@link #onReceive}. diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index 5b2841ba7496d..8089fb1025132 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -7870,56 +7870,52 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { void sendDeviceOwnerCommand(String action, Bundle extras) { final int deviceOwnerUserId; + final ComponentName receiverComponent; synchronized (getLockObject()) { deviceOwnerUserId = mOwners.getDeviceOwnerUserId(); + receiverComponent = mOwners.getDeviceOwnerComponent(); } - - ComponentName receiverComponent = null; - if (action.equals(DeviceAdminReceiver.ACTION_NETWORK_LOGS_AVAILABLE)) { - receiverComponent = resolveDelegateReceiver(DELEGATION_NETWORK_LOGGING, action, - deviceOwnerUserId); - } - if (action.equals(DeviceAdminReceiver.ACTION_SECURITY_LOGS_AVAILABLE)) { - receiverComponent = resolveDelegateReceiver(DELEGATION_SECURITY_LOGGING, action, - deviceOwnerUserId); - } - if (receiverComponent == null) { - synchronized (getLockObject()) { - receiverComponent = mOwners.getDeviceOwnerComponent(); - } - } - sendActiveAdminCommand(action, extras, deviceOwnerUserId, receiverComponent); + sendActiveAdminCommand(action, extras, deviceOwnerUserId, receiverComponent, + /* inForeground */ false); } void sendDeviceOwnerOrProfileOwnerCommand(String action, Bundle extras, int userId) { if (userId == UserHandle.USER_ALL) { userId = UserHandle.USER_SYSTEM; } + boolean inForeground = false; ComponentName receiverComponent = null; if (action.equals(DeviceAdminReceiver.ACTION_NETWORK_LOGS_AVAILABLE)) { + inForeground = true; receiverComponent = resolveDelegateReceiver(DELEGATION_NETWORK_LOGGING, action, userId); } if (action.equals(DeviceAdminReceiver.ACTION_SECURITY_LOGS_AVAILABLE)) { + inForeground = true; receiverComponent = resolveDelegateReceiver( DELEGATION_SECURITY_LOGGING, action, userId); } if (receiverComponent == null) { receiverComponent = getOwnerComponent(userId); } - sendActiveAdminCommand(action, extras, userId, receiverComponent); + sendActiveAdminCommand(action, extras, userId, receiverComponent, inForeground); } private void sendProfileOwnerCommand(String action, Bundle extras, @UserIdInt int userId) { - sendActiveAdminCommand(action, extras, userId, mOwners.getProfileOwnerComponent(userId)); + sendActiveAdminCommand(action, extras, userId, mOwners.getProfileOwnerComponent(userId), + /* inForeground */ false); } private void sendActiveAdminCommand(String action, Bundle extras, - @UserIdInt int userId, ComponentName receiverComponent) { + @UserIdInt int userId, ComponentName receiverComponent, boolean inForeground) { final Intent intent = new Intent(action); intent.setComponent(receiverComponent); if (extras != null) { intent.putExtras(extras); } + if (inForeground) { + intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); + } + if (VERBOSE_LOG) { Slogf.v(LOG_TAG, "sendActiveAdminCommand(): broadcasting " + action + " to " + receiverComponent.flattenToShortString() + " on user " + userId);