Fixed bug 2415 - Message Boxes aren't implemented on Android
authorSam Lantinga <slouken@libsdl.org>
Sat, 13 Sep 2014 02:15:18 -0700
changeset 91356cd8e6b54f4d
parent 9134 dbd8fe54ebb9
child 9136 64fcdfcd5bca
Fixed bug 2415 - Message Boxes aren't implemented on Android

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.
android-project/src/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.c
src/video/SDL_video.c
src/video/android/SDL_androidmessagebox.c
src/video/android/SDL_androidmessagebox.h
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Fri Sep 12 15:09:33 2014 -0700
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Sat Sep 13 02:15:18 2014 -0700
     1.3 @@ -17,8 +17,12 @@
     1.4  import android.view.inputmethod.InputConnection;
     1.5  import android.view.inputmethod.InputMethodManager;
     1.6  import android.widget.AbsoluteLayout;
     1.7 +import android.widget.Button;
     1.8 +import android.widget.LinearLayout;
     1.9 +import android.widget.TextView;
    1.10  import android.os.*;
    1.11  import android.util.Log;
    1.12 +import android.util.SparseArray;
    1.13  import android.graphics.*;
    1.14  import android.media.*;
    1.15  import android.hardware.*;
    1.16 @@ -583,6 +587,199 @@
    1.17  
    1.18          return fileStream;
    1.19      }
    1.20 +
    1.21 +    // Messagebox
    1.22 +
    1.23 +    /** Result of current messagebox. Also used for blocking the calling thread. */
    1.24 +    protected final int[] messageboxSelection = new int[1];
    1.25 +
    1.26 +    /** Id of current dialog. */
    1.27 +    protected int dialogs = 0;
    1.28 +
    1.29 +    /**
    1.30 +     * This method is called by SDL using JNI.
    1.31 +     * Shows the messagebox from UI thread and block calling thread.
    1.32 +     * buttonFlags, buttonIds and buttonTexts must have same length.
    1.33 +     * @param buttonFlags array containing flags for every button.
    1.34 +     * @param buttonIds array containing id for every button.
    1.35 +     * @param buttonTexts array containing text for every button.
    1.36 +     * @param colors null for default or array of length 5 containing colors.
    1.37 +     * @return button id or -1.
    1.38 +     */
    1.39 +    public int messageboxShowMessageBox(
    1.40 +            final int flags,
    1.41 +            final String title,
    1.42 +            final String message,
    1.43 +            final int[] buttonFlags,
    1.44 +            final int[] buttonIds,
    1.45 +            final String[] buttonTexts,
    1.46 +            final int[] colors) {
    1.47 +
    1.48 +        messageboxSelection[0] = -1;
    1.49 +
    1.50 +        // sanity checks
    1.51 +
    1.52 +        if ((buttonFlags.length != buttonIds.length) && (buttonIds.length != buttonTexts.length)) {
    1.53 +            return -1; // implementation broken
    1.54 +        }
    1.55 +
    1.56 +        // collect arguments for Dialog
    1.57 +
    1.58 +        final Bundle args = new Bundle();
    1.59 +        args.putInt("flags", flags);
    1.60 +        args.putString("title", title);
    1.61 +        args.putString("message", message);
    1.62 +        args.putIntArray("buttonFlags", buttonFlags);
    1.63 +        args.putIntArray("buttonIds", buttonIds);
    1.64 +        args.putStringArray("buttonTexts", buttonTexts);
    1.65 +        args.putIntArray("colors", colors);
    1.66 +
    1.67 +        // trigger Dialog creation on UI thread
    1.68 +
    1.69 +        runOnUiThread(new Runnable() {
    1.70 +            @Override
    1.71 +            public void run() {
    1.72 +                showDialog(dialogs++, args);
    1.73 +            }
    1.74 +        });
    1.75 +
    1.76 +        // block the calling thread
    1.77 +
    1.78 +        synchronized (messageboxSelection) {
    1.79 +            try {
    1.80 +                messageboxSelection.wait();
    1.81 +            } catch (InterruptedException ex) {
    1.82 +                ex.printStackTrace();
    1.83 +                return -1;
    1.84 +            }
    1.85 +        }
    1.86 +
    1.87 +        // return selected value
    1.88 +
    1.89 +        return messageboxSelection[0];
    1.90 +    }
    1.91 +
    1.92 +    @Override
    1.93 +    protected Dialog onCreateDialog(int ignore, Bundle args) {
    1.94 +
    1.95 +        // TODO set values from "flags" to messagebox dialog
    1.96 +
    1.97 +        // get colors
    1.98 +
    1.99 +        int[] colors = args.getIntArray("colors");
   1.100 +        int backgroundColor;
   1.101 +        int textColor;
   1.102 +        int buttonBorderColor;
   1.103 +        int buttonBackgroundColor;
   1.104 +        int buttonSelectedColor;
   1.105 +        if (colors != null) {
   1.106 +            int i = -1;
   1.107 +            backgroundColor = colors[++i];
   1.108 +            textColor = colors[++i];
   1.109 +            buttonBorderColor = colors[++i];
   1.110 +            buttonBackgroundColor = colors[++i];
   1.111 +            buttonSelectedColor = colors[++i];
   1.112 +        } else {
   1.113 +            backgroundColor = Color.TRANSPARENT;
   1.114 +            textColor = Color.TRANSPARENT;
   1.115 +            buttonBorderColor = Color.TRANSPARENT;
   1.116 +            buttonBackgroundColor = Color.TRANSPARENT;
   1.117 +            buttonSelectedColor = Color.TRANSPARENT;
   1.118 +        }
   1.119 +
   1.120 +        // create dialog with title and a listener to wake up calling thread
   1.121 +
   1.122 +        final Dialog dialog = new Dialog(this);
   1.123 +        dialog.setTitle(args.getString("title"));
   1.124 +        dialog.setCancelable(false);
   1.125 +        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
   1.126 +            @Override
   1.127 +            public void onDismiss(DialogInterface unused) {
   1.128 +                synchronized (messageboxSelection) {
   1.129 +                    messageboxSelection.notify();
   1.130 +                }
   1.131 +            }
   1.132 +        });
   1.133 +
   1.134 +        // create text
   1.135 +
   1.136 +        TextView message = new TextView(this);
   1.137 +        message.setGravity(Gravity.CENTER);
   1.138 +        message.setText(args.getString("message"));
   1.139 +        if (textColor != Color.TRANSPARENT) {
   1.140 +            message.setTextColor(textColor);
   1.141 +        }
   1.142 +
   1.143 +        // create buttons
   1.144 +
   1.145 +        int[] buttonFlags = args.getIntArray("buttonFlags");
   1.146 +        int[] buttonIds = args.getIntArray("buttonIds");
   1.147 +        String[] buttonTexts = args.getStringArray("buttonTexts");
   1.148 +
   1.149 +        final SparseArray<Button> mapping = new SparseArray<Button>();
   1.150 +
   1.151 +        LinearLayout buttons = new LinearLayout(this);
   1.152 +        buttons.setOrientation(LinearLayout.HORIZONTAL);
   1.153 +        buttons.setGravity(Gravity.CENTER);
   1.154 +        for (int i = 0; i < buttonTexts.length; ++i) {
   1.155 +            Button button = new Button(this);
   1.156 +            final int id = buttonIds[i];
   1.157 +            button.setOnClickListener(new View.OnClickListener() {
   1.158 +                @Override
   1.159 +                public void onClick(View v) {
   1.160 +                    messageboxSelection[0] = id;
   1.161 +                    dialog.dismiss();
   1.162 +                }
   1.163 +            });
   1.164 +            if (buttonFlags[i] != 0) {
   1.165 +                // see SDL_messagebox.h
   1.166 +                if ((buttonFlags[i] & 0x00000001) != 0) {
   1.167 +                    mapping.put(KeyEvent.KEYCODE_ENTER, button);
   1.168 +                }
   1.169 +                if ((buttonFlags[i] & 0x00000002) != 0) {
   1.170 +                    mapping.put(111, button); /* API 11: KeyEvent.KEYCODE_ESCAPE */
   1.171 +                }
   1.172 +            }
   1.173 +            button.setText(buttonTexts[i]);
   1.174 +            if (buttonBorderColor != Color.TRANSPARENT) {
   1.175 +                // TODO set color for border of messagebox button
   1.176 +            }
   1.177 +            if (buttonBackgroundColor != Color.TRANSPARENT) {
   1.178 +                button.setBackgroundColor(buttonBackgroundColor);
   1.179 +            }
   1.180 +            if (buttonSelectedColor != Color.TRANSPARENT) {
   1.181 +                // TODO set color for selected messagebox button
   1.182 +            }
   1.183 +            buttons.addView(button);
   1.184 +        }
   1.185 +
   1.186 +        // create content
   1.187 +
   1.188 +        LinearLayout content = new LinearLayout(this);
   1.189 +        content.setOrientation(LinearLayout.VERTICAL);
   1.190 +        content.addView(message);
   1.191 +        content.addView(buttons);
   1.192 +        if (backgroundColor != Color.TRANSPARENT) {
   1.193 +            content.setBackgroundColor(backgroundColor);
   1.194 +        }
   1.195 +
   1.196 +        // add content to dialog and return
   1.197 +
   1.198 +        dialog.setContentView(content);
   1.199 +        dialog.setOnKeyListener(new Dialog.OnKeyListener() {
   1.200 +            @Override
   1.201 +            public boolean onKey(DialogInterface d, int keyCode, KeyEvent event) {
   1.202 +                Button button = mapping.get(keyCode);
   1.203 +                if (button != null) {
   1.204 +                    button.performClick();
   1.205 +                    return true;
   1.206 +                }
   1.207 +                return false;
   1.208 +            }
   1.209 +        });
   1.210 +
   1.211 +        return dialog;
   1.212 +    }
   1.213  }
   1.214  
   1.215  /**
     2.1 --- a/src/core/android/SDL_android.c	Fri Sep 12 15:09:33 2014 -0700
     2.2 +++ b/src/core/android/SDL_android.c	Sat Sep 13 02:15:18 2014 -0700
     2.3 @@ -1342,6 +1342,85 @@
     2.4      Android_JNI_SendMessage(COMMAND_TEXTEDIT_HIDE, 0);
     2.5  }
     2.6  
     2.7 +int Android_JNI_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
     2.8 +{
     2.9 +    JNIEnv *env;
    2.10 +    jmethodID mid;
    2.11 +    jobject context;
    2.12 +    jstring title;
    2.13 +    jstring message;
    2.14 +    jintArray button_flags;
    2.15 +    jintArray button_ids;
    2.16 +    jobjectArray button_texts;
    2.17 +    jintArray colors;
    2.18 +    jint temp;
    2.19 +    int i;
    2.20 +
    2.21 +    env = Android_JNI_GetEnv();
    2.22 +
    2.23 +    /* convert parameters */
    2.24 +
    2.25 +    title = (*env)->NewStringUTF(env, messageboxdata->title);
    2.26 +    message = (*env)->NewStringUTF(env, messageboxdata->message);
    2.27 +
    2.28 +    button_flags = (*env)->NewIntArray(env, messageboxdata->numbuttons);
    2.29 +    button_ids = (*env)->NewIntArray(env, messageboxdata->numbuttons);
    2.30 +    button_texts = (*env)->NewObjectArray(env, messageboxdata->numbuttons,
    2.31 +        (*env)->FindClass(env, "java/lang/String"), NULL);
    2.32 +    for (i = 0; i < messageboxdata->numbuttons; ++i) {
    2.33 +        temp = messageboxdata->buttons[i].flags;
    2.34 +        (*env)->SetIntArrayRegion(env, button_flags, i, 1, &temp);
    2.35 +        temp = messageboxdata->buttons[i].buttonid;
    2.36 +        (*env)->SetIntArrayRegion(env, button_ids, i, 1, &temp);
    2.37 +        (*env)->SetObjectArrayElement(env, button_texts, i, (*env)->NewStringUTF(env, messageboxdata->buttons[i].text));
    2.38 +    }
    2.39 +
    2.40 +    if (messageboxdata->colorScheme) {
    2.41 +        colors = (*env)->NewIntArray(env, SDL_MESSAGEBOX_COLOR_MAX);
    2.42 +        for (i = 0; i < SDL_MESSAGEBOX_COLOR_MAX; ++i) {
    2.43 +            temp = (0xFF << 24) |
    2.44 +                   (messageboxdata->colorScheme->colors[i].r << 16) |
    2.45 +                   (messageboxdata->colorScheme->colors[i].g << 8) |
    2.46 +                   (messageboxdata->colorScheme->colors[i].b << 0);
    2.47 +            (*env)->SetIntArrayRegion(env, colors, i, 1, &temp);
    2.48 +        }
    2.49 +    } else {
    2.50 +        colors = NULL;
    2.51 +    }
    2.52 +
    2.53 +    /* call function */
    2.54 +
    2.55 +    mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext","()Landroid/content/Context;");
    2.56 +
    2.57 +    context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
    2.58 +
    2.59 +    mid = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, context),
    2.60 +        "messageboxShowMessageBox", "(ILjava/lang/String;Ljava/lang/String;[I[I[Ljava/lang/String;[I)I");
    2.61 +    *buttonid = (*env)->CallIntMethod(env, context, mid,
    2.62 +        messageboxdata->flags,
    2.63 +        title,
    2.64 +        message,
    2.65 +        button_flags,
    2.66 +        button_ids,
    2.67 +        button_texts,
    2.68 +        colors);
    2.69 +
    2.70 +    /* delete parameters */
    2.71 +
    2.72 +    (*env)->DeleteLocalRef(env, title);
    2.73 +    (*env)->DeleteLocalRef(env, message);
    2.74 +    (*env)->DeleteLocalRef(env, button_flags);
    2.75 +    (*env)->DeleteLocalRef(env, button_ids);
    2.76 +    for (i = 0; i < messageboxdata->numbuttons; ++i) {
    2.77 +        (*env)->DeleteLocalRef(env, (*env)->GetObjectArrayElement(env, button_texts, i));
    2.78 +        (*env)->SetObjectArrayElement(env, button_texts, i, NULL);
    2.79 +    }
    2.80 +    (*env)->DeleteLocalRef(env, button_texts);
    2.81 +    (*env)->DeleteLocalRef(env, colors);
    2.82 +
    2.83 +    return 0;
    2.84 +}
    2.85 +
    2.86  /*
    2.87  //////////////////////////////////////////////////////////////////////////////
    2.88  //
     3.1 --- a/src/video/SDL_video.c	Fri Sep 12 15:09:33 2014 -0700
     3.2 +++ b/src/video/SDL_video.c	Sat Sep 13 02:15:18 2014 -0700
     3.3 @@ -3265,6 +3265,9 @@
     3.4      return SDL_FALSE;
     3.5  }
     3.6  
     3.7 +#if SDL_VIDEO_DRIVER_ANDROID
     3.8 +#include "android/SDL_androidmessagebox.h"
     3.9 +#endif
    3.10  #if SDL_VIDEO_DRIVER_WINDOWS
    3.11  #include "windows/SDL_windowsmessagebox.h"
    3.12  #endif
    3.13 @@ -3329,6 +3332,12 @@
    3.14      }
    3.15  
    3.16      /* It's completely fine to call this function before video is initialized */
    3.17 +#if SDL_VIDEO_DRIVER_ANDROID
    3.18 +    if (retval == -1 &&
    3.19 +        Android_ShowMessageBox(messageboxdata, buttonid) == 0) {
    3.20 +        retval = 0;
    3.21 +    }
    3.22 +#endif
    3.23  #if SDL_VIDEO_DRIVER_WINDOWS
    3.24      if (retval == -1 &&
    3.25          SDL_MessageboxValidForDriver(messageboxdata, SDL_SYSWM_WINDOWS) &&
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/video/android/SDL_androidmessagebox.c	Sat Sep 13 02:15:18 2014 -0700
     4.3 @@ -0,0 +1,37 @@
     4.4 +/*
     4.5 +  Simple DirectMedia Layer
     4.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     4.7 +
     4.8 +  This software is provided 'as-is', without any express or implied
     4.9 +  warranty.  In no event will the authors be held liable for any damages
    4.10 +  arising from the use of this software.
    4.11 +
    4.12 +  Permission is granted to anyone to use this software for any purpose,
    4.13 +  including commercial applications, and to alter it and redistribute it
    4.14 +  freely, subject to the following restrictions:
    4.15 +
    4.16 +  1. The origin of this software must not be misrepresented; you must not
    4.17 +     claim that you wrote the original software. If you use this software
    4.18 +     in a product, an acknowledgment in the product documentation would be
    4.19 +     appreciated but is not required.
    4.20 +  2. Altered source versions must be plainly marked as such, and must not be
    4.21 +     misrepresented as being the original software.
    4.22 +  3. This notice may not be removed or altered from any source distribution.
    4.23 +*/
    4.24 +#include "SDL_config.h"
    4.25 +
    4.26 +#if SDL_VIDEO_DRIVER_ANDROID
    4.27 +
    4.28 +#include "SDL_messagebox.h"
    4.29 +
    4.30 +int
    4.31 +Android_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
    4.32 +{
    4.33 +    int Android_JNI_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid);
    4.34 +
    4.35 +    return Android_JNI_ShowMessageBox(messageboxdata, buttonid);
    4.36 +}
    4.37 +
    4.38 +#endif /* SDL_VIDEO_DRIVER_ANDROID */
    4.39 +
    4.40 +/* vi: set ts=4 sw=4 expandtab: */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/video/android/SDL_androidmessagebox.h	Sat Sep 13 02:15:18 2014 -0700
     5.3 @@ -0,0 +1,29 @@
     5.4 +/*
     5.5 +  Simple DirectMedia Layer
     5.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     5.7 +
     5.8 +  This software is provided 'as-is', without any express or implied
     5.9 +  warranty.  In no event will the authors be held liable for any damages
    5.10 +  arising from the use of this software.
    5.11 +
    5.12 +  Permission is granted to anyone to use this software for any purpose,
    5.13 +  including commercial applications, and to alter it and redistribute it
    5.14 +  freely, subject to the following restrictions:
    5.15 +
    5.16 +  1. The origin of this software must not be misrepresented; you must not
    5.17 +     claim that you wrote the original software. If you use this software
    5.18 +     in a product, an acknowledgment in the product documentation would be
    5.19 +     appreciated but is not required.
    5.20 +  2. Altered source versions must be plainly marked as such, and must not be
    5.21 +     misrepresented as being the original software.
    5.22 +  3. This notice may not be removed or altered from any source distribution.
    5.23 +*/
    5.24 +#include "../../SDL_internal.h"
    5.25 +
    5.26 +#if SDL_VIDEO_DRIVER_ANDROID
    5.27 +
    5.28 +extern int Android_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid);
    5.29 +
    5.30 +#endif /* SDL_VIDEO_DRIVER_ANDROID */
    5.31 +
    5.32 +/* vi: set ts=4 sw=4 expandtab: */