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 "content/common/date_time_suggestion.h" | |
| 8 #include "content/common/view_messages.h" | 10 #include "content/common/view_messages.h" |
| 9 #include "content/public/browser/android/content_view_core.h" | 11 #include "content/public/browser/android/content_view_core.h" |
| 10 #include "content/public/browser/render_view_host.h" | 12 #include "content/public/browser/render_view_host.h" |
| 11 #include "jni/DateTimeChooserAndroid_jni.h" | 13 #include "jni/DateTimeChooserAndroid_jni.h" |
| 14 #include "third_party/icu/source/common/unicode/uchar.h" | |
| 12 | 15 |
| 13 using base::android::AttachCurrentThread; | 16 using base::android::AttachCurrentThread; |
| 14 using base::android::ConvertJavaStringToUTF16; | 17 using base::android::ConvertJavaStringToUTF16; |
| 15 using base::android::ConvertUTF8ToJavaString; | 18 using base::android::ConvertUTF8ToJavaString; |
| 19 using base::android::ConvertUTF16ToJavaString; | |
| 16 | 20 |
| 17 | 21 |
| 22 namespace { | |
| 23 | |
| 24 bool IsNonPrintableChar(char16 c) { | |
| 25 return !u_isprint(static_cast<UChar>(c)); | |
| 26 } | |
| 27 | |
| 28 string16 SanitizeSuggestionString(string16 string) { | |
|
bulach
2013/12/03 14:22:29
nit: parameter as const&?
keishi
2013/12/03 16:52:34
Done.
| |
| 29 string.erase( | |
| 30 std::remove_if(string.begin(), string.end(), &IsNonPrintableChar), | |
|
bulach
2013/12/03 14:22:29
shouldn't this be using base::i18n::UTF16CharItera
keishi
2013/12/03 16:52:34
Done.
| |
| 31 string.end()); | |
| 32 return string.substr(0, 255); | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 18 namespace content { | 37 namespace content { |
| 19 | 38 |
| 20 // DateTimeChooserAndroid implementation | 39 // DateTimeChooserAndroid implementation |
| 21 DateTimeChooserAndroid::DateTimeChooserAndroid() | 40 DateTimeChooserAndroid::DateTimeChooserAndroid() |
| 22 : host_(NULL) { | 41 : host_(NULL) { |
| 23 } | 42 } |
| 24 | 43 |
| 25 DateTimeChooserAndroid::~DateTimeChooserAndroid() { | 44 DateTimeChooserAndroid::~DateTimeChooserAndroid() { |
| 26 } | 45 } |
| 27 | 46 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 48 host_->Send(new ViewMsg_CancelDateTimeDialog(host_->GetRoutingID())); | 67 host_->Send(new ViewMsg_CancelDateTimeDialog(host_->GetRoutingID())); |
| 49 } | 68 } |
| 50 | 69 |
| 51 void DateTimeChooserAndroid::ShowDialog( | 70 void DateTimeChooserAndroid::ShowDialog( |
| 52 ContentViewCore* content, | 71 ContentViewCore* content, |
| 53 RenderViewHost* host, | 72 RenderViewHost* host, |
| 54 ui::TextInputType dialog_type, | 73 ui::TextInputType dialog_type, |
| 55 double dialog_value, | 74 double dialog_value, |
| 56 double min, | 75 double min, |
| 57 double max, | 76 double max, |
| 58 double step) { | 77 double step, |
| 78 std::vector<DateTimeSuggestion> suggestions) { | |
| 59 host_ = host; | 79 host_ = host; |
| 60 | 80 |
| 61 JNIEnv* env = AttachCurrentThread(); | 81 JNIEnv* env = AttachCurrentThread(); |
| 82 ScopedJavaLocalRef<jobjectArray> suggestions_array; | |
| 83 | |
| 84 if (suggestions.size() > 0) { | |
| 85 ScopedJavaLocalRef<jclass> suggestion_class = base::android::GetClass( | |
| 86 env, | |
| 87 "org/chromium/content/browser/input/DateTimeSuggestion"); | |
|
bulach
2013/12/03 14:22:29
we try as much as possible to use GetClass, NewObj
keishi
2013/12/03 16:52:34
Done.
| |
| 88 suggestions_array.Reset(env, env->NewObjectArray(suggestions.size(), | |
| 89 suggestion_class.obj(), | |
| 90 NULL)); | |
| 91 for (size_t i = 0; i < suggestions.size(); ++i) { | |
| 92 const content::DateTimeSuggestion& suggestion = suggestions[i]; | |
| 93 ScopedJavaLocalRef<jstring> localizedValue = ConvertUTF16ToJavaString( | |
|
bulach
2013/12/03 14:22:29
nit: localized_value
keishi
2013/12/03 16:52:34
Done.
| |
| 94 env, SanitizeSuggestionString(suggestion.localizedValue)); | |
| 95 ScopedJavaLocalRef<jstring> label = ConvertUTF16ToJavaString( | |
| 96 env, SanitizeSuggestionString(suggestion.label)); | |
| 97 env->SetObjectArrayElement( | |
| 98 suggestions_array.obj(), | |
| 99 i, | |
| 100 Java_DateTimeChooserAndroid_createDateTimeSuggestion( | |
| 101 env, | |
| 102 suggestion.value, | |
| 103 localizedValue.obj(), | |
| 104 label.obj()).obj()); | |
| 105 } | |
| 106 } | |
| 107 | |
| 62 j_date_time_chooser_.Reset(Java_DateTimeChooserAndroid_createDateTimeChooser( | 108 j_date_time_chooser_.Reset(Java_DateTimeChooserAndroid_createDateTimeChooser( |
| 63 env, | 109 env, |
| 64 content->GetJavaObject().obj(), | 110 content->GetJavaObject().obj(), |
| 65 reinterpret_cast<intptr_t>(this), | 111 reinterpret_cast<intptr_t>(this), |
| 66 dialog_type, | 112 dialog_type, |
| 67 dialog_value, | 113 dialog_value, |
| 68 min, | 114 min, |
| 69 max, | 115 max, |
| 70 step)); | 116 step, |
| 117 suggestions_array.obj())); | |
| 71 } | 118 } |
| 72 | 119 |
| 73 // ---------------------------------------------------------------------------- | 120 // ---------------------------------------------------------------------------- |
| 74 // Native JNI methods | 121 // Native JNI methods |
| 75 // ---------------------------------------------------------------------------- | 122 // ---------------------------------------------------------------------------- |
| 76 bool RegisterDateTimeChooserAndroid(JNIEnv* env) { | 123 bool RegisterDateTimeChooserAndroid(JNIEnv* env) { |
| 77 bool registered = RegisterNativesImpl(env); | 124 bool registered = RegisterNativesImpl(env); |
| 78 if (registered) | 125 if (registered) |
| 79 DateTimeChooserAndroid::InitializeDateInputTypes( | 126 DateTimeChooserAndroid::InitializeDateInputTypes( |
| 80 ui::TEXT_INPUT_TYPE_DATE, | 127 ui::TEXT_INPUT_TYPE_DATE, |
| 81 ui::TEXT_INPUT_TYPE_DATE_TIME, | 128 ui::TEXT_INPUT_TYPE_DATE_TIME, |
| 82 ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL, | 129 ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL, |
| 83 ui::TEXT_INPUT_TYPE_MONTH, | 130 ui::TEXT_INPUT_TYPE_MONTH, |
| 84 ui::TEXT_INPUT_TYPE_TIME, | 131 ui::TEXT_INPUT_TYPE_TIME, |
| 85 ui::TEXT_INPUT_TYPE_WEEK); | 132 ui::TEXT_INPUT_TYPE_WEEK); |
| 86 return registered; | 133 return registered; |
| 87 } | 134 } |
| 88 | 135 |
| 89 } // namespace content | 136 } // namespace content |
| OLD | NEW |