|
| 1 | +package com.firebase.ui.auth.util.ui; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.TypedArray; |
| 5 | +import android.graphics.drawable.Drawable; |
| 6 | +import android.os.Build; |
| 7 | +import android.util.AttributeSet; |
| 8 | + |
| 9 | +import com.firebase.ui.auth.R; |
| 10 | + |
| 11 | +import androidx.annotation.RestrictTo; |
| 12 | +import androidx.appcompat.content.res.AppCompatResources; |
| 13 | +import androidx.appcompat.widget.AppCompatButton; |
| 14 | +import androidx.core.widget.TextViewCompat; |
| 15 | + |
| 16 | +/** |
| 17 | + * A custom button that supports using vector drawables with the {@code |
| 18 | + * android:drawable[Start/End/Top/Bottom]} attribute pre-L. |
| 19 | + * <p> |
| 20 | + * AppCompat can only load vector drawables with srcCompat pre-L and doesn't provide a similar |
| 21 | + * compatibility attribute for compound drawables. Thus, we must load compound drawables at runtime |
| 22 | + * using AppCompat and inject them into the button to support pre-L devices. |
| 23 | + */ |
| 24 | +@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) |
| 25 | +public class SupportVectorDrawablesButton extends AppCompatButton { |
| 26 | + public SupportVectorDrawablesButton(Context context) { |
| 27 | + super(context); |
| 28 | + } |
| 29 | + |
| 30 | + public SupportVectorDrawablesButton(Context context, AttributeSet attrs) { |
| 31 | + super(context, attrs); |
| 32 | + initSupportVectorDrawablesAttrs(attrs); |
| 33 | + } |
| 34 | + |
| 35 | + public SupportVectorDrawablesButton(Context context, AttributeSet attrs, int defStyleAttr) { |
| 36 | + super(context, attrs, defStyleAttr); |
| 37 | + initSupportVectorDrawablesAttrs(attrs); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Loads the compound drawables natively on L+ devices and using AppCompat pre-L. |
| 42 | + * <p> |
| 43 | + * <i>Note:</i> If we ever need a TextView with compound drawables, this same technique is |
| 44 | + * applicable. |
| 45 | + */ |
| 46 | + private void initSupportVectorDrawablesAttrs(AttributeSet attrs) { |
| 47 | + if (attrs == null) { return; } |
| 48 | + |
| 49 | + TypedArray attributeArray = getContext().obtainStyledAttributes( |
| 50 | + attrs, |
| 51 | + R.styleable.SupportVectorDrawablesButton); |
| 52 | + |
| 53 | + Drawable drawableStart = null; |
| 54 | + Drawable drawableEnd = null; |
| 55 | + Drawable drawableTop = null; |
| 56 | + Drawable drawableBottom = null; |
| 57 | + |
| 58 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 59 | + drawableStart = attributeArray.getDrawable( |
| 60 | + R.styleable.SupportVectorDrawablesButton_drawableStartCompat); |
| 61 | + drawableEnd = attributeArray.getDrawable( |
| 62 | + R.styleable.SupportVectorDrawablesButton_drawableEndCompat); |
| 63 | + drawableTop = attributeArray.getDrawable( |
| 64 | + R.styleable.SupportVectorDrawablesButton_drawableTopCompat); |
| 65 | + drawableBottom = attributeArray.getDrawable( |
| 66 | + R.styleable.SupportVectorDrawablesButton_drawableBottomCompat); |
| 67 | + } else { |
| 68 | + int drawableStartId = attributeArray.getResourceId( |
| 69 | + R.styleable.SupportVectorDrawablesButton_drawableStartCompat, -1); |
| 70 | + int drawableEndId = attributeArray.getResourceId( |
| 71 | + R.styleable.SupportVectorDrawablesButton_drawableEndCompat, -1); |
| 72 | + int drawableTopId = attributeArray.getResourceId( |
| 73 | + R.styleable.SupportVectorDrawablesButton_drawableTopCompat, -1); |
| 74 | + int drawableBottomId = attributeArray.getResourceId( |
| 75 | + R.styleable.SupportVectorDrawablesButton_drawableBottomCompat, -1); |
| 76 | + |
| 77 | + if (drawableStartId != -1) { |
| 78 | + drawableStart = AppCompatResources.getDrawable(getContext(), drawableStartId); |
| 79 | + } |
| 80 | + if (drawableEndId != -1) { |
| 81 | + drawableEnd = AppCompatResources.getDrawable(getContext(), drawableEndId); |
| 82 | + } |
| 83 | + if (drawableTopId != -1) { |
| 84 | + drawableTop = AppCompatResources.getDrawable(getContext(), drawableTopId); |
| 85 | + } |
| 86 | + if (drawableBottomId != -1) { |
| 87 | + drawableBottom = AppCompatResources.getDrawable(getContext(), drawableBottomId); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds( |
| 92 | + this, drawableStart, drawableTop, drawableEnd, drawableBottom); |
| 93 | + |
| 94 | + attributeArray.recycle(); |
| 95 | + } |
| 96 | +} |
0 commit comments