1 package com.android.cts.verifier.managedprovisioning;
2 
3 import android.content.ComponentName;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.pm.PackageManager;
7 import android.os.UserManager;
8 import android.util.Log;
9 
10 public class ByodFlowTestHelper {
11 
12     private static final String TAG = ByodFlowTestHelper.class.getSimpleName();
13 
14     private Context mContext;
15     private PackageManager mPackageManager;
16 
ByodFlowTestHelper(Context context)17     public ByodFlowTestHelper(Context context) {
18         this.mContext = context;
19         this.mPackageManager = mContext.getPackageManager();
20     }
21 
setup()22     public void setup() {
23         setComponentsEnabledState(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
24     }
25 
26     /** Reports result to ByodFlowTestActivity if it is impossible via normal setResult. */
sendResultToPrimary(Intent result)27     public void sendResultToPrimary(Intent result) {
28         final Intent intent = new Intent(ByodFlowTestActivity.ACTION_TEST_RESULT);
29         intent.putExtra(ByodFlowTestActivity.EXTRA_RESULT, result);
30         startActivityInPrimary(intent);
31     }
32 
startActivityInPrimary(Intent intent)33     public void startActivityInPrimary(Intent intent) {
34         // Disable app components in the current profile, so only the counterpart in the other
35         // profile can respond (via cross-profile intent filter)
36         mContext.getPackageManager().setComponentEnabledSetting(
37                 new ComponentName(mContext, ByodFlowTestActivity.class),
38                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
39                 PackageManager.DONT_KILL_APP);
40         mContext.startActivity(intent);
41     }
42 
43     /**
44      * Clean up things. This has to be working even it is called multiple times.
45      */
tearDown()46     public void tearDown() {
47         if (!UserManager.isHeadlessSystemUserMode()) {
48             // TODO(b/177554984): figure out how to use it on headless system user mode - right now,
49             // it removes the current user on teardown
50             Log.i(TAG, "tearDown(): not deleting managed profile on headless system user mode");
51             Utils.requestDeleteManagedProfile(mContext);
52         }
53         setComponentsEnabledState(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
54     }
55 
56     /**
57      * Disable or enable app components in the current profile. When they are disabled only the
58      * counterpart in the other profile can respond (via cross-profile intent filter).
59      *
60      * @param enabledState {@link PackageManager#COMPONENT_ENABLED_STATE_DISABLED} or
61      *                     {@link PackageManager#COMPONENT_ENABLED_STATE_DEFAULT}
62      */
setComponentsEnabledState(final int enabledState)63     private void setComponentsEnabledState(final int enabledState) {
64         final String[] components = {
65                 ByodHelperActivity.class.getName(),
66                 WorkStatusTestActivity.class.getName(),
67                 PermissionLockdownTestActivity.ACTIVITY_ALIAS,
68                 AuthenticationBoundKeyTestActivity.class.getName(),
69                 VpnTestActivity.class.getName(),
70                 AlwaysOnVpnSettingsTestActivity.class.getName(),
71                 CrossProfilePermissionControlActivity.class.getName(),
72                 IntermediateRecentActivity.class.getName(),
73                 CommandReceiverActivity.class.getName(),
74                 SetSupportMessageActivity.class.getName(),
75                 KeyChainTestActivity.class.getName(),
76                 WorkProfileWidgetActivity.class.getName(),
77                 LocationCheckerActivity.WORK_ACTIVITY_ALIAS
78         };
79         for (String component : components) {
80             mPackageManager.setComponentEnabledSetting(new ComponentName(mContext, component),
81                     enabledState, PackageManager.DONT_KILL_APP);
82         }
83     }
84 }
85