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 5package org.chromium.ui.base; 6 7import android.os.Build; 8import android.text.TextUtils; 9import android.view.View; 10 11import org.chromium.base.CalledByNative; 12import org.chromium.base.JNINamespace; 13 14import java.util.Locale; 15 16/** 17 * This class provides the locale related methods for the native library. 18 */ 19@JNINamespace("l10n_util") 20public class LocalizationUtils { 21 22 // This is mirrored from base/i18n/rtl.h. Please keep in sync. 23 public static final int UNKNOWN_DIRECTION = 0; 24 public static final int RIGHT_TO_LEFT = 1; 25 public static final int LEFT_TO_RIGHT = 2; 26 27 private LocalizationUtils() { /* cannot be instantiated */ } 28 29 /** 30 * @return the default locale, translating Android deprecated 31 * language codes into the modern ones used by Chromium. 32 */ 33 @CalledByNative 34 public static String getDefaultLocale() { 35 Locale locale = Locale.getDefault(); 36 String language = locale.getLanguage(); 37 String country = locale.getCountry(); 38 39 // Android uses deprecated lanuages codes for Hebrew and Indonesian but Chromium uses the 40 // updated codes. Also, Android uses "tl" while Chromium uses "fil" for Tagalog/Filipino. 41 // So apply a mapping. 42 // See http://developer.android.com/reference/java/util/Locale.html 43 if ("iw".equals(language)) { 44 language = "he"; 45 } else if ("in".equals(language)) { 46 language = "id"; 47 } else if ("tl".equals(language)) { 48 language = "fil"; 49 } 50 return country.isEmpty() ? language : language + "-" + country; 51 } 52 53 @CalledByNative 54 private static Locale getJavaLocale(String language, String country, String variant) { 55 return new Locale(language, country, variant); 56 } 57 58 @CalledByNative 59 private static String getDisplayNameForLocale(Locale locale, Locale displayLocale) { 60 return locale.getDisplayName(displayLocale); 61 } 62 63 /** 64 * @return true if the system default layout direction is RTL, false otherwise. 65 * RTL layout support is from Jelly Bean MR1, so if the version is lower 66 * than that, it is always false. 67 */ 68 public static boolean isSystemLayoutDirectionRtl() { 69 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 70 return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) 71 == View.LAYOUT_DIRECTION_RTL; 72 } 73 return false; 74 } 75 76 /** 77 * Jni binding to base::i18n::IsRTL. 78 * @return true if the current locale is right to left. 79 */ 80 public static boolean isRtl() { 81 return nativeIsRTL(); 82 } 83 84 /** 85 * Jni binding to base::i18n::GetFirstStrongCharacterDirection 86 * @param string String to decide the direction. 87 * @return One of the UNKNOWN_DIRECTION, RIGHT_TO_LEFT, and LEFT_TO_RIGHT. 88 */ 89 public static int getFirstStrongCharacterDirection(String string) { 90 return nativeGetFirstStrongCharacterDirection(string); 91 } 92 93 /** 94 * Jni binding to ui::TimeFormat::TimeRemaining. Converts milliseconds to 95 * time remaining format : "3 mins left", "2 days left". 96 * @param timeInMillis time in milliseconds 97 * @return time remaining 98 */ 99 public static String getDurationString(long timeInMillis) { 100 return nativeGetDurationString(timeInMillis); 101 } 102 103 private static native boolean nativeIsRTL(); 104 105 private static native int nativeGetFirstStrongCharacterDirection(String string); 106 107 private static native String nativeGetDurationString(long timeInMillis); 108} 109