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