Search in sources :

Example 46 with TypedArray

use of android.content.res.TypedArray in project platform_frameworks_base by android.

the class MetaDataTest method checkMetaData.

private void checkMetaData(ComponentName cn, PackageItemInfo ci) throws IOException, XmlPullParserException {
    assertNotNull("Unable to find component " + cn, ci);
    Bundle md = ci.metaData;
    assertNotNull("No meta data found", md);
    assertEquals("foo", md.getString("com.android.frameworks.coretests.string"));
    assertTrue(md.getBoolean("com.android.frameworks.coretests.boolean"));
    assertEquals(100, md.getInt("com.android.frameworks.coretests.integer"));
    assertEquals(0xff000000, md.getInt("com.android.frameworks.coretests.color"));
    assertEquals((double) 1001, Math.floor(md.getFloat("com.android.frameworks.coretests.float") * 10 + .5));
    assertEquals(R.xml.metadata, md.getInt("com.android.frameworks.coretests.reference"));
    XmlResourceParser xml = ci.loadXmlMetaData(mContext.getPackageManager(), "com.android.frameworks.coretests.reference");
    assertNotNull(xml);
    int type;
    while ((type = xml.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
    }
    assertEquals(XmlPullParser.START_TAG, type);
    assertEquals("thedata", xml.getName());
    // method 1: direct access
    final String rawAttr = xml.getAttributeValue(null, "rawText");
    assertEquals("some raw text", rawAttr);
    // method 2: direct access of typed value
    final int rawColorIntAttr = xml.getAttributeIntValue(null, "rawColor", 0);
    assertEquals(0xffffff00, rawColorIntAttr);
    final String rawColorStrAttr = xml.getAttributeValue(null, "rawColor");
    assertEquals("#ffffff00", rawColorStrAttr);
    // method 2: direct access of resource attribute
    final String nameSpace = "http://schemas.android.com/apk/res/android";
    final int colorIntAttr = xml.getAttributeIntValue(nameSpace, "color", 0);
    assertEquals(0xffff0000, colorIntAttr);
    final String colorStrAttr = xml.getAttributeValue(nameSpace, "color");
    assertEquals("#ffff0000", colorStrAttr);
    // method 3: styled access (borrowing an attr from view system here)
    TypedArray a = mContext.obtainStyledAttributes(xml, android.R.styleable.TextView);
    String styledAttr = a.getString(android.R.styleable.TextView_text);
    assertEquals("text", styledAttr);
    a.recycle();
    xml.close();
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) Bundle(android.os.Bundle) TypedArray(android.content.res.TypedArray)

Example 47 with TypedArray

use of android.content.res.TypedArray in project platform_frameworks_base by android.

the class SuggestionsPopupWindowTest method testTextAppearanceInSuggestionsPopup.

@SmallTest
public void testTextAppearanceInSuggestionsPopup() {
    final String text = "abc def ghi";
    final String[] singleWordCandidates = { "DEF", "Def" };
    final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(), singleWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
    final String[] multiWordCandidates = { "ABC DEF GHI", "Abc Def Ghi" };
    final SuggestionSpan multiWordSuggestionSpan = new SuggestionSpan(getActivity(), multiWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
    final TypedArray array = getActivity().obtainStyledAttributes(com.android.internal.R.styleable.Theme);
    final int id = array.getResourceId(com.android.internal.R.styleable.Theme_textEditSuggestionHighlightStyle, 0);
    array.recycle();
    final TextAppearanceSpan expectedSpan = new TextAppearanceSpan(getActivity(), id);
    final TextPaint tmpTp = new TextPaint();
    expectedSpan.updateDrawState(tmpTp);
    final int expectedHighlightTextColor = tmpTp.getColor();
    final float expectedHighlightTextSize = tmpTp.getTextSize();
    final TextView textView = (TextView) getActivity().findViewById(R.id.textview);
    // *XX* means that XX is highlighted.
    for (int i = 0; i < 2; i++) {
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(replaceText(text));
        setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
        setSuggestionSpan(multiWordSuggestionSpan, 0, text.length());
        showSuggestionsPopup();
        assertSuggestionsPopupIsDisplayed();
        assertSuggestionsPopupContainsItem("abc DEF ghi");
        assertSuggestionsPopupContainsItem("abc Def ghi");
        assertSuggestionsPopupContainsItem("ABC DEF GHI");
        assertSuggestionsPopupContainsItem("Abc Def Ghi");
        assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
        onSuggestionsPopup().check(new ViewAssertion() {

            @Override
            public void check(View view, NoMatchingViewException e) {
                final ListView listView = (ListView) view.findViewById(com.android.internal.R.id.suggestionContainer);
                assertNotNull(listView);
                final int childNum = listView.getChildCount();
                assertEquals(singleWordCandidates.length + multiWordCandidates.length, childNum);
                for (int j = 0; j < childNum; j++) {
                    final TextView suggestion = (TextView) listView.getChildAt(j);
                    assertNotNull(suggestion);
                    final Spanned spanned = (Spanned) suggestion.getText();
                    assertNotNull(spanned);
                    // Check that the suggestion item order is kept.
                    final String expectedText;
                    if (j < singleWordCandidates.length) {
                        expectedText = "abc " + singleWordCandidates[j] + " ghi";
                    } else {
                        expectedText = multiWordCandidates[j - singleWordCandidates.length];
                    }
                    assertEquals(expectedText, spanned.toString());
                    // Check that the text is highlighted with correct color and text size.
                    final TextAppearanceSpan[] taSpan = spanned.getSpans(text.indexOf('d'), text.indexOf('f') + 1, TextAppearanceSpan.class);
                    assertEquals(1, taSpan.length);
                    TextPaint tp = new TextPaint();
                    taSpan[0].updateDrawState(tp);
                    assertEquals(expectedHighlightTextColor, tp.getColor());
                    assertEquals(expectedHighlightTextSize, tp.getTextSize());
                    // Check the correct part of the text is highlighted.
                    final int expectedStart;
                    final int expectedEnd;
                    if (j < singleWordCandidates.length) {
                        expectedStart = text.indexOf('d');
                        expectedEnd = text.indexOf('f') + 1;
                    } else {
                        expectedStart = 0;
                        expectedEnd = text.length();
                    }
                    assertEquals(expectedStart, spanned.getSpanStart(taSpan[0]));
                    assertEquals(expectedEnd, spanned.getSpanEnd(taSpan[0]));
                }
            }
        });
        pressBack();
        onView(withId(R.id.textview)).inRoot(withDecorView(is(getActivity().getWindow().getDecorView()))).perform(clearText());
    }
}
Also used : TextAppearanceSpan(android.text.style.TextAppearanceSpan) NoMatchingViewException(android.support.test.espresso.NoMatchingViewException) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView) RootMatchers.withDecorView(android.support.test.espresso.matcher.RootMatchers.withDecorView) DragHandleUtils.onHandleView(android.widget.espresso.DragHandleUtils.onHandleView) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint) ViewAssertion(android.support.test.espresso.ViewAssertion) TypedArray(android.content.res.TypedArray) SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 48 with TypedArray

