comparison 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
comparison
equal deleted inserted replaced
2794:7ce51a7e8009 2795:5c61aba17b69
1279 jstring jresult = (jstring)_dw_jni_check_result(env, env->CallObjectMethod(_dw_obj, fileBrowse, jstr, path, jext, flags), _DW_REFERENCE_NONE); 1279 jstring jresult = (jstring)_dw_jni_check_result(env, env->CallObjectMethod(_dw_obj, fileBrowse, jstr, path, jext, flags), _DW_REFERENCE_NONE);
1280 if(jresult) 1280 if(jresult)
1281 { 1281 {
1282 const char *str = env->GetStringUTFChars(jresult, nullptr); 1282 const char *str = env->GetStringUTFChars(jresult, nullptr);
1283 if(str) 1283 if(str)
1284 { 1284 retval = strdup(str);
1285 size_t len = strlen(str);
1286
1287 // Allocate a string with an extra two bytes... so we can
1288 // check for the trailing \n in dw_file_open() without a
1289 // memory violation.
1290 if((retval = (char *)calloc(1, len + 2)))
1291 {
1292 char *tmp;
1293
1294 strncpy(retval, str, len);
1295 // If we have a URI encoded, find the double \n and replace the
1296 // first \n with NULL so the string still looks like a normal path.
1297 tmp = strstr(retval, "\n\n");
1298 if (tmp)
1299 *tmp = 0;
1300 }
1301 }
1302 } 1285 }
1303 } 1286 }
1304 return retval; 1287 return retval;
1305 } 1288 }
1306 1289
1307 int API dw_file_open(const char *path, int mode) 1290 int API dw_file_open(const char *path, int mode)
1308 { 1291 {
1309 JNIEnv *env;
1310 int retval = -1; 1292 int retval = -1;
1311 1293
1312 if((env = (JNIEnv *)pthread_getspecific(_dw_env_key))) 1294 if(path)
1313 { 1295 {
1314 // dw_file_browse saves a second string with the URI after the path 1296 JNIEnv *env;
1315 // So find the end of the string and check for a trailing \n 1297
1316 // The URI will be after that if found. 1298 // If we have no URI we can use the normal open call
1317 const char *uri = strchr(path, 0); 1299 if(!strstr(path, "://"))
1318 jstring jstr = env->NewStringUTF((uri && *(uri+1) == '\n') ? (uri+2) : path); 1300 return open(path, mode);
1319 // First get the class that contains the method you need to call 1301 // If we have a URI, use Android calls to get the URI file descriptor
1320 jclass clazz = _dw_find_class(env, DW_CLASS_NAME); 1302 if ((env = (JNIEnv *) pthread_getspecific(_dw_env_key)))
1321 // Get the method that you want to call 1303 {
1322 jmethodID fileOpen = env->GetMethodID(clazz, "fileOpen", 1304 // dw_file_browse saves a second string with the URI after the path
1323 "(Ljava/lang/String;I)I"); 1305 // So find the end of the string and check for a trailing \n
1324 // Call the method on the object 1306 // The URI will be after that if found.
1325 retval = (int)env->CallIntMethod(_dw_obj, fileOpen, jstr, (jint)mode); 1307 jstring jstr = env->NewStringUTF(path);
1326 _dw_jni_check_exception(env); 1308 // First get the class that contains the method you need to call
1309 jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
1310 // Get the method that you want to call
1311 jmethodID fileOpen = env->GetMethodID(clazz, "fileOpen",
1312 "(Ljava/lang/String;I)I");
1313 // Call the method on the object
1314 retval = (int) env->CallIntMethod(_dw_obj, fileOpen, jstr, (jint) mode);
1315 _dw_jni_check_exception(env);
1316 }
1327 } 1317 }
1328 return retval; 1318 return retval;
1329 } 1319 }
1330 1320
1331 /* 1321 /*