add SDL_AndroidShowToast for https://developer.android.com/reference/android/widget/Toast
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
index e7e5a2a..ff8ed76 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
@@ -52,6 +52,7 @@ import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
+import android.widget.Toast;
import java.util.Hashtable;
import java.util.Locale;
@@ -1630,6 +1631,52 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
}
return 0;
}
+
+ /**
+ * This method is called by SDL using JNI.
+ */
+ public static int showToast(String message, int duration, int gravity, int xOffset, int yOffset)
+ {
+ if(null == mSingleton) {
+ return - 1;
+ }
+
+ try
+ {
+ class OneShotTask implements Runnable {
+ String mMessage;
+ int mDuration;
+ int mGravity;
+ int mXOffset;
+ int mYOffset;
+
+ OneShotTask(String message, int duration, int gravity, int xOffset, int yOffset) {
+ mMessage = message;
+ mDuration = duration;
+ mGravity = gravity;
+ mXOffset = xOffset;
+ mYOffset = yOffset;
+ }
+
+ public void run() {
+ try
+ {
+ Toast toast = Toast.makeText(mSingleton, mMessage, mDuration);
+ if (mGravity >= 0) {
+ toast.setGravity(mGravity, mXOffset, mYOffset);
+ }
+ toast.show();
+ } catch(Exception ex) {
+ Log.e(TAG, ex.getMessage());
+ }
+ }
+ }
+ mSingleton.runOnUiThread(new OneShotTask(message, duration, gravity, xOffset, yOffset));
+ } catch(Exception ex) {
+ return -1;
+ }
+ return 0;
+ }
}
/**
diff --git a/include/SDL_system.h b/include/SDL_system.h
index 06ac41c..f013441 100644
--- a/include/SDL_system.h
+++ b/include/SDL_system.h
@@ -215,6 +215,22 @@ extern DECLSPEC const char * SDLCALL SDL_AndroidGetExternalStoragePath(void);
*/
extern DECLSPEC SDL_bool SDLCALL SDL_AndroidRequestPermission(const char *permission);
+/**
+ \brief Show android toast notification
+
+ Show toast in UI thread [https://developer.android.com/guide/topics/ui/notifiers/toasts]
+ Params description
+ message : text message to be shown
+ duration : 0 - short [https://developer.android.com/reference/android/widget/Toast#LENGTH_SHORT],
+ 1 - long [https://developer.android.com/reference/android/widget/Toast#LENGTH_LONG]
+ gravity : the location at which the notification should appear on the screen.
+ It's an optional parameter. Set -1 if you don't want specify any gravity or
+ choose some value from https://developer.android.com/reference/android/view/Gravity
+ xOffset : set this parameter only when gravity >=0
+ yOffset : set this parameter only when gravity >=0
+*/
+extern DECLSPEC int SDLCALL SDL_AndroidShowToast(const char* message, int duration, int gravity, int xOffset, int yOffset);
+
#endif /* __ANDROID__ */
/* Platform specific functions for WinRT */
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index 5831ea9..a01787e 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -312,6 +312,7 @@ static jmethodID midManualBackButton;
static jmethodID midMinimizeWindow;
static jmethodID midOpenURL;
static jmethodID midRequestPermission;
+static jmethodID midShowToast;
static jmethodID midSendMessage;
static jmethodID midSetActivityTitle;
static jmethodID midSetCustomCursor;
@@ -591,6 +592,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *env, jclass cl
midMinimizeWindow = (*env)->GetStaticMethodID(env, mActivityClass, "minimizeWindow","()V");
midOpenURL = (*env)->GetStaticMethodID(env, mActivityClass, "openURL", "(Ljava/lang/String;)I");
midRequestPermission = (*env)->GetStaticMethodID(env, mActivityClass, "requestPermission", "(Ljava/lang/String;I)V");
+ midShowToast = (*env)->GetStaticMethodID(env, mActivityClass, "showToast", "(Ljava/lang/String;IIII)I");
midSendMessage = (*env)->GetStaticMethodID(env, mActivityClass, "sendMessage", "(II)Z");
midSetActivityTitle = (*env)->GetStaticMethodID(env, mActivityClass, "setActivityTitle","(Ljava/lang/String;)Z");
midSetCustomCursor = (*env)->GetStaticMethodID(env, mActivityClass, "setCustomCursor", "(I)Z");
@@ -621,6 +623,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *env, jclass cl
!midMinimizeWindow ||
!midOpenURL ||
!midRequestPermission ||
+ !midShowToast ||
!midSendMessage ||
!midSetActivityTitle ||
!midSetCustomCursor ||
@@ -2468,6 +2471,11 @@ SDL_bool SDL_AndroidRequestPermission(const char *permission)
return Android_JNI_RequestPermission(permission);
}
+int SDL_AndroidShowToast(const char* message, int duration, int gravity, int xOffset, int yOffset)
+{
+ return Android_JNI_ShowToast(message, duration, gravity, xOffset, yOffset);
+}
+
void Android_JNI_GetManifestEnvironmentVariables(void)
{
if (!mActivityClass || !midGetManifestEnvironmentVariables) {
@@ -2547,6 +2555,17 @@ SDL_bool Android_JNI_RequestPermission(const char *permission)
return bPermissionRequestResult;
}
+/* Show toast notification */
+int Android_JNI_ShowToast(const char* message, int duration, int gravity, int xOffset, int yOffset)
+{
+ int result = 0;
+ JNIEnv *env = Android_JNI_GetEnv();
+ jstring jmessage = (*env)->NewStringUTF(env, message);
+ result = (*env)->CallStaticIntMethod(env, mActivityClass, midShowToast, jmessage, duration, gravity, xOffset, yOffset);
+ (*env)->DeleteLocalRef(env, jmessage);
+ return result;
+}
+
int Android_JNI_GetLocale(char *buf, size_t buflen)
{
AConfiguration *cfg;
diff --git a/src/core/android/SDL_android.h b/src/core/android/SDL_android.h
index 0fb07d6..7c4e3db 100644
--- a/src/core/android/SDL_android.h
+++ b/src/core/android/SDL_android.h
@@ -129,6 +129,9 @@ SDL_bool Android_JNI_SetRelativeMouseEnabled(SDL_bool enabled);
/* Request permission */
SDL_bool Android_JNI_RequestPermission(const char *permission);
+/* Show toast notification */
+int Android_JNI_ShowToast(const char* message, int duration, int gravity, int xOffset, int yOffset);
+
int Android_JNI_OpenURL(const char *url);
int SDL_GetAndroidSDKVersion(void);
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 4d698c6..e32cd1f 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -766,6 +766,7 @@
#define SDL_GetPreferredLocales SDL_GetPreferredLocales_REAL
#define SDL_SIMDRealloc SDL_SIMDRealloc_REAL
#define SDL_AndroidRequestPermission SDL_AndroidRequestPermission_REAL
+#define SDL_AndroidShowToast SDL_AndroidShowToast_REAL
#define SDL_OpenURL SDL_OpenURL_REAL
#define SDL_HasSurfaceRLE SDL_HasSurfaceRLE_REAL
#define SDL_GameControllerHasLED SDL_GameControllerHasLED_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 8e67893..61437c8 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -826,6 +826,7 @@ SDL_DYNAPI_PROC(SDL_Locale *,SDL_GetPreferredLocales,(void),(),return)
SDL_DYNAPI_PROC(void*,SDL_SIMDRealloc,(void *a, const size_t b),(a, b),return)
#ifdef __ANDROID__
SDL_DYNAPI_PROC(SDL_bool,SDL_AndroidRequestPermission,(const char *a),(a),return)
+SDL_DYNAPI_PROC(int,SDL_AndroidShowToast,(const char* message, int duration, int gravity, int xOffset, int yOffset),(message,duration,gravity,xOffset,yOffset),return)
#endif
SDL_DYNAPI_PROC(int,SDL_OpenURL,(const char *a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_HasSurfaceRLE,(SDL_Surface *a),(a),return)