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 androidx.core.text;
18
19import android.os.Build;
20import android.util.Log;
21
22import androidx.annotation.Nullable;
23
24import java.lang.reflect.InvocationTargetException;
25import java.lang.reflect.Method;
26import java.util.Locale;
27
28public final class ICUCompat {
29    private static final String TAG = "ICUCompat";
30
31    private static Method sGetScriptMethod;
32    private static Method sAddLikelySubtagsMethod;
33
34    static {
35        if (Build.VERSION.SDK_INT >= 21) {
36            try {
37                // This class should always exist on API-21 since it's CTS tested.
38                final Class<?> clazz = Class.forName("libcore.icu.ICU");
39                sAddLikelySubtagsMethod = clazz.getMethod("addLikelySubtags",
40                        new Class[]{ Locale.class });
41            } catch (Exception e) {
42                throw new IllegalStateException(e);
43            }
44        } else {
45            try {
46                final Class<?> clazz = Class.forName("libcore.icu.ICU");
47                if (clazz != null) {
48                    sGetScriptMethod = clazz.getMethod("getScript",
49                            new Class[]{ String.class });
50                    sAddLikelySubtagsMethod = clazz.getMethod("addLikelySubtags",
51                            new Class[]{ String.class });
52                }
53            } catch (Exception e) {
54                sGetScriptMethod = null;
55                sAddLikelySubtagsMethod = null;
56
57                // Nothing we can do here, we just log the exception
58                Log.w(TAG, e);
59            }
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 The script for a given Locale if ICU library is available, otherwise null.
85     */
86    @Nullable
87    public static String maximizeAndGetScript(Locale locale) {
88        if (Build.VERSION.SDK_INT >= 21) {
89            try {
90                final Object[] args = new Object[] { locale };
91                return ((Locale) sAddLikelySubtagsMethod.invoke(null, args)).getScript();
92            } catch (InvocationTargetException e) {
93                Log.w(TAG, e);
94            } catch (IllegalAccessException e) {
95                Log.w(TAG, e);
96            }
97            return locale.getScript();
98        } else {
99            final String localeWithSubtags = addLikelySubtags(locale);
100            if (localeWithSubtags != null) {
101                return getScript(localeWithSubtags);
102            }
103
104            return null;
105        }
106    }
107
108    private static String getScript(String localeStr) {
109        try {
110            if (sGetScriptMethod != null) {
111                final Object[] args = new Object[] { localeStr };
112                return (String) sGetScriptMethod.invoke(null, args);
113            }
114        } catch (IllegalAccessException e) {
115            // Nothing we can do here, we just log the exception
116            Log.w(TAG, e);
117        } catch (InvocationTargetException e) {
118            // Nothing we can do here, we just log the exception
119            Log.w(TAG, e);
120        }
121        return null;
122    }
123
124    private static String addLikelySubtags(Locale locale) {
125        final String localeStr = locale.toString();
126        try {
127            if (sAddLikelySubtagsMethod != null) {
128                final Object[] args = new Object[] { localeStr };
129                return (String) sAddLikelySubtagsMethod.invoke(null, args);
130            }
131        } catch (IllegalAccessException e) {
132            // Nothing we can do here, we just log the exception
133            Log.w(TAG, e);
134        } catch (InvocationTargetException e) {
135            // Nothing we can do here, we just log the exception
136            Log.w(TAG, e);
137        }
138
139        return localeStr;
140    }
141
142    private ICUCompat() {}
143}
144