Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/android/date_time_chooser_android.h" | 5 #include "content/browser/android/date_time_chooser_android.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | |
| 7 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/i18n/char_iterator.h" | |
| 10 #include "content/common/date_time_suggestion.h" | |
| 8 #include "content/common/view_messages.h" | 11 #include "content/common/view_messages.h" |
| 9 #include "content/public/browser/android/content_view_core.h" | 12 #include "content/public/browser/android/content_view_core.h" |
| 10 #include "content/public/browser/render_view_host.h" | 13 #include "content/public/browser/render_view_host.h" |
| 11 #include "jni/DateTimeChooserAndroid_jni.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" | |
| 12 | 17 |
| 13 using base::android::AttachCurrentThread; | 18 using base::android::AttachCurrentThread; |
| 14 using base::android::ConvertJavaStringToUTF16; | 19 using base::android::ConvertJavaStringToUTF16; |
| 15 using base::android::ConvertUTF8ToJavaString; | 20 using base::android::ConvertUTF8ToJavaString; |
| 21 using base::android::ConvertUTF16ToJavaString; | |
| 16 | 22 |
| 17 | 23 |
| 24 namespace { | |
| 25 | |
| 26 string16 SanitizeSuggestionString(const string16& string) { | |
| 27 string16 trimmed = string.substr(0, 255); | |
| 28 icu::UnicodeString sanitized; | |
| 29 base::i18n::UTF16CharIterator sanitized_iterator(&trimmed); | |
| 30 do { | |
| 31 UChar c = sanitized_iterator.get(); | |
| 32 if (u_isprint(c)) | |
| 33 sanitized.append(c); | |
| 34 } while(sanitized_iterator.Advance()); | |
|
bulach
2013/12/04 10:37:54
nit: space after "while"
keishi
2013/12/04 12:21:24
Done.
| |
| 35 return string16(sanitized.getBuffer(), | |
| 36 static_cast<size_t>(sanitized.length())); | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 18 namespace content { | 41 namespace content { |
| 19 | 42 |
| 20 // DateTimeChooserAndroid implementation | 43 // DateTimeChooserAndroid implementation |
| 21 DateTimeChooserAndroid::DateTimeChooserAndroid() | 44 DateTimeChooserAndroid::DateTimeChooserAndroid() |
| 22 : host_(NULL) { | 45 : host_(NULL) { |
| 23 } | 46 } |
| 24 | 47 |
| 25 DateTimeChooserAndroid::~DateTimeChooserAndroid() { | 48 DateTimeChooserAndroid::~DateTimeChooserAndroid() { |
| 26 } | 49 } |
| 27 | 50 |
| 28 // static | 51 // static |
| 29 void DateTimeChooserAndroid::InitializeDateInputTypes( | 52 void DateTimeChooserAndroid::InitializeDateInputTypes( |
| 30 int text_input_type_date, int text_input_type_date_time, | 53 int text_input_type_date, int text_input_type_date_time, |
| 31 int text_input_type_date_time_local, int text_input_type_month, | 54 int text_input_type_date_time_local, int text_input_type_month, |
| 32 int text_input_type_time, int text_input_type_week) { | 55 int text_input_type_time, int text_input_type_week) { |
| 33 JNIEnv* env = AttachCurrentThread(); | 56 JNIEnv* env = AttachCurrentThread(); |
| 34 Java_DateTimeChooserAndroid_initializeDateInputTypes( | 57 Java_DateTimeChooserAndroid_initializeDateInputTypes( |
| 35 env, | 58 env, |
| 36 text_input_type_date, text_input_type_date_time, | 59 text_input_type_date, text_input_type_date_time, |
| 37 text_input_type_date_time_local, text_input_type_month, | 60 text_input_type_date_time_local, text_input_type_month, |
| 38 text_input_type_time, text_input_type_week); | 61 text_input_type_time, text_input_type_week); |
| 39 } | 62 } |
| 40 | 63 |
| 41 void DateTimeChooserAndroid::ReplaceDateTime(JNIEnv* env, | 64 void DateTimeChooserAndroid::ReplaceDateTime(JNIEnv* env, |
| 42 jobject, | 65 jobject, |
| 43 int dialog_type, | 66 jdouble value) { |
| 44 int year, | |
| 45 int month, | |
| 46 int day, | |
| 47 int hour, | |
| 48 int minute, | |
| 49 int second, | |
| 50 int milli, | |
| 51 int week) { | |
| 52 ViewHostMsg_DateTimeDialogValue_Params value; | |
| 53 value.year = year; | |
| 54 value.month = month; | |
| 55 value.day = day; | |
| 56 value.hour = hour; | |
| 57 value.minute = minute; | |
| 58 value.second = second; | |
| 59 value.milli = milli; | |
| 60 value.week = week; | |
| 61 value.dialog_type = dialog_type; | |
| 62 host_->Send(new ViewMsg_ReplaceDateTime(host_->GetRoutingID(), value)); | 67 host_->Send(new ViewMsg_ReplaceDateTime(host_->GetRoutingID(), value)); |
| 63 } | 68 } |
| 64 | 69 |
| 65 void DateTimeChooserAndroid::CancelDialog(JNIEnv* env, jobject) { | 70 void DateTimeChooserAndroid::CancelDialog(JNIEnv* env, jobject) { |
| 66 host_->Send(new ViewMsg_CancelDateTimeDialog(host_->GetRoutingID())); | 71 host_->Send(new ViewMsg_CancelDateTimeDialog(host_->GetRoutingID())); |
| 67 } | 72 } |
| 68 | 73 |
| 69 void DateTimeChooserAndroid::ShowDialog(ContentViewCore* content, | 74 void DateTimeChooserAndroid::ShowDialog( |
| 70 RenderViewHost* host, | 75 ContentViewCore* content, |
| 71 int type, | 76 RenderViewHost* host, |
| 72 int year, | 77 ui::TextInputType dialog_type, |
| 73 int month, | 78 double dialog_value, |
| 74 int day, | 79 double min, |
| 75 int hour, | 80 double max, |
| 76 int minute, | 81 double step, |
| 77 int second, | 82 const std::vector<DateTimeSuggestion>& suggestions) { |
| 78 int milli, | |
| 79 int week, | |
| 80 double min, | |
| 81 double max, | |
| 82 double step) { | |
| 83 host_ = host; | 83 host_ = host; |
| 84 | 84 |
| 85 JNIEnv* env = AttachCurrentThread(); | 85 JNIEnv* env = AttachCurrentThread(); |
| 86 ScopedJavaLocalRef<jobjectArray> suggestions_array; | |
| 87 | |
| 88 if (suggestions.size() > 0) { | |
| 89 ScopedJavaLocalRef<jobjectArray> j_suggestions = | |
| 90 Java_DateTimeChooserAndroid_createSuggestionsArray(env, | |
|
bulach
2013/12/04 10:37:54
nit: indent + 2
keishi
2013/12/04 12:21:24
Done.
| |
| 91 suggestions.size()); | |
| 92 for (size_t i = 0; i < suggestions.size(); ++i) { | |
| 93 const content::DateTimeSuggestion& suggestion = suggestions[i]; | |
| 94 ScopedJavaLocalRef<jstring> localized_value = ConvertUTF16ToJavaString( | |
| 95 env, SanitizeSuggestionString(suggestion.localized_value)); | |
| 96 ScopedJavaLocalRef<jstring> label = ConvertUTF16ToJavaString( | |
| 97 env, SanitizeSuggestionString(suggestion.label)); | |
| 98 Java_DateTimeChooserAndroid_setDateTimeSuggestionAt(env, | |
| 99 j_suggestions.obj(), i, | |
| 100 suggestion.value, localized_value.obj(), label.obj()); | |
| 101 } | |
| 102 } | |
| 103 | |
| 86 j_date_time_chooser_.Reset(Java_DateTimeChooserAndroid_createDateTimeChooser( | 104 j_date_time_chooser_.Reset(Java_DateTimeChooserAndroid_createDateTimeChooser( |
| 87 env, | 105 env, |
| 88 content->GetJavaObject().obj(), | 106 content->GetJavaObject().obj(), |
| 89 reinterpret_cast<intptr_t>(this), | 107 reinterpret_cast<intptr_t>(this), |
| 90 type, | 108 dialog_type, |
| 91 year, | 109 dialog_value, |
| 92 month, | |
| 93 day, | |
| 94 hour, | |
| 95 minute, | |
| 96 second, | |
| 97 milli, | |
| 98 week, | |
| 99 min, | 110 min, |
| 100 max, | 111 max, |
| 101 step)); | 112 step, |
| 113 suggestions_array.obj())); | |
| 102 } | 114 } |
| 103 | 115 |
| 104 // ---------------------------------------------------------------------------- | 116 // ---------------------------------------------------------------------------- |
| 105 // Native JNI methods | 117 // Native JNI methods |
| 106 // ---------------------------------------------------------------------------- | 118 // ---------------------------------------------------------------------------- |
| 107 bool RegisterDateTimeChooserAndroid(JNIEnv* env) { | 119 bool RegisterDateTimeChooserAndroid(JNIEnv* env) { |
| 108 bool registered = RegisterNativesImpl(env); | 120 bool registered = RegisterNativesImpl(env); |
| 109 if (registered) | 121 if (registered) |
| 110 DateTimeChooserAndroid::InitializeDateInputTypes( | 122 DateTimeChooserAndroid::InitializeDateInputTypes( |
| 111 ui::TEXT_INPUT_TYPE_DATE, | 123 ui::TEXT_INPUT_TYPE_DATE, |
| 112 ui::TEXT_INPUT_TYPE_DATE_TIME, | 124 ui::TEXT_INPUT_TYPE_DATE_TIME, |
| 113 ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL, | 125 ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL, |
| 114 ui::TEXT_INPUT_TYPE_MONTH, | 126 ui::TEXT_INPUT_TYPE_MONTH, |
| 115 ui::TEXT_INPUT_TYPE_TIME, | 127 ui::TEXT_INPUT_TYPE_TIME, |
| 116 ui::TEXT_INPUT_TYPE_WEEK); | 128 ui::TEXT_INPUT_TYPE_WEEK); |
| 117 return registered; | 129 return registered; |
| 118 } | 130 } |
| 119 | 131 |
| 120 } // namespace content | 132 } // namespace content |
| OLD | NEW |