Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/renderer_host/ime_adapter_android.h" | 5 #include "content/browser/renderer_host/ime_adapter_android.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 7 #include <android/input.h> | 8 #include <android/input.h> |
| 8 | 9 |
| 9 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 11 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 14 #include "content/browser/frame_host/frame_tree.h" | 15 #include "content/browser/frame_host/frame_tree.h" |
| 15 #include "content/browser/frame_host/frame_tree_node.h" | 16 #include "content/browser/frame_host/frame_tree_node.h" |
| 16 #include "content/browser/frame_host/render_frame_host_impl.h" | 17 #include "content/browser/frame_host/render_frame_host_impl.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 int unicode_char) { | 51 int unicode_char) { |
| 51 blink::WebInputEvent::Type type = blink::WebInputEvent::Undefined; | 52 blink::WebInputEvent::Type type = blink::WebInputEvent::Undefined; |
| 52 if (action == AKEY_EVENT_ACTION_DOWN) | 53 if (action == AKEY_EVENT_ACTION_DOWN) |
| 53 type = blink::WebInputEvent::RawKeyDown; | 54 type = blink::WebInputEvent::RawKeyDown; |
| 54 else if (action == AKEY_EVENT_ACTION_UP) | 55 else if (action == AKEY_EVENT_ACTION_UP) |
| 55 type = blink::WebInputEvent::KeyUp; | 56 type = blink::WebInputEvent::KeyUp; |
| 56 return NativeWebKeyboardEvent(java_key_event, type, modifiers, | 57 return NativeWebKeyboardEvent(java_key_event, type, modifiers, |
| 57 time_ms / 1000.0, key_code, unicode_char, is_system_key); | 58 time_ms / 1000.0, key_code, unicode_char, is_system_key); |
| 58 } | 59 } |
| 59 | 60 |
| 61 // Uses JNI to iterate over |spanObjArray| obtianed from |spannableString|, and | |
| 62 // populates a list of blink::WebCompositionUnderline with data from spans | |
| 63 // that we care about (e.g., BackgroundColorSpan). | |
| 64 void GetUnderlinesFromSpanArray( | |
| 65 JNIEnv* env, | |
| 66 jobject spannableString, | |
| 67 jobjectArray spanObjArray, | |
| 68 std::vector<blink::WebCompositionUnderline>* underlines) { | |
| 69 if (!spannableString || !spanObjArray) | |
| 70 return; | |
| 71 | |
| 72 jclass clsSpannableString = env->FindClass("android/text/SpannableString"); | |
|
aurimas (slooooooooow)
2014/06/10 00:30:48
In Chrome for Android we avoid using JNI FindClass
huangs
2014/06/10 04:39:13
Done.
| |
| 73 jclass clsBackgroundColorSpan = | |
| 74 env->FindClass("android/text/style/BackgroundColorSpan"); | |
| 75 if (!clsSpannableString || !clsBackgroundColorSpan) | |
| 76 return; | |
| 77 | |
| 78 jmethodID midGetSpanStart = env->GetMethodID( | |
| 79 clsSpannableString, "getSpanStart", "(Ljava/lang/Object;)I"); | |
| 80 jmethodID midGetSpanEnd = env->GetMethodID( | |
| 81 clsSpannableString, "getSpanEnd", "(Ljava/lang/Object;)I"); | |
| 82 jmethodID midGetBackgroundColor = env->GetMethodID( | |
| 83 clsBackgroundColorSpan, "getBackgroundColor", "()I"); | |
| 84 | |
| 85 if (!midGetSpanStart || !midGetSpanEnd || !midGetBackgroundColor) | |
| 86 return; | |
| 87 | |
| 88 jsize n = env->GetArrayLength(spanObjArray); | |
| 89 for (int i = 0; i < n; ++i) { | |
| 90 jobject spanObj = env->GetObjectArrayElement(spanObjArray, i); | |
| 91 if (env->IsInstanceOf(spanObj, clsBackgroundColorSpan) == JNI_FALSE) | |
| 92 continue; | |
| 93 | |
| 94 jint start = env->CallIntMethod(spannableString, midGetSpanStart, spanObj); | |
| 95 jint end = env->CallIntMethod(spannableString, midGetSpanEnd, spanObj); | |
| 96 jint background = env->CallIntMethod(spanObj, midGetBackgroundColor); | |
| 97 | |
| 98 underlines->push_back(blink::WebCompositionUnderline( | |
| 99 static_cast<int>(start), | |
| 100 static_cast<int>(end), | |
| 101 SK_ColorBLACK, | |
| 102 false, | |
| 103 background)); | |
| 104 } | |
| 105 } | |
| 106 | |
| 60 } // anonymous namespace | 107 } // anonymous namespace |
| 61 | 108 |
| 62 bool RegisterImeAdapter(JNIEnv* env) { | 109 bool RegisterImeAdapter(JNIEnv* env) { |
| 63 if (!RegisterNativesImpl(env)) | 110 if (!RegisterNativesImpl(env)) |
| 64 return false; | 111 return false; |
| 65 | 112 |
| 66 Java_ImeAdapter_initializeWebInputEvents(env, | 113 Java_ImeAdapter_initializeWebInputEvents(env, |
| 67 blink::WebInputEvent::RawKeyDown, | 114 blink::WebInputEvent::RawKeyDown, |
| 68 blink::WebInputEvent::KeyUp, | 115 blink::WebInputEvent::KeyUp, |
| 69 blink::WebInputEvent::Char, | 116 blink::WebInputEvent::Char, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 // roundtrip back to java such synthetic event. | 178 // roundtrip back to java such synthetic event. |
| 132 NativeWebKeyboardEvent char_event(blink::WebInputEvent::Char, modifiers, | 179 NativeWebKeyboardEvent char_event(blink::WebInputEvent::Char, modifiers, |
| 133 time_ms / 1000.0, key_code, unicode_char, | 180 time_ms / 1000.0, key_code, unicode_char, |
| 134 is_system_key); | 181 is_system_key); |
| 135 char_event.skip_in_browser = key_down_text_insertion; | 182 char_event.skip_in_browser = key_down_text_insertion; |
| 136 rwhva_->SendKeyEvent(char_event); | 183 rwhva_->SendKeyEvent(char_event); |
| 137 } | 184 } |
| 138 return true; | 185 return true; |
| 139 } | 186 } |
| 140 | 187 |
| 141 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, jobject, jstring text, | 188 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, |
| 142 int new_cursor_pos) { | 189 jobject, |
| 190 jstring text, | |
| 191 int new_cursor_pos, | |
| 192 jobject spannableString, | |
| 193 jobjectArray spanObjArray) { | |
| 143 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 194 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 144 if (!rwhi) | 195 if (!rwhi) |
| 145 return; | 196 return; |
| 146 | 197 |
| 147 base::string16 text16 = ConvertJavaStringToUTF16(env, text); | 198 base::string16 text16 = ConvertJavaStringToUTF16(env, text); |
| 148 std::vector<blink::WebCompositionUnderline> underlines; | 199 std::vector<blink::WebCompositionUnderline> underlines; |
| 149 underlines.push_back( | 200 |
| 150 blink::WebCompositionUnderline(0, text16.length(), SK_ColorBLACK, | 201 GetUnderlinesFromSpanArray(env, spannableString, spanObjArray, &underlines); |
|
aurimas (slooooooooow)
2014/06/10 00:30:49
Here you should call Java_ImeAdapter_getUnderlines
huangs
2014/06/10 04:39:13
Done. I'm looping over Object[] because in the ne
| |
| 151 false)); | 202 |
| 203 // Default to simple underline if we find no spans that we care about. | |
| 204 if (underlines.empty()) { | |
| 205 underlines.push_back( | |
| 206 blink::WebCompositionUnderline(0, text16.length(), SK_ColorBLACK, false, | |
| 207 SK_ColorTRANSPARENT)); | |
| 208 } | |
| 209 // Sort spans by |.startOffset|. | |
| 210 std::sort(underlines.begin(), underlines.end()); | |
| 211 | |
| 152 // new_cursor_position is as described in the Android API for | 212 // new_cursor_position is as described in the Android API for |
| 153 // InputConnection#setComposingText, whereas the parameters for | 213 // InputConnection#setComposingText, whereas the parameters for |
| 154 // ImeSetComposition are relative to the start of the composition. | 214 // ImeSetComposition are relative to the start of the composition. |
| 155 if (new_cursor_pos > 0) | 215 if (new_cursor_pos > 0) |
| 156 new_cursor_pos = text16.length() + new_cursor_pos - 1; | 216 new_cursor_pos = text16.length() + new_cursor_pos - 1; |
| 157 | 217 |
| 158 rwhi->ImeSetComposition(text16, underlines, new_cursor_pos, new_cursor_pos); | 218 rwhi->ImeSetComposition(text16, underlines, new_cursor_pos, new_cursor_pos); |
| 159 } | 219 } |
| 160 | 220 |
| 161 void ImeAdapterAndroid::CommitText(JNIEnv* env, jobject, jstring text) { | 221 void ImeAdapterAndroid::CommitText(JNIEnv* env, jobject, jstring text) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 } | 268 } |
| 209 | 269 |
| 210 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, jobject, | 270 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, jobject, |
| 211 int start, int end) { | 271 int start, int end) { |
| 212 RenderFrameHost* rfh = GetFocusedFrame(); | 272 RenderFrameHost* rfh = GetFocusedFrame(); |
| 213 if (!rfh) | 273 if (!rfh) |
| 214 return; | 274 return; |
| 215 | 275 |
| 216 std::vector<blink::WebCompositionUnderline> underlines; | 276 std::vector<blink::WebCompositionUnderline> underlines; |
| 217 underlines.push_back( | 277 underlines.push_back( |
| 218 blink::WebCompositionUnderline(0, end - start, SK_ColorBLACK, false)); | 278 blink::WebCompositionUnderline(0, end - start, SK_ColorBLACK, false, |
| 279 SK_ColorTRANSPARENT)); | |
| 219 | 280 |
| 220 rfh->Send(new FrameMsg_SetCompositionFromExistingText( | 281 rfh->Send(new FrameMsg_SetCompositionFromExistingText( |
| 221 rfh->GetRoutingID(), start, end, underlines)); | 282 rfh->GetRoutingID(), start, end, underlines)); |
| 222 } | 283 } |
| 223 | 284 |
| 224 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, jobject, | 285 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, jobject, |
| 225 int before, int after) { | 286 int before, int after) { |
| 226 RenderFrameHostImpl* rfh = | 287 RenderFrameHostImpl* rfh = |
| 227 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); | 288 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); |
| 228 if (rfh) | 289 if (rfh) |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 WebContents* ImeAdapterAndroid::GetWebContents() { | 352 WebContents* ImeAdapterAndroid::GetWebContents() { |
| 292 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); | 353 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); |
| 293 if (!rwh) | 354 if (!rwh) |
| 294 return NULL; | 355 return NULL; |
| 295 if (!rwh->IsRenderView()) | 356 if (!rwh->IsRenderView()) |
| 296 return NULL; | 357 return NULL; |
| 297 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); | 358 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); |
| 298 } | 359 } |
| 299 | 360 |
| 300 } // namespace content | 361 } // namespace content |
| OLD | NEW |