@Override public ActivityInfo getActivityInfo(ComponentName component, int flags) throws NameNotFoundException { return mBase.getActivityInfo(component, flags); }
@Override public Object call(Object who, Method method, Object... args) throws Throwable { ComponentName component = (ComponentName) args[0]; if (component != null) { ActivityInfo activityInfo = VirtualCore.getPM().getActivityInfo(component, 0); if (activityInfo != null) { return null; } } return method.invoke(who, args); } }
private ActivityInfo getActivityInfo(Application application) { try { return application.getPackageManager().getActivityInfo(new ComponentName(application.getPackageName(), component.getClass().getName()), PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { throw new RuntimeException(e); } }
static CharSequence getActivityLabel(@NonNull Context context, @NonNull Class<? extends Activity> cls) { PackageManager packageManager = context.getPackageManager(); try { return packageManager.getActivityInfo(new ComponentName(context, cls), 0).loadLabel(packageManager); } catch (PackageManager.NameNotFoundException e) { return null; } }
try { ComponentName cn = new ComponentName(packageName, className); ActivityInfo aInfo = packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA); alarmClockIntent.setComponent(cn); debug("Found " + vendor + " --> " + packageName + "/" + className);
@SuppressWarnings("WrongConstant") public static void hasFacebookActivity(Context context, boolean shouldThrow) { Validate.notNull(context, "context"); PackageManager pm = context.getPackageManager(); ActivityInfo activityInfo = null; if (pm != null) { ComponentName componentName = new ComponentName(context, "com.facebook.FacebookActivity"); try { activityInfo = pm.getActivityInfo(componentName, PackageManager.GET_ACTIVITIES); } catch (PackageManager.NameNotFoundException e) { // ignore } } if (activityInfo == null) { if (shouldThrow) { throw new IllegalStateException(FACEBOOK_ACTIVITY_NOT_FOUND_REASON); } else { Log.w(TAG, FACEBOOK_ACTIVITY_NOT_FOUND_REASON); } } }
@Test public void getCompoent_disabledComponent_doesntInclude() throws Exception { ComponentName disabledActivityName = new ComponentName(context, "org.robolectric.testapp.DisabledTestActivity"); try { pm.getActivityInfo(disabledActivityName, 0); fail("NameNotFoundException expected"); } catch (NameNotFoundException expected) { } }
@Test public void addActicityIfNotPresent_existing() throws Exception { String packageName = ApplicationProvider.getApplicationContext().getPackageName(); ComponentName componentName = new ComponentName(packageName, ActivityWithFilters.class.getName()); shadowPackageManager.addActivityIfNotPresent(componentName); ActivityInfo activityInfo = packageManager.getActivityInfo(componentName, 0); assertThat(activityInfo).isNotNull(); assertThat(activityInfo.packageName).isEqualTo(packageName); assertThat(activityInfo.name).isEqualTo(ActivityWithFilters.class.getName()); }
@Test public void getCompoent_disabledComponent_include() throws Exception { ComponentName disabledActivityName = new ComponentName(context, "org.robolectric.testapp.DisabledTestActivity"); ActivityInfo info = pm.getActivityInfo(disabledActivityName, MATCH_DISABLED_COMPONENTS); assertThat(info).isNotNull(); assertThat(info.enabled).isFalse(); }
@Test public void addActicityIfNotPresent_newPackage() throws Exception { ComponentName componentName = new ComponentName("test.package", "Activity"); shadowPackageManager.addActivityIfNotPresent(componentName); ActivityInfo activityInfo = packageManager.getActivityInfo(componentName, 0); assertThat(activityInfo).isNotNull(); assertThat(activityInfo.packageName).isEqualTo("test.package"); assertThat(activityInfo.name).isEqualTo("Activity"); }
/** MCC + MNC are always present in config changes since Oreo. */ @Test @Config(minSdk = O) public void getActivityMetaData_configChangesAlwaysIncludesMccAndMnc() throws Exception { Activity activity = setupActivity(ShadowPackageManagerTest.ActivityWithConfigChanges.class); ActivityInfo activityInfo = activity.getPackageManager().getActivityInfo(activity.getComponentName(), 0); int configChanges = activityInfo.configChanges; assertThat(configChanges & ActivityInfo.CONFIG_MCC).isEqualTo(ActivityInfo.CONFIG_MCC); assertThat(configChanges & ActivityInfo.CONFIG_MNC).isEqualTo(ActivityInfo.CONFIG_MNC); }
@Test public void getActivityMetaData() throws Exception { Activity activity = setupActivity(ActivityWithMetadata.class); ActivityInfo activityInfo = packageManager.getActivityInfo( activity.getComponentName(), PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA); assertThat(activityInfo.metaData.get("someName")).isEqualTo("someValue"); }
@Test public void addActicityIfNotPresent_newActivity() throws Exception { String packageName = ApplicationProvider.getApplicationContext().getPackageName(); ComponentName componentName = new ComponentName(packageName, "NewActivity"); shadowPackageManager.addActivityIfNotPresent(componentName); ActivityInfo activityInfo = packageManager.getActivityInfo(componentName, 0); assertThat(activityInfo).isNotNull(); assertThat(activityInfo.packageName).isEqualTo(packageName); assertThat(activityInfo.name).isEqualTo("NewActivity"); }
@Test public void addActivity() throws Exception { ActivityInfo activityInfo = new ActivityInfo(); activityInfo.name = "name"; activityInfo.packageName = "package"; shadowPackageManager.addOrUpdateActivity(activityInfo); assertThat(packageManager.getActivityInfo(new ComponentName("package", "name"), 0)).isNotNull(); }
application .getPackageManager() .getActivityInfo( new ComponentName( application.getPackageName(), realActivity.getClass().getName()),
@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 removeActivity() throws Exception { ComponentName componentName = new ComponentName( ApplicationProvider.getApplicationContext(), "org.robolectric.shadows.TestActivity"); ActivityInfo removed = shadowPackageManager.removeActivity(componentName); assertThat(removed).isNotNull(); try { packageManager.getActivityInfo(componentName, 0); // for now it goes here because package manager autocreates activities... // fail(); } catch (NameNotFoundException e) { // expected } }
@Test public void getActivityMetaData_configChanges() throws Exception { Activity activity = setupActivity(ShadowPackageManagerTest.ActivityWithConfigChanges.class); ActivityInfo activityInfo = activity.getPackageManager().getActivityInfo(activity.getComponentName(), 0); int configChanges = activityInfo.configChanges; assertThat(configChanges & ActivityInfo.CONFIG_SCREEN_LAYOUT) .isEqualTo(ActivityInfo.CONFIG_SCREEN_LAYOUT); assertThat(configChanges & ActivityInfo.CONFIG_ORIENTATION) .isEqualTo(ActivityInfo.CONFIG_ORIENTATION); // Spot check a few other possible values that shouldn't be in the flags. assertThat(configChanges & ActivityInfo.CONFIG_FONT_SCALE).isEqualTo(0); assertThat(configChanges & ActivityInfo.CONFIG_SCREEN_SIZE).isEqualTo(0); }
@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(); }