<commit_before>package com.xtremelabs.robolectric.shadows;

import android.graphics.drawable.Drawable;
import android.text.style.URLSpan;
import android.text.util.Linkify;
import android.view.KeyEvent;
import android.widget.TextView;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;

import java.util.ArrayList;
import java.util.List;

import static android.view.View.VISIBLE;
import static com.xtremelabs.robolectric.Robolectric.shadowOf_;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(TextView.class)
public class ShadowTextView extends ShadowView {
    private CharSequence text = "";
    private CompoundDrawables compoundDrawablesImpl;
    private Integer textColorHexValue;
    private float textSize = 14.0f;
    private boolean autoLinkPhoneNumbers;
    private int autoLinkMask;
    private CharSequence hintText;
    private int compoundDrawablePadding;

    @Override public void applyAttributes() {
        super.applyAttributes();
        applyTextAttribute();
        applyCompoundDrawablesWithIntrinsicBoundsAttributes();
    }

  @Implementation
    public void setText(CharSequence text) {
        if (text == null) {
            text = "";
        }
        this.text = text;
    }

    @Implementation
    public void setText(int textResourceId) {
        this.text = getResources().getText(textResourceId);
    }

    @Implementation
    public CharSequence getText() {
        return text;
    }

    @Implementation
    public int length() {
        return text.length();
    }

    @Implementation
    public void setTextColor(int color) {
        textColorHexValue = color;
    }

    @Implementation
    public void setTextSize(float size) {
        textSize = size;
    }

    @Implementation
    public final void setHint(int resId) {
        this.hintText = getResources().getText(resId);
    }

    @Implementation
    public CharSequence getHint() {
        return hintText;
    }

    @Implementation
    public URLSpan[] getUrls() {
        String[] words = text.toString().split("\\s+");
        List<URLSpan> urlSpans = new ArrayList<URLSpan>();
        for (String word : words) {
            if (word.startsWith("http://")) {
                urlSpans.add(new URLSpan(word));
            }
        }
        return urlSpans.toArray(new URLSpan[urlSpans.size()]);
    }

    @Implementation
    public final void setAutoLinkMask(int mask) {
        autoLinkMask = mask;

        autoLinkPhoneNumbers = (mask & Linkify.PHONE_NUMBERS) != 0;
    }

