Commit dd55bfe89c93cffcca9d2a9dec31bf3e6c983d50

Sylvain Becker 2020-10-01T14:41:09

Android: add helper function to open an URL/URI (see bug 2783)

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) {