use of android.content.res.TypedArray in project platform_frameworks_base by android.

the class LayerDrawable method inflate.

@Override
public void inflate(@NonNull Resources r, @NonNull XmlPullParser parser, @NonNull AttributeSet attrs, @Nullable Theme theme) throws XmlPullParserException, IOException {
    super.inflate(r, parser, attrs, theme);
    final LayerState state = mLayerState;
    if (state == null) {
        return;
    }
    // The density may have changed since the last update. This will
    // apply scaling to any existing constant state properties.
    final int density = Drawable.resolveDensity(r, 0);
    state.setDensity(density);
    final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.LayerDrawable);
    updateStateFromTypedArray(a);
    a.recycle();
    final ChildDrawable[] array = state.mChildren;
    final int N = state.mNum;
    for (int i = 0; i < N; i++) {
        final ChildDrawable layer = array[i];
        layer.setDensity(density);
    }
    inflateLayers(r, parser, attrs, theme);
    ensurePadding();
    refreshPadding();
}
Also used : TypedArray(android.content.res.TypedArray)

Example 49 with TypedArray

use of android.content.res.TypedArray in project platform_frameworks_base by android.

the class LayerDrawable method applyTheme.

@Override
public void applyTheme(@NonNull Theme t) {
    super.applyTheme(t);
    final LayerState state = mLayerState;
    if (state == null) {
        return;
    }
    final int density = Drawable.resolveDensity(t.getResources(), 0);
    state.setDensity(density);
    if (state.mThemeAttrs != null) {
        final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.LayerDrawable);
        updateStateFromTypedArray(a);
        a.recycle();
    }
    final ChildDrawable[] array = state.mChildren;
    final int N = state.mNum;
    for (int i = 0; i < N; i++) {
        final ChildDrawable layer = array[i];
        layer.setDensity(density);
        if (layer.mThemeAttrs != null) {
            final TypedArray a = t.resolveAttributes(layer.mThemeAttrs, R.styleable.LayerDrawableItem);
            updateLayerFromTypedArray(layer, a);
            a.recycle();
        }
        final Drawable d = layer.mDrawable;
        if (d != null && d.canApplyTheme()) {
            d.applyTheme(t);
            // Update cached mask of child changing configurations.
            state.mChildrenChangingConfigurations |= d.getChangingConfigurations();
        }
    }
}
Also used : TypedArray(android.content.res.TypedArray)

