Android: add helper function to open an URL/URI (see bug 2783)
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
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 b5c3193..c50af2b 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
@@ -1588,6 +1588,30 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
nativePermissionResult(requestCode, false);
}
}
+
+ /**
+ * This method is called by SDL using JNI.
+ */
+ public static int openURL(String url)
+ {
+ try {
+ Intent i = new Intent(Intent.ACTION_VIEW);
+ i.setData(Uri.parse(url));
+
+ int flags = Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
+ if (Build.VERSION.SDK_INT >= 21) {
+ flags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
+ } else {
+ flags |= Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
+ }
+ i.addFlags(flags);
+
+ mSingleton.startActivity(i);
+ } catch (Exception ex) {
+ return -1;
+ }
+ return 0;
+ }
}
/**
diff --git a/include/SDL_system.h b/include/SDL_system.h
index 48878b7..8ad0184 100644
--- a/include/SDL_system.h
+++ b/include/SDL_system.h
@@ -206,6 +206,13 @@ extern DECLSPEC const char * SDLCALL SDL_AndroidGetExternalStoragePath(void);
*/
extern DECLSPEC SDL_bool SDLCALL SDL_AndroidRequestPermission(const char *permission);
+/**
+ \brief Open an URL / URI in the browser or other
+
+ \return 0 on success, or -1 on error.
+ */
+extern DECLSPEC int SDLCALL SDL_AndroidOpenURL(const char *url);
+
#endif /* __ANDROID__ */
/* Platform specific functions for WinRT */
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index 1c00762..598d171 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -311,6 +311,7 @@ static jmethodID midIsScreenKeyboardShown;
static jmethodID midIsTablet;
static jmethodID midManualBackButton;
static jmethodID midMinimizeWindow;
+static jmethodID midOpenURL;
static jmethodID midRequestPermission;
static jmethodID midSendMessage;
static jmethodID midSetActivityTitle;
@@ -589,6 +590,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *env, jclass cl
midIsTablet = (*env)->GetStaticMethodID(env, mActivityClass, "isTablet", "()Z");
midManualBackButton = (*env)->GetStaticMethodID(env, mActivityClass, "manualBackButton", "()V");
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");
midSendMessage = (*env)->GetStaticMethodID(env, mActivityClass, "sendMessage", "(II)Z");
midSetActivityTitle = (*env)->GetStaticMethodID(env, mActivityClass, "setActivityTitle","(Ljava/lang/String;)Z");
@@ -618,6 +620,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetupJNI)(JNIEnv *env, jclass cl
!midIsTablet ||
!midManualBackButton ||
!midMinimizeWindow ||
+ !midOpenURL ||
!midRequestPermission ||
!midSendMessage ||
!midSetActivityTitle ||
@@ -2466,6 +2469,19 @@ SDL_bool SDL_AndroidRequestPermission(const char *permission)
return Android_JNI_RequestPermission(permission);
}
+int SDL_AndroidOpenURL(const char *url)
+{
+ JNIEnv *env = Android_JNI_GetEnv();
+ int ret = -1;
+
+ if (url) {
+ jstring jurl = (*env)->NewStringUTF(env, url);
+ ret = (*env)->CallStaticIntMethod(env, mActivityClass, midOpenURL, jurl);
+ (*env)->DeleteLocalRef(env, jurl);
+ }
+ return ret;
+}
+
void Android_JNI_GetManifestEnvironmentVariables(void)
{
if (!mActivityClass || !midGetManifestEnvironmentVariables) {