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/android/scoped_java_ref.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "content/browser/renderer_host/render_widget_host_impl.h"
15 #include "content/browser/renderer_host/render_widget_host_view_android.h"
16 #include "content/common/view_messages.h"
17 #include "content/public/browser/native_web_keyboard_event.h"
18 #include "jni/ImeAdapter_jni.h"
19 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
20 #include "third_party/WebKit/public/web/WebInputEvent.h"
22 using base::android::AttachCurrentThread
;
23 using base::android::ConvertJavaStringToUTF16
;
28 // Maps a java KeyEvent into a NativeWebKeyboardEvent.
29 // |java_key_event| is used to maintain a globalref for KeyEvent.
30 // |action| will help determine the WebInputEvent type.
31 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create
32 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key
33 // as a key press of character \r.
34 NativeWebKeyboardEvent
NativeWebKeyboardEventFromKeyEvent(
36 jobject java_key_event
,
43 blink::WebInputEvent::Type type
= blink::WebInputEvent::Undefined
;
44 if (action
== AKEY_EVENT_ACTION_DOWN
)
45 type
= blink::WebInputEvent::RawKeyDown
;
46 else if (action
== AKEY_EVENT_ACTION_UP
)
47 type
= blink::WebInputEvent::KeyUp
;
48 return NativeWebKeyboardEvent(java_key_event
, type
, modifiers
,
49 time_ms
, key_code
, unicode_char
, is_system_key
);
52 } // anonymous namespace
54 bool RegisterImeAdapter(JNIEnv
* env
) {
55 if (!RegisterNativesImpl(env
))
58 Java_ImeAdapter_initializeWebInputEvents(env
,
59 blink::WebInputEvent::RawKeyDown
,
60 blink::WebInputEvent::KeyUp
,
61 blink::WebInputEvent::Char
,
62 blink::WebInputEvent::ShiftKey
,
63 blink::WebInputEvent::AltKey
,
64 blink::WebInputEvent::ControlKey
,
65 blink::WebInputEvent::CapsLockOn
,
66 blink::WebInputEvent::NumLockOn
);
67 Java_ImeAdapter_initializeTextInputTypes(
69 ui::TEXT_INPUT_TYPE_NONE
,
70 ui::TEXT_INPUT_TYPE_TEXT
,
71 ui::TEXT_INPUT_TYPE_TEXT_AREA
,
72 ui::TEXT_INPUT_TYPE_PASSWORD
,
73 ui::TEXT_INPUT_TYPE_SEARCH
,
74 ui::TEXT_INPUT_TYPE_URL
,
75 ui::TEXT_INPUT_TYPE_EMAIL
,
76 ui::TEXT_INPUT_TYPE_TELEPHONE
,
77 ui::TEXT_INPUT_TYPE_NUMBER
,
78 ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE
);
82 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid
* rwhva
)
86 ImeAdapterAndroid::~ImeAdapterAndroid() {
87 JNIEnv
* env
= AttachCurrentThread();
88 base::android::ScopedJavaLocalRef
<jobject
> obj
= java_ime_adapter_
.get(env
);
90 Java_ImeAdapter_detach(env
, obj
.obj());
93 bool ImeAdapterAndroid::SendSyntheticKeyEvent(JNIEnv
*,
99 NativeWebKeyboardEvent
event(static_cast<blink::WebInputEvent::Type
>(type
),
100 0 /* modifiers */, time_ms
/ 1000.0, key_code
,
101 text
, false /* is_system_key */);
102 rwhva_
->SendKeyEvent(event
);
106 bool ImeAdapterAndroid::SendKeyEvent(JNIEnv
* env
, jobject
,
107 jobject original_key_event
,
108 int action
, int modifiers
,
109 long time_ms
, int key_code
,
110 bool is_system_key
, int unicode_char
) {
111 NativeWebKeyboardEvent event
= NativeWebKeyboardEventFromKeyEvent(
112 env
, original_key_event
, action
, modifiers
,
113 time_ms
, key_code
, is_system_key
, unicode_char
);
114 bool key_down_text_insertion
=
115 event
.type
== blink::WebInputEvent::RawKeyDown
&& event
.text
[0];
116 // If we are going to follow up with a synthetic Char event, then that's the
117 // one we expect to test if it's handled or unhandled, so skip handling the
118 // "real" event in the browser.
119 event
.skip_in_browser
= key_down_text_insertion
;
120 rwhva_
->SendKeyEvent(event
);
121 if (key_down_text_insertion
) {
122 // Send a Char event, but without an os_event since we don't want to
123 // roundtrip back to java such synthetic event.
124 NativeWebKeyboardEvent
char_event(blink::WebInputEvent::Char
, modifiers
,
125 time_ms
, key_code
, unicode_char
,
127 char_event
.skip_in_browser
= key_down_text_insertion
;
128 rwhva_
->SendKeyEvent(char_event
);
133 void ImeAdapterAndroid::SetComposingText(JNIEnv
* env
, jobject
, jstring text
,
134 int new_cursor_pos
) {
135 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
139 base::string16 text16
= ConvertJavaStringToUTF16(env
, text
);
140 std::vector
<blink::WebCompositionUnderline
> underlines
;
141 underlines
.push_back(
142 blink::WebCompositionUnderline(0, text16
.length(), SK_ColorBLACK
,
144 // new_cursor_position is as described in the Android API for
145 // InputConnection#setComposingText, whereas the parameters for
146 // ImeSetComposition are relative to the start of the composition.
147 if (new_cursor_pos
> 0)
148 new_cursor_pos
= text16
.length() + new_cursor_pos
- 1;
150 rwhi
->ImeSetComposition(text16
, underlines
, new_cursor_pos
, new_cursor_pos
);
153 void ImeAdapterAndroid::CommitText(JNIEnv
* env
, jobject
, jstring text
) {
154 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
158 base::string16 text16
= ConvertJavaStringToUTF16(env
, text
);
159 rwhi
->ImeConfirmComposition(text16
, gfx::Range::InvalidRange(), false);
162 void ImeAdapterAndroid::FinishComposingText(JNIEnv
* env
, jobject
) {
163 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
167 rwhi
->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(),
171 void ImeAdapterAndroid::AttachImeAdapter(JNIEnv
* env
, jobject java_object
) {
172 java_ime_adapter_
= JavaObjectWeakGlobalRef(env
, java_object
);
175 void ImeAdapterAndroid::CancelComposition() {
176 base::android::ScopedJavaLocalRef
<jobject
> obj
=
177 java_ime_adapter_
.get(AttachCurrentThread());
179 Java_ImeAdapter_cancelComposition(AttachCurrentThread(), obj
.obj());
182 void ImeAdapterAndroid::SetEditableSelectionOffsets(JNIEnv
*, jobject
,
183 int start
, int end
) {
184 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
188 rwhi
->Send(new ViewMsg_SetEditableSelectionOffsets(rwhi
->GetRoutingID(),
192 void ImeAdapterAndroid::SetComposingRegion(JNIEnv
*, jobject
,
193 int start
, int end
) {
194 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
198 std::vector
<blink::WebCompositionUnderline
> underlines
;
199 underlines
.push_back(
200 blink::WebCompositionUnderline(0, end
- start
, SK_ColorBLACK
, false));
202 rwhi
->Send(new ViewMsg_SetCompositionFromExistingText(
203 rwhi
->GetRoutingID(), start
, end
, underlines
));
206 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv
*, jobject
,
207 int before
, int after
) {
208 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
212 rwhi
->Send(new ViewMsg_ExtendSelectionAndDelete(rwhi
->GetRoutingID(),
216 void ImeAdapterAndroid::Unselect(JNIEnv
* env
, jobject
) {
217 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
224 void ImeAdapterAndroid::SelectAll(JNIEnv
* env
, jobject
) {
225 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
232 void ImeAdapterAndroid::Cut(JNIEnv
* env
, jobject
) {
233 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
240 void ImeAdapterAndroid::Copy(JNIEnv
* env
, jobject
) {
241 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
248 void ImeAdapterAndroid::Paste(JNIEnv
* env
, jobject
) {
249 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
256 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv
* env
, jobject
) {
257 java_ime_adapter_
.reset();
260 RenderWidgetHostImpl
* ImeAdapterAndroid::GetRenderWidgetHostImpl() {
262 RenderWidgetHost
* rwh
= rwhva_
->GetRenderWidgetHost();
266 return RenderWidgetHostImpl::From(rwh
);
269 } // namespace content