    @Implementation
    public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) {
        compoundDrawablesImpl = new CompoundDrawables(left, top, right, bottom);
    }

    @Implementation
    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top,
                                                        Drawable right, Drawable bottom) {
        compoundDrawablesImpl = new CompoundDrawables(left, top, right, bottom);
    }

    @Implementation
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        compoundDrawablesImpl = new CompoundDrawables(left, top, right, bottom);
    }

    @Implementation
    public Drawable[] getCompoundDrawables() {
        if (compoundDrawablesImpl == null) {
            return new Drawable[]{null, null, null, null};
        }
        return new Drawable[]{
                compoundDrawablesImpl.leftDrawable,
                compoundDrawablesImpl.topDrawable,
                compoundDrawablesImpl.rightDrawable,
                compoundDrawablesImpl.bottomDrawable
        };
    }

    @Implementation
    public void setCompoundDrawablePadding(int compoundDrawablePadding) {
        this.compoundDrawablePadding = compoundDrawablePadding;
    }

    @Implementation
    public int getCompoundDrawablePadding() {
        return compoundDrawablePadding;
    }

    @Implementation
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (onKeyListener != null) {
            return onKeyListener.onKey(realView, keyCode, event);
        } else {
            return false;
        }
    }

    /**
     * Returns the text string of this {@code TextView}.
     * <p/>
     * Robolectric extension.
     */
    @Override
    public String innerText() {
        return (text == null || getVisibility() != VISIBLE) ? "" : text.toString();
    }

    @Override @Implementation
    public boolean equals(Object o) {
        return super.equals(shadowOf_(o));
    }

    @Override @Implementation
    public int hashCode() {
        return super.hashCode();
    }

    public CompoundDrawables getCompoundDrawablesImpl() {
        return compoundDrawablesImpl;
    }

    void setCompoundDrawablesImpl(CompoundDrawables compoundDrawablesImpl) {
        this.compoundDrawablesImpl = compoundDrawablesImpl;
    }

    public Integer getTextColorHexValue() {
        return textColorHexValue;
    }

    @Implementation
    public float getTextSize() {
        return textSize;
    }

    public boolean isAutoLinkPhoneNumbers() {
        return autoLinkPhoneNumbers;
    }

    private void applyTextAttribute() {
        String text = attributeSet.getAttributeValue("android", "text");
        if (text != null) {
            if (text.startsWith("@string/")) {
                int textResId = attributeSet.getAttributeResourceValue("android", "text", 0);
                text = context.getResources().getString(textResId);
            }
            setText(text);
        }
    }

    private void applyCompoundDrawablesWithIntrinsicBoundsAttributes() {
        setCompoundDrawablesWithIntrinsicBounds(
                attributeSet.getAttributeResourceValue("android", "drawableLeft", 0),
                attributeSet.getAttributeResourceValue("android", "drawableTop", 0),
                attributeSet.getAttributeResourceValue("android", "drawableRight", 0),
                attributeSet.getAttributeResourceValue("android", "drawableBottom", 0));
    }

    public static class CompoundDrawables {
        public int left;
        public int top;
        public int right;
        public int bottom;

        public Drawable leftDrawable;
        public Drawable topDrawable;
        public Drawable rightDrawable;
        public Drawable bottomDrawable;

        public CompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
            leftDrawable = left;
            topDrawable = top;
            rightDrawable = right;
            bottomDrawable = bottom;<commit_msg>parse android:textColor attribute from xml layout files for TextView's and use the parsed value to setTextColor()<commit_after>package com.xtremelabs.robolectric.shadows;

import android.graphics.drawable.Drawable;
import android.text.style.URLSpan;
import android.text.util.Linkify;
import android.view.KeyEvent;
import android.widget.TextView;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;

import java.util.ArrayList;
import java.util.List;

import static android.view.View.VISIBLE;
import static com.xtremelabs.robolectric.Robolectric.shadowOf_;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(TextView.class)
public class ShadowTextView extends ShadowView {
    private CharSequence text = "";
    private CompoundDrawables compoundDrawablesImpl;
    private Integer textColorHexValue;
    private float textSize = 14.0f;
    private boolean autoLinkPhoneNumbers;
    private int autoLinkMask;
    private CharSequence hintText;
    private int compoundDrawablePadding;

    @Override
    public void applyAttributes() {
        super.applyAttributes();
        applyTextAttribute();
        applyTextColorAttribute();
        applyCompoundDrawablesWithIntrinsicBoundsAttributes();
    }

    @Implementation
    public void setText(CharSequence text) {
        if (text == null) {
            text = "";
        }
        this.text = text;
    }

    @Implementation
    public void setText(int textResourceId) {
        this.text = getResources().getText(textResourceId);
    }

    @Implementation
    public CharSequence getText() {
        return text;
    }

    @Implementation
    public int length() {
        return text.length();
    }

    @Implementation
    public void setTextColor(int color) {
        textColorHexValue = color;
    }

    @Implementation
    public void setTextSize(float size) {
        textSize = size;
    }

    @Implementation
    public final void setHint(int resId) {
        this.hintText = getResources().getText(resId);
    }

    @Implementation
    public CharSequence getHint() {
        return hintText;
    }

    @Implementation
    public URLSpan[] getUrls() {
        String[] words = text.toString().split("\\s+");
        List<URLSpan> urlSpans = new ArrayList<URLSpan>();
        for (String word : words) {
            if (word.startsWith("http://")) {
                urlSpans.add(new URLSpan(word));
            }
        }
        return urlSpans.toArray(new URLSpan[urlSpans.size()]);
    }

    @Implementation
    public final void setAutoLinkMask(int mask) {
        autoLinkMask = mask;

        autoLinkPhoneNumbers = (mask & Linkify.PHONE_NUMBERS) != 0;
    }

    @Implementation
    public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) {
        compoundDrawablesImpl = new CompoundDrawables(left, top, right, bottom);
    }

    @Implementation
    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top,
                                                        Drawable right, Drawable bottom) {
        compoundDrawablesImpl = new CompoundDrawables(left, top, right, bottom);
    }

    @Implementation
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        compoundDrawablesImpl = new CompoundDrawables(left, top, right, bottom);
    }

    @Implementation
    public Drawable[] getCompoundDrawables() {
        if (compoundDrawablesImpl == null) {
            return new Drawable[]{null, null, null, null};
        }
        return new Drawable[]{
                compoundDrawablesImpl.leftDrawable,
                compoundDrawablesImpl.topDrawable,
                compoundDrawablesImpl.rightDrawable,
                compoundDrawablesImpl.bottomDrawable
        };
    }

    @Implementation
    public void setCompoundDrawablePadding(int compoundDrawablePadding) {
        this.compoundDrawablePadding = compoundDrawablePadding;
    }

    @Implementation
    public int getCompoundDrawablePadding() {
        return compoundDrawablePadding;
    }

    @Implementation
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (onKeyListener != null) {
            return onKeyListener.onKey(realView, keyCode, event);
        } else {
            return false;
        }
    }

    /**
     * Returns the text string of this {@code TextView}.
     * <p/>
     * Robolectric extension.
     */
    @Override
    public String innerText() {
        return (text == null || getVisibility() != VISIBLE) ? "" : text.toString();
    }

    @Override @Implementation
    public boolean equals(Object o) {
        return super.equals(shadowOf_(o));
    }

    @Override @Implementation
    public int hashCode() {
        return super.hashCode();
    }

    public CompoundDrawables getCompoundDrawablesImpl() {
        return compoundDrawablesImpl;
    }

    void setCompoundDrawablesImpl(CompoundDrawables compoundDrawablesImpl) {
        this.compoundDrawablesImpl = compoundDrawablesImpl;
    }

    public Integer getTextColorHexValue() {
        return textColorHexValue;
    }

    @Implementation
    public float getTextSize() {
        return textSize;
    }

    public boolean isAutoLinkPhoneNumbers() {
        return autoLinkPhoneNumbers;
    }

    private void applyTextAttribute() {
        String text = attributeSet.getAttributeValue("android", "text");
        if (text != null) {
            if (text.startsWith("@string/")) {
                int textResId = attributeSet.getAttributeResourceValue("android", "text", 0);
                text = context.getResources().getString(textResId);
            }
            setText(text);
        }
    }

    private void applyTextColorAttribute() {
        String colorValue = attributeSet.getAttributeValue("android", "textColor");
        if (colorValue != null) {
            if (colorValue.startsWith("@color/") || colorValue.startsWith("@android:color/")) {
                int colorResId = attributeSet.getAttributeResourceValue("android", "textColor", 0);
                setTextColor(context.getResources().getColor(colorResId));
            } else if (colorValue.startsWith("#")) {
                int colorFromHex = (int) Long.valueOf(colorValue.replaceAll("#", ""), 16).longValue();
                setTextColor(colorFromHex);
            }
        }
    }

    private void applyCompoundDrawablesWithIntrinsicBoundsAttributes() {
        setCompoundDrawablesWithIntrinsicBounds(
                attributeSet.getAttributeResourceValue("android", "drawableLeft", 0),
                attributeSet.getAttributeResourceValue("android", "drawableTop", 0),
                attributeSet.getAttributeResourceValue("android", "drawableRight", 0),
                attributeSet.getAttributeResourceValue("android", "drawableBottom", 0));
    }

    public static class CompoundDrawables {
        public int left;
        public int top;
        public int right;
        public int bottom;

        public Drawable leftDrawable;
        public Drawable topDrawable;
        public Drawable rightDrawable;
        public Drawable bottomDrawable;

        public CompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
            leftDrawable = left;
            topDrawable = top;
            rightDrawable = right;
            bottomDrawable = bottom;
{'fraction_non_alphanumeric': 0.08836513813728282, 'fraction_numerical': 0.0014953004841925378, 'mean_word_length': 2.651846073842954, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3873220', 'n_tokens_mistral': 3739, 'n_tokens_neox': 3653, 'n_words': 845}