Suppress sending mousedown / mouseup when in fling
[chromium-blink-merge.git] / content / browser / renderer_host / ime_adapter_android.cc
blob3cae5ec00d87c0eb10b9687cdb3cc3003c7affe4
1 // Copyright (c) 2012 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/renderer_host/ime_adapter_android.h"
7 #include <android/input.h>
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/time.h"
12 #include "base/utf_string_conversions.h"
13 #include "content/browser/renderer_host/render_widget_host_impl.h"
14 #include "content/browser/renderer_host/render_widget_host_view_android.h"
15 #include "content/common/view_messages.h"
16 #include "content/public/browser/native_web_keyboard_event.h"
17 #include "jni/ImeAdapter_jni.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCompositionUnderline.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
21 using base::android::AttachCurrentThread;
22 using base::android::ConvertJavaStringToUTF16;
24 namespace content {
25 namespace {
27 // Maps a java KeyEvent into a NativeWebKeyboardEvent.
28 // |java_key_event| is used to maintain a globalref for KeyEvent.
29 // |action| will help determine the WebInputEvent type.
30 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create
31 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key
32 // as a key press of character \r.
33 NativeWebKeyboardEvent NativeWebKeyboardEventFromKeyEvent(
34 JNIEnv* env,
35 jobject java_key_event,
36 int action,
37 int modifiers,
38 long time_ms,
39 int key_code,
40 bool is_system_key,
41 int unicode_char) {
42 WebKit::WebInputEvent::Type type = WebKit::WebInputEvent::Undefined;
43 if (action == AKEY_EVENT_ACTION_DOWN)
44 type = WebKit::WebInputEvent::RawKeyDown;
45 else if (action == AKEY_EVENT_ACTION_UP)
46 type = WebKit::WebInputEvent::KeyUp;
47 return NativeWebKeyboardEvent(java_key_event, type, modifiers,
48 time_ms, key_code, unicode_char, is_system_key);
51 } // anonymous namespace
53 bool RegisterImeAdapter(JNIEnv* env) {
54 if (!RegisterNativesImpl(env))
55 return false;
57 Java_ImeAdapter_initializeWebInputEvents(env,
58 WebKit::WebInputEvent::RawKeyDown,
59 WebKit::WebInputEvent::KeyUp,
60 WebKit::WebInputEvent::Char,
61 WebKit::WebInputEvent::ShiftKey,
62 WebKit::WebInputEvent::AltKey,
63 WebKit::WebInputEvent::ControlKey,
64 WebKit::WebInputEvent::CapsLockOn,
65 WebKit::WebInputEvent::NumLockOn);
66 Java_ImeAdapter_initializeTextInputTypes(
67 env,
68 ui::TEXT_INPUT_TYPE_NONE,
69 ui::TEXT_INPUT_TYPE_TEXT,
70 ui::TEXT_INPUT_TYPE_TEXT_AREA,
71 ui::TEXT_INPUT_TYPE_PASSWORD,
72 ui::TEXT_INPUT_TYPE_SEARCH,
73 ui::TEXT_INPUT_TYPE_URL,
74 ui::TEXT_INPUT_TYPE_EMAIL,
75 ui::TEXT_INPUT_TYPE_TELEPHONE,
76 ui::TEXT_INPUT_TYPE_NUMBER,
77 ui::TEXT_INPUT_TYPE_DATE,
78 ui::TEXT_INPUT_TYPE_DATE_TIME,
79 ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL,
80 ui::TEXT_INPUT_TYPE_MONTH,
81 ui::TEXT_INPUT_TYPE_TIME,
82 ui::TEXT_INPUT_TYPE_WEEK,
83 ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE);
84 return true;
87 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid* rwhva)
88 : rwhva_(rwhva),
89 java_ime_adapter_(NULL) {
92 ImeAdapterAndroid::~ImeAdapterAndroid() {
93 if (java_ime_adapter_) {
94 JNIEnv* env = base::android::AttachCurrentThread();
95 Java_ImeAdapter_detach(env, java_ime_adapter_);
96 env->DeleteGlobalRef(java_ime_adapter_);
100 bool ImeAdapterAndroid::SendSyntheticKeyEvent(JNIEnv*,
101 jobject,
102 int type,
103 long time_ms,
104 int key_code,
105 int text) {
106 NativeWebKeyboardEvent event(static_cast<WebKit::WebInputEvent::Type>(type),
107 0 /* modifiers */, time_ms / 1000.0, key_code,
108 text, false /* is_system_key */);
109 rwhva_->SendKeyEvent(event);
110 return true;
113 bool ImeAdapterAndroid::SendKeyEvent(JNIEnv* env, jobject,
114 jobject original_key_event,
115 int action, int modifiers,
116 long time_ms, int key_code,
117 bool is_system_key, int unicode_char) {
118 NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent(
119 env, original_key_event, action, modifiers,
120 time_ms, key_code, is_system_key, unicode_char);
121 rwhva_->SendKeyEvent(event);
122 if (event.type == WebKit::WebInputEvent::RawKeyDown && event.text[0]) {
123 // Send a Char event, but without an os_event since we don't want to
124 // roundtrip back to java such synthetic event.
125 NativeWebKeyboardEvent char_event(event);
126 char_event.os_event = NULL;
127 event.type = WebKit::WebInputEvent::Char;
128 rwhva_->SendKeyEvent(event);
130 return true;
133 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, jobject, jstring text,
134 int new_cursor_pos) {
135 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
136 rwhva_->GetRenderWidgetHost());
137 if (!rwhi)
138 return;
140 string16 text16 = ConvertJavaStringToUTF16(env, text);
141 std::vector<WebKit::WebCompositionUnderline> underlines;
142 underlines.push_back(
143 WebKit::WebCompositionUnderline(0, text16.length(), SK_ColorBLACK,
144 false));
145 // new_cursor_position is as described in the Android API for
146 // InputConnection#setComposingText, whereas the parameters for
147 // ImeSetComposition are relative to the start of the composition.
148 if (new_cursor_pos > 0)
149 new_cursor_pos = text16.length() + new_cursor_pos - 1;
151 rwhi->ImeSetComposition(text16, underlines, new_cursor_pos, new_cursor_pos);
155 void ImeAdapterAndroid::CommitText(JNIEnv* env, jobject, jstring text) {
156 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
157 rwhva_->GetRenderWidgetHost());
158 if (!rwhi)
159 return;
161 string16 text16 = ConvertJavaStringToUTF16(env, text);
162 rwhi->ImeConfirmComposition(text16);
165 void ImeAdapterAndroid::AttachImeAdapter(JNIEnv* env, jobject java_object) {
166 java_ime_adapter_ = AttachCurrentThread()->NewGlobalRef(java_object);
169 void ImeAdapterAndroid::CancelComposition() {
170 Java_ImeAdapter_cancelComposition(AttachCurrentThread(), java_ime_adapter_);
173 void ImeAdapterAndroid::ReplaceText(JNIEnv* env, jobject, jstring text) {
174 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
175 rwhva_->GetRenderWidgetHost());
176 if (!rwhi)
177 return;
179 string16 text16 = ConvertJavaStringToUTF16(env, text);
180 rwhi->Send(new ViewMsg_ReplaceAll(rwhi->GetRoutingID(), text16));
183 void ImeAdapterAndroid::ClearFocus(JNIEnv* env, jobject) {
184 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
185 rwhva_->GetRenderWidgetHost());
186 if (!rwhi)
187 return;
189 rwhi->Send(new ViewMsg_ClearFocusedNode(rwhi->GetRoutingID()));
192 void ImeAdapterAndroid::SetEditableSelectionOffsets(JNIEnv*, jobject,
193 int start, int end) {
194 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
195 rwhva_->GetRenderWidgetHost());
196 if (!rwhi)
197 return;
199 rwhi->Send(new ViewMsg_SetEditableSelectionOffsets(rwhi->GetRoutingID(),
200 start, end));
203 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, jobject,
204 int start, int end) {
205 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
206 rwhva_->GetRenderWidgetHost());
207 if (!rwhi)
208 return;
210 std::vector<WebKit::WebCompositionUnderline> underlines;
211 underlines.push_back(
212 WebKit::WebCompositionUnderline(0, end - start, SK_ColorBLACK, false));
214 rwhi->Send(new ViewMsg_SetCompositionFromExistingText(
215 rwhi->GetRoutingID(), start, end, underlines));
218 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, jobject,
219 int before, int after) {
220 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
221 rwhva_->GetRenderWidgetHost());
222 if (!rwhi)
223 return;
225 rwhi->Send(new ViewMsg_ExtendSelectionAndDelete(rwhi->GetRoutingID(),
226 before, after));
229 void ImeAdapterAndroid::Unselect(JNIEnv* env, jobject) {
230 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
231 rwhva_->GetRenderWidgetHost());
232 if (!rwhi)
233 return;
235 rwhi->Send(new ViewMsg_Unselect(rwhi->GetRoutingID()));
238 void ImeAdapterAndroid::SelectAll(JNIEnv* env, jobject) {
239 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
240 rwhva_->GetRenderWidgetHost());
241 if (!rwhi)
242 return;
244 rwhi->Send(new ViewMsg_SelectAll(rwhi->GetRoutingID()));
247 void ImeAdapterAndroid::Cut(JNIEnv* env, jobject) {
248 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
249 rwhva_->GetRenderWidgetHost());
250 if (!rwhi)
251 return;
253 rwhi->Send(new ViewMsg_Cut(rwhi->GetRoutingID()));
256 void ImeAdapterAndroid::Copy(JNIEnv* env, jobject) {
257 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
258 rwhva_->GetRenderWidgetHost());
259 if (!rwhi)
260 return;
262 rwhi->Send(new ViewMsg_Copy(rwhi->GetRoutingID()));
265 void ImeAdapterAndroid::Paste(JNIEnv* env, jobject) {
266 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
267 rwhva_->GetRenderWidgetHost());
268 if (!rwhi)
269 return;
271 rwhi->Send(new ViewMsg_Paste(rwhi->GetRoutingID()));
274 } // namespace content