From 372ab606e7585c2848ef2d4874877f1cde43afdb Mon Sep 17 00:00:00 2001 From: Rubin Xu Date: Thu, 13 May 2021 14:11:13 +0100 Subject: [PATCH] Send network & security logging callbacks in foreground Send ACTION_NETWORK_LOGS_AVAILABLE and ACTION_SECURITY_LOGS_AVAILABLE as foreground broadcasts to reduce test time. Updated javadoc on callbacks in DeviceAdminReceiver and DelegatedAdminReceiver. Remove obsolete special handling of the two broadcasts above in sendDeviceOwnerCommand(), now that they are only sent through sendDeviceOwnerOrProfileOwnerCommand(). Bug: 183066771 Test: atest MixedManagedProfileOwnerTest#testNetworkLogging Change-Id: I0758c0eb98be43b24cd08105a2449c349cfd4e6d --- .../app/admin/DelegatedAdminReceiver.java | 8 +++++ .../app/admin/DeviceAdminReceiver.java | 13 +++++-- .../DevicePolicyManagerService.java | 34 ++++++++----------- 3 files changed, 34 insertions(+), 21 deletions(-) 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 23a67b72c7346..bdf55916ffa9e 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -7848,56 +7848,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);