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 Utils.requestDeleteManagedProfile(mContext); 48 setComponentsEnabledState(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); 49 } 50 51 /** 52 * Disable or enable app components in the current profile. When they are disabled only the 53 * counterpart in the other profile can respond (via cross-profile intent filter). 54 * 55 * @param enabledState {@link PackageManager#COMPONENT_ENABLED_STATE_DISABLED} or 56 * {@link PackageManager#COMPONENT_ENABLED_STATE_DEFAULT} 57 */ setComponentsEnabledState(final int enabledState)58 private void setComponentsEnabledState(final int enabledState) { 59 final String[] components = { 60 ByodHelperActivity.class.getName(), 61 PermissionLockdownTestActivity.ACTIVITY_ALIAS, 62 AuthenticationBoundKeyTestActivity.class.getName(), 63 VpnTestActivity.class.getName(), 64 AlwaysOnVpnSettingsTestActivity.class.getName(), 65 CrossProfilePermissionControlActivity.class.getName(), 66 IntermediateRecentActivity.class.getName(), 67 CommandReceiverActivity.class.getName(), 68 SetSupportMessageActivity.class.getName(), 69 KeyChainTestActivity.class.getName(), 70 WorkProfileWidgetActivity.class.getName(), 71 LocationCheckerActivity.WORK_ACTIVITY_ALIAS, 72 ScreenshotCaptureActivity.class.getName() 73 }; 74 for (String component : components) { 75 mPackageManager.setComponentEnabledSetting(new ComponentName(mContext, component), 76 enabledState, PackageManager.DONT_KILL_APP); 77 } 78 } 79 } 80