Skip to content

Commit

Permalink
Add BadgeDrawable constructor that takes a xml resource.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 252709395
  • Loading branch information
wcshi authored and ymarian committed Jun 18, 2019
1 parent 4710d61 commit ccc4890
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 35 deletions.
29 changes: 27 additions & 2 deletions lib/java/com/google/android/material/badge/BadgeDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.annotation.StyleableRes;
import androidx.annotation.XmlRes;
import com.google.android.material.internal.DrawableUtils;
import com.google.android.material.internal.TextDrawableHelper;
import com.google.android.material.internal.TextDrawableHelper.TextDrawableDelegate;
import com.google.android.material.internal.ThemeEnforcement;
Expand Down Expand Up @@ -109,6 +111,9 @@ public class BadgeDrawable extends Drawable implements TextDrawableDelegate {
/** Maximum value of number that can be displayed in a circular badge. */
private static final int MAX_CIRCULAR_BADGE_NUMBER_COUNT = 99;

@StyleRes private static final int DEFAULT_STYLE = R.style.Widget_MaterialComponents_Badge;
@AttrRes private static final int DEFAULT_THEME_ATTR = R.attr.badgeStyle;

/**
* If the badge number exceeds the maximum allowed number, append this suffix to the max badge
* number and display is as the badge text instead.
Expand Down Expand Up @@ -209,8 +214,28 @@ static BadgeDrawable createFromSavedState(Context context, @NonNull SavedState s

/** Creates an instance of BadgeDrawable with default values. */
public static BadgeDrawable create(Context context) {
return createFromAttributes(
context, /* attrs= */ null, R.attr.badgeStyle, R.style.Widget_MaterialComponents_Badge);
return createFromAttributes(context, /* attrs= */ null, DEFAULT_THEME_ATTR, DEFAULT_STYLE);
}

/**
* Returns a BadgeDrawable from the given XML resource. All attributes from {@link
* R.styleable#Badge} and a custom <code>style</code> attribute are supported. A badge resource
* may look like:
*
* <pre>{@code
* <badge
* xmlns:app="http://schemas.android.com/apk/res-auto"
* style="@style/Widget.MaterialComponents.Badge"
* app:maxCharacterCount="2"/>
* }</pre>
*/
public static BadgeDrawable createFromResource(Context context, @XmlRes int id) {
AttributeSet attrs = DrawableUtils.parseDrawableXml(context, id, "badge");
@StyleRes int style = attrs.getStyleAttribute();
if (style == 0) {
style = DEFAULT_STYLE;
}
return createFromAttributes(context, attrs, DEFAULT_THEME_ATTR, style);
}

/** Returns a BadgeDrawable from the given attributes. */
Expand Down
39 changes: 6 additions & 33 deletions lib/java/com/google/android/material/chip/ChipDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
Expand Down Expand Up @@ -74,17 +72,13 @@
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.util.Xml;
import android.view.View;
import com.google.android.material.canvas.CanvasCompat;
import com.google.android.material.color.MaterialColors;
import com.google.android.material.drawable.DrawableUtils;
import com.google.android.material.ripple.RippleUtils;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
* ChipDrawable contains all the layout and draw logic for {@link Chip}.
Expand Down Expand Up @@ -299,34 +293,13 @@ public static ChipDrawable createFromAttributes(
* }</pre>
*/
public static ChipDrawable createFromResource(Context context, @XmlRes int id) {
try {
XmlPullParser parser = context.getResources().getXml(id);

int type;
do {
type = parser.next();
} while (type != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT);
if (type != XmlPullParser.START_TAG) {
throw new XmlPullParserException("No start tag found");
}

if (!TextUtils.equals(parser.getName(), "chip")) {
throw new XmlPullParserException("Must have a <chip> start tag");
}

AttributeSet attrs = Xml.asAttributeSet(parser);
@StyleRes int style = attrs.getStyleAttribute();
if (style == 0) {
style = R.style.Widget_MaterialComponents_Chip_Entry;
}

return createFromAttributes(context, attrs, R.attr.chipStandaloneStyle, style);
} catch (XmlPullParserException | IOException e) {
Resources.NotFoundException exception =
new NotFoundException("Can't load chip resource ID #0x" + Integer.toHexString(id));
exception.initCause(e);
throw exception;
AttributeSet attrs =
com.google.android.material.internal.DrawableUtils.parseDrawableXml(context, id, "chip");
@StyleRes int style = attrs.getStyleAttribute();
if (style == 0) {
style = R.style.Widget_MaterialComponents_Chip_Entry;
}
return createFromAttributes(context, attrs, R.attr.chipStandaloneStyle, style);
}

private ChipDrawable(
Expand Down
37 changes: 37 additions & 0 deletions lib/java/com/google/android/material/internal/DrawableUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.DrawableContainer;
import androidx.annotation.RestrictTo;
import androidx.annotation.XmlRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;
import java.io.IOException;
import java.lang.reflect.Method;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
* Utility class for functionality relating to {@link Drawable}s.
Expand Down Expand Up @@ -68,4 +77,32 @@ private static boolean setContainerConstantStateV9(
}
return false;
}

public static AttributeSet parseDrawableXml(
final Context context, @XmlRes int id, CharSequence startTag) {
try {
XmlPullParser parser = context.getResources().getXml(id);

int type;
do {
type = parser.next();
} while (type != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT);
if (type != XmlPullParser.START_TAG) {
throw new XmlPullParserException("No start tag found");
}

if (!TextUtils.equals(parser.getName(), startTag)) {
throw new XmlPullParserException("Must have a <" + startTag + "> start tag");
}

AttributeSet attrs = Xml.asAttributeSet(parser);

return attrs;
} catch (XmlPullParserException | IOException e) {
NotFoundException exception =
new NotFoundException("Can't load badge resource ID #0x" + Integer.toHexString(id));
exception.initCause(e);
throw exception;
}
}
}

0 comments on commit ccc4890

Please sign in to comment.