refactor Dialog implementation to support custom content

This commit is contained in:
Julian Winkler
2023-08-23 10:32:29 +02:00
parent 029b26beb2
commit 0f7548f189
7 changed files with 106 additions and 73 deletions

View File

@@ -88,6 +88,7 @@ libtranslationlayer_so = shared_library('translation_layer_main', [
'src/api-impl-jni/location/android_location_LocationManager.c',
'src/api-impl-jni/app/android_app_Activity.c',
'src/api-impl-jni/app/android_app_AlertDialog.c',
'src/api-impl-jni/app/android_app_Dialog.c',
] + marshal_files,
install: true,
install_dir : get_option('libdir') / 'java/dex/android_translation_layer/natives',

View File

@@ -4,21 +4,6 @@
#include "../defines.h"
#include "../generated_headers/android_app_AlertDialog.h"
JNIEXPORT jlong JNICALL Java_android_app_AlertDialog_nativeInit(JNIEnv *env, jobject this)
{
GtkWidget *dialog = gtk_dialog_new();
g_signal_connect_swapped(dialog, "response", G_CALLBACK(gtk_window_destroy), dialog);
return _INTPTR(dialog);
}
JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeSetTitle(JNIEnv *env, jobject this, jlong ptr, jstring title)
{
GtkWindow *dialog = GTK_WINDOW(_PTR(ptr));
const char* nativeTitle = (*env)->GetStringUTFChars(env, title, NULL);
gtk_window_set_title(dialog, nativeTitle);
(*env)->ReleaseStringUTFChars(env, title, nativeTitle);
}
JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeSetMessage(JNIEnv *env, jobject this, jlong ptr, jstring message)
{
GtkDialog *dialog = GTK_DIALOG(_PTR(ptr));
@@ -107,9 +92,3 @@ JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeSetItems(JNIEnv *env,
g_signal_connect(list, "activate", G_CALLBACK(activate_cb), callback_data);
}
JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeShow(JNIEnv *env, jobject this, jlong ptr)
{
GtkWindow *dialog = GTK_WINDOW(_PTR(ptr));
gtk_window_present(dialog);
}

View File

@@ -0,0 +1,34 @@
#include <gtk/gtk.h>
#include <jni.h>
#include "../defines.h"
#include "../generated_headers/android_app_Dialog.h"
JNIEXPORT jlong JNICALL Java_android_app_Dialog_nativeInit(JNIEnv *env, jobject this)
{
GtkWidget *dialog = gtk_dialog_new();
g_signal_connect_swapped(dialog, "response", G_CALLBACK(gtk_window_destroy), dialog);
return _INTPTR(dialog);
}
JNIEXPORT void JNICALL Java_android_app_Dialog_nativeSetTitle(JNIEnv *env, jobject this, jlong ptr, jstring title)
{
GtkWindow *dialog = GTK_WINDOW(_PTR(ptr));
const char* nativeTitle = (*env)->GetStringUTFChars(env, title, NULL);
gtk_window_set_title(dialog, nativeTitle);
(*env)->ReleaseStringUTFChars(env, title, nativeTitle);
}
JNIEXPORT void JNICALL Java_android_app_Dialog_nativeSetContentView(JNIEnv *env, jobject this, jlong ptr, jlong widget_ptr) {
GtkDialog *dialog = GTK_DIALOG(_PTR(ptr));
GtkWidget *widget = GTK_WIDGET(_PTR(widget_ptr));
GtkWidget *content_area = gtk_dialog_get_content_area(dialog);
gtk_box_append(GTK_BOX(content_area), gtk_widget_get_parent(widget));
}
JNIEXPORT void JNICALL Java_android_app_Dialog_nativeShow(JNIEnv *env, jobject this, jlong ptr)
{
GtkWindow *dialog = GTK_WINDOW(_PTR(ptr));
gtk_window_present(dialog);
}

View File

@@ -7,22 +7,6 @@
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: android_app_AlertDialog
* Method: nativeInit
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_android_app_AlertDialog_nativeInit
(JNIEnv *, jobject);
/*
* Class: android_app_AlertDialog
* Method: nativeSetTitle
* Signature: (JLjava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeSetTitle
(JNIEnv *, jobject, jlong, jstring);
/*
* Class: android_app_AlertDialog
* Method: nativeSetMessage
@@ -47,14 +31,6 @@ JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeSetButton
JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeSetItems
(JNIEnv *, jobject, jlong, jobjectArray, jobject);
/*
* Class: android_app_AlertDialog
* Method: nativeShow
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_android_app_AlertDialog_nativeShow
(JNIEnv *, jobject, jlong);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,45 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class android_app_Dialog */
#ifndef _Included_android_app_Dialog
#define _Included_android_app_Dialog
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: android_app_Dialog
* Method: nativeInit
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_android_app_Dialog_nativeInit
(JNIEnv *, jobject);
/*
* Class: android_app_Dialog
* Method: nativeSetTitle
* Signature: (JLjava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_android_app_Dialog_nativeSetTitle
(JNIEnv *, jobject, jlong, jstring);
/*
* Class: android_app_Dialog
* Method: nativeSetContentView
* Signature: (JJ)V
*/
JNIEXPORT void JNICALL Java_android_app_Dialog_nativeSetContentView
(JNIEnv *, jobject, jlong, jlong);
/*
* Class: android_app_Dialog
* Method: nativeShow
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_android_app_Dialog_nativeShow
(JNIEnv *, jobject, jlong);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -2,28 +2,16 @@ package android.app;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
public class AlertDialog extends Dialog implements DialogInterface {
private long nativePtr;
private native long nativeInit();
private native void nativeSetTitle(long ptr, String title);
private native void nativeSetMessage(long ptr, String message);
private native void nativeSetButton(long ptr, int whichButton, String text);
private native void nativeSetItems(long ptr, String[] items, DialogInterface.OnClickListener listener);
private native void nativeShow(long ptr);
public AlertDialog(Context context) {
super(context, 0);
nativePtr = nativeInit();
}
public void setTitle(CharSequence title) {
nativeSetTitle(nativePtr, String.valueOf(title));
}
public void setMessage(CharSequence message) {
@@ -35,21 +23,6 @@ public class AlertDialog extends Dialog implements DialogInterface {
nativeSetButton(nativePtr, whichButton, String.valueOf(text));
}
public boolean isShowing() {
return false;
}
@Override
public void show() {
super.show();
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
nativeShow(nativePtr);
}
});
}
public static class Builder {
private AlertDialog dialog;

View File

@@ -3,14 +3,23 @@ package android.app;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
public class Dialog {
protected long nativePtr;
private native long nativeInit();
private native void nativeSetTitle(long ptr, String title);
private native void nativeSetContentView(long ptr, long widget);
private native void nativeShow(long ptr);
private Context context;
public Dialog(Context context, int themeResId) {
this.context = context;
nativePtr = nativeInit();
}
public final boolean requestWindowFeature(int featureId) {
@@ -21,7 +30,13 @@ public class Dialog {
return context;
}
public void setContentView(View view) {}
public void setContentView(View view) {
nativeSetContentView(nativePtr, view.widget);
}
public void setTitle(CharSequence title) {
nativeSetTitle(nativePtr, String.valueOf(title));
}
public void setOwnerActivity(Activity activity) {}
@@ -33,6 +48,16 @@ public class Dialog {
public void show() {
System.out.println("totally showing the Dialog " + this + " right now, most definitely doing that");
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
nativeShow(nativePtr);
}
});
}
public boolean isShowing() {
return false;
}
public void dismiss() {