diff --git a/src/api-impl-jni/generated_headers/android_widget_EditText.h b/src/api-impl-jni/generated_headers/android_widget_EditText.h index 2fb54e9b..5cf8e4ea 100644 --- a/src/api-impl-jni/generated_headers/android_widget_EditText.h +++ b/src/api-impl-jni/generated_headers/android_widget_EditText.h @@ -223,6 +223,14 @@ JNIEXPORT jstring JNICALL Java_android_widget_EditText_native_1getText JNIEXPORT void JNICALL Java_android_widget_EditText_native_1addTextChangedListener (JNIEnv *, jobject, jlong, jobject); +/* + * Class: android_widget_EditText + * Method: native_setOnEditorActionListener + * Signature: (JLandroid/widget/TextView/OnEditorActionListener;)V + */ +JNIEXPORT void JNICALL Java_android_widget_EditText_native_1setOnEditorActionListener + (JNIEnv *, jobject, jlong, jobject); + #ifdef __cplusplus } #endif diff --git a/src/api-impl-jni/util.c b/src/api-impl-jni/util.c index c8d9cfa7..35306b32 100644 --- a/src/api-impl-jni/util.c +++ b/src/api-impl-jni/util.c @@ -136,6 +136,9 @@ void set_up_handle_cache(JNIEnv *env) handle_cache.looper.class = _REF((*env)->FindClass(env, "android/os/Looper")); handle_cache.looper.loop = _STATIC_METHOD(handle_cache.looper.class, "loop", "()V"); handle_cache.looper.prepareMainLooper = _STATIC_METHOD(handle_cache.looper.class, "prepareMainLooper", "()V"); + + handle_cache.key_event.class = _REF((*env)->FindClass(env, "android/view/KeyEvent")); + handle_cache.key_event.constructor = _METHOD(handle_cache.key_event.class, "", "(II)V"); } void extract_from_apk(const char *path, const char *target) { diff --git a/src/api-impl-jni/util.h b/src/api-impl-jni/util.h index ab867cff..39b744ab 100644 --- a/src/api-impl-jni/util.h +++ b/src/api-impl-jni/util.h @@ -91,6 +91,10 @@ struct handle_cache { jmethodID loop; jmethodID prepareMainLooper; } looper; + struct { + jclass class; + jmethodID constructor; + } key_event; }; extern struct handle_cache handle_cache; diff --git a/src/api-impl-jni/widgets/android_widget_EditText.c b/src/api-impl-jni/widgets/android_widget_EditText.c index 3aa8e62c..a9d9516c 100644 --- a/src/api-impl-jni/widgets/android_widget_EditText.c +++ b/src/api-impl-jni/widgets/android_widget_EditText.c @@ -49,3 +49,31 @@ JNIEXPORT void JNICALL Java_android_widget_EditText_native_1addTextChangedListen g_signal_connect(GTK_EDITABLE(entry), "changed", G_CALLBACK(changed_cb), callback_data); } + +#define IME_ACTION_SEARCH 3 +#define KEYCODE_ENTER 66 + +static void on_activate(GtkEntry *entry, struct changed_callback_data *d) +{ + JNIEnv *env = get_jni_env(); + + jobject key_event = (*env)->NewObject(env, handle_cache.key_event.class, handle_cache.key_event.constructor, IME_ACTION_SEARCH, KEYCODE_ENTER); + (*env)->CallBooleanMethod(env, d->listener, d->listener_method, d->this, 0, key_event); + if((*env)->ExceptionCheck(env)) + (*env)->ExceptionDescribe(env); +} + +JNIEXPORT void JNICALL Java_android_widget_EditText_native_1setOnEditorActionListener(JNIEnv *env, jobject this, jlong widget_ptr, jobject listener) +{ + GtkEntry *entry = GTK_ENTRY(_PTR(widget_ptr)); + + if (!listener) + return; + + struct changed_callback_data *callback_data = malloc(sizeof(struct changed_callback_data)); + callback_data->this = _REF(this); + callback_data->listener = _REF(listener); + callback_data->listener_method = _METHOD(_CLASS(listener), "onEditorAction", "(Landroid/widget/TextView;ILandroid/view/KeyEvent;)Z"); + + g_signal_connect(entry, "activate", G_CALLBACK(on_activate), callback_data); +} diff --git a/src/api-impl/android/widget/EditText.java b/src/api-impl/android/widget/EditText.java index 454f902f..5ba6563e 100644 --- a/src/api-impl/android/widget/EditText.java +++ b/src/api-impl/android/widget/EditText.java @@ -19,6 +19,7 @@ public class EditText extends TextView { protected native long native_constructor(Context context, AttributeSet attrs); protected native String native_getText(long widget); protected native void native_addTextChangedListener(long widget, TextWatcher watcher); + protected native void native_setOnEditorActionListener(long widget, OnEditorActionListener l); public Editable getText() { return new SpannableStringBuilder(native_getText(widget)); @@ -37,4 +38,9 @@ public class EditText extends TextView { public void addTextChangedListener(TextWatcher watcher) { native_addTextChangedListener(widget, watcher); } + + @Override + public void setOnEditorActionListener(OnEditorActionListener l) { + native_setOnEditorActionListener(widget, l); + } } diff --git a/src/api-impl/android/widget/TextView.java b/src/api-impl/android/widget/TextView.java index 42a7b055..1668171c 100644 --- a/src/api-impl/android/widget/TextView.java +++ b/src/api-impl/android/widget/TextView.java @@ -16,6 +16,7 @@ import android.text.method.TransformationMethod; import android.text.style.URLSpan; import android.util.AttributeSet; import android.view.Gravity; +import android.view.KeyEvent; import android.view.View; public class TextView extends View { @@ -186,6 +187,7 @@ public class TextView extends View { public CharSequence getHint() {return "HINT";} public static interface OnEditorActionListener { + public abstract boolean onEditorAction(TextView v, int actionId, KeyEvent event); } public static enum BufferType {