diff android/dw.cpp @ 2795:5c61aba17b69

Android: Change dw_file_browse() to return URIs or paths on Android. Paths will be returned if either DW_DIRECTORY_OPEN or DW_FILE_PATH flags are specified. Otherwise a URI may be returned. The double string method of returning both path and URI has been removed. DW_FILE_PATH and DW_FILE_MASK have been added, but are really only used on Android. __DW_MOBILE__ will be defined on Mobile platforms such as iOS and Android. __DW_DESKTOP__ will be defined on most other desktop operating systems.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 15 Jul 2022 11:50:09 +0000
parents 20d39af27aa4
children 0c534743b7a9
line wrap: on
line diff
--- a/android/dw.cpp	Fri Jul 15 08:15:11 2022 +0000
+++ b/android/dw.cpp	Fri Jul 15 11:50:09 2022 +0000
@@ -1281,24 +1281,7 @@
         {
             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;
-                }
-            }
+                retval = strdup(str);
         }
     }
     return retval;
@@ -1306,24 +1289,31 @@
 
 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);
+    if(path)
+    {
+        JNIEnv *env;
+
+        // If we have no URI we can use the normal open call
+        if(!strstr(path, "://"))
+            return open(path, mode);
+        // If we have a URI, use Android calls to get the URI file descriptor
+        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.
+            jstring jstr = env->NewStringUTF(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;
 }