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" |
8 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.h" |
9 #include "content/public/browser/android/content_view_core.h" | 10 #include "content/public/browser/android/content_view_core.h" |
10 #include "content/public/browser/render_view_host.h" | 11 #include "content/public/browser/render_view_host.h" |
12 #include "content/public/common/date_time_suggestion.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) { | |
29 string.erase( | |
30 std::remove_if(string.begin(), string.end(), &IsNonPrintableChar), | |
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 |
28 // static | 47 // static |
29 void DateTimeChooserAndroid::InitializeDateInputTypes( | 48 void DateTimeChooserAndroid::InitializeDateInputTypes( |
30 int text_input_type_date, int text_input_type_date_time, | 49 int text_input_type_date, int text_input_type_date_time, |
31 int text_input_type_date_time_local, int text_input_type_month, | 50 int text_input_type_date_time_local, int text_input_type_month, |
32 int text_input_type_time, int text_input_type_week) { | 51 int text_input_type_time, int text_input_type_week) { |
33 JNIEnv* env = AttachCurrentThread(); | 52 JNIEnv* env = AttachCurrentThread(); |
34 Java_DateTimeChooserAndroid_initializeDateInputTypes( | 53 Java_DateTimeChooserAndroid_initializeDateInputTypes( |
35 env, | 54 env, |
36 text_input_type_date, text_input_type_date_time, | 55 text_input_type_date, text_input_type_date_time, |
37 text_input_type_date_time_local, text_input_type_month, | 56 text_input_type_date_time_local, text_input_type_month, |
38 text_input_type_time, text_input_type_week); | 57 text_input_type_time, text_input_type_week); |
39 } | 58 } |
40 | 59 |
41 void DateTimeChooserAndroid::ReplaceDateTime(JNIEnv* env, | 60 void DateTimeChooserAndroid::ReplaceDateTime(JNIEnv* env, |
42 jobject, | 61 jobject, |
43 int dialog_type, | 62 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)); | 63 host_->Send(new ViewMsg_ReplaceDateTime(host_->GetRoutingID(), value)); |
63 } | 64 } |
64 | 65 |
65 void DateTimeChooserAndroid::CancelDialog(JNIEnv* env, jobject) { | 66 void DateTimeChooserAndroid::CancelDialog(JNIEnv* env, jobject) { |
66 host_->Send(new ViewMsg_CancelDateTimeDialog(host_->GetRoutingID())); | 67 host_->Send(new ViewMsg_CancelDateTimeDialog(host_->GetRoutingID())); |
67 } | 68 } |
68 | 69 |
69 void DateTimeChooserAndroid::ShowDialog(ContentViewCore* content, | 70 void DateTimeChooserAndroid::ShowDialog( |
70 RenderViewHost* host, | 71 ContentViewCore* content, |
71 int type, | 72 RenderViewHost* host, |
72 int year, | 73 ui::TextInputType dialog_type, |
73 int month, | 74 double dialog_value, |
74 int day, | 75 double min, |
75 int hour, | 76 double max, |
76 int minute, | 77 double step, |
77 int second, | 78 std::vector<DateTimeSuggestion> suggestions) { |
78 int milli, | |
79 int week, | |
80 double min, | |
81 double max, | |
82 double step) { | |
83 host_ = host; | 79 host_ = host; |
84 | 80 |
85 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"); | |
joth
2013/12/02 20:48:55
+bulach could you double check this? I know we nor
| |
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( | |
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 | |
86 j_date_time_chooser_.Reset(Java_DateTimeChooserAndroid_createDateTimeChooser( | 108 j_date_time_chooser_.Reset(Java_DateTimeChooserAndroid_createDateTimeChooser( |
87 env, | 109 env, |
88 content->GetJavaObject().obj(), | 110 content->GetJavaObject().obj(), |
89 reinterpret_cast<intptr_t>(this), | 111 reinterpret_cast<intptr_t>(this), |
90 type, | 112 dialog_type, |
91 year, | 113 dialog_value, |
92 month, | |
93 day, | |
94 hour, | |
95 minute, | |
96 second, | |
97 milli, | |
98 week, | |
99 min, | 114 min, |
100 max, | 115 max, |
101 step)); | 116 step, |
117 suggestions_array.obj())); | |
102 } | 118 } |
103 | 119 |
104 // ---------------------------------------------------------------------------- | 120 // ---------------------------------------------------------------------------- |
105 // Native JNI methods | 121 // Native JNI methods |
106 // ---------------------------------------------------------------------------- | 122 // ---------------------------------------------------------------------------- |
107 bool RegisterDateTimeChooserAndroid(JNIEnv* env) { | 123 bool RegisterDateTimeChooserAndroid(JNIEnv* env) { |
108 bool registered = RegisterNativesImpl(env); | 124 bool registered = RegisterNativesImpl(env); |
109 if (registered) | 125 if (registered) |
110 DateTimeChooserAndroid::InitializeDateInputTypes( | 126 DateTimeChooserAndroid::InitializeDateInputTypes( |
111 ui::TEXT_INPUT_TYPE_DATE, | 127 ui::TEXT_INPUT_TYPE_DATE, |
112 ui::TEXT_INPUT_TYPE_DATE_TIME, | 128 ui::TEXT_INPUT_TYPE_DATE_TIME, |
113 ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL, | 129 ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL, |
114 ui::TEXT_INPUT_TYPE_MONTH, | 130 ui::TEXT_INPUT_TYPE_MONTH, |
115 ui::TEXT_INPUT_TYPE_TIME, | 131 ui::TEXT_INPUT_TYPE_TIME, |
116 ui::TEXT_INPUT_TYPE_WEEK); | 132 ui::TEXT_INPUT_TYPE_WEEK); |
117 return registered; | 133 return registered; |
118 } | 134 } |
119 | 135 |
120 } // namespace content | 136 } // namespace content |
OLD | NEW |