implement EditText.setOnEditorActionListener()

This makes the NewPipe search work again
This commit is contained in:
Julian Winkler
2023-11-08 18:00:31 +01:00
parent 1aa1ee64c1
commit bcfb384ba4
6 changed files with 51 additions and 0 deletions

View File

@@ -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

View File

@@ -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, "<init>", "(II)V");
}
void extract_from_apk(const char *path, const char *target) {

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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 {