1/*
2 * Copyright (C) 2013 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.support.v4.text;
18
19import android.os.Build;
20
21import java.util.Locale;
22
23public final class ICUCompat {
24
25    interface ICUCompatImpl {
26        public String maximizeAndGetScript(Locale locale);
27    }
28
29    static class ICUCompatImplBase implements ICUCompatImpl {
30        @Override
31        public String maximizeAndGetScript(Locale locale) {
32            return null;
33        }
34    }
35
36    static class ICUCompatImplIcs implements ICUCompatImpl {
37        @Override
38        public String maximizeAndGetScript(Locale locale) {
39            return ICUCompatIcs.maximizeAndGetScript(locale);
40        }
41    }
42
43    static class ICUCompatImplLollipop implements ICUCompatImpl {
44        @Override
45        public String maximizeAndGetScript(Locale locale) {
46            return ICUCompatApi23.maximizeAndGetScript(locale);
47        }
48    }
49
50    private static final ICUCompatImpl IMPL;
51
52    static {
53        final int version = Build.VERSION.SDK_INT;
54        if (version >= 21) {
55            IMPL = new ICUCompatImplLollipop();
56        } else if (version >= 14) {
57            IMPL = new ICUCompatImplIcs();
58        } else {
59            IMPL = new ICUCompatImplBase();
60        }
61    }
62
63    /**
64     * Returns the script for a given Locale.
65     *
66     * If the locale isn't already in its maximal form, likely subtags for the provided locale
67     * ID are added before we determine the script. For further details, see the following CLDR
68     * technical report :
69     *
70     * http://www.unicode.org/reports/tr35/#Likely_Subtags
71     *
72     * If locale is already in the maximal form, or there is no data available for maximization,
73     * it will be just returned. For example, "und-Zzzz" cannot be maximized, since there is no
74     * reasonable maximization.
75     *
76     * Examples:
77     *
78     * "en" maximizes to "en_Latn_US"
79     * "de" maximizes to "de_Latn_US"
80     * "sr" maximizes to "sr_Cyrl_RS"
81     * "sh" maximizes to "sr_Latn_RS" (Note this will not reverse.)
82     * "zh_Hani" maximizes to "zh_Hans_CN" (Note this will not reverse.)
83     *
84     * @return
85     */
86    public static String maximizeAndGetScript(Locale locale) {
87        return IMPL.maximizeAndGetScript(locale);
88    }
89
90    private ICUCompat() {}
91}
92