Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ui / android / java / src / org / chromium / ui / base / LocalizationUtils.java
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.ui.base;
6
7 import android.content.res.Configuration;
8 import android.view.View;
9
10 import org.chromium.base.ApiCompatibilityUtils;
11 import org.chromium.base.ApplicationStatus;
12 import org.chromium.base.CalledByNative;
13 import org.chromium.base.JNINamespace;
14 import org.chromium.base.LocaleUtils;
15
16 import java.util.Locale;
17
18 /**
19  * This class provides the locale related methods for the native library.
20  */
21 @JNINamespace("l10n_util")
22 public class LocalizationUtils {
23
24     // This is mirrored from base/i18n/rtl.h. Please keep in sync.
25     public static final int UNKNOWN_DIRECTION = 0;
26     public static final int RIGHT_TO_LEFT = 1;
27     public static final int LEFT_TO_RIGHT = 2;
28
29     private static Boolean sIsLayoutRtl;
30
31     private LocalizationUtils() { /* cannot be instantiated */ }
32
33     /**
34      * @return the default locale, translating Android deprecated
35      * language codes into the modern ones used by Chromium.
36      */
37     @CalledByNative
38     public static String getDefaultLocale() {
39         // TODO(vivekg): Native callers should use LocaleUtils directly instead of the redirection.
40         return LocaleUtils.getDefaultLocale();
41     }
42
43     @CalledByNative
44     private static Locale getJavaLocale(String language, String country, String variant) {
45         return new Locale(language, country, variant);
46     }
47
48     @CalledByNative
49     private static String getDisplayNameForLocale(Locale locale, Locale displayLocale) {
50         return locale.getDisplayName(displayLocale);
51     }
52
53     /**
54      * Returns whether the Android layout direction is RTL.
55      *
56      * Note that the locale direction can be different from layout direction. Two known cases:
57      * - RTL languages on Android 4.1, due to the lack of RTL layout support on 4.1.
58      * - When user turned on force RTL layout option under developer options.
59      *
60      * Therefore, only this function should be used to query RTL for layout purposes.
61      */
62     @CalledByNative
63     public static boolean isLayoutRtl() {
64         if (sIsLayoutRtl == null) {
65             Configuration configuration =
66                     ApplicationStatus.getApplicationContext().getResources().getConfiguration();
67             sIsLayoutRtl = Boolean.valueOf(
68                     ApiCompatibilityUtils.getLayoutDirection(configuration) ==
69                     View.LAYOUT_DIRECTION_RTL);
70         }
71
72         return sIsLayoutRtl.booleanValue();
73     }
74
75     /**
76      * Jni binding to base::i18n::GetFirstStrongCharacterDirection
77      * @param string String to decide the direction.
78      * @return One of the UNKNOWN_DIRECTION, RIGHT_TO_LEFT, and LEFT_TO_RIGHT.
79      */
80     public static int getFirstStrongCharacterDirection(String string) {
81         return nativeGetFirstStrongCharacterDirection(string);
82     }
83
84     /**
85      * Jni binding to ui::TimeFormat::TimeRemaining. Converts milliseconds to
86      * time remaining format : "3 mins left", "2 days left".
87      * @param timeInMillis time in milliseconds
88      * @return time remaining
89      */
90     public static String getDurationString(long timeInMillis) {
91         return nativeGetDurationString(timeInMillis);
92     }
93
94     private static native int nativeGetFirstStrongCharacterDirection(String string);
95
96     private static native String nativeGetDurationString(long timeInMillis);
97 }