Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / base / l10n / l10n_util_android.cc
blob8530fcd08322c040e19b487271ece651457ded5a
1 // Copyright (c) 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 #include "ui/base/l10n/l10n_util_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/i18n/rtl.h"
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
13 #include "base/time/time.h"
14 #include "jni/LocalizationUtils_jni.h"
15 #include "third_party/icu/source/common/unicode/uloc.h"
16 #include "ui/base/l10n/time_format.h"
18 namespace l10n_util {
20 jint GetFirstStrongCharacterDirection(JNIEnv* env, jclass clazz,
21 jstring string) {
22 return base::i18n::GetFirstStrongCharacterDirection(
23 base::android::ConvertJavaStringToUTF16(env, string));
26 bool IsLayoutRtl() {
27 static bool is_layout_rtl_cached = false;
28 static bool layout_rtl_cache;
30 if (!is_layout_rtl_cached) {
31 is_layout_rtl_cached = true;
32 JNIEnv* env = base::android::AttachCurrentThread();
33 layout_rtl_cache =
34 static_cast<bool>(Java_LocalizationUtils_isLayoutRtl(env));
37 return layout_rtl_cache;
40 namespace {
42 // Common prototype of ICU uloc_getXXX() functions.
43 typedef int32_t (*UlocGetComponentFunc)(const char*, char*, int32_t,
44 UErrorCode*);
46 std::string GetLocaleComponent(const std::string& locale,
47 UlocGetComponentFunc uloc_func,
48 int32_t max_capacity) {
49 std::string result;
50 UErrorCode error = U_ZERO_ERROR;
51 int32_t actual_length = uloc_func(locale.c_str(),
52 WriteInto(&result, max_capacity),
53 max_capacity,
54 &error);
55 DCHECK(U_SUCCESS(error));
56 DCHECK(actual_length < max_capacity);
57 result.resize(actual_length);
58 return result;
61 ScopedJavaLocalRef<jobject> NewJavaLocale(
62 JNIEnv* env,
63 const std::string& locale) {
64 // TODO(wangxianzhu): Use new Locale API once Android supports scripts.
65 std::string language = GetLocaleComponent(
66 locale, uloc_getLanguage, ULOC_LANG_CAPACITY);
67 std::string country = GetLocaleComponent(
68 locale, uloc_getCountry, ULOC_COUNTRY_CAPACITY);
69 std::string variant = GetLocaleComponent(
70 locale, uloc_getVariant, ULOC_FULLNAME_CAPACITY);
71 return Java_LocalizationUtils_getJavaLocale(env,
72 base::android::ConvertUTF8ToJavaString(env, language).obj(),
73 base::android::ConvertUTF8ToJavaString(env, country).obj(),
74 base::android::ConvertUTF8ToJavaString(env, variant).obj());
77 } // namespace
79 base::string16 GetDisplayNameForLocale(const std::string& locale,
80 const std::string& display_locale) {
81 JNIEnv* env = base::android::AttachCurrentThread();
82 ScopedJavaLocalRef<jobject> java_locale =
83 NewJavaLocale(env, locale);
84 ScopedJavaLocalRef<jobject> java_display_locale =
85 NewJavaLocale(env, display_locale);
87 ScopedJavaLocalRef<jstring> java_result(
88 Java_LocalizationUtils_getDisplayNameForLocale(
89 env,
90 java_locale.obj(),
91 java_display_locale.obj()));
92 return ConvertJavaStringToUTF16(java_result);
95 jstring GetDurationString(JNIEnv* env, jclass clazz, jlong timeInMillis) {
96 ScopedJavaLocalRef<jstring> jtime_remaining =
97 base::android::ConvertUTF16ToJavaString(
98 env,
99 ui::TimeFormat::Simple(
100 ui::TimeFormat::FORMAT_REMAINING, ui::TimeFormat::LENGTH_SHORT,
101 base::TimeDelta::FromMilliseconds(timeInMillis)));
102 return jtime_remaining.Release();
105 bool RegisterLocalizationUtil(JNIEnv* env) {
106 return RegisterNativesImpl(env);
109 } // namespace l10n_util