Skip to content

Commit

Permalink
Fixed bug 2415 - Message Boxes aren't implemented on Android
Browse files Browse the repository at this point in the history
Philipp Wiesemann

I attached a patch for an incomplete implementation of the messagebox parts.

It was not tested on lots of devices yet and features a very fragile workaround to block the calling SDL thread while the dialog is handled on Android's UI thread. Although it works for testmessage.c I assume there are lot of situations were it may fail (standby, device rotation and other changes). Also not all flags and colors are implemented.

On the other hand most uses of the messagebox are to show an error on start and fragility (or working at all) may not matter there.
  • Loading branch information
slouken committed Sep 13, 2014
1 parent 2bfb3c3 commit 5f39ea8
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 0 deletions.
197 changes: 197 additions & 0 deletions android-project/src/org/libsdl/app/SDLActivity.java
Expand Up @@ -17,8 +17,12 @@
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.os.*;
import android.util.Log;
import android.util.SparseArray;
import android.graphics.*;
import android.media.*;
import android.hardware.*;
Expand Down Expand Up @@ -583,6 +587,199 @@ public InputStream openAPKExtensionInputStream(String fileName) throws IOExcepti

return fileStream;
}

// Messagebox

/** Result of current messagebox. Also used for blocking the calling thread. */
protected final int[] messageboxSelection = new int[1];

/** Id of current dialog. */
protected int dialogs = 0;

/**
* This method is called by SDL using JNI.
* Shows the messagebox from UI thread and block calling thread.
* buttonFlags, buttonIds and buttonTexts must have same length.
* @param buttonFlags array containing flags for every button.
* @param buttonIds array containing id for every button.
* @param buttonTexts array containing text for every button.
* @param colors null for default or array of length 5 containing colors.
* @return button id or -1.
*/
public int messageboxShowMessageBox(
final int flags,
final String title,
final String message,
final int[] buttonFlags,
final int[] buttonIds,
final String[] buttonTexts,
final int[] colors) {

messageboxSelection[0] = -1;

// sanity checks

if ((buttonFlags.length != buttonIds.length) && (buttonIds.length != buttonTexts.length)) {
return -1; // implementation broken
}

// collect arguments for Dialog

final Bundle args = new Bundle();
args.putInt("flags", flags);
args.putString("title", title);
args.putString("message", message);
args.putIntArray("buttonFlags", buttonFlags);
args.putIntArray("buttonIds", buttonIds);
args.putStringArray("buttonTexts", buttonTexts);
args.putIntArray("colors", colors);

// trigger Dialog creation on UI thread

runOnUiThread(new Runnable() {
@Override
public void run() {
showDialog(dialogs++, args);
}
});

// block the calling thread

synchronized (messageboxSelection) {
try {
messageboxSelection.wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
return -1;
}
}

// return selected value

return messageboxSelection[0];
}

@Override
protected Dialog onCreateDialog(int ignore, Bundle args) {

// TODO set values from "flags" to messagebox dialog

// get colors

int[] colors = args.getIntArray("colors");
int backgroundColor;
int textColor;
int buttonBorderColor;
int buttonBackgroundColor;
int buttonSelectedColor;
if (colors != null) {
int i = -1;
backgroundColor = colors[++i];
textColor = colors[++i];
buttonBorderColor = colors[++i];
buttonBackgroundColor = colors[++i];
buttonSelectedColor = colors[++i];
} else {
backgroundColor = Color.TRANSPARENT;
textColor = Color.TRANSPARENT;
buttonBorderColor = Color.TRANSPARENT;
buttonBackgroundColor = Color.TRANSPARENT;
buttonSelectedColor = Color.TRANSPARENT;
}

// create dialog with title and a listener to wake up calling thread

final Dialog dialog = new Dialog(this);
dialog.setTitle(args.getString("title"));
dialog.setCancelable(false);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface unused) {
synchronized (messageboxSelection) {
messageboxSelection.notify();
}
}
});

// create text

TextView message = new TextView(this);
message.setGravity(Gravity.CENTER);
message.setText(args.getString("message"));
if (textColor != Color.TRANSPARENT) {
message.setTextColor(textColor);
}

// create buttons

int[] buttonFlags = args.getIntArray("buttonFlags");
int[] buttonIds = args.getIntArray("buttonIds");
String[] buttonTexts = args.getStringArray("buttonTexts");

final SparseArray<Button> mapping = new SparseArray<Button>();

LinearLayout buttons = new LinearLayout(this);
buttons.setOrientation(LinearLayout.HORIZONTAL);
buttons.setGravity(Gravity.CENTER);
for (int i = 0; i < buttonTexts.length; ++i) {
Button button = new Button(this);
final int id = buttonIds[i];
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
messageboxSelection[0] = id;
dialog.dismiss();
}
});
if (buttonFlags[i] != 0) {
// see SDL_messagebox.h
if ((buttonFlags[i] & 0x00000001) != 0) {
mapping.put(KeyEvent.KEYCODE_ENTER, button);
}
if ((buttonFlags[i] & 0x00000002) != 0) {
mapping.put(111, button); /* API 11: KeyEvent.KEYCODE_ESCAPE */
}
}
button.setText(buttonTexts[i]);
if (buttonBorderColor != Color.TRANSPARENT) {
// TODO set color for border of messagebox button
}
if (buttonBackgroundColor != Color.TRANSPARENT) {
button.setBackgroundColor(buttonBackgroundColor);
}
if (buttonSelectedColor != Color.TRANSPARENT) {
// TODO set color for selected messagebox button
}
buttons.addView(button);
}

// create content

