Home | History | Annotate | Download | only in managedprovisioning
      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 
      8 public class ByodFlowTestHelper {
      9     private Context mContext;
     10     private PackageManager mPackageManager;
     11 
     12     public ByodFlowTestHelper(Context context) {
     13         this.mContext = context;
     14         this.mPackageManager = mContext.getPackageManager();
     15     }
     16 
     17     public void setup() {
     18         setComponentsEnabledState(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
     19     }
     20 
     21     /** Reports result to ByodFlowTestActivity if it is impossible via normal setResult. */
     22     public void sendResultToPrimary(Intent result) {
     23         final Intent intent = new Intent(ByodFlowTestActivity.ACTION_TEST_RESULT);
     24         intent.putExtra(ByodFlowTestActivity.EXTRA_RESULT, result);
     25         startActivityInPrimary(intent);
     26     }
     27 
     28     public void startActivityInPrimary(Intent intent) {
     29         // Disable app components in the current profile, so only the counterpart in the other
     30         // profile can respond (via cross-profile intent filter)
     31         mContext.getPackageManager().setComponentEnabledSetting(
     32                 new ComponentName(mContext, ByodFlowTestActivity.class),
     33                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
     34                 PackageManager.DONT_KILL_APP);
     35         mContext.startActivity(intent);
     36     }
     37 
     38     /**
     39      * Clean up things. This has to be working even it is called multiple times.
     40      */
     41     public void tearDown() {
     42         Utils.requestDeleteManagedProfile(mContext);
     43         setComponentsEnabledState(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
     44     }
     45 
     46     /**
     47      * Disable or enable app components in the current profile. When they are disabled only the
     48      * counterpart in the other profile can respond (via cross-profile intent filter).
     49      *
     50      * @param enabledState {@link PackageManager#COMPONENT_ENABLED_STATE_DISABLED} or
     51      *                     {@link PackageManager#COMPONENT_ENABLED_STATE_DEFAULT}
     52      */
     53     private void setComponentsEnabledState(final int enabledState) {
     54         final String[] components = {
     55                 ByodHelperActivity.class.getName(),
     56                 WorkStatusTestActivity.class.getName(),
     57                 PermissionLockdownTestActivity.ACTIVITY_ALIAS,
     58                 AuthenticationBoundKeyTestActivity.class.getName(),
     59                 VpnTestActivity.class.getName(),
     60                 AlwaysOnVpnSettingsTestActivity.class.getName(),
     61                 IntermediateRecentActivity.class.getName(),
     62                 CommandReceiverActivity.class.getName(),
     63                 SetSupportMessageActivity.class.getName(),
     64                 KeyChainTestActivity.class.getName(),
     65                 WorkProfileWidgetActivity.class.getName()
     66         };
     67         for (String component : components) {
     68             mPackageManager.setComponentEnabledSetting(new ComponentName(mContext, component),
     69                     enabledState, PackageManager.DONT_KILL_APP);
     70         }
     71     }
     72 }
     73