[Cherrypick] Make sure we do clean up ByodFlowTest

Pull out setup and teardown from ByodFlowTestActivity.
And run the teardown in TestActivitys that needs things cleaned up.

Test: Run byod flow test -> Start byod provisioning flow
      -> home -> kill the CtsVerifier in recent.
      Go to device owner test, back, no crash.
Test: Run byod flow test -> kick start provisioning -> tap fail
      Observer that managed profile is removed

Fix: 31498910

Merged-in: I083a124ad5761d7faeb543f898eb8decc63fb7c4

Change-Id: I083a124ad5761d7faeb543f898eb8decc63fb7c4
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestActivity.java
index 0f28b0d..3867d7f 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestActivity.java
@@ -59,6 +59,8 @@
 
     private ComponentName mAdminReceiverComponent;
 
+    private ByodFlowTestHelper mByodFlowTestHelper;
+
     private DialogTestListItem mProfileOwnerInstalled;
     private DialogTestListItem mProfileAccountVisibleTest;
     private DialogTestListItem mDeviceAdminVisibleTest;
@@ -108,9 +110,11 @@
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        mByodFlowTestHelper = new ByodFlowTestHelper(this);
         mAdminReceiverComponent = new ComponentName(this, DeviceAdminTestReceiver.class.getName());
 
-        enableComponent(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
+        mByodFlowTestHelper.setup();
+
         mPrepareTestButton.setText(R.string.provisioning_byod_start);
         mPrepareTestButton.setOnClickListener(new OnClickListener() {
             @Override
@@ -172,8 +176,7 @@
     public void finish() {
         // Pass and fail buttons are known to call finish() when clicked, and this is when we want to
         // clean up the provisioned profile.
-        Utils.requestDeleteManagedProfile(this);
-        enableComponent(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
+        mByodFlowTestHelper.tearDown();
         super.finish();
     }
 
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestHelper.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestHelper.java
new file mode 100644
index 0000000..fb92248
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestHelper.java
@@ -0,0 +1,50 @@
+package com.android.cts.verifier.managedprovisioning;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.PackageManager;
+
+public class ByodFlowTestHelper {
+    private Context mContext;
+    private PackageManager mPackageManager;
+
+    public ByodFlowTestHelper(Context context) {
+        this.mContext = context;
+        this.mPackageManager = mContext.getPackageManager();
+    }
+
+    public void setup() {
+        setComponentsEnabledState(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
+    }
+
+    /**
+     * Clean up things. This has to be working even it is called multiple times.
+     */
+    public void tearDown() {
+        Utils.requestDeleteManagedProfile(mContext);
+        setComponentsEnabledState(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
+    }
+
+    /**
+     * Disable or enable app components in the current profile. When they are disabled only the
+     * counterpart in the other profile can respond (via cross-profile intent filter).
+     *
+     * @param enabledState {@link PackageManager#COMPONENT_ENABLED_STATE_DISABLED} or
+     *                     {@link PackageManager#COMPONENT_ENABLED_STATE_DEFAULT}
+     */
+    private void setComponentsEnabledState(final int enabledState) {
+        final String[] components = {
+                ByodHelperActivity.class.getName(),
+                WorkStatusTestActivity.class.getName(),
+                PermissionLockdownTestActivity.ACTIVITY_ALIAS,
+                AuthenticationBoundKeyTestActivity.class.getName(),
+                VpnTestActivity.class.getName(),
+                CommandReceiverActivity.class.getName(),
+                SetSupportMessageActivity.class.getName()
+        };
+        for (String component : components) {
+            mPackageManager.setComponentEnabledSetting(new ComponentName(mContext, component),
+                    enabledState, PackageManager.DONT_KILL_APP);
+        }
+    }
+}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/DeviceOwnerPositiveTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/DeviceOwnerPositiveTestActivity.java
index 724f03d..1cd4beb 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/DeviceOwnerPositiveTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/DeviceOwnerPositiveTestActivity.java
@@ -74,6 +74,10 @@
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+
+        // Tidy up in case previous run crashed.
+        new ByodFlowTestHelper(this).tearDown();
+
         if (ACTION_CHECK_DEVICE_OWNER.equals(getIntent().getAction())) {
             DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(
                     Context.DEVICE_POLICY_SERVICE);
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/DeviceOwnerRequestingBugreportTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/DeviceOwnerRequestingBugreportTestActivity.java
index 1d23175..7ab8e7e 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/DeviceOwnerRequestingBugreportTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/DeviceOwnerRequestingBugreportTestActivity.java
@@ -80,6 +80,9 @@
             return;
         }
 
+        // Tidy up in case previous run crashed.
+        new ByodFlowTestHelper(this).tearDown();
+
         setContentView(R.layout.requesting_bugreport_device_owner);
         setInfoResources(R.string.device_owner_requesting_bugreport_tests,
                 R.string.device_owner_requesting_bugreport_tests_info, 0);
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/Utils.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/Utils.java
index 17e83c1..ffc1e07 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/Utils.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/Utils.java
@@ -56,9 +56,8 @@
     static void requestDeleteManagedProfile(Context context) {
         try {
             Intent intent = new Intent(ByodHelperActivity.ACTION_REMOVE_MANAGED_PROFILE);
+            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             context.startActivity(intent);
-            String message = context.getString(R.string.provisioning_byod_delete_profile);
-            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
         }
         catch (ActivityNotFoundException e) {
             Log.d(TAG, "requestDeleteProfileOwner: ActivityNotFoundException", e);