From cc829bdd1e7be0c0a23aebe3545627e6611374ae Mon Sep 17 00:00:00 2001 From: fysx Date: Thu, 3 Apr 2014 18:20:37 +0200 Subject: [PATCH] added code to support the upcoming love.system.openURL --- jni/love/src/common/android.cpp | 15 +++++++++ jni/love/src/common/android.h | 4 +++ src/org/love2d/android/GameActivity.java | 43 +++++++++++++++--------- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/jni/love/src/common/android.cpp b/jni/love/src/common/android.cpp index 88068c9e..7cf109cc 100644 --- a/jni/love/src/common/android.cpp +++ b/jni/love/src/common/android.cpp @@ -76,6 +76,21 @@ const char* getSelectedGameFile() return path; } +bool openURL(const std::string &url) +{ + JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv(); + jclass activity = env->FindClass("org/love2d/android/GameActivity"); + + jmethodID openURL= env->GetStaticMethodID(activity, "openURL", "(Ljava/lang/String;)V"); + jstring url_jstring = (jstring) env->NewStringUTF (url.c_str()); + + env->CallStaticVoidMethod (activity, openURL, url_jstring); + + env->DeleteLocalRef (url_jstring); + env->DeleteLocalRef (activity); + return true; +} + } // android } // love diff --git a/jni/love/src/common/android.h b/jni/love/src/common/android.h index 6e8fd5f1..b8864def 100644 --- a/jni/love/src/common/android.h +++ b/jni/love/src/common/android.h @@ -25,6 +25,8 @@ #ifdef LOVE_ANDROID +#include + namespace love { namespace android @@ -41,6 +43,8 @@ double getScreenScale(); **/ const char* getSelectedGameFile(); +bool openURL (const std::string &url); + } // android } // love diff --git a/src/org/love2d/android/GameActivity.java b/src/org/love2d/android/GameActivity.java index 20a0286d..79bb95d2 100644 --- a/src/org/love2d/android/GameActivity.java +++ b/src/org/love2d/android/GameActivity.java @@ -31,33 +31,44 @@ import android.widget.Toast; public class GameActivity extends SDLActivity { private static DisplayMetrics metrics = new DisplayMetrics(); private static String gamePath = ""; - + private static Context context; + @Override protected void onCreate(Bundle savedInstanceState) { - Log.d("GameActivity", "started"); - - Uri game = this.getIntent().getData(); - if (game != null) { - if (game.getScheme().equals ("file")) { - gamePath = game.getPath(); - } else { - copyGameToCache (game); - } - Log.d("GameActivity", "Selected the file: " + getGamePath()); + Log.d("GameActivity", "started"); + + context = this.getApplicationContext(); + + Uri game = this.getIntent().getData(); + if (game != null) { + if (game.getScheme().equals ("file")) { + gamePath = game.getPath(); + } else { + copyGameToCache (game); } - - super.onCreate(savedInstanceState); - getWindowManager().getDefaultDisplay().getMetrics(metrics); + Log.d("GameActivity", "Selected the file: " + getGamePath()); + } + + super.onCreate(savedInstanceState); + getWindowManager().getDefaultDisplay().getMetrics(metrics); } public static String getGamePath() { - Log.d ("GameActivity", "called getGamePath(), game path = " + gamePath); + Log.d ("GameActivity", "called getGamePath(), game path = " + gamePath); return gamePath; } - public static DisplayMetrics getMetrics() { + public static DisplayMetrics getMetrics() { return metrics; } + + public static void openURL (String url) { + Log.d ("GameActivity", "opening url = " + url); + Intent i = new Intent(Intent.ACTION_VIEW); + i.setData(Uri.parse(url)); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(i); + } void copyGameToCache (Uri sourceuri) {