static TypedArray obtainAttributes( Resources res, Resources.Theme theme, AttributeSet set, int[] attrs) { if (theme == null) { return res.obtainAttributes(set, attrs); } return theme.obtainStyledAttributes(set, attrs, 0, 0); }
static TypedArray obtainAttributes(Resources res, Resources.Theme theme, AttributeSet set, int[] attrs) { if (theme == null) { return res.obtainAttributes(set, attrs); } return theme.obtainStyledAttributes(set, attrs, 0, 0); }
static TypedArray obtainAttributes( Resources res, Resources.Theme theme, AttributeSet set, int[] attrs) { if (theme == null) { return res.obtainAttributes(set, attrs); } return theme.obtainStyledAttributes(set, attrs, 0, 0); }
private static AuthenticatorDescription parseAuthenticatorDescription(Resources resources, String packageName, AttributeSet attributeSet) { TypedArray array = resources.obtainAttributes(attributeSet, R_Hide.styleable.AccountAuthenticator.get()); try { String accountType = array.getString(R_Hide.styleable.AccountAuthenticator_accountType.get()); int label = array.getResourceId(R_Hide.styleable.AccountAuthenticator_label.get(), 0); int icon = array.getResourceId(R_Hide.styleable.AccountAuthenticator_icon.get(), 0); int smallIcon = array.getResourceId(R_Hide.styleable.AccountAuthenticator_smallIcon.get(), 0); int accountPreferences = array.getResourceId(R_Hide.styleable.AccountAuthenticator_accountPreferences.get(), 0); boolean customTokens = array.getBoolean(R_Hide.styleable.AccountAuthenticator_customTokens.get(), false); if (TextUtils.isEmpty(accountType)) { return null; } return new AuthenticatorDescription(accountType, packageName, label, icon, smallIcon, accountPreferences, customTokens); } finally { array.recycle(); } }
@Implementation protected TypedArray obtainAttributes(AttributeSet set, int[] attrs) { if (isLegacyAssetManager()) { return legacyShadowOf(realResources.getAssets()) .attrsToTypedArray(realResources, set, attrs, 0, 0, 0); } else { return directlyOn(realResources, Resources.class).obtainAttributes(set, attrs); } }
private SyncAdapterType parseSyncAdapterType(Resources res, AttributeSet set) { TypedArray obtainAttributes = res.obtainAttributes(set, R_Hide.styleable.SyncAdapter.get()); try { String contentAuthority = obtainAttributes.getString(R_Hide.styleable.SyncAdapter_contentAuthority.get()); String accountType = obtainAttributes.getString(R_Hide.styleable.SyncAdapter_accountType.get()); if (contentAuthority == null || accountType == null) { obtainAttributes.recycle(); return null; } boolean userVisible = obtainAttributes.getBoolean(R_Hide.styleable.SyncAdapter_userVisible.get(), true); boolean supportsUploading = obtainAttributes.getBoolean(R_Hide.styleable.SyncAdapter_supportsUploading.get(), true); boolean isAlwaysSyncable = obtainAttributes.getBoolean(R_Hide.styleable.SyncAdapter_isAlwaysSyncable.get(), true); boolean allowParallelSyncs = obtainAttributes.getBoolean(R_Hide.styleable.SyncAdapter_allowParallelSyncs.get(), true); String settingsActivity = obtainAttributes.getString(R_Hide.styleable.SyncAdapter_settingsActivity.get()); SyncAdapterType type; if (SyncAdapterTypeN.ctor != null) { type = SyncAdapterTypeN.ctor.newInstance(contentAuthority, accountType, userVisible, supportsUploading, isAlwaysSyncable, allowParallelSyncs, settingsActivity, null); obtainAttributes.recycle(); return type; } type = mirror.android.content.SyncAdapterType.ctor.newInstance(contentAuthority, accountType, userVisible, supportsUploading, isAlwaysSyncable, allowParallelSyncs, settingsActivity); obtainAttributes.recycle(); return type; } catch (Throwable e) { e.printStackTrace(); } return null; } }
@Test public void attrsToTypedArray_shouldAllowMockedAttributeSets() throws Exception { if (!useLegacy()) return; AttributeSet mockAttributeSet = mock(AttributeSet.class); when(mockAttributeSet.getAttributeCount()).thenReturn(1); when(mockAttributeSet.getAttributeNameResource(0)).thenReturn(android.R.attr.windowBackground); when(mockAttributeSet.getAttributeName(0)).thenReturn("android:windowBackground"); when(mockAttributeSet.getAttributeValue(0)).thenReturn("value"); resources.obtainAttributes(mockAttributeSet, new int[]{android.R.attr.windowBackground}); }
@Test public void obtainAttributes_shouldReturnValuesFromResources() throws Exception { XmlPullParser parser = resources.getXml(R.xml.xml_attrs); parser.next(); parser.next(); AttributeSet attributes = Xml.asAttributeSet(parser); TypedArray typedArray = resources .obtainAttributes(attributes, new int[]{android.R.attr.title, android.R.attr.scrollbarFadeDuration}); assertThat(typedArray.getString(0)).isEqualTo("Android Title"); assertThat(typedArray.getInt(1, 0)).isEqualTo(1111); typedArray.recycle(); }
@Test public void obtainAttributes_shouldUseReferencedIdFromAttributeSet() throws Exception { // android:id/mask was introduced in API 21, but it's still possible for apps built against API 21 to refer to it // in older runtimes because referenced resource ids are compiled (by aapt) into the binary XML format. AttributeSet attributeSet = Robolectric.buildAttributeSet() .addAttribute(android.R.attr.id, "@android:id/mask").build(); TypedArray typedArray = resources.obtainAttributes(attributeSet, new int[]{android.R.attr.id}); assertThat(typedArray.getResourceId(0, -9)).isEqualTo(android.R.id.mask); }
@Test public void obtainAttributes_shouldReturnValuesFromAttributeSet() throws Exception { AttributeSet attributes = Robolectric.buildAttributeSet() .addAttribute(android.R.attr.title, "A title!") .addAttribute(android.R.attr.width, "12px") .addAttribute(android.R.attr.height, "1in") .build(); TypedArray typedArray = resources .obtainAttributes(attributes, new int[]{android.R.attr.height, android.R.attr.width, android.R.attr.title}); assertThat(typedArray.getDimension(0, 0)).isEqualTo(160f); assertThat(typedArray.getDimension(1, 0)).isEqualTo(12f); assertThat(typedArray.getString(2)).isEqualTo("A title!"); typedArray.recycle(); }
@Test public void multiFormatAttributes_integerHexValue() { AttributeSet attributeSet = Robolectric.buildAttributeSet().addAttribute(R.attr.multiformat, "0x10").build(); TypedArray typedArray = resources.obtainAttributes(attributeSet, new int[] {R.attr.multiformat}); TypedValue outValue = new TypedValue(); typedArray.getValue(0, outValue); assertThat(outValue.type).isEqualTo(TypedValue.TYPE_INT_HEX); }
@Test public void multiFormatAttributes_integerDecimalValue() { AttributeSet attributeSet = Robolectric.buildAttributeSet().addAttribute(R.attr.multiformat, "16").build(); TypedArray typedArray = resources.obtainAttributes(attributeSet, new int[] {R.attr.multiformat}); TypedValue outValue = new TypedValue(); typedArray.getValue(0, outValue); assertThat(outValue.type).isEqualTo(TypedValue.TYPE_INT_DEC); }
@Test public void multiFormatAttributes_stringValue() { AttributeSet attributeSet = Robolectric.buildAttributeSet().addAttribute(R.attr.multiformat, "Hello World").build(); TypedArray typedArray = resources.obtainAttributes(attributeSet, new int[] {R.attr.multiformat}); TypedValue outValue = new TypedValue(); typedArray.getValue(0, outValue); assertThat(outValue.type).isEqualTo(TypedValue.TYPE_STRING); }
@Test public void multiFormatAttributes_booleanValue() { AttributeSet attributeSet = Robolectric.buildAttributeSet().addAttribute(R.attr.multiformat, "true").build(); TypedArray typedArray = resources.obtainAttributes(attributeSet, new int[] {R.attr.multiformat}); TypedValue outValue = new TypedValue(); typedArray.getValue(0, outValue); assertThat(outValue.type).isEqualTo(TypedValue.TYPE_INT_BOOLEAN); }
@Test public void obtainAttributes() { TypedArray typedArray = resources.obtainAttributes(Robolectric.buildAttributeSet() .addAttribute(R.attr.styleReference, "@xml/shortcuts") .build(), new int[]{R.attr.styleReference}); assertThat(typedArray).isNotNull(); assertThat(typedArray.peekValue(0).resourceId).isEqualTo(R.xml.shortcuts); }
@Override public TypedArray obtainAttributes(AttributeSet set, int[] attrs) { try { return super.obtainAttributes(set, attrs); } catch (NotFoundException e) { return mHostResources.obtainAttributes(set, attrs); } }
@Override public TypedArray obtainAttributes(AttributeSet set, int[] attrs) { try { return super.obtainAttributes(set, attrs); } catch (NotFoundException e) { return mHostResources.obtainAttributes(set, attrs); } }
static TypedArray obtainAttributes(Resources res, Resources.Theme theme, AttributeSet set, int[] attrs) { if (theme == null) { return res.obtainAttributes(set, attrs); } return theme.obtainStyledAttributes(set, attrs, 0, 0); }
/** * Obtains styled attributes from the theme, if available, or unstyled resources if the theme is * null. */ @NonNull private static TypedArray obtainAttributes( Resources res, @Nullable Resources.Theme theme, AttributeSet set, @StyleableRes int[] attrs) { return theme == null ? res.obtainAttributes(set, attrs) : theme.obtainStyledAttributes(set, attrs, 0, 0); }
@Implementation protected TypedArray obtainAttributes(AttributeSet set, int[] attrs) { if (isLegacyAssetManager()) { return legacyShadowOf(realResources.getAssets()) .attrsToTypedArray(realResources, set, attrs, 0, 0, 0); } else { return directlyOn(realResources, Resources.class).obtainAttributes(set, attrs); } }