changeset 2694:cee79add3669

Andrdoid: Implement dw_browse() to load a URL in a new Activity.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 02 Nov 2021 19:04:56 +0000
parents 0dac724f890f
children 11aaf443d64b
files android/DWindows.kt android/dw.cpp
diffstat 2 files changed, 37 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Mon Nov 01 01:32:42 2021 +0000
+++ b/android/DWindows.kt	Tue Nov 02 19:04:56 2021 +0000
@@ -62,6 +62,10 @@
 import java.util.concurrent.locks.ReentrantLock
 import java.util.zip.ZipEntry
 import java.util.zip.ZipFile
+import android.content.Intent
+
+
+
 
 object DWEvent {
     const val TIMER = 0
@@ -1024,6 +1028,21 @@
         return darkMode
     }
 
+    fun browseURL(url: String): Int {
+        var retval: Int = -1 // DW_ERROR_UNKNOWN
+
+        waitOnUiThread {
+            val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
+            try {
+                retval = 0 // DW_ERROR_NONE
+                startActivity(browserIntent)
+            } catch (e: ActivityNotFoundException) {
+                retval = -1 // DW_ERROR_UNKNOWN
+            }
+        }
+       return retval
+    }
+
     fun menuPopup(menu: DWMenu, parent: View, x: Int, y: Int)
     {
         var anchor: View? = parent
--- a/android/dw.cpp	Mon Nov 01 01:32:42 2021 +0000
+++ b/android/dw.cpp	Tue Nov 02 19:04:56 2021 +0000
@@ -7232,7 +7232,24 @@
  */
 int API dw_browse(const char *url)
 {
-    return DW_ERROR_UNKNOWN;
+    JNIEnv *env;
+    int retval = DW_ERROR_UNKNOWN;
+
+    if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        // Construct a string
+        jstring jstr = env->NewStringUTF(url);
+        // First get the class that contains the method you need to call
+        jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
+        // Get the method that you want to call
+        jmethodID browseURL = env->GetMethodID(clazz, "browseURL",
+                                               "(Ljava/lang/String;)I");
+        // Call the method on the object
+        retval = env->CallIntMethod(_dw_obj, browseURL, jstr);
+        if(_dw_jni_check_exception(env))
+            retval = DW_ERROR_UNKNOWN;
+    }
+    return retval;
 }
 
 /*