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. 4 5 #include "content/browser/android/date_time_chooser_android.h" 6 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/android/content_view_core.h" 13 #include "content/public/browser/render_view_host.h" 14 #include "jni/DateTimeChooserAndroid_jni.h" 15 #include "third_party/icu/source/common/unicode/uchar.h" 16 #include "third_party/icu/source/common/unicode/unistr.h" 17 18 using base::android::AttachCurrentThread; 19 using base::android::ConvertJavaStringToUTF16; 20 using base::android::ConvertUTF8ToJavaString; 21 using base::android::ConvertUTF16ToJavaString; 22 23 24 namespace { 25 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(); 35 } 36 return base::string16(sanitized.getBuffer(), 37 static_cast<size_t>(sanitized.length())); 38 } 39 40 } // namespace 41 42 namespace content { 43 44 // DateTimeChooserAndroid implementation 45 DateTimeChooserAndroid::DateTimeChooserAndroid() 46 : host_(NULL) { 47 } 48 49 DateTimeChooserAndroid::~DateTimeChooserAndroid() { 50 } 51 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); 63 } 64 65 void DateTimeChooserAndroid::ReplaceDateTime(JNIEnv* env, 66 jobject, 67 jdouble value) { 68 host_->Send(new ViewMsg_ReplaceDateTime(host_->GetRoutingID(), value)); 69 } 70 71 void DateTimeChooserAndroid::CancelDialog(JNIEnv* env, jobject) { 72 host_->Send(new ViewMsg_CancelDateTimeDialog(host_->GetRoutingID())); 73 } 74 75 void DateTimeChooserAndroid::ShowDialog( 76 ContentViewCore* content, 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; 85 86 JNIEnv* env = AttachCurrentThread(); 87 ScopedJavaLocalRef<jobjectArray> suggestions_array; 88 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()); 102 } 103 } 104 105 j_date_time_chooser_.Reset(Java_DateTimeChooserAndroid_createDateTimeChooser( 106 env, 107 content->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 } 116 117 // ---------------------------------------------------------------------------- 118 // Native JNI methods 119 // ---------------------------------------------------------------------------- 120 bool RegisterDateTimeChooserAndroid(JNIEnv* env) { 121 bool registered = RegisterNativesImpl(env); 122 if (registered) 123 DateTimeChooserAndroid::InitializeDateInputTypes( 124 ui::TEXT_INPUT_TYPE_DATE, 125 ui::TEXT_INPUT_TYPE_DATE_TIME, 126 ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL, 127 ui::TEXT_INPUT_TYPE_MONTH, 128 ui::TEXT_INPUT_TYPE_TIME, 129 ui::TEXT_INPUT_TYPE_WEEK); 130 return registered; 131 } 132 133 } // namespace content 134