Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / android / date_time_chooser_android.cc
blobb555ef6d6b2e804035e2c510eed29f3c14ffda63
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/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 void DateTimeChooserAndroid::ReplaceDateTime(JNIEnv* env,
53 jobject,
54 jdouble value) {
55 host_->Send(new ViewMsg_ReplaceDateTime(host_->GetRoutingID(), value));
58 void DateTimeChooserAndroid::CancelDialog(JNIEnv* env, jobject) {
59 host_->Send(new ViewMsg_CancelDateTimeDialog(host_->GetRoutingID()));
62 void DateTimeChooserAndroid::ShowDialog(
63 gfx::NativeWindow native_window,
64 RenderViewHost* host,
65 ui::TextInputType dialog_type,
66 double dialog_value,
67 double min,
68 double max,
69 double step,
70 const std::vector<DateTimeSuggestion>& suggestions) {
71 host_ = host;
73 JNIEnv* env = AttachCurrentThread();
74 ScopedJavaLocalRef<jobjectArray> suggestions_array;
76 if (suggestions.size() > 0) {
77 suggestions_array =
78 Java_DateTimeChooserAndroid_createSuggestionsArray(env,
79 suggestions.size());
80 for (size_t i = 0; i < suggestions.size(); ++i) {
81 const content::DateTimeSuggestion& suggestion = suggestions[i];
82 ScopedJavaLocalRef<jstring> localized_value = ConvertUTF16ToJavaString(
83 env, SanitizeSuggestionString(suggestion.localized_value));
84 ScopedJavaLocalRef<jstring> label = ConvertUTF16ToJavaString(
85 env, SanitizeSuggestionString(suggestion.label));
86 Java_DateTimeChooserAndroid_setDateTimeSuggestionAt(env,
87 suggestions_array.obj(), i,
88 suggestion.value, localized_value.obj(), label.obj());
92 j_date_time_chooser_.Reset(Java_DateTimeChooserAndroid_createDateTimeChooser(
93 env,
94 native_window->GetJavaObject().obj(),
95 reinterpret_cast<intptr_t>(this),
96 dialog_type,
97 dialog_value,
98 min,
99 max,
100 step,
101 suggestions_array.obj()));
102 if (j_date_time_chooser_.is_null())
103 ReplaceDateTime(env, j_date_time_chooser_.obj(), dialog_value);
106 // ----------------------------------------------------------------------------
107 // Native JNI methods
108 // ----------------------------------------------------------------------------
109 bool RegisterDateTimeChooserAndroid(JNIEnv* env) {
110 return RegisterNativesImpl(env);
113 } // namespace content