Implement link helper for android

This commit is contained in:
Benau 2019-10-28 16:47:05 +08:00
parent aa647cce67
commit ab3d8ef415
4 changed files with 82 additions and 5 deletions

View File

@ -4,7 +4,9 @@ import org.supertuxkart.stk_dbg.STKEditText;
import android.app.NativeActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
@ -181,6 +183,14 @@ public class SuperTuxKartActivity extends NativeActivity
}
// ------------------------------------------------------------------------
/* Called by STK in JNI. */
public void openURL(final String url)
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
// ------------------------------------------------------------------------
/* Called by STK in JNI. */
public void fromSTKEditBox(final int widget_id, final String text,
final int selection_start,
final int selection_end, final int type)

View File

@ -1151,6 +1151,64 @@ wchar_t CIrrDeviceAndroid::getKeyChar(SEvent& event)
return key_char;
}
void CIrrDeviceAndroid::openURL(const std::string& url)
{
if (!Android)
return;
bool was_detached = false;
JNIEnv* env = NULL;
jint status = Android->activity->vm->GetEnv((void**)&env, JNI_VERSION_1_6);
if (status == JNI_EDETACHED)
{
JavaVMAttachArgs args;
args.version = JNI_VERSION_1_6;
args.name = "NativeThread";
args.group = NULL;
status = Android->activity->vm->AttachCurrentThread(&env, &args);
was_detached = true;
}
if (status != JNI_OK)
{
os::Printer::log("Cannot attach current thread in openURL.", ELL_DEBUG);
return;
}
jobject native_activity = Android->activity->clazz;
jclass class_native_activity = env->GetObjectClass(native_activity);
if (class_native_activity == NULL)
{
os::Printer::log("openURL unable to find object class.", ELL_ERROR);
if (was_detached)
{
Android->activity->vm->DetachCurrentThread();
}
return;
}
jmethodID method_id = env->GetMethodID(class_native_activity, "openURL", "(Ljava/lang/String;)V");
if (method_id == NULL)
{
os::Printer::log("openURL unable to find method id.", ELL_ERROR);
if (was_detached)
{
Android->activity->vm->DetachCurrentThread();
}
return;
}
jstring url_jstring = env->NewStringUTF(url.c_str());
env->CallVoidMethod(native_activity, method_id, url_jstring);
if (was_detached)
{
Android->activity->vm->DetachCurrentThread();
}
}
void CIrrDeviceAndroid::toggleOnScreenKeyboard(bool show, s32 type)
{
if (!Android)

View File

@ -18,7 +18,7 @@
#include "IImagePresenter.h"
#include <map>
#include <string>
namespace irr
{
@ -89,7 +89,7 @@ namespace irr
static void onCreate();
static const AndroidApplicationInfo& getApplicationInfo(
ANativeActivity* activity);
void openURL(const std::string& url);
private:
s32 m_moved_height;
u32 m_screen_height;

View File

@ -15,6 +15,7 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "online/link_helper.hpp"
#include "graphics/irr_driver.hpp"
#include "utils/log.hpp"
#include <string>
#ifdef _WIN32
@ -22,13 +23,17 @@
#include <shellapi.h>
#endif
#ifdef ANDROID
#include "../../../lib/irrlicht/source/Irrlicht/CIrrDeviceAndroid.h"
#endif
using namespace Online;
namespace Online
{
bool LinkHelper::isSupported()
{
#if defined(_WIN32) || defined(__APPLE__) || (defined(__linux__) && !defined(__ANDROID__))
#if defined(_WIN32) || defined(__APPLE__) || (defined(__linux__))
return true;
#else
return false;
@ -37,7 +42,11 @@ namespace Online
void LinkHelper::openURL (std::string url)
{
#if defined(_WIN32)
#if defined(ANDROID)
CIrrDeviceAndroid* android = dynamic_cast<CIrrDeviceAndroid*>(irr_driver->getDevice());
if (android)
android->openURL(url);
#elif defined(_WIN32)
ShellExecuteA(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL);
#elif defined(__APPLE__) && !defined(IOS_STK)
std::string command = std::string("open ").append(url);
@ -45,7 +54,7 @@ namespace Online
{
Log::error("OpenURL", "Command returned non-zero exit status");
}
#elif defined(__linux__) && !defined(__ANDROID__)
#elif defined(__linux__)
std::string command = std::string("xdg-open ").append(url);
const char* lib_path = getenv("LD_LIBRARY_PATH");