2023-01-05 20:25:55 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* dir_access_jandroid.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "dir_access_jandroid.h"
|
2022-02-16 20:56:32 +08:00
|
|
|
|
2019-02-27 04:54:45 +08:00
|
|
|
#include "string_android.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "thread_jandroid.h"
|
2015-04-28 20:38:07 +08:00
|
|
|
|
2023-06-08 20:51:32 +08:00
|
|
|
#include "core/string/print_string.h"
|
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
jobject DirAccessJAndroid::dir_access_handler = nullptr;
|
2020-04-02 07:20:12 +08:00
|
|
|
jclass DirAccessJAndroid::cls = nullptr;
|
|
|
|
jmethodID DirAccessJAndroid::_dir_open = nullptr;
|
|
|
|
jmethodID DirAccessJAndroid::_dir_next = nullptr;
|
|
|
|
jmethodID DirAccessJAndroid::_dir_close = nullptr;
|
|
|
|
jmethodID DirAccessJAndroid::_dir_is_dir = nullptr;
|
2021-07-11 09:39:31 +08:00
|
|
|
jmethodID DirAccessJAndroid::_dir_exists = nullptr;
|
|
|
|
jmethodID DirAccessJAndroid::_file_exists = nullptr;
|
|
|
|
jmethodID DirAccessJAndroid::_get_drive_count = nullptr;
|
|
|
|
jmethodID DirAccessJAndroid::_get_drive = nullptr;
|
|
|
|
jmethodID DirAccessJAndroid::_make_dir = nullptr;
|
|
|
|
jmethodID DirAccessJAndroid::_get_space_left = nullptr;
|
|
|
|
jmethodID DirAccessJAndroid::_rename = nullptr;
|
|
|
|
jmethodID DirAccessJAndroid::_remove = nullptr;
|
|
|
|
jmethodID DirAccessJAndroid::_current_is_hidden = nullptr;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-01-14 20:16:41 +08:00
|
|
|
Error DirAccessJAndroid::list_dir_begin() {
|
2014-02-10 09:10:30 +08:00
|
|
|
list_dir_end();
|
2021-07-11 09:39:31 +08:00
|
|
|
int res = dir_open(current_dir);
|
2022-02-16 20:56:32 +08:00
|
|
|
if (res <= 0) {
|
2017-01-14 20:16:41 +08:00
|
|
|
return ERR_CANT_OPEN;
|
2022-02-16 20:56:32 +08:00
|
|
|
}
|
2021-07-11 09:39:31 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
id = res;
|
|
|
|
|
2017-01-14 20:16:41 +08:00
|
|
|
return OK;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
String DirAccessJAndroid::get_next() {
|
|
|
|
ERR_FAIL_COND_V(id == 0, "");
|
2021-07-11 09:39:31 +08:00
|
|
|
if (_dir_next) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND_V(env == nullptr, "");
|
|
|
|
jstring str = (jstring)env->CallObjectMethod(dir_access_handler, _dir_next, get_access_type(), id);
|
|
|
|
if (!str) {
|
|
|
|
return "";
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
String ret = jstring_to_string((jstring)str, env);
|
|
|
|
env->DeleteLocalRef((jobject)str);
|
|
|
|
return ret;
|
|
|
|
} else {
|
2014-02-10 09:10:30 +08:00
|
|
|
return "";
|
2022-02-16 20:56:32 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2016-07-21 18:07:01 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
bool DirAccessJAndroid::current_is_dir() const {
|
2021-07-11 09:39:31 +08:00
|
|
|
if (_dir_is_dir) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND_V(env == nullptr, false);
|
|
|
|
return env->CallBooleanMethod(dir_access_handler, _dir_is_dir, get_access_type(), id);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2015-03-23 22:31:03 +08:00
|
|
|
|
|
|
|
bool DirAccessJAndroid::current_is_hidden() const {
|
2021-07-11 09:39:31 +08:00
|
|
|
if (_current_is_hidden) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND_V(env == nullptr, false);
|
|
|
|
return env->CallBooleanMethod(dir_access_handler, _current_is_hidden, get_access_type(), id);
|
|
|
|
}
|
|
|
|
return false;
|
2015-03-23 22:31:03 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void DirAccessJAndroid::list_dir_end() {
|
2022-02-16 20:56:32 +08:00
|
|
|
if (id == 0) {
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
2022-02-16 20:56:32 +08:00
|
|
|
}
|
2021-07-11 09:39:31 +08:00
|
|
|
|
|
|
|
dir_close(id);
|
2014-02-10 09:10:30 +08:00
|
|
|
id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DirAccessJAndroid::get_drive_count() {
|
2021-07-11 09:39:31 +08:00
|
|
|
if (_get_drive_count) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND_V(env == nullptr, 0);
|
|
|
|
return env->CallIntMethod(dir_access_handler, _get_drive_count, get_access_type());
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2016-07-21 18:07:01 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
String DirAccessJAndroid::get_drive(int p_drive) {
|
2021-07-11 09:39:31 +08:00
|
|
|
if (_get_drive) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND_V(env == nullptr, "");
|
|
|
|
jstring j_drive = (jstring)env->CallObjectMethod(dir_access_handler, _get_drive, get_access_type(), p_drive);
|
|
|
|
if (!j_drive) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
String drive = jstring_to_string(j_drive, env);
|
|
|
|
env->DeleteLocalRef(j_drive);
|
|
|
|
return drive;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2022-08-31 01:00:03 +08:00
|
|
|
String DirAccessJAndroid::_get_root_string() const {
|
|
|
|
if (get_access_type() == ACCESS_FILESYSTEM) {
|
|
|
|
return "/";
|
|
|
|
}
|
|
|
|
return DirAccessUnix::_get_root_string();
|
|
|
|
}
|
|
|
|
|
2022-08-15 10:29:18 +08:00
|
|
|
String DirAccessJAndroid::get_current_dir(bool p_include_drive) const {
|
|
|
|
String base = _get_root_path();
|
|
|
|
String bd = current_dir;
|
|
|
|
if (!base.is_empty()) {
|
|
|
|
bd = current_dir.replace_first(base, "");
|
|
|
|
}
|
|
|
|
|
2022-08-31 01:00:03 +08:00
|
|
|
String root_string = _get_root_string();
|
|
|
|
if (bd.begins_with(root_string)) {
|
|
|
|
return bd;
|
|
|
|
} else if (bd.begins_with("/")) {
|
|
|
|
return root_string + bd.substr(1, bd.length());
|
2022-08-15 10:29:18 +08:00
|
|
|
} else {
|
2022-08-31 01:00:03 +08:00
|
|
|
return root_string + bd;
|
2022-08-15 10:29:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
Error DirAccessJAndroid::change_dir(String p_dir) {
|
2021-07-11 09:39:31 +08:00
|
|
|
String new_dir = get_absolute_path(p_dir);
|
|
|
|
if (new_dir == current_dir) {
|
2014-02-10 09:10:30 +08:00
|
|
|
return OK;
|
2022-02-16 20:56:32 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
if (!dir_exists(new_dir)) {
|
2014-02-10 09:10:30 +08:00
|
|
|
return ERR_INVALID_PARAMETER;
|
2022-02-16 20:56:32 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2015-04-28 20:38:07 +08:00
|
|
|
current_dir = new_dir;
|
2014-02-10 09:10:30 +08:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
String DirAccessJAndroid::get_absolute_path(String p_path) {
|
|
|
|
if (current_dir != "" && p_path == current_dir) {
|
|
|
|
return current_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_path.is_relative_path()) {
|
2022-08-30 08:34:01 +08:00
|
|
|
p_path = get_current_dir().path_join(p_path);
|
2021-07-11 09:39:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p_path = fix_path(p_path);
|
|
|
|
p_path = p_path.simplify_path();
|
|
|
|
return p_path;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DirAccessJAndroid::file_exists(String p_file) {
|
2021-07-11 09:39:31 +08:00
|
|
|
if (_file_exists) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND_V(env == nullptr, false);
|
|
|
|
|
|
|
|
String path = get_absolute_path(p_file);
|
|
|
|
jstring j_path = env->NewStringUTF(path.utf8().get_data());
|
|
|
|
bool result = env->CallBooleanMethod(dir_access_handler, _file_exists, get_access_type(), j_path);
|
|
|
|
env->DeleteLocalRef(j_path);
|
|
|
|
return result;
|
2022-02-16 20:56:32 +08:00
|
|
|
} else {
|
2021-07-11 09:39:31 +08:00
|
|
|
return false;
|
2022-02-16 20:56:32 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2014-05-25 11:34:51 +08:00
|
|
|
bool DirAccessJAndroid::dir_exists(String p_dir) {
|
2021-07-11 09:39:31 +08:00
|
|
|
if (_dir_exists) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND_V(env == nullptr, false);
|
|
|
|
|
|
|
|
String path = get_absolute_path(p_dir);
|
|
|
|
jstring j_path = env->NewStringUTF(path.utf8().get_data());
|
|
|
|
bool result = env->CallBooleanMethod(dir_access_handler, _dir_exists, get_access_type(), j_path);
|
|
|
|
env->DeleteLocalRef(j_path);
|
|
|
|
return result;
|
2022-02-16 20:56:32 +08:00
|
|
|
} else {
|
2021-07-11 09:39:31 +08:00
|
|
|
return false;
|
2022-02-16 20:56:32 +08:00
|
|
|
}
|
2021-07-11 09:39:31 +08:00
|
|
|
}
|
2014-05-25 11:34:51 +08:00
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
Error DirAccessJAndroid::make_dir_recursive(String p_dir) {
|
|
|
|
// Check if the directory exists already
|
|
|
|
if (dir_exists(p_dir)) {
|
|
|
|
return ERR_ALREADY_EXISTS;
|
2022-02-16 20:56:32 +08:00
|
|
|
}
|
2014-05-25 11:34:51 +08:00
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
if (_make_dir) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND_V(env == nullptr, ERR_UNCONFIGURED);
|
2014-05-25 11:34:51 +08:00
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
String path = get_absolute_path(p_dir);
|
|
|
|
jstring j_dir = env->NewStringUTF(path.utf8().get_data());
|
|
|
|
bool result = env->CallBooleanMethod(dir_access_handler, _make_dir, get_access_type(), j_dir);
|
|
|
|
env->DeleteLocalRef(j_dir);
|
|
|
|
if (result) {
|
|
|
|
return OK;
|
|
|
|
} else {
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ERR_UNCONFIGURED;
|
|
|
|
}
|
2014-05-25 11:34:51 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
Error DirAccessJAndroid::make_dir(String p_dir) {
|
2021-07-11 09:39:31 +08:00
|
|
|
return make_dir_recursive(p_dir);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccessJAndroid::rename(String p_from, String p_to) {
|
2021-07-11 09:39:31 +08:00
|
|
|
if (_rename) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND_V(env == nullptr, ERR_UNCONFIGURED);
|
2016-07-21 18:07:01 +08:00
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
String from_path = get_absolute_path(p_from);
|
|
|
|
jstring j_from = env->NewStringUTF(from_path.utf8().get_data());
|
|
|
|
|
|
|
|
String to_path = get_absolute_path(p_to);
|
|
|
|
jstring j_to = env->NewStringUTF(to_path.utf8().get_data());
|
|
|
|
|
|
|
|
bool result = env->CallBooleanMethod(dir_access_handler, _rename, get_access_type(), j_from, j_to);
|
|
|
|
env->DeleteLocalRef(j_from);
|
|
|
|
env->DeleteLocalRef(j_to);
|
|
|
|
if (result) {
|
|
|
|
return OK;
|
|
|
|
} else {
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ERR_UNCONFIGURED;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
Error DirAccessJAndroid::remove(String p_name) {
|
|
|
|
if (_remove) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND_V(env == nullptr, ERR_UNCONFIGURED);
|
|
|
|
|
|
|
|
String path = get_absolute_path(p_name);
|
|
|
|
jstring j_name = env->NewStringUTF(path.utf8().get_data());
|
|
|
|
bool result = env->CallBooleanMethod(dir_access_handler, _remove, get_access_type(), j_name);
|
|
|
|
env->DeleteLocalRef(j_name);
|
|
|
|
if (result) {
|
|
|
|
return OK;
|
|
|
|
} else {
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ERR_UNCONFIGURED;
|
|
|
|
}
|
2019-01-22 02:23:08 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 01:51:13 +08:00
|
|
|
uint64_t DirAccessJAndroid::get_space_left() {
|
2021-07-11 09:39:31 +08:00
|
|
|
if (_get_space_left) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND_V(env == nullptr, 0);
|
|
|
|
return env->CallLongMethod(dir_access_handler, _get_space_left, get_access_type());
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
void DirAccessJAndroid::setup(jobject p_dir_access_handler) {
|
2021-01-19 20:29:41 +08:00
|
|
|
JNIEnv *env = get_jni_env();
|
2021-07-11 09:39:31 +08:00
|
|
|
dir_access_handler = env->NewGlobalRef(p_dir_access_handler);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
jclass c = env->GetObjectClass(dir_access_handler);
|
2014-02-10 09:10:30 +08:00
|
|
|
cls = (jclass)env->NewGlobalRef(c);
|
|
|
|
|
2021-07-11 09:39:31 +08:00
|
|
|
_dir_open = env->GetMethodID(cls, "dirOpen", "(ILjava/lang/String;)I");
|
|
|
|
_dir_next = env->GetMethodID(cls, "dirNext", "(II)Ljava/lang/String;");
|
|
|
|
_dir_close = env->GetMethodID(cls, "dirClose", "(II)V");
|
|
|
|
_dir_is_dir = env->GetMethodID(cls, "dirIsDir", "(II)Z");
|
|
|
|
_dir_exists = env->GetMethodID(cls, "dirExists", "(ILjava/lang/String;)Z");
|
|
|
|
_file_exists = env->GetMethodID(cls, "fileExists", "(ILjava/lang/String;)Z");
|
|
|
|
_get_drive_count = env->GetMethodID(cls, "getDriveCount", "(I)I");
|
|
|
|
_get_drive = env->GetMethodID(cls, "getDrive", "(II)Ljava/lang/String;");
|
|
|
|
_make_dir = env->GetMethodID(cls, "makeDir", "(ILjava/lang/String;)Z");
|
|
|
|
_get_space_left = env->GetMethodID(cls, "getSpaceLeft", "(I)J");
|
|
|
|
_rename = env->GetMethodID(cls, "rename", "(ILjava/lang/String;Ljava/lang/String;)Z");
|
|
|
|
_remove = env->GetMethodID(cls, "remove", "(ILjava/lang/String;)Z");
|
|
|
|
_current_is_hidden = env->GetMethodID(cls, "isCurrentHidden", "(II)Z");
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DirAccessJAndroid::DirAccessJAndroid() {
|
|
|
|
}
|
|
|
|
|
|
|
|
DirAccessJAndroid::~DirAccessJAndroid() {
|
2016-11-03 23:41:16 +08:00
|
|
|
list_dir_end();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2021-07-11 09:39:31 +08:00
|
|
|
|
|
|
|
int DirAccessJAndroid::dir_open(String p_path) {
|
|
|
|
if (_dir_open) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND_V(env == nullptr, 0);
|
|
|
|
|
|
|
|
String path = get_absolute_path(p_path);
|
|
|
|
jstring js = env->NewStringUTF(path.utf8().get_data());
|
|
|
|
int dirId = env->CallIntMethod(dir_access_handler, _dir_open, get_access_type(), js);
|
|
|
|
env->DeleteLocalRef(js);
|
|
|
|
return dirId;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirAccessJAndroid::dir_close(int p_id) {
|
|
|
|
if (_dir_close) {
|
|
|
|
JNIEnv *env = get_jni_env();
|
|
|
|
ERR_FAIL_COND(env == nullptr);
|
|
|
|
env->CallVoidMethod(dir_access_handler, _dir_close, get_access_type(), p_id);
|
|
|
|
}
|
|
|
|
}
|