LinearLayout content = new LinearLayout(this);
content.setOrientation(LinearLayout.VERTICAL);
content.addView(message);
content.addView(buttons);
if (backgroundColor != Color.TRANSPARENT) {
content.setBackgroundColor(backgroundColor);
}

// add content to dialog and return

dialog.setContentView(content);
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface d, int keyCode, KeyEvent event) {
Button button = mapping.get(keyCode);
if (button != null) {
button.performClick();
return true;
}
return false;
}
});

return dialog;
}
}

/**
Expand Down
79 changes: 79 additions & 0 deletions src/core/android/SDL_android.c
Expand Up @@ -1342,6 +1342,85 @@ void Android_JNI_HideTextInput()
Android_JNI_SendMessage(COMMAND_TEXTEDIT_HIDE, 0);
}

int Android_JNI_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
{
JNIEnv *env;
jmethodID mid;
jobject context;
jstring title;
jstring message;
jintArray button_flags;
jintArray button_ids;
jobjectArray button_texts;
jintArray colors;
jint temp;
int i;

env = Android_JNI_GetEnv();

/* convert parameters */

title = (*env)->NewStringUTF(env, messageboxdata->title);
message = (*env)->NewStringUTF(env, messageboxdata->message);

button_flags = (*env)->NewIntArray(env, messageboxdata->numbuttons);
button_ids = (*env)->NewIntArray(env, messageboxdata->numbuttons);
button_texts = (*env)->NewObjectArray(env, messageboxdata->numbuttons,
(*env)->FindClass(env, "java/lang/String"), NULL);
for (i = 0; i < messageboxdata->numbuttons; ++i) {
temp = messageboxdata->buttons[i].flags;
(*env)->SetIntArrayRegion(env, button_flags, i, 1, &temp);
temp = messageboxdata->buttons[i].buttonid;
(*env)->SetIntArrayRegion(env, button_ids, i, 1, &temp);
(*env)->SetObjectArrayElement(env, button_texts, i, (*env)->NewStringUTF(env, messageboxdata->buttons[i].text));
}

if (messageboxdata->colorScheme) {
colors = (*env)->NewIntArray(env, SDL_MESSAGEBOX_COLOR_MAX);
for (i = 0; i < SDL_MESSAGEBOX_COLOR_MAX; ++i) {
temp = (0xFF << 24) |
(messageboxdata->colorScheme->colors[i].r << 16) |
(messageboxdata->colorScheme->colors[i].g << 8) |
(messageboxdata->colorScheme->colors[i].b << 0);
(*env)->SetIntArrayRegion(env, colors, i, 1, &temp);
}
} else {
colors = NULL;
}

/* call function */

mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext","()Landroid/content/Context;");

context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);

mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, context),
"messageboxShowMessageBox", "(ILjava/lang/String;Ljava/lang/String;[I[I[Ljava/lang/String;[I)I");
*buttonid = (*env)->CallIntMethod(env, context, mid,
messageboxdata->flags,
title,
message,
button_flags,
button_ids,
button_texts,
colors);

/* delete parameters */

(*env)->DeleteLocalRef(env, title);
(*env)->DeleteLocalRef(env, message);
(*env)->DeleteLocalRef(env, button_flags);
(*env)->DeleteLocalRef(env, button_ids);
for (i = 0; i < messageboxdata->numbuttons; ++i) {
(*env)->DeleteLocalRef(env, (*env)->GetObjectArrayElement(env, button_texts, i));
(*env)->SetObjectArrayElement(env, button_texts, i, NULL);
}
(*env)->DeleteLocalRef(env, button_texts);
(*env)->DeleteLocalRef(env, colors);

return 0;
}

/*
//////////////////////////////////////////////////////////////////////////////
//
Expand Down
9 changes: 9 additions & 0 deletions src/video/SDL_video.c
Expand Up @@ -3265,6 +3265,9 @@ SDL_IsScreenKeyboardShown(SDL_Window *window)
return SDL_FALSE;
}

#if SDL_VIDEO_DRIVER_ANDROID
#include "android/SDL_androidmessagebox.h"
#endif
#if SDL_VIDEO_DRIVER_WINDOWS
#include "windows/SDL_windowsmessagebox.h"
#endif
Expand Down Expand Up @@ -3329,6 +3332,12 @@ SDL_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
}

/* It's completely fine to call this function before video is initialized */
#if SDL_VIDEO_DRIVER_ANDROID
if (retval == -1 &&
Android_ShowMessageBox(messageboxdata, buttonid) == 0) {
retval = 0;
}
#endif
#if SDL_VIDEO_DRIVER_WINDOWS
if (retval == -1 &&
SDL_MessageboxValidForDriver(messageboxdata, SDL_SYSWM_WINDOWS) &&
Expand Down
37 changes: 37 additions & 0 deletions src/video/android/SDL_androidmessagebox.c
@@ -0,0 +1,37 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"

#if SDL_VIDEO_DRIVER_ANDROID

#include "SDL_messagebox.h"

int
Android_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
{
int Android_JNI_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid);

return Android_JNI_ShowMessageBox(messageboxdata, buttonid);
}

#endif /* SDL_VIDEO_DRIVER_ANDROID */

/* vi: set ts=4 sw=4 expandtab: */
29 changes: 29 additions & 0 deletions src/video/android/SDL_androidmessagebox.h
@@ -0,0 +1,29 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"

#if SDL_VIDEO_DRIVER_ANDROID

extern int Android_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid);

#endif /* SDL_VIDEO_DRIVER_ANDROID */

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit 5f39ea8

Please sign in to comment.