implement android.app.AlertDialog using GtkDialog

This commit is contained in:
Julian Winkler
2023-08-10 11:06:02 +02:00
committed by Mis012
parent fac8e6e7b5
commit 6547e66d4f
5 changed files with 263 additions and 4 deletions

View File

@@ -2,21 +2,74 @@ 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 {
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) {
nativePtr = nativeInit();
}
public void setTitle(CharSequence title) {
nativeSetTitle(nativePtr, String.valueOf(title));
}
public void setOnCancelListener(OnCancelListener onCancelListener) {}
public void setCancelable(boolean cancelable) {}
public void setMessage(CharSequence message) {
System.out.println("AlertDialog setMessage called with: '" + message + "'");
nativeSetMessage(nativePtr, String.valueOf(message));
}
public void setButton(int whichButton, CharSequence text, OnClickListener listener) {
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 Context context;
private AlertDialog dialog;
public Builder(Context context) {
System.out.println("making an AlertDialog$Builder as we speak, my word!");
this.context = context;
dialog = new AlertDialog(context);
}
public AlertDialog.Builder setPositiveButton(int textId, DialogInterface.OnClickListener listener) {
System.out.println("AlertDialog.Builder setPositiveButton called with textId: '" + textId + "'");
return this;
return setPositiveButton(context.getResources().getText(textId), listener);
}
public AlertDialog.Builder setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) {
System.out.println("AlertDialog.Builder setPositiveButton called with text: '" + text + "'");
dialog.setButton(DialogInterface.BUTTON_POSITIVE, text, listener);
return this;
}
@@ -30,11 +83,17 @@ public class AlertDialog extends Dialog {
public AlertDialog.Builder setTitle(CharSequence title) {
System.out.println("AlertDialog.Builder setTitle called with: '" + title + "'");
dialog.setTitle(title);
return this;
}
public AlertDialog.Builder setTitle(int title) {
return setTitle(context.getResources().getText(title));
}
public AlertDialog.Builder setMessage(CharSequence message) {
System.out.println("AlertDialog.Builder setMessage called with: '" + message + "'");
dialog.setMessage(message);
return this;
}
@@ -42,8 +101,21 @@ public class AlertDialog extends Dialog {
return this;
}
public AlertDialog.Builder setItems(CharSequence[] items, final DialogInterface.OnClickListener listener) {
String[] stringItems = new String[items.length];
for (int i = 0; i < items.length; i++) {
stringItems[i] = String.valueOf(items[i]);
}
dialog.nativeSetItems(dialog.nativePtr, stringItems, listener);
return this;
}
public Builder setOnCancelListener(OnCancelListener onCancelListener) {
return this;
}
public AlertDialog create() {
return new AlertDialog();
return dialog;
}
}
}

View File

@@ -1,10 +1,20 @@
package android.content;
public interface DialogInterface {
/** The identifier for the positive button. */
int BUTTON_POSITIVE = -1;
/** The identifier for the negative button. */
int BUTTON_NEGATIVE = -2;
/** The identifier for the neutral button. */
int BUTTON_NEUTRAL = -3;
public interface OnDismissListener {
}
public interface OnClickListener {
void onClick(DialogInterface dialog, int which);
}
public interface OnShowListener {
}
public interface OnCancelListener {
}
}