diff --git a/api/current.txt b/api/current.txt index d6d1732fd9707..0c71009cb109d 100644 --- a/api/current.txt +++ b/api/current.txt @@ -22503,6 +22503,17 @@ package android.text.style { method public abstract void chooseHeight(java.lang.CharSequence, int, int, int, int, android.graphics.Paint.FontMetricsInt, android.text.TextPaint); } + public class LocaleSpan extends android.text.style.MetricAffectingSpan implements android.text.ParcelableSpan { + ctor public LocaleSpan(java.util.Locale); + ctor public LocaleSpan(android.os.Parcel); + method public int describeContents(); + method public java.util.Locale getLocale(); + method public int getSpanTypeId(); + method public void updateDrawState(android.text.TextPaint); + method public void updateMeasureState(android.text.TextPaint); + method public void writeToParcel(android.os.Parcel, int); + } + public class MaskFilterSpan extends android.text.style.CharacterStyle implements android.text.style.UpdateAppearance { ctor public MaskFilterSpan(android.graphics.MaskFilter); method public android.graphics.MaskFilter getMaskFilter(); @@ -29303,6 +29314,7 @@ package android.widget { method public static int getTextColor(android.content.Context, android.content.res.TypedArray, int); method public final android.content.res.ColorStateList getTextColors(); method public static android.content.res.ColorStateList getTextColors(android.content.Context, android.content.res.TypedArray); + method public java.util.Locale getTextLocale(); method public float getTextScaleX(); method public float getTextSize(); method public int getTotalPaddingBottom(); @@ -29405,6 +29417,7 @@ package android.widget { method public void setTextIsSelectable(boolean); method public final void setTextKeepState(java.lang.CharSequence); method public final void setTextKeepState(java.lang.CharSequence, android.widget.TextView.BufferType); + method public void setTextLocale(java.util.Locale); method public void setTextScaleX(float); method public void setTextSize(float); method public void setTextSize(int, float); diff --git a/core/java/android/text/TextUtils.java b/core/java/android/text/TextUtils.java index 83a70f3fce0d0..987062ab08a24 100644 --- a/core/java/android/text/TextUtils.java +++ b/core/java/android/text/TextUtils.java @@ -27,6 +27,7 @@ import android.text.style.CharacterStyle; import android.text.style.EasyEditSpan; import android.text.style.ForegroundColorSpan; import android.text.style.LeadingMarginSpan; +import android.text.style.LocaleSpan; import android.text.style.MetricAffectingSpan; import android.text.style.QuoteSpan; import android.text.style.RelativeSizeSpan; @@ -587,6 +588,8 @@ public class TextUtils { public static final int SUGGESTION_RANGE_SPAN = 21; /** @hide */ public static final int EASY_EDIT_SPAN = 22; + /** @hide */ + public static final int LOCALE_SPAN = 23; /** * Flatten a CharSequence and whatever styles can be copied across processes @@ -754,6 +757,10 @@ public class TextUtils { readSpan(p, sp, new EasyEditSpan()); break; + case LOCALE_SPAN: + readSpan(p, sp, new LocaleSpan(p)); + break; + default: throw new RuntimeException("bogus span encoding " + kind); } diff --git a/core/java/android/text/style/LocaleSpan.java b/core/java/android/text/style/LocaleSpan.java new file mode 100644 index 0000000000000..a12c42fadb166 --- /dev/null +++ b/core/java/android/text/style/LocaleSpan.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.os.Parcel; +import android.text.ParcelableSpan; +import android.text.TextPaint; +import android.text.TextUtils; +import java.util.Locale; + +/** + * Changes the {@link Locale} of the text to which the span is attached. + */ +public class LocaleSpan extends MetricAffectingSpan implements ParcelableSpan { + private final Locale mLocale; + + /** + * Creates a LocaleSpan. + * @param locale The {@link Locale} of the text to which the span is + * attached. + */ + public LocaleSpan(Locale locale) { + mLocale = locale; + } + + public LocaleSpan(Parcel src) { + mLocale = new Locale(src.readString(), src.readString(), src.readString()); + } + + @Override + public int getSpanTypeId() { + return TextUtils.LOCALE_SPAN; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(mLocale.getLanguage()); + dest.writeString(mLocale.getCountry()); + dest.writeString(mLocale.getVariant()); + } + + /** + * Returns the {@link Locale}. + * + * @return The {@link Locale} for this span. + */ + public Locale getLocale() { + return mLocale; + } + + @Override + public void updateDrawState(TextPaint ds) { + apply(ds, mLocale); + } + + @Override + public void updateMeasureState(TextPaint paint) { + apply(paint, mLocale); + } + + private static void apply(Paint paint, Locale locale) { + paint.setTextLocale(locale); + } +} diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 5be9899f29bc8..f502de450c74d 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -2202,6 +2202,27 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener appearance.recycle(); } + /** + * Get the default {@link Locale} of the text in this TextView. + * @return the default {@link Locale} of the text in this TextView. + */ + public Locale getTextLocale() { + return mTextPaint.getTextLocale(); + } + + /** + * Set the default {@link Locale} of the text in this TextView to the given value. This value + * is used to choose appropriate typefaces for ambiguous characters. Typically used for CJK + * locales to disambiguate Hanzi/Kanji/Hanja characters. + * + * @param locale the {@link Locale} for drawing text, must not be null. + * + * @see Paint#setTextLocale + */ + public void setTextLocale(Locale locale) { + mTextPaint.setTextLocale(locale); + } + /** * @return the size (in pixels) of the default text size in this TextView. */