2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* dir_access_jandroid.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* 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 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "dir_access_jandroid.h"
|
2022-02-16 13:56:32 +01:00
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/string/print_string.h"
|
2020-12-06 19:39:45 +01:00
|
|
|
#include "file_access_android.h"
|
2019-02-26 21:54:45 +01:00
|
|
|
#include "string_android.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "thread_jandroid.h"
|
2015-04-28 14:38:07 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
jobject DirAccessJAndroid::io = nullptr;
|
|
|
|
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;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<DirAccess> DirAccessJAndroid::create_fs() {
|
2014-02-10 02:10:30 +01:00
|
|
|
return memnew(DirAccessJAndroid);
|
|
|
|
}
|
|
|
|
|
2017-01-14 13:16:41 +01:00
|
|
|
Error DirAccessJAndroid::list_dir_begin() {
|
2014-02-10 02:10:30 +01:00
|
|
|
list_dir_end();
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
jstring js = env->NewStringUTF(current_dir.utf8().get_data());
|
2017-03-05 16:44:50 +01:00
|
|
|
int res = env->CallIntMethod(io, _dir_open, js);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (res <= 0) {
|
2017-01-14 13:16:41 +01:00
|
|
|
return ERR_CANT_OPEN;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
id = res;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 13:16:41 +01:00
|
|
|
return OK;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String DirAccessJAndroid::get_next() {
|
|
|
|
ERR_FAIL_COND_V(id == 0, "");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2017-03-05 16:44:50 +01:00
|
|
|
jstring str = (jstring)env->CallObjectMethod(io, _dir_next, id);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!str) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return "";
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2019-02-26 21:54:45 +01:00
|
|
|
String ret = jstring_to_string((jstring)str, env);
|
2014-02-10 02:10:30 +01:00
|
|
|
env->DeleteLocalRef((jobject)str);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-07-21 12:07:01 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool DirAccessJAndroid::current_is_dir() const {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return env->CallBooleanMethod(io, _dir_is_dir, id);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2015-03-23 15:31:03 +01:00
|
|
|
|
|
|
|
bool DirAccessJAndroid::current_is_hidden() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
return current != "." && current != ".." && current.begins_with(".");
|
2015-03-23 15:31:03 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void DirAccessJAndroid::list_dir_end() {
|
2022-02-16 13:56:32 +01:00
|
|
|
if (id == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2017-03-05 16:44:50 +01:00
|
|
|
env->CallVoidMethod(io, _dir_close, id);
|
|
|
|
id = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int DirAccessJAndroid::get_drive_count() {
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2016-07-21 12:07:01 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String DirAccessJAndroid::get_drive(int p_drive) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error DirAccessJAndroid::change_dir(String p_dir) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (p_dir.is_empty() || p_dir == "." || (p_dir == ".." && current_dir.is_empty())) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return OK;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
String new_dir;
|
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (p_dir != "res://" && p_dir.length() > 1 && p_dir.ends_with("/")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_dir = p_dir.substr(0, p_dir.length() - 1);
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2015-04-28 14:38:07 +02:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (p_dir.begins_with("/")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
new_dir = p_dir.substr(1, p_dir.length());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else if (p_dir.begins_with("res://")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
new_dir = p_dir.substr(6, p_dir.length());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else if (current_dir.is_empty()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
new_dir = p_dir;
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
new_dir = current_dir.plus_file(p_dir);
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
//test if newdir exists
|
|
|
|
new_dir = new_dir.simplify_path();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
jstring js = env->NewStringUTF(new_dir.utf8().get_data());
|
2017-03-05 16:44:50 +01:00
|
|
|
int res = env->CallIntMethod(io, _dir_open, js);
|
2015-04-28 14:38:07 +02:00
|
|
|
env->DeleteLocalRef(js);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (res <= 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_INVALID_PARAMETER;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
env->CallVoidMethod(io, _dir_close, res);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
current_dir = new_dir;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
String DirAccessJAndroid::get_current_dir(bool p_include_drive) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
return "res://" + current_dir;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool DirAccessJAndroid::file_exists(String p_file) {
|
2014-02-10 02:10:30 +01:00
|
|
|
String sd;
|
2022-02-16 13:56:32 +01:00
|
|
|
if (current_dir.is_empty()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
sd = p_file;
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
sd = current_dir.plus_file(p_file);
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<FileAccessAndroid> f;
|
|
|
|
f.instantiate();
|
2014-02-10 02:10:30 +01:00
|
|
|
bool exists = f->file_exists(sd);
|
|
|
|
|
|
|
|
return exists;
|
|
|
|
}
|
|
|
|
|
2014-05-25 05:34:51 +02:00
|
|
|
bool DirAccessJAndroid::dir_exists(String p_dir) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2014-05-25 05:34:51 +02:00
|
|
|
|
|
|
|
String sd;
|
2015-04-19 01:11:33 +02:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (current_dir.is_empty()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
sd = p_dir;
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
|
|
|
if (p_dir.is_relative_path()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
sd = current_dir.plus_file(p_dir);
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
sd = fix_path(p_dir);
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2015-04-19 01:11:33 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String path = sd.simplify_path();
|
2014-05-25 05:34:51 +02:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (path.begins_with("/")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
path = path.substr(1, path.length());
|
2022-02-16 13:56:32 +01:00
|
|
|
} else if (path.begins_with("res://")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
path = path.substr(6, path.length());
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-05-25 05:34:51 +02:00
|
|
|
|
|
|
|
jstring js = env->NewStringUTF(path.utf8().get_data());
|
2017-03-05 16:44:50 +01:00
|
|
|
int res = env->CallIntMethod(io, _dir_open, js);
|
2015-04-28 14:38:07 +02:00
|
|
|
env->DeleteLocalRef(js);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (res <= 0) {
|
2014-05-25 05:34:51 +02:00
|
|
|
return false;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2014-05-25 05:34:51 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
env->CallVoidMethod(io, _dir_close, res);
|
2014-05-25 05:34:51 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error DirAccessJAndroid::make_dir(String p_dir) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(ERR_UNAVAILABLE);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error DirAccessJAndroid::rename(String p_from, String p_to) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(ERR_UNAVAILABLE);
|
|
|
|
}
|
2016-07-21 12:07:01 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error DirAccessJAndroid::remove(String p_name) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_V(ERR_UNAVAILABLE);
|
|
|
|
}
|
|
|
|
|
2019-01-21 19:23:08 +01:00
|
|
|
String DirAccessJAndroid::get_filesystem_type() const {
|
|
|
|
return "APK";
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:51:13 +01:00
|
|
|
uint64_t DirAccessJAndroid::get_space_left() {
|
2014-02-10 02:10:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void DirAccessJAndroid::setup(jobject p_io) {
|
2021-01-19 13:29:41 +01:00
|
|
|
JNIEnv *env = get_jni_env();
|
2017-03-05 16:44:50 +01:00
|
|
|
io = p_io;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
jclass c = env->GetObjectClass(io);
|
|
|
|
cls = (jclass)env->NewGlobalRef(c);
|
|
|
|
|
|
|
|
_dir_open = env->GetMethodID(cls, "dir_open", "(Ljava/lang/String;)I");
|
|
|
|
_dir_next = env->GetMethodID(cls, "dir_next", "(I)Ljava/lang/String;");
|
|
|
|
_dir_close = env->GetMethodID(cls, "dir_close", "(I)V");
|
2015-04-28 14:38:07 +02:00
|
|
|
_dir_is_dir = env->GetMethodID(cls, "dir_is_dir", "(I)Z");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DirAccessJAndroid::DirAccessJAndroid() {
|
|
|
|
}
|
|
|
|
|
|
|
|
DirAccessJAndroid::~DirAccessJAndroid() {
|
2016-11-03 16:41:16 +01:00
|
|
|
list_dir_end();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|