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.
5 package org.chromium.ui.base;
7 import android.content.res.Configuration;
8 import android.view.View;
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;
16 import java.util.Locale;
19 * This class provides the locale related methods for the native library.
21 @JNINamespace("l10n_util")
22 public class LocalizationUtils {
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;
29 private static Boolean sIsLayoutRtl;
31 private LocalizationUtils() { /* cannot be instantiated */ }
34 * @return the default locale, translating Android deprecated
35 * language codes into the modern ones used by Chromium.
38 public static String getDefaultLocale() {
39 // TODO(vivekg): Native callers should use LocaleUtils directly instead of the redirection.
40 return LocaleUtils.getDefaultLocale();
44 private static Locale getJavaLocale(String language, String country, String variant) {
45 return new Locale(language, country, variant);
49 private static String getDisplayNameForLocale(Locale locale, Locale displayLocale) {
50 return locale.getDisplayName(displayLocale);
54 * Returns whether the Android layout direction is RTL.
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.
60 * Therefore, only this function should be used to query RTL for layout purposes.
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);
72 return sIsLayoutRtl.booleanValue();
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.
80 public static int getFirstStrongCharacterDirection(String string) {
81 return nativeGetFirstStrongCharacterDirection(string);
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
90 public static String getDurationString(long timeInMillis) {
91 return nativeGetDurationString(timeInMillis);
94 private static native int nativeGetFirstStrongCharacterDirection(String string);
96 private static native String nativeGetDurationString(long timeInMillis);