[scv2] RESTRICT AUTOMERGE Use finalizeWorkProfileProvisioning.

Test: Manual
Bug: 210469972
Change-Id: I6f71f11871c940b41978d35185051b431371a1b5
diff --git a/src/com/android/managedprovisioning/finalization/PrimaryProfileFinalizationHelper.java b/src/com/android/managedprovisioning/finalization/PrimaryProfileFinalizationHelper.java
index f1d2d7d..7988404 100644
--- a/src/com/android/managedprovisioning/finalization/PrimaryProfileFinalizationHelper.java
+++ b/src/com/android/managedprovisioning/finalization/PrimaryProfileFinalizationHelper.java
@@ -16,14 +16,11 @@
 
 package com.android.managedprovisioning.finalization;
 
-import static android.app.admin.DevicePolicyManager.ACTION_MANAGED_PROFILE_PROVISIONED;
-import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;
-
 import static com.android.internal.util.Preconditions.checkNotNull;
 
 import android.accounts.Account;
+import android.app.admin.DevicePolicyManager;
 import android.content.Context;
-import android.content.Intent;
 import android.os.UserHandle;
 
 /**
@@ -46,22 +43,11 @@
 
     void finalizeProvisioningInPrimaryProfile(Context context,
             DpcReceivedSuccessReceiver.Callback callback) {
-        final Intent primaryProfileSuccessIntent = new Intent(ACTION_MANAGED_PROFILE_PROVISIONED);
-        primaryProfileSuccessIntent.setPackage(mMdmPackageName);
-        primaryProfileSuccessIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES |
-                Intent.FLAG_RECEIVER_FOREGROUND);
-        primaryProfileSuccessIntent.putExtra(Intent.EXTRA_USER, mManagedUserHandle);
+        DevicePolicyManager devicePolicyManager = (DevicePolicyManager)
+                context.getSystemService(Context.DEVICE_POLICY_SERVICE);
+        devicePolicyManager.finalizeWorkProfileProvisioning(
+                mManagedUserHandle, mMigratedAccount);
 
-        if (mMigratedAccount != null) {
-            primaryProfileSuccessIntent.putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE,
-                    mMigratedAccount);
-        }
-        handleFinalization(context, callback, primaryProfileSuccessIntent);
-    }
-
-    private void handleFinalization(Context context, DpcReceivedSuccessReceiver.Callback callback,
-            Intent primaryProfileSuccessIntent) {
-        context.sendBroadcast(primaryProfileSuccessIntent);
         if (callback != null) {
             callback.cleanup();
         }
diff --git a/tests/instrumentation/src/com/android/managedprovisioning/finalization/DpcReceivedSuccessReceiverTest.java b/tests/instrumentation/src/com/android/managedprovisioning/finalization/DpcReceivedSuccessReceiverTest.java
index 4b91686..30d30de 100644
--- a/tests/instrumentation/src/com/android/managedprovisioning/finalization/DpcReceivedSuccessReceiverTest.java
+++ b/tests/instrumentation/src/com/android/managedprovisioning/finalization/DpcReceivedSuccessReceiverTest.java
@@ -17,10 +17,11 @@
 package com.android.managedprovisioning.finalization;
 
 import static android.app.admin.DeviceAdminReceiver.ACTION_PROFILE_PROVISIONING_COMPLETE;
-import static android.app.admin.DevicePolicyManager.ACTION_MANAGED_PROFILE_PROVISIONED;
 
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
+import android.app.admin.DevicePolicyManager;
 import android.content.Context;
 import android.content.Intent;
 import android.os.UserHandle;
@@ -29,7 +30,6 @@
 
 import com.android.managedprovisioning.common.Utils;
 
-import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
@@ -43,12 +43,18 @@
 
     @Mock private Context mContext;
     @Mock private Utils mUtils;
+    @Mock private DevicePolicyManager mDevicePolicyManager;
 
     @Override
     public void setUp() {
         // this is necessary for mockito to work
         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
         MockitoAnnotations.initMocks(this);
+
+        when(mContext.getSystemServiceName(DevicePolicyManager.class))
+                .thenReturn(Context.DEVICE_POLICY_SERVICE);
+        when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
+                .thenReturn(mDevicePolicyManager);
     }
 
     @SmallTest
@@ -61,18 +67,8 @@
         // WHEN the profile provisioning complete intent was received by the DPC
         receiver.onReceive(mContext, TEST_INTENT);
 
-        // THEN an intent should be sent to the primary user
-        ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
-        verify(mContext).sendBroadcast(intentCaptor.capture());
-
-        // THEN the broadcast action is ACTION_MANAGED_PROFILE_PROVISIONED
-        assertEquals(ACTION_MANAGED_PROFILE_PROVISIONED, intentCaptor.getValue().getAction());
-
-        // THEN the receiver package is the DPC
-        assertEquals(TEST_MDM_PACKAGE_NAME, intentCaptor.getValue().getPackage());
-
-        // THEN the extra user handle should be of managed profile
-        assertEquals(MANAGED_PROFILE_USER_HANDLE,
-                intentCaptor.getValue().getExtra(Intent.EXTRA_USER));
+        // THEN the system should be told to finalize the provisioning
+        verify(mDevicePolicyManager).finalizeWorkProfileProvisioning(
+                MANAGED_PROFILE_USER_HANDLE, /* migratedAccount= */ null);
     }
 }
