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

Commit 79abc1da authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Send admin enabled broadcast in foreground queue"

parents a1f7fb9b 714f77b9
Loading
Loading
Loading
Loading
+39 −10
Original line number Original line Diff line number Diff line
@@ -615,11 +615,15 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                }
                }
            } else if (Intent.ACTION_USER_STARTED.equals(action)) {
            } else if (Intent.ACTION_USER_STARTED.equals(action)) {
                synchronized (DevicePolicyManagerService.this) {
                synchronized (DevicePolicyManagerService.this) {
                    maybeSendAdminEnabledBroadcastLocked(userHandle);
                    // Reset the policy data
                    // Reset the policy data
                    mUserData.remove(userHandle);
                    mUserData.remove(userHandle);
                    sendAdminEnabledBroadcastLocked(userHandle);
                }
                }
                handlePackagesChanged(null /* check all admins */, userHandle);
                handlePackagesChanged(null /* check all admins */, userHandle);
            } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) {
                synchronized (DevicePolicyManagerService.this) {
                    maybeSendAdminEnabledBroadcastLocked(userHandle);
                }
            } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
            } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
                handlePackagesChanged(null /* check all admins */, userHandle);
                handlePackagesChanged(null /* check all admins */, userHandle);
            } else if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
            } else if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
@@ -1855,6 +1859,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        filter.addAction(Intent.ACTION_USER_ADDED);
        filter.addAction(Intent.ACTION_USER_ADDED);
        filter.addAction(Intent.ACTION_USER_REMOVED);
        filter.addAction(Intent.ACTION_USER_REMOVED);
        filter.addAction(Intent.ACTION_USER_STARTED);
        filter.addAction(Intent.ACTION_USER_STARTED);
        filter.addAction(Intent.ACTION_USER_UNLOCKED);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, mHandler);
        mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, mHandler);
        filter = new IntentFilter();
        filter = new IntentFilter();
@@ -2373,11 +2378,18 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        sendAdminCommandLocked(admin, action, null, result);
        sendAdminCommandLocked(admin, action, null, result);
    }
    }


    void sendAdminCommandLocked(ActiveAdmin admin, String action, Bundle adminExtras,
            BroadcastReceiver result) {
        sendAdminCommandLocked(admin, action, adminExtras, result, false);
    }

    /**
    /**
     * Send an update to one specific admin, get notified when that admin returns a result.
     * Send an update to one specific admin, get notified when that admin returns a result.
     *
     * @return whether the broadcast was successfully sent
     */
     */
    void sendAdminCommandLocked(ActiveAdmin admin, String action, Bundle adminExtras,
    boolean sendAdminCommandLocked(ActiveAdmin admin, String action, Bundle adminExtras,
            BroadcastReceiver result) {
            BroadcastReceiver result, boolean inForeground) {
        Intent intent = new Intent(action);
        Intent intent = new Intent(action);
        intent.setComponent(admin.info.getComponent());
        intent.setComponent(admin.info.getComponent());
        if (UserManager.isDeviceInDemoMode(mContext)) {
        if (UserManager.isDeviceInDemoMode(mContext)) {
@@ -2386,15 +2398,25 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        if (action.equals(DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING)) {
        if (action.equals(DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING)) {
            intent.putExtra("expiration", admin.passwordExpirationDate);
            intent.putExtra("expiration", admin.passwordExpirationDate);
        }
        }
        if (inForeground) {
            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        }
        if (adminExtras != null) {
        if (adminExtras != null) {
            intent.putExtras(adminExtras);
            intent.putExtras(adminExtras);
        }
        }
        if (mInjector.getPackageManager().queryBroadcastReceiversAsUser(
                intent,
                PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
                admin.getUserHandle()).isEmpty()) {
            return false;
        }
        if (result != null) {
        if (result != null) {
            mContext.sendOrderedBroadcastAsUser(intent, admin.getUserHandle(),
            mContext.sendOrderedBroadcastAsUser(intent, admin.getUserHandle(),
                    null, result, mHandler, Activity.RESULT_OK, null, null);
                    null, result, mHandler, Activity.RESULT_OK, null, null);
        } else {
        } else {
            mContext.sendBroadcastAsUser(intent, admin.getUserHandle());
            mContext.sendBroadcastAsUser(intent, admin.getUserHandle());
        }
        }
        return true;
    }
    }


    /**
    /**
@@ -8100,22 +8122,29 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
        }
        }
    }
    }



    private void maybeSendAdminEnabledBroadcastLocked(int userHandle) {
    private void sendAdminEnabledBroadcastLocked(int userHandle) {
        DevicePolicyData policyData = getUserData(userHandle);
        DevicePolicyData policyData = getUserData(userHandle);
        if (policyData.mAdminBroadcastPending) {
        if (policyData.mAdminBroadcastPending) {
            // Send the initialization data to profile owner and delete the data
            // Send the initialization data to profile owner and delete the data
            ActiveAdmin admin = getProfileOwnerAdminLocked(userHandle);
            ActiveAdmin admin = getProfileOwnerAdminLocked(userHandle);
            boolean clearInitBundle = true;
            if (admin != null) {
            if (admin != null) {
                PersistableBundle initBundle = policyData.mInitBundle;
                PersistableBundle initBundle = policyData.mInitBundle;
                sendAdminCommandLocked(admin, DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED,
                clearInitBundle = sendAdminCommandLocked(admin,
                        initBundle == null ? null : new Bundle(initBundle), null);
                        DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED,
            }
                        initBundle == null ? null : new Bundle(initBundle),
                        null /* result receiver */,
                        true /* send in foreground */);
            }
            if (clearInitBundle) {
                // If there's no admin or we've successfully called the admin, clear the init bundle
                // otherwise, keep it around
                policyData.mInitBundle = null;
                policyData.mInitBundle = null;
                policyData.mAdminBroadcastPending = false;
                policyData.mAdminBroadcastPending = false;
                saveSettingsLocked(userHandle);
                saveSettingsLocked(userHandle);
            }
            }
        }
        }
    }


    @Override
    @Override
    public UserHandle createAndManageUser(ComponentName admin, String name,
    public UserHandle createAndManageUser(ComponentName admin, String name,
+6 −0
Original line number Original line Diff line number Diff line
@@ -58,6 +58,7 @@ import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.StringParceledListSlice;
import android.content.pm.StringParceledListSlice;
import android.content.pm.UserInfo;
import android.content.pm.UserInfo;
import android.graphics.Color;
import android.graphics.Color;
@@ -166,6 +167,11 @@ public class DevicePolicyManagerTest extends DpmTestBase {
        mServiceContext.binder.callingUid = DpmMockContext.CALLER_UID;
        mServiceContext.binder.callingUid = DpmMockContext.CALLER_UID;
        when(getServices().packageManager.hasSystemFeature(eq(PackageManager.FEATURE_DEVICE_ADMIN)))
        when(getServices().packageManager.hasSystemFeature(eq(PackageManager.FEATURE_DEVICE_ADMIN)))
                .thenReturn(true);
                .thenReturn(true);
        doReturn(Collections.singletonList(new ResolveInfo()))
                .when(getServices().packageManager).queryBroadcastReceiversAsUser(
                        any(Intent.class),
                        anyInt(),
                        any(UserHandle.class));


        // By default, pretend all users are running and unlocked.
        // By default, pretend all users are running and unlocked.
        when(getServices().userManager.isUserUnlocked(anyInt())).thenReturn(true);
        when(getServices().userManager.isUserUnlocked(anyInt())).thenReturn(true);