Skip to content

Commit

Permalink
Implement URL handler for markdown on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
JVital2013 committed Nov 21, 2023
1 parent 566f422 commit 29097c4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
5 changes: 5 additions & 0 deletions android/app/src/main/java/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ class MainActivity : NativeActivity(), TextWatcher {
return tmp;
}

public fun openURL(url: String) {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}

// Receive results of the above
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data);
Expand Down
13 changes: 6 additions & 7 deletions android/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ static int ShowSoftKeyboardInput();
static int HideSoftKeyboardInput();
static int PollUnicodeChars();
static int GetAssetData(const char *filename, void **out_data);

float get_dpi(struct android_app* app);
static float get_dpi();

#include "logger.h"
#include "init.h"
Expand Down Expand Up @@ -99,7 +98,7 @@ void init(struct android_app *app)
// ImGui::StyleColorsDark();
// ImGui::StyleColorsClassic();

float display_scale = get_dpi(app);
float display_scale = get_dpi();
initLogger();
style::setFonts(display_scale);
HideSoftKeyboardInput();
Expand Down Expand Up @@ -457,9 +456,9 @@ static int PollUnicodeChars()
return 0;
}

float get_dpi(struct android_app* app)
static float get_dpi()
{
JavaVM* java_vm = app->activity->vm;
JavaVM* java_vm = g_App->activity->vm;
JNIEnv* java_env = NULL;

jint jni_return = java_vm->GetEnv((void**)&java_env, JNI_VERSION_1_6);
Expand All @@ -470,15 +469,15 @@ float get_dpi(struct android_app* app)
if (jni_return != JNI_OK)
throw std::runtime_error("Could not attach to thread");

jclass native_activity_clazz = java_env->GetObjectClass(app->activity->clazz);
jclass native_activity_clazz = java_env->GetObjectClass(g_App->activity->clazz);
if (native_activity_clazz == NULL)
throw std::runtime_error("Could not get MainActivity class");

jmethodID method_id = java_env->GetMethodID(native_activity_clazz, "get_dpi", "()F");
if (method_id == NULL)
throw std::runtime_error("Could not get methode ID");

jfloat jflt = java_env->CallFloatMethod(app->activity->clazz, method_id);
jfloat jflt = java_env->CallFloatMethod(g_App->activity->clazz, method_id);

jni_return = java_vm->DetachCurrentThread();
if (jni_return != JNI_OK)
Expand Down
31 changes: 30 additions & 1 deletion src-core/common/widgets/markdown_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#if defined(_WIN32)
#include <windows.h>
#elif defined(__ANDROID__)
#include <android_native_app_glue.h>
extern struct android_app *android_app_ptr;
#endif

namespace widgets
Expand All @@ -23,6 +26,32 @@ namespace widgets
ShellExecuteA(0, 0, url.c_str(), 0, 0, SW_SHOW);
#elif defined(__APPLE__)
system(std::string("open " + url).c_str());
#elif defined(__ANDROID__)
JavaVM *java_vm = android_app_ptr->activity->vm;
JNIEnv *java_env = NULL;

jint jni_return = java_vm->GetEnv((void **)&java_env, JNI_VERSION_1_6);
if (jni_return == JNI_ERR)
throw std::runtime_error("Could not get JNI environement");

jni_return = java_vm->AttachCurrentThread(&java_env, NULL);
if (jni_return != JNI_OK)
throw std::runtime_error("Could not attach to thread");

jclass native_activity_clazz = java_env->GetObjectClass(android_app_ptr->activity->clazz);
if (native_activity_clazz == NULL)
throw std::runtime_error("Could not get MainActivity class");

jmethodID method_id = java_env->GetMethodID(native_activity_clazz, "openURL", "(Ljava/lang/String;)V");
if (method_id == NULL)
throw std::runtime_error("Could not get methode ID");

jstring jurl = java_env->NewStringUTF(url.c_str());
java_env->CallVoidMethod(android_app_ptr->activity->clazz, method_id, jurl);

jni_return = java_vm->DetachCurrentThread();
if (jni_return != JNI_OK)
throw std::runtime_error("Could not detach from thread");
#else
system(std::string("xdg-open " + url).c_str());
#endif
Expand Down Expand Up @@ -99,4 +128,4 @@ namespace widgets
markdown_ = md;
texture_buffer.clear();
}
}
}

0 comments on commit 29097c4

Please sign in to comment.