public static void setEnabledBlocking(Context appContext, Class<?> componentClass, boolean enabled) { ComponentName component = new ComponentName(appContext, componentClass); PackageManager packageManager = appContext.getPackageManager(); int newState = enabled ? COMPONENT_ENABLED_STATE_ENABLED : COMPONENT_ENABLED_STATE_DISABLED; // Blocks on IPC. packageManager.setComponentEnabledSetting(component, newState, DONT_KILL_APP); }
@Override public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags) { mBase.setComponentEnabledSetting(componentName, newState, flags); }
private void updateReceiver(boolean state) { ComponentName cn= new ComponentName(this, PackageReceiver.class); int componentState=state ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED; pm.setComponentEnabledSetting(cn, componentState, PackageManager.DONT_KILL_APP); }
private static void setEnabledBlocking(Context appContext, Class<?> componentClass, boolean enabled) { ComponentName component = new ComponentName(appContext, componentClass); PackageManager packageManager = appContext.getPackageManager(); int newState = enabled ? COMPONENT_ENABLED_STATE_ENABLED : COMPONENT_ENABLED_STATE_DISABLED; // Blocks on IPC. packageManager.setComponentEnabledSetting(component, newState, DONT_KILL_APP); } // end of lines copied from LeakCanary
/** * set the component in our package default * @param context * @param componentClassName */ public static void setComponentDefault(Context context, String componentClassName){ PackageManager pm = context.getPackageManager(); ComponentName componentName = new ComponentName(context.getPackageName(), componentClassName); pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP); }
private ActivityController<T> attach( @Nullable @WithType("android.app.Activity$NonConfigurationInstances") Object lastNonConfigurationInstances) { if (attached) { return this; } // make sure the component is enabled Context context = RuntimeEnvironment.application.getBaseContext(); context .getPackageManager() .setComponentEnabledSetting( new ComponentName(context.getPackageName(), component.getClass().getName()), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0); ShadowActivity shadowActivity = Shadow.extract(component); shadowActivity.callAttach(getIntent(), lastNonConfigurationInstances); attached = true; return this; }
/** * Create and register {@link ContentProvider} using the given {@link ProviderInfo}. * * @param providerInfo the {@link ProviderInfo} to use * @return this {@link ContentProviderController} */ public ContentProviderController<T> create(ProviderInfo providerInfo) { Context baseContext = RuntimeEnvironment.application.getBaseContext(); // make sure the component is enabled ComponentName componentName = createRelative(baseContext.getPackageName(), contentProvider.getClass().getName()); baseContext .getPackageManager() .setComponentEnabledSetting( componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0); contentProvider.attachInfo(baseContext, providerInfo); if (providerInfo != null) { ShadowContentResolver.registerProviderInternal(providerInfo.authority, contentProvider); } return this; }
@After public void cleanup() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mPackageManager.setComponentEnabledSetting(mComponent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP); } }
@Override protected void onStop() { if (iconType != SettingUtil.getInstance().getCustomIconValue()) { new Thread(() -> { String act = ".SplashActivity_"; for (String s : Constant.ICONS_TYPE) { getPackageManager().setComponentEnabledSetting(new ComponentName(BaseActivity.this, getPackageName() + act + s), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); } act += Constant.ICONS_TYPE[SettingUtil.getInstance().getCustomIconValue()]; getPackageManager().setComponentEnabledSetting(new ComponentName(BaseActivity.this, getPackageName() + act), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); }).start(); } super.onStop(); }
@After public void tearDown() { pm.setApplicationEnabledSetting( context.getPackageName(), COMPONENT_ENABLED_STATE_DEFAULT, DONT_KILL_APP); pm.setComponentEnabledSetting( new ComponentName(context, "org.robolectric.testapp.TestActivity"), COMPONENT_ENABLED_STATE_DEFAULT, DONT_KILL_APP); pm.setComponentEnabledSetting( new ComponentName(context, "org.robolectric.testapp.DisabledTestActivity"), COMPONENT_ENABLED_STATE_DEFAULT, DONT_KILL_APP); }
private IntentServiceController<T> attach() { if (attached) { return this; } // make sure the component is enabled Context context = RuntimeEnvironment.application.getBaseContext(); ComponentName name = new ComponentName(context.getPackageName(), component.getClass().getName()); context .getPackageManager() .setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0); ReflectionHelpers.callInstanceMethod(Service.class, component, "attach", from(Context.class, RuntimeEnvironment.application.getBaseContext()), from(ActivityThread.class, null), from(String.class, component.getClass().getSimpleName()), from(IBinder.class, null), from(Application.class, RuntimeEnvironment.application), from(Object.class, null)); attached = true; return this; }
private ServiceController<T> attach() { if (attached) { return this; } // make sure the component is enabled Context context = RuntimeEnvironment.application.getBaseContext(); ComponentName name = new ComponentName(context.getPackageName(), component.getClass().getName()); context .getPackageManager() .setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0); ReflectionHelpers.callInstanceMethod(Service.class, component, "attach", from(Context.class, RuntimeEnvironment.application.getBaseContext()), from(ActivityThread.class, null), from(String.class, component.getClass().getSimpleName()), from(IBinder.class, null), from(Application.class, RuntimeEnvironment.application), from(Object.class, null)); attached = true; return this; }
@Test public void queryIntentActivities_DisabledComponentViaPmImplicitIntent() throws Exception { Uri uri = Uri.parse("content://testhost1.com:1/testPath/test.jpeg"); Intent i = new Intent(Intent.ACTION_VIEW); i.addCategory(Intent.CATEGORY_DEFAULT); i.setDataAndType(uri, "image/jpeg"); ComponentName componentToDisable = new ComponentName( ApplicationProvider.getApplicationContext(), "org.robolectric.shadows.TestActivity"); packageManager.setComponentEnabledSetting( componentToDisable, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(i, 0); assertThat(resolveInfos).isEmpty(); }
@Test public void queryIntentActivities_DisabledComponentViaPmExplicitIntent() throws Exception { Intent i = new Intent(); i.setClassName( ApplicationProvider.getApplicationContext(), "org.robolectric.shadows.TestActivity"); ComponentName componentToDisable = new ComponentName( ApplicationProvider.getApplicationContext(), "org.robolectric.shadows.TestActivity"); packageManager.setComponentEnabledSetting( componentToDisable, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(i, 0); assertThat(resolveInfos).isEmpty(); }
@Test public void queryIntentActivities_MatchDisabledViaPmComponents() throws Exception { Uri uri = Uri.parse("content://testhost1.com:1/testPath/test.jpeg"); Intent i = new Intent(Intent.ACTION_VIEW); i.addCategory(Intent.CATEGORY_DEFAULT); i.setDataAndType(uri, "image/jpeg"); ComponentName componentToDisable = new ComponentName( ApplicationProvider.getApplicationContext(), "org.robolectric.shadows.TestActivity"); packageManager.setComponentEnabledSetting( componentToDisable, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(i, PackageManager.MATCH_DISABLED_COMPONENTS); assertThat(resolveInfos).isNotNull(); assertThat(resolveInfos).hasSize(1); assertThat(resolveInfos.get(0).activityInfo.enabled).isTrue(); }
@Test public void queryIntentActivities_DisabledComponentEnabledViaPmExplicitIntent() throws Exception { Intent i = new Intent(); i.setClassName( ApplicationProvider.getApplicationContext(), "org.robolectric.shadows.DisabledActivity"); ComponentName componentToDisable = new ComponentName( ApplicationProvider.getApplicationContext(), "org.robolectric.shadows.DisabledActivity"); packageManager.setComponentEnabledSetting( componentToDisable, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(i, 0); assertThat(resolveInfos).hasSize(1); assertThat(resolveInfos.get(0).activityInfo.enabled).isFalse(); }
@Test public void verifyJobApiNotSupportedWhenServiceIsDisabled() { assumeTrue(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { assertThat(JobApi.V_26.isSupported(mContext)).isTrue(); } assertThat(JobApi.V_24.isSupported(mContext)).isTrue(); assertThat(JobApi.V_21.isSupported(mContext)).isTrue(); mPackageManager.setComponentEnabledSetting(mComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); assertThat(JobApi.V_26.isSupported(mContext)).isFalse(); assertThat(JobApi.V_24.isSupported(mContext)).isFalse(); assertThat(JobApi.V_21.isSupported(mContext)).isFalse(); } }
@Test public void getPackageInfo_programmaticallyDisabledComponent_noFlags_notReturned() throws Exception { ComponentName activityName = new ComponentName(context, "org.robolectric.testapp.TestActivity"); pm.setComponentEnabledSetting(activityName, COMPONENT_ENABLED_STATE_DISABLED, DONT_KILL_APP); try { pm.getActivityInfo(activityName, 0); fail("NameNotFoundException expected"); } catch (NameNotFoundException expected) { } }
@Test public void getPackageInfo_programmaticallyEnabledComponent_returned() throws Exception { ComponentName activityName = new ComponentName(context, "org.robolectric.testapp.DisabledTestActivity"); pm.setComponentEnabledSetting(activityName, COMPONENT_ENABLED_STATE_ENABLED, DONT_KILL_APP); ActivityInfo info = pm.getActivityInfo(activityName, 0); assertThat(info).isNotNull(); // WHAT?? Seems like we always get the manifest value for ComponentInfo.enabled assertThat(info.enabled).isFalse(); assertThat(info.isEnabled()).isFalse(); }
@Test public void getPackageInfo_programmaticallyDisabledComponent_withFlags_returned() throws Exception { ComponentName activityName = new ComponentName(context, "org.robolectric.testapp.TestActivity"); pm.setComponentEnabledSetting(activityName, COMPONENT_ENABLED_STATE_DISABLED, DONT_KILL_APP); ActivityInfo info = pm.getActivityInfo(activityName, MATCH_DISABLED_COMPONENTS); assertThat(info).isNotNull(); // WHAT?? Seems like we always get the manifest value for ComponentInfo.enabled assertThat(info.enabled).isTrue(); assertThat(info.isEnabled()).isTrue(); }