diff --git a/tests/instrumentation/src/com/android/managedprovisioning/finalization/FinalizationInsideSuwControllerTest.java b/tests/instrumentation/src/com/android/managedprovisioning/finalization/FinalizationInsideSuwControllerTest.java
index e52220e..363712b 100644
--- a/tests/instrumentation/src/com/android/managedprovisioning/finalization/FinalizationInsideSuwControllerTest.java
+++ b/tests/instrumentation/src/com/android/managedprovisioning/finalization/FinalizationInsideSuwControllerTest.java
@@ -91,6 +91,7 @@
     @Mock private DeferredMetricsReader mDeferredMetricsReader;
     @Mock private ProvisioningAnalyticsTracker mProvisioningAnalyticsTracker;
     @Mock private UserManager mUserManager;
+    @Mock private DevicePolicyManager mDevicePolicyManager;
     @Mock private SharedPreferences mSharedPreferences;
 
     private PreFinalizationController mPreFinalizationController;
@@ -103,6 +104,10 @@
         // this is necessary for mockito to work
         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
         MockitoAnnotations.initMocks(this);
+        when(mActivity.getSystemService(Context.DEVICE_POLICY_SERVICE))
+                .thenReturn(mDevicePolicyManager);
+        when(mActivity.getSystemServiceName(DevicePolicyManager.class))
+                .thenReturn(Context.DEVICE_POLICY_SERVICE);
         when(mUtils.canResolveIntentAsUser(any(Context.class), any(Intent.class), anyInt()))
                 .thenReturn(true);
         when(mActivity.getFilesDir()).thenReturn(getContext().getFilesDir());
@@ -362,13 +367,9 @@
         when(mUtils.getManagedProfile(mActivity))
                 .thenReturn(MANAGED_PROFILE_USER_HANDLE);
 
-        // Mock DPM for testing access to device IDs is granted.
-        final DevicePolicyManager mockDpm = mock(DevicePolicyManager.class);
-        when(mActivity.getSystemServiceName(DevicePolicyManager.class))
-                .thenReturn(Context.DEVICE_POLICY_SERVICE);
-        when(mActivity.getSystemService(DevicePolicyManager.class)).thenReturn(mockDpm);
         final int managedProfileUserId = MANAGED_PROFILE_USER_HANDLE.getIdentifier();
-        when(mockDpm.getProfileOwnerAsUser(managedProfileUserId)).thenReturn(TEST_MDM_ADMIN);
+        when(mDevicePolicyManager.getProfileOwnerAsUser(managedProfileUserId))
+                .thenReturn(TEST_MDM_ADMIN);
 
         // Actual Device IDs access is  granted to the DPM of the managed profile, in the context
         // of the managed profile.