diff android/dw.cpp @ 2790:20d39af27aa4

Android: Add a new function for Android dw_file_open() which will open the URI that dw_file_browse() now saves at the end of the returned path. This is a hacky method of letting things function on Android... since Android seems to not let you open files with a pure path. Even if the file exists it will fail to open, you need to use the ugly Android URIs. Not sure if I will keep this solution or not, but committing it so Dynamic Windows Interface Builder will function on Android in the meantime. dw_file_open() just calls the system open() on non-Android platforms.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 13 Jul 2022 14:57:38 +0000
parents 220d63da2183
children 5c61aba17b69
line wrap: on
line diff
--- a/android/dw.cpp	Wed Jul 13 08:26:49 2022 +0000
+++ b/android/dw.cpp	Wed Jul 13 14:57:38 2022 +0000
@@ -1278,7 +1278,52 @@
         // Call the method on the object
         jstring jresult = (jstring)_dw_jni_check_result(env, env->CallObjectMethod(_dw_obj, fileBrowse, jstr, path, jext, flags), _DW_REFERENCE_NONE);
         if(jresult)
-            retval = strdup(env->GetStringUTFChars(jresult, nullptr));
+        {
+            const char *str = env->GetStringUTFChars(jresult, nullptr);
+            if(str)
+            {
+                size_t len = strlen(str);
+
+                // Allocate a string with an extra two bytes... so we can
+                // check for the trailing \n in dw_file_open() without a
+                // memory violation.
+                if((retval = (char *)calloc(1, len + 2)))
+                {
+                    char *tmp;
+
+                    strncpy(retval, str, len);
+                    // If we have a URI encoded, find the double \n and replace the
+                    // first \n with NULL so the string still looks like a normal path.
+                    tmp = strstr(retval, "\n\n");
+                    if (tmp)
+                        *tmp = 0;
+                }
+            }
+        }
+    }
+    return retval;
+}
+
+int API dw_file_open(const char *path, int mode)
+{
+    JNIEnv *env;
+    int retval = -1;
+
+    if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        // dw_file_browse saves a second string with the URI after the path
+        // So find the end of the string and check for a trailing \n
+        // The URI will be after that if found.
+        const char *uri = strchr(path, 0);
+        jstring jstr = env->NewStringUTF((uri && *(uri+1) == '\n') ? (uri+2) : path);
+        // 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 fileOpen = env->GetMethodID(clazz, "fileOpen",
+                                              "(Ljava/lang/String;I)I");
+        // Call the method on the object
+        retval = (int)env->CallIntMethod(_dw_obj, fileOpen, jstr, (jint)mode);
+        _dw_jni_check_exception(env);
     }
     return retval;
 }
@@ -5736,7 +5781,7 @@
         jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
         // Get the method that you want to call
         jmethodID windowSetStyle = env->GetMethodID(clazz, "windowSetStyle",
-                                                    "(Landroid/view/View;II)V");
+                                                    "(Ljava/lang/Object;II)V");
         // Call the method on the object
         env->CallVoidMethod(_dw_obj, windowSetStyle, handle, (jint)style, (jint)mask);
         _dw_jni_check_exception(env);