Example 50 with TypedArray

use of android.content.res.TypedArray in project platform_frameworks_base by android.

the class LevelListDrawable method inflateChildElements.

private void inflateChildElements(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme) throws XmlPullParserException, IOException {
    int type;
    int low = 0;
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        if (depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }
        TypedArray a = obtainAttributes(r, theme, attrs, com.android.internal.R.styleable.LevelListDrawableItem);
        low = a.getInt(com.android.internal.R.styleable.LevelListDrawableItem_minLevel, 0);
        int high = a.getInt(com.android.internal.R.styleable.LevelListDrawableItem_maxLevel, 0);
        int drawableRes = a.getResourceId(com.android.internal.R.styleable.LevelListDrawableItem_drawable, 0);
        a.recycle();
        if (high < 0) {
            throw new XmlPullParserException(parser.getPositionDescription() + ": <item> tag requires a 'maxLevel' attribute");
        }
        Drawable dr;
        if (drawableRes != 0) {
            dr = r.getDrawable(drawableRes, theme);
        } else {
            while ((type = parser.next()) == XmlPullParser.TEXT) {
            }
            if (type != XmlPullParser.START_TAG) {
                throw new XmlPullParserException(parser.getPositionDescription() + ": <item> tag requires a 'drawable' attribute or " + "child tag defining a drawable");
            }
            dr = Drawable.createFromXmlInner(r, parser, attrs, theme);
        }
        mLevelListState.addLevel(low, high, dr);
    }
    onLevelChange(getLevel());
}
Also used : TypedArray(android.content.res.TypedArray) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Aggregations

TypedArray (android.content.res.TypedArray)2031 Paint (android.graphics.Paint)193 TypedValue (android.util.TypedValue)190 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)162 Drawable (android.graphics.drawable.Drawable)119 Resources (android.content.res.Resources)118 View (android.view.View)117 Context (android.content.Context)80 XmlResourceParser (android.content.res.XmlResourceParser)80 ColorStateList (android.content.res.ColorStateList)78 AttributeSet (android.util.AttributeSet)78 IOException (java.io.IOException)77 SuppressLint (android.annotation.SuppressLint)66 TextPaint (android.text.TextPaint)63 TextView (android.widget.TextView)63 ViewGroup (android.view.ViewGroup)59 Bundle (android.os.Bundle)48 Point (android.graphics.Point)43 LayoutInflater (android.view.LayoutInflater)42 ImageView (android.widget.ImageView)40