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