ICUCompatIcs.java revision 0dd57e65bcb8c9f0c7468a69ea3549a54a2482df
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.util.Log;
20
21import java.lang.reflect.InvocationTargetException;
22import java.lang.reflect.Method;
23import java.util.Locale;
24
25class ICUCompatIcs {
26
27    private static final String TAG = "ICUCompatIcs";
28
29    private static Method sGetScriptMethod;
30    private static Method sAddLikelySubtagsMethod;
31
32    static {
33        try {
34            final Class<?> clazz = Class.forName("libcore.icu.ICU");
35            if (clazz != null) {
36                sGetScriptMethod = clazz.getMethod("getScript",
37                        new Class[]{ String.class });
38                sAddLikelySubtagsMethod = clazz.getMethod("addLikelySubtags",
39                        new Class[]{ String.class });
40            }
41        } catch (Exception e) {
42            sGetScriptMethod = null;
43            sAddLikelySubtagsMethod = null;
44
45            // Nothing we can do here, we just log the exception
46            Log.w(TAG, e);
47        }
48    }
49
50    public static String maximizeAndGetScript(Locale locale) {
51        final String localeWithSubtags = addLikelySubtags(locale);
52        if (localeWithSubtags != null) {
53            return getScript(localeWithSubtags);
54        }
55
56        return null;
57    }
58
59    private static String getScript(String localeStr) {
60        try {
61            if (sGetScriptMethod != null) {
62                final Object[] args = new Object[] { localeStr };
63                return (String) sGetScriptMethod.invoke(null, args);
64            }
65        } catch (IllegalAccessException e) {
66            // Nothing we can do here, we just log the exception
67            Log.w(TAG, e);
68        }
69        catch (InvocationTargetException e) {
70            // Nothing we can do here, we just log the exception
71            Log.w(TAG, e);
72        }
73        return null;
74    }
75
76    private static String addLikelySubtags(Locale locale) {
77        final String localeStr = locale.toString();
78        try {
79            if (sAddLikelySubtagsMethod != null) {
80                final Object[] args = new Object[] { localeStr };
81                return (String) sAddLikelySubtagsMethod.invoke(null, args);
82            }
83        } catch (IllegalAccessException e) {
84            // Nothing we can do here, we just log the exception
85            Log.w(TAG, e);
86        }
87        catch (InvocationTargetException e) {
88            // Nothing we can do here, we just log the exception
89            Log.w(TAG, e);
90        }
91
92        return localeStr;
93    }
94}
95