1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text.style;
18
19import android.graphics.Paint;
20import android.os.Parcel;
21import android.text.ParcelableSpan;
22import android.text.TextPaint;
23import android.text.TextUtils;
24import java.util.Locale;
25
26/**
27 * Changes the {@link Locale} of the text to which the span is attached.
28 */
29public class LocaleSpan extends MetricAffectingSpan implements ParcelableSpan {
30    private final Locale mLocale;
31
32    /**
33     * Creates a LocaleSpan.
34     * @param locale The {@link Locale} of the text to which the span is
35     * attached.
36     */
37    public LocaleSpan(Locale locale) {
38        mLocale = locale;
39    }
40
41    public LocaleSpan(Parcel src) {
42        mLocale = new Locale(src.readString(), src.readString(), src.readString());
43    }
44
45    @Override
46    public int getSpanTypeId() {
47        return TextUtils.LOCALE_SPAN;
48    }
49
50    @Override
51    public int describeContents() {
52        return 0;
53    }
54
55    @Override
56    public void writeToParcel(Parcel dest, int flags) {
57        dest.writeString(mLocale.getLanguage());
58        dest.writeString(mLocale.getCountry());
59        dest.writeString(mLocale.getVariant());
60    }
61
62    /**
63     * Returns the {@link Locale}.
64     *
65     * @return The {@link Locale} for this span.
66     */
67    public Locale getLocale() {
68        return mLocale;
69    }
70
71    @Override
72    public void updateDrawState(TextPaint ds) {
73        apply(ds, mLocale);
74    }
75
76    @Override
77    public void updateMeasureState(TextPaint paint) {
78        apply(paint, mLocale);
79    }
80
81    private static void apply(Paint paint, Locale locale) {
82        paint.setTextLocale(locale);
83    }
84}
85