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

Commit 372ab606 authored by Rubin Xu's avatar Rubin Xu
Browse files

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
parent bd5c79d2
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -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}.
     *
     * <p>
     * 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}.
     *
     * <p>
     * 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
+11 −2
Original line number Diff line number Diff line
@@ -942,7 +942,12 @@ public class DeviceAdminReceiver extends BroadcastReceiver {
     *
     * <p>This callback will be re-triggered if the logs are not retrieved.
     *
     * <p>This callback is only applicable to device owners.
     * <p>This callback is only applicable to device owners and profile owners of
     * organization-owned managed profiles.
     *
     * <p>
     * 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}.
     *
     * <p>This callback is only applicable to device owners.
     * <p>This callback is only applicable to device owners and profile owners.
     *
     * <p>
     * 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}.
+15 −19
Original line number Diff line number Diff line
@@ -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();
        }
        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);