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

This commit is contained in:
Sylvain Becker 2020-10-01 14:41:09 +02:00
parent 45c644cc99
commit dd55bfe89c
3 changed files with 47 additions and 0 deletions

View file

@ -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;
}
}
/**

View file

@ -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 */

View file

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