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
;
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(
35 jobject java_key_event
,
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
))
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(
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
);
87 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid
* 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
*,
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
);
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
);
133 void ImeAdapterAndroid::SetComposingText(JNIEnv
* env
, jobject
, jstring text
,
134 int new_cursor_pos
) {
135 RenderWidgetHostImpl
* rwhi
= RenderWidgetHostImpl::From(
136 rwhva_
->GetRenderWidgetHost());
140 string16 text16
= ConvertJavaStringToUTF16(env
, text
);
141 std::vector
<WebKit::WebCompositionUnderline
> underlines
;
142 underlines
.push_back(
143 WebKit::WebCompositionUnderline(0, text16
.length(), SK_ColorBLACK
,
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());
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());
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());
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());
199 rwhi
->Send(new ViewMsg_SetEditableSelectionOffsets(rwhi
->GetRoutingID(),
203 void ImeAdapterAndroid::SetComposingRegion(JNIEnv
*, jobject
,
204 int start
, int end
) {
205 RenderWidgetHostImpl
* rwhi
= RenderWidgetHostImpl::From(
206 rwhva_
->GetRenderWidgetHost());
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());
225 rwhi
->Send(new ViewMsg_ExtendSelectionAndDelete(rwhi
->GetRoutingID(),
229 void ImeAdapterAndroid::Unselect(JNIEnv
* env
, jobject
) {
230 RenderWidgetHostImpl
* rwhi
= RenderWidgetHostImpl::From(
231 rwhva_
->GetRenderWidgetHost());
235 rwhi
->Send(new ViewMsg_Unselect(rwhi
->GetRoutingID()));
238 void ImeAdapterAndroid::SelectAll(JNIEnv
* env
, jobject
) {
239 RenderWidgetHostImpl
* rwhi
= RenderWidgetHostImpl::From(
240 rwhva_
->GetRenderWidgetHost());
244 rwhi
->Send(new ViewMsg_SelectAll(rwhi
->GetRoutingID()));
247 void ImeAdapterAndroid::Cut(JNIEnv
* env
, jobject
) {
248 RenderWidgetHostImpl
* rwhi
= RenderWidgetHostImpl::From(
249 rwhva_
->GetRenderWidgetHost());
253 rwhi
->Send(new ViewMsg_Cut(rwhi
->GetRoutingID()));
256 void ImeAdapterAndroid::Copy(JNIEnv
* env
, jobject
) {
257 RenderWidgetHostImpl
* rwhi
= RenderWidgetHostImpl::From(
258 rwhva_
->GetRenderWidgetHost());
262 rwhi
->Send(new ViewMsg_Copy(rwhi
->GetRoutingID()));
265 void ImeAdapterAndroid::Paste(JNIEnv
* env
, jobject
) {
266 RenderWidgetHostImpl
* rwhi
= RenderWidgetHostImpl::From(
267 rwhva_
->GetRenderWidgetHost());
271 rwhi
->Send(new ViewMsg_Paste(rwhi
->GetRoutingID()));
274 } // namespace content