public MenuNode(String name, Map<String, String> attributes) { this.name = name; this.attributes = new TestAttributeSet(attributes, resourceExtractor, attrResourceLoader, null, false); }
@Override public String getAttributeValue(String namespace, String attribute) { return getAttributeValueInMap(namespace, attribute); }
private void addChildrenInGroup(MenuNode source, int groupId, Menu root) { for (MenuNode child : source.children) { String name = child.name; TestAttributeSet attributes = child.attributes; if (strictI18n) { attributes.validateStrictI18n(); } if (name.equals("item")) { if (isSubMenuItem(child)) { SubMenu sub = root.addSubMenu(groupId, attributes .getAttributeResourceValue("android", "id", 0), 0, attributes.getAttributeValue("android", "title")); MenuNode subMenuNode = child.children.get(0); addChildrenInGroup(subMenuNode, groupId, sub); } else { MenuItem menuItem = root.add(groupId, attributes .getAttributeResourceValue("android", "id", 0), 0, attributes.getAttributeValue("android", "title")); menuItem.setVisible(attributes.getAttributeBooleanValue("android", "visible", true)); } } else if (name.equals("group")) { int newGroupId = attributes.getAttributeResourceValue("android", "id", 0); addChildrenInGroup(child, newGroupId, root); } } }
private FrameLayout constructFragment(Context context) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { TestAttributeSet attributeSet = new TestAttributeSet(attributes, resourceExtractor, attrResourceLoader, View.class, isSystem); if (strictI18n) { attributeSet.validateStrictI18n(); } Class<? extends Fragment> clazz = loadFragmentClass(attributes.get("android:name")); Fragment fragment = ((Constructor<? extends Fragment>) clazz.getConstructor()).newInstance(); if (!(context instanceof FragmentActivity)) { throw new RuntimeException("Cannot inflate a fragment unless the activity is a FragmentActivity"); } FragmentActivity activity = (FragmentActivity) context; String tag = attributeSet.getAttributeValue("android", "tag"); int id = attributeSet.getAttributeResourceValue("android", "id", 0); // TODO: this should probably be changed to call TestFragmentManager.addFragment so that the // inflated fragments don't get started twice (once in the commit, and once in ShadowFragmentActivity's // onStart() activity.getSupportFragmentManager().beginTransaction().add(id, fragment, tag).commit(); View view = fragment.getView(); FrameLayout container = new FrameLayout(context); container.setId(id); container.addView(view); return container; }
private View constructView(Context context) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { Class<? extends View> clazz = pickViewClass(); try { TestAttributeSet attributeSet = new TestAttributeSet(attributes, resourceExtractor, attrResourceLoader, clazz, isSystem); if (strictI18n) { attributeSet.validateStrictI18n(); } return ((Constructor<? extends View>) clazz.getConstructor(Context.class, AttributeSet.class)).newInstance(context, attributeSet); } catch (NoSuchMethodException e) { try { return ((Constructor<? extends View>) clazz.getConstructor(Context.class)).newInstance(context); } catch (NoSuchMethodException e1) { return ((Constructor<? extends View>) clazz.getConstructor(Context.class, String.class)).newInstance(context, ""); } } }
private void inflateMenu(Context context, String key, Map<String, String> attributes, Menu root) { MenuNode menuNode = menuNodesByMenuName.get(key); if (menuNode == null) { throw new RuntimeException("Could not find menu " + key); } try { if (attributes != null) { for (Map.Entry<String, String> entry : attributes.entrySet()) { if (!entry.getKey().equals("menu")) { menuNode.attributes.put(entry.getKey(), entry.getValue()); } } } menuNode.inflate(context, root); } catch (I18nException e) { throw e; } catch (Exception e) { throw new RuntimeException("error inflating " + key, e); } }
private Preference constructPreference(Context context, PreferenceGroup parent) throws Exception { Class<? extends Preference> clazz = pickViewClass(); if (clazz.equals(PreferenceScreen.class)) { return Robolectric.newInstanceOf(PreferenceScreen.class); } try { TestAttributeSet attributeSet = new TestAttributeSet(attributes); if (strictI18n) { attributeSet.validateStrictI18n(); } return ((Constructor<? extends Preference>) clazz.getConstructor(Context.class, AttributeSet.class)).newInstance(context, attributeSet); } catch (NoSuchMethodException e) { try { return ((Constructor<? extends Preference>) clazz.getConstructor(Context.class)).newInstance(context); } catch (NoSuchMethodException e1) { return ((Constructor<? extends Preference>) clazz.getConstructor(Context.class, String.class)).newInstance(context, ""); } } }
@Override public boolean getAttributeBooleanValue(String namespace, String attribute, boolean defaultValue) { String value = getAttributeValueInMap(namespace, attribute); return (value != null) ? Boolean.valueOf(value) : defaultValue; }
public void __constructor__(Context context) { __constructor__(context, new TestAttributeSet(), 0); }
@Override public String getAttributeValue(int resourceId) { String qualifiedResourceName = resourceExtractor.getResourceName(resourceId); if (qualifiedResourceName != null) { String resourceName = qualifiedResourceName.substring(qualifiedResourceName.indexOf('/') + 1); return getAttributeValueInMap(null, resourceName); } else { return null; } }
@Override public int getAttributeResourceValue(String namespace, String attribute, int defaultValue) { String value = getAttributeValueInMap(namespace, attribute); Integer resourceId = defaultValue; if (value != null) { resourceId = resourceExtractor.getResourceId(value); } return resourceId == null ? defaultValue : resourceId; }
@Override public float getAttributeFloatValue(String namespace, String attribute, float defaultValue) { String value = getAttributeValueInMap(namespace, attribute); if (attrResourceLoader.hasAttributeFor(viewClass, "xxx", attribute)) { value = attrResourceLoader.convertValueToEnum(viewClass, "xxx", attribute, value); } return (value != null) ? Float.valueOf(value) : defaultValue; }
@Override public int getAttributeIntValue(String namespace, String attribute, int defaultValue) { String value = getAttributeValueInMap(namespace, attribute); if (attrResourceLoader.hasAttributeFor(viewClass, "xxx", attribute)) { value = attrResourceLoader.convertValueToEnum(viewClass, "xxx", attribute, value); } return (value != null) ? Integer.valueOf(value) : defaultValue; }
@Override public int getAttributeResourceValue(int resourceId, int defaultValue) { String attrName = resourceExtractor.getResourceName(resourceId); String value = getAttributeValueInMap(null, attrName); return (value == null) ? defaultValue : resourceExtractor.getResourceId(value); }