Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / android / date_time_chooser_android.cc
blob31ff4656bef04fcc404d0b322824d1b835475b49
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;
24 namespace {
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();
32 if (u_isprint(c))
33 sanitized.append(c);
34 sanitized_iterator.Advance();
36 return base::string16(sanitized.getBuffer(),
37 static_cast<size_t>(sanitized.length()));
40 } // namespace
42 namespace content {
44 // DateTimeChooserAndroid implementation
45 DateTimeChooserAndroid::DateTimeChooserAndroid()
46 : host_(NULL) {
49 DateTimeChooserAndroid::~DateTimeChooserAndroid() {
52 // static
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(
59 env,
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,
66 jobject,
67 jdouble value) {
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,
77 RenderViewHost* host,
78 ui::TextInputType dialog_type,
79 double dialog_value,
80 double min,
81 double max,
82 double step,
83 const std::vector<DateTimeSuggestion>& suggestions) {
84 host_ = host;
86 JNIEnv* env = AttachCurrentThread();
87 ScopedJavaLocalRef<jobjectArray> suggestions_array;
89 if (suggestions.size() > 0) {
90 suggestions_array =
91 Java_DateTimeChooserAndroid_createSuggestionsArray(env,
92 suggestions.size());
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(
106 env,
107 native_window->GetJavaObject().obj(),
108 reinterpret_cast<intptr_t>(this),
109 dialog_type,
110 dialog_value,
111 min,
112 max,
113 step,
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);
124 if (registered)
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);
132 return registered;
135 } // namespace content