android_translation_layer/src/api-impl/android/widget/TextView.java
Mis012 bf79a77993 api-impl: import ToggleButton from AOSP, clean up constructors
taken from AOSP master (commit ed8c91e410671fa153f1587f61d05b303bf94f95)
2025-02-15 21:24:26 +01:00

395 lines
12 KiB
Java

package android.widget;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.BoringLayout;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.BaseMovementMethod;
import android.text.method.KeyListener;
import android.text.method.MovementMethod;
import android.text.method.TransformationMethod;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ActionMode;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
public class TextView extends View {
private ColorStateList colors = new ColorStateList(new int[][] {new int[0]}, new int[1]);
private CharSequence text = "";
public TextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TextView(Context context) {
this(context, null);
}
public TextView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public TextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView, defStyleAttr, 0);
try {
if (a.hasValue(com.android.internal.R.styleable.TextView_text)) {
setText(a.getText(com.android.internal.R.styleable.TextView_text));
}
int ap = a.getResourceId(com.android.internal.R.styleable.TextView_textAppearance, -1);
if (ap != -1) {
TypedArray aa = context.obtainStyledAttributes(ap, com.android.internal.R.styleable.TextAppearance);
if (aa.hasValue(com.android.internal.R.styleable.TextAppearance_textColor)) {
setTextColor(aa.getColorStateList(com.android.internal.R.styleable.TextAppearance_textColor));
}
if (aa.hasValue(com.android.internal.R.styleable.TextAppearance_textSize)) {
setTextSize(aa.getDimensionPixelSize(com.android.internal.R.styleable.TextAppearance_textSize, 10));
}
aa.recycle();
}
if (a.hasValue(com.android.internal.R.styleable.TextView_textColor)) {
setTextColor(a.getColorStateList(com.android.internal.R.styleable.TextView_textColor));
}
if (a.hasValue(com.android.internal.R.styleable.TextView_textSize)) {
setTextSize(a.getDimensionPixelSize(com.android.internal.R.styleable.TextView_textSize, 10));
}
if(a.hasValue(com.android.internal.R.styleable.TextView_textStyle)) {
int textStyle = a.getInt(com.android.internal.R.styleable.TextView_textStyle, 0);
setTypeface(getTypeface(), textStyle);
}
if(a.hasValue(com.android.internal.R.styleable.TextView_textAllCaps)) {
boolean allCaps = a.getBoolean(com.android.internal.R.styleable.TextView_textAllCaps, false);
setAllCaps(allCaps);
}
} catch(java.lang.Exception e) { System.out.println("exception while inflating TextView:"); e.printStackTrace(); }
a.recycle();
haveCustomMeasure = false;
}
@Override
protected native long native_constructor(Context context, AttributeSet attrs);
public void setText(CharSequence text) {
this.text = text;
native_setText(text != null ? text.toString() : null);
if (text instanceof android.text.Spanned)
native_set_markup(1);
if (!isLayoutRequested())
requestLayout();
}
public void setText(int resId) {
setText(getContext().getResources().getText(resId));
}
private native final void native_set_markup(int bool);
public native final void native_setText(String text);
public void setTextSize(int unit, float size) {
if(unit != TypedValue.COMPLEX_UNIT_SP)
System.out.println("setTextSize called with non-SP unit ("+unit+"), we don't currently handle that");
setTextSize(size);
}
public native void setTextSize(float size);
public native final void native_setTextColor(int color);
public void setTextColor(int color) {
native_setTextColor(color);
}
public void setTextColor(ColorStateList colors) {
if (colors != null) {
this.colors = colors;
setTextColor(colors.getDefaultColor()); // TODO: do this properly
}
}
public void setTypeface(Typeface tf, int style) {
String[] classesToRemove = {"ATL-font-bold", "ATL-font-italic"};
native_removeClasses(widget, classesToRemove);
switch(style) {
case Typeface.BOLD:
native_addClass(widget, "ATL-font-bold");
break;
case Typeface.ITALIC:
native_addClass(widget, "ATL-font-italic");
break;
case Typeface.BOLD_ITALIC:
native_addClass(widget, "ATL-font-bold");
native_addClass(widget, "ATL-font-italic");
break;
default:
break;
}
}
public void setTypeface(Typeface tf) {}
public void setLineSpacing(float add, float mult) {}
public final void setLinksClickable(boolean whether) {}
public void setInputType(int type) {}
public void setFilters(InputFilter[] filters) {}
public void setCursorVisible(boolean visible) {}
public void setImeOptions(int imeOptions) {}
public final ColorStateList getTextColors() { return colors; }
public static ColorStateList getTextColors(Context context, TypedArray attrs) { return new ColorStateList(new int[][] {new int[0]}, new int[1]); }
public TextPaint getPaint() {
return new TextPaint();
}
public void addTextChangedListener(TextWatcher watcher) {}
public void removeTextChangedListener(TextWatcher watcher) {}
public void setOnEditorActionListener(TextView.OnEditorActionListener l) {}
public TransformationMethod getTransformationMethod() {
return null;
}
public void setHintTextColor(ColorStateList colorStateList) {}
public void setHintTextColor(int i) {}
public void setLinkTextColor(ColorStateList colorStateList) {}
public void setSingleLine() {}
public void setSelection(int i) {}
public void setSelection(int i, int j) {}
public int getSelectionStart() {
return 0;
}
public int getSelectionEnd() {
return 0;
}
public void setEllipsize(TextUtils.TruncateAt truncateAt) {}
public void setTextAppearance(Context context, int appearance) {}
public void setMaxLines(int maxLines) {}
public void setMinWidth(int minWidth) {}
public void setMaxWidth(int maxWidth) {}
public Typeface getTypeface() {return null;}
public float getTextSize() {return 10;}
public int getGravity() {
return Gravity.CENTER;
}
public int getCompoundPaddingTop() {return 0;}
public int getCompoundPaddingBottom() {return 0;}
public CharSequence getText() {
return text;
};
public void setCompoundDrawablePadding(int pad) {}
protected native void native_setCompoundDrawables(long widget, long left, long top, long right, long bottom);
// just to prevent garbage collection while native side uses it
private Drawable drawableLeft = null;
private Drawable drawableTop = null;
private Drawable drawableRight = null;
private Drawable drawableBottom = null;
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
native_setCompoundDrawables(widget,
left != null ? left.paintable : 0,
top != null ? top.paintable : 0,
right != null ? right.paintable : 0,
bottom != null ? bottom.paintable : 0);
drawableLeft = left;
drawableTop = top;
drawableRight = right;
drawableBottom = bottom;
}
public void setAllCaps(boolean allCaps) {
String[] classesToRemove = {"ATL-text-uppercase"};
native_removeClasses(widget, classesToRemove);
if(allCaps){
native_addClass(widget, "ATL-text-uppercase");
}
}
public void setSaveEnabled(boolean enabled) {}
public final void setAutoLinkMask(int mask) {}
public void setEditableFactory(Editable.Factory factory) {}
public KeyListener getKeyListener() {return null;}
public int getInputType() {return 0;}
public final void setTransformationMethod(TransformationMethod method) {}
public InputFilter[] getFilters() {return new InputFilter[0];}
public int getMaxLines() {return -1;}
public void setCompoundDrawablesRelative(Drawable start, Drawable top, Drawable end, Drawable bottom) {
setCompoundDrawables(start, top, end, bottom);
}
public int getLineCount() {return 1;}
public URLSpan[] getUrls() {return new URLSpan[0];}
public void setMovementMethod(MovementMethod method) {}
public void setTextIsSelectable(boolean selectable) {}
public void setText(CharSequence text, BufferType type) {
setText(text);
}
public MovementMethod getMovementMethod() {
return new BaseMovementMethod();
}
public CharSequence getHint() {return "HINT";}
public int getMinHeight() {return 0;}
public int getMinWidth() {return 0;}
public void setMinHeight(int minHeight) {}
public void setHorizontallyScrolling(boolean whether) {}
public static interface OnEditorActionListener {
public abstract boolean onEditorAction(TextView v, int actionId, KeyEvent event);
}
public static enum BufferType {
EDITABLE,
NORMAL,
SPANNABLE,
}
public Layout getLayout() {
return new BoringLayout(getText(), getPaint(), getWidth(), Layout.Alignment.ALIGN_NORMAL, 1, 0, new BoringLayout.Metrics(), false);
}
public int getCurrentTextColor() {return 0;}
public void setSingleLine(boolean singleLine) {}
public int getCompoundPaddingLeft() {return 0;}
public int getCompoundPaddingRight() {return 0;}
public void setHint(int resId) {}
public float getLetterSpacing() {return 0.f;}
public void setCompoundDrawablesRelativeWithIntrinsicBounds(int start, int top, int end, int bottom) {}
public void setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable start, Drawable top, Drawable end, Drawable bottom) {}
public boolean getLinksClickable() {return true;}
public boolean isTextSelectable() {return true;}
public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) {}
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {}
public void setHint(CharSequence s) {}
public Drawable[] getCompoundDrawablesRelative() {return new Drawable[4];}
public Drawable[] getCompoundDrawables() {return new Drawable[4];}
public void setTextAppearance(int dummy) {}
public int length() {
return getText().length();
}
public void setHighlightColor(int color) {}
public Editable getEditableText() {
return new SpannableStringBuilder(getText());
}
public int getMaxWidth() {return 1000;}
public void nullLayouts() {}
public void setLinkTextColor(int color) {}
public void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback) {}
public int getExtendedPaddingTop() {return 0;}
public void setRawInputType(int type) {}
public TextUtils.TruncateAt getEllipsize() {return null;}
public void setLines(int lines) {}
public void setMinLines(int lines) {}
public void setSelectAllOnFocus(boolean selectAllOnFocus) {}
public int getCompoundDrawablePadding() {return 0;}
public int getPaintFlags() {return 0;}
public void setPaintFlags(int flags) {}
public int getLineHeight() {
return 10; // FIXME
}
public boolean isAllCaps() { return false; }
public int getAutoSizeStepGranularity() {
return -1;
}
public void setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit) {}
public void setCompoundDrawableTintList(ColorStateList tint) {}
public void setIncludeFontPadding(boolean includePadding) {}
public float getLineSpacingExtra() {return 0.f;}
public Bundle getInputExtras(boolean key) {
return new Bundle();
}
public void setError(CharSequence error) {
System.out.println("ERROR: " + error);
}
public int getTotalPaddingLeft() {return 0;}
public int getTotalPaddingTop() {return 0;}
public int getImeOptions() {return 0;}
}