1 // Copyright (c) 2013 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 "content/browser/android/date_time_chooser_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/i18n/char_iterator.h"
10 #include "content/common/date_time_suggestion.h"
11 #include "content/common/view_messages.h"
12 #include "content/public/browser/render_view_host.h"
13 #include "jni/DateTimeChooserAndroid_jni.h"
14 #include "third_party/icu/source/common/unicode/uchar.h"
15 #include "third_party/icu/source/common/unicode/unistr.h"
16 #include "ui/base/android/window_android.h"
18 using base::android::AttachCurrentThread
;
19 using base::android::ConvertJavaStringToUTF16
;
20 using base::android::ConvertUTF8ToJavaString
;
21 using base::android::ConvertUTF16ToJavaString
;
26 base::string16
SanitizeSuggestionString(const base::string16
& string
) {
27 base::string16 trimmed
= string
.substr(0, 255);
28 icu::UnicodeString sanitized
;
29 base::i18n::UTF16CharIterator
sanitized_iterator(&trimmed
);
30 while (!sanitized_iterator
.end()) {
31 UChar c
= sanitized_iterator
.get();
34 sanitized_iterator
.Advance();
36 return base::string16(sanitized
.getBuffer(),
37 static_cast<size_t>(sanitized
.length()));
44 // DateTimeChooserAndroid implementation
45 DateTimeChooserAndroid::DateTimeChooserAndroid()
49 DateTimeChooserAndroid::~DateTimeChooserAndroid() {
53 void DateTimeChooserAndroid::InitializeDateInputTypes(
54 int text_input_type_date
, int text_input_type_date_time
,
55 int text_input_type_date_time_local
, int text_input_type_month
,
56 int text_input_type_time
, int text_input_type_week
) {
57 JNIEnv
* env
= AttachCurrentThread();
58 Java_DateTimeChooserAndroid_initializeDateInputTypes(
60 text_input_type_date
, text_input_type_date_time
,
61 text_input_type_date_time_local
, text_input_type_month
,
62 text_input_type_time
, text_input_type_week
);
65 void DateTimeChooserAndroid::ReplaceDateTime(JNIEnv
* env
,
68 host_
->Send(new ViewMsg_ReplaceDateTime(host_
->GetRoutingID(), value
));
71 void DateTimeChooserAndroid::CancelDialog(JNIEnv
* env
, jobject
) {
72 host_
->Send(new ViewMsg_CancelDateTimeDialog(host_
->GetRoutingID()));
75 void DateTimeChooserAndroid::ShowDialog(
76 gfx::NativeWindow native_window
,
78 ui::TextInputType dialog_type
,
83 const std::vector
<DateTimeSuggestion
>& suggestions
) {
86 JNIEnv
* env
= AttachCurrentThread();
87 ScopedJavaLocalRef
<jobjectArray
> suggestions_array
;
89 if (suggestions
.size() > 0) {
91 Java_DateTimeChooserAndroid_createSuggestionsArray(env
,
93 for (size_t i
= 0; i
< suggestions
.size(); ++i
) {
94 const content::DateTimeSuggestion
& suggestion
= suggestions
[i
];
95 ScopedJavaLocalRef
<jstring
> localized_value
= ConvertUTF16ToJavaString(
96 env
, SanitizeSuggestionString(suggestion
.localized_value
));
97 ScopedJavaLocalRef
<jstring
> label
= ConvertUTF16ToJavaString(
98 env
, SanitizeSuggestionString(suggestion
.label
));
99 Java_DateTimeChooserAndroid_setDateTimeSuggestionAt(env
,
100 suggestions_array
.obj(), i
,
101 suggestion
.value
, localized_value
.obj(), label
.obj());
105 j_date_time_chooser_
.Reset(Java_DateTimeChooserAndroid_createDateTimeChooser(
107 native_window
->GetJavaObject().obj(),
108 reinterpret_cast<intptr_t>(this),
114 suggestions_array
.obj()));
115 if (j_date_time_chooser_
.is_null())
116 ReplaceDateTime(env
, j_date_time_chooser_
.obj(), dialog_value
);
119 // ----------------------------------------------------------------------------
120 // Native JNI methods
121 // ----------------------------------------------------------------------------
122 bool RegisterDateTimeChooserAndroid(JNIEnv
* env
) {
123 bool registered
= RegisterNativesImpl(env
);
125 DateTimeChooserAndroid::InitializeDateInputTypes(
126 ui::TEXT_INPUT_TYPE_DATE
,
127 ui::TEXT_INPUT_TYPE_DATE_TIME
,
128 ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL
,
129 ui::TEXT_INPUT_TYPE_MONTH
,
130 ui::TEXT_INPUT_TYPE_TIME
,
131 ui::TEXT_INPUT_TYPE_WEEK
);
135 } // namespace content