dir_access_jandroid.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**************************************************************************/
  2. /* dir_access_jandroid.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "dir_access_jandroid.h"
  31. #include "string_android.h"
  32. #include "thread_jandroid.h"
  33. #include "core/string/print_string.h"
  34. jobject DirAccessJAndroid::dir_access_handler = nullptr;
  35. jclass DirAccessJAndroid::cls = nullptr;
  36. jmethodID DirAccessJAndroid::_dir_open = nullptr;
  37. jmethodID DirAccessJAndroid::_dir_next = nullptr;
  38. jmethodID DirAccessJAndroid::_dir_close = nullptr;
  39. jmethodID DirAccessJAndroid::_dir_is_dir = nullptr;
  40. jmethodID DirAccessJAndroid::_dir_exists = nullptr;
  41. jmethodID DirAccessJAndroid::_file_exists = nullptr;
  42. jmethodID DirAccessJAndroid::_get_drive_count = nullptr;
  43. jmethodID DirAccessJAndroid::_get_drive = nullptr;
  44. jmethodID DirAccessJAndroid::_make_dir = nullptr;
  45. jmethodID DirAccessJAndroid::_get_space_left = nullptr;
  46. jmethodID DirAccessJAndroid::_rename = nullptr;
  47. jmethodID DirAccessJAndroid::_remove = nullptr;
  48. jmethodID DirAccessJAndroid::_current_is_hidden = nullptr;
  49. Error DirAccessJAndroid::list_dir_begin() {
  50. list_dir_end();
  51. int res = dir_open(current_dir);
  52. if (res <= 0) {
  53. return ERR_CANT_OPEN;
  54. }
  55. id = res;
  56. return OK;
  57. }
  58. String DirAccessJAndroid::get_next() {
  59. ERR_FAIL_COND_V(id == 0, "");
  60. if (_dir_next) {
  61. JNIEnv *env = get_jni_env();
  62. ERR_FAIL_NULL_V(env, "");
  63. jstring str = (jstring)env->CallObjectMethod(dir_access_handler, _dir_next, id);
  64. if (!str) {
  65. return "";
  66. }
  67. String ret = jstring_to_string((jstring)str, env);
  68. env->DeleteLocalRef((jobject)str);
  69. return ret;
  70. } else {
  71. return "";
  72. }
  73. }
  74. bool DirAccessJAndroid::current_is_dir() const {
  75. if (_dir_is_dir) {
  76. JNIEnv *env = get_jni_env();
  77. ERR_FAIL_NULL_V(env, false);
  78. return env->CallBooleanMethod(dir_access_handler, _dir_is_dir, id);
  79. } else {
  80. return false;
  81. }
  82. }
  83. bool DirAccessJAndroid::current_is_hidden() const {
  84. if (_current_is_hidden) {
  85. JNIEnv *env = get_jni_env();
  86. ERR_FAIL_NULL_V(env, false);
  87. return env->CallBooleanMethod(dir_access_handler, _current_is_hidden, id);
  88. }
  89. return false;
  90. }
  91. void DirAccessJAndroid::list_dir_end() {
  92. if (id == 0) {
  93. return;
  94. }
  95. dir_close(id);
  96. id = 0;
  97. }
  98. int DirAccessJAndroid::get_drive_count() {
  99. if (_get_drive_count) {
  100. JNIEnv *env = get_jni_env();
  101. ERR_FAIL_NULL_V(env, 0);
  102. return env->CallIntMethod(dir_access_handler, _get_drive_count, get_access_type());
  103. } else {
  104. return 0;
  105. }
  106. }
  107. String DirAccessJAndroid::get_drive(int p_drive) {
  108. if (_get_drive) {
  109. JNIEnv *env = get_jni_env();
  110. ERR_FAIL_NULL_V(env, "");
  111. jstring j_drive = (jstring)env->CallObjectMethod(dir_access_handler, _get_drive, get_access_type(), p_drive);
  112. if (!j_drive) {
  113. return "";
  114. }
  115. String drive = jstring_to_string(j_drive, env);
  116. env->DeleteLocalRef(j_drive);
  117. return drive;
  118. } else {
  119. return "";
  120. }
  121. }
  122. String DirAccessJAndroid::_get_root_string() const {
  123. if (get_access_type() == ACCESS_FILESYSTEM) {
  124. return "/";
  125. }
  126. return DirAccessUnix::_get_root_string();
  127. }
  128. String DirAccessJAndroid::get_current_dir(bool p_include_drive) const {
  129. String base = _get_root_path();
  130. String bd = current_dir;
  131. if (!base.is_empty()) {
  132. bd = current_dir.replace_first(base, "");
  133. }
  134. String root_string = _get_root_string();
  135. if (bd.begins_with(root_string)) {
  136. return bd;
  137. } else if (bd.begins_with("/")) {
  138. return root_string + bd.substr(1, bd.length());
  139. } else {
  140. return root_string + bd;
  141. }
  142. }
  143. Error DirAccessJAndroid::change_dir(String p_dir) {
  144. String new_dir = get_absolute_path(p_dir);
  145. if (new_dir == current_dir) {
  146. return OK;
  147. }
  148. if (!dir_exists(new_dir)) {
  149. return ERR_INVALID_PARAMETER;
  150. }
  151. current_dir = new_dir;
  152. return OK;
  153. }
  154. String DirAccessJAndroid::get_absolute_path(String p_path) {
  155. if (current_dir != "" && p_path == current_dir) {
  156. return current_dir;
  157. }
  158. if (p_path.is_relative_path()) {
  159. p_path = get_current_dir().path_join(p_path);
  160. }
  161. p_path = fix_path(p_path);
  162. p_path = p_path.simplify_path();
  163. return p_path;
  164. }
  165. bool DirAccessJAndroid::file_exists(String p_file) {
  166. if (_file_exists) {
  167. JNIEnv *env = get_jni_env();
  168. ERR_FAIL_NULL_V(env, false);
  169. String path = get_absolute_path(p_file);
  170. jstring j_path = env->NewStringUTF(path.utf8().get_data());
  171. bool result = env->CallBooleanMethod(dir_access_handler, _file_exists, get_access_type(), j_path);
  172. env->DeleteLocalRef(j_path);
  173. return result;
  174. } else {
  175. return false;
  176. }
  177. }
  178. bool DirAccessJAndroid::dir_exists(String p_dir) {
  179. if (_dir_exists) {
  180. JNIEnv *env = get_jni_env();
  181. ERR_FAIL_NULL_V(env, false);
  182. String path = get_absolute_path(p_dir);
  183. jstring j_path = env->NewStringUTF(path.utf8().get_data());
  184. bool result = env->CallBooleanMethod(dir_access_handler, _dir_exists, get_access_type(), j_path);
  185. env->DeleteLocalRef(j_path);
  186. return result;
  187. } else {
  188. return false;
  189. }
  190. }
  191. Error DirAccessJAndroid::make_dir(String p_dir) {
  192. // Check if the directory exists already
  193. if (dir_exists(p_dir)) {
  194. return ERR_ALREADY_EXISTS;
  195. }
  196. if (_make_dir) {
  197. JNIEnv *env = get_jni_env();
  198. ERR_FAIL_NULL_V(env, ERR_UNCONFIGURED);
  199. String path = get_absolute_path(p_dir);
  200. jstring j_dir = env->NewStringUTF(path.utf8().get_data());
  201. bool result = env->CallBooleanMethod(dir_access_handler, _make_dir, get_access_type(), j_dir);
  202. env->DeleteLocalRef(j_dir);
  203. if (result) {
  204. return OK;
  205. } else {
  206. return FAILED;
  207. }
  208. } else {
  209. return ERR_UNCONFIGURED;
  210. }
  211. }
  212. Error DirAccessJAndroid::make_dir_recursive(const String &p_dir) {
  213. Error err = make_dir(p_dir);
  214. if (err != OK && err != ERR_ALREADY_EXISTS) {
  215. ERR_FAIL_V_MSG(err, "Could not create directory: " + p_dir);
  216. }
  217. return OK;
  218. }
  219. Error DirAccessJAndroid::rename(String p_from, String p_to) {
  220. if (_rename) {
  221. JNIEnv *env = get_jni_env();
  222. ERR_FAIL_NULL_V(env, ERR_UNCONFIGURED);
  223. String from_path = get_absolute_path(p_from);
  224. jstring j_from = env->NewStringUTF(from_path.utf8().get_data());
  225. String to_path = get_absolute_path(p_to);
  226. jstring j_to = env->NewStringUTF(to_path.utf8().get_data());
  227. bool result = env->CallBooleanMethod(dir_access_handler, _rename, get_access_type(), j_from, j_to);
  228. env->DeleteLocalRef(j_from);
  229. env->DeleteLocalRef(j_to);
  230. if (result) {
  231. return OK;
  232. } else {
  233. return FAILED;
  234. }
  235. } else {
  236. return ERR_UNCONFIGURED;
  237. }
  238. }
  239. Error DirAccessJAndroid::remove(String p_name) {
  240. if (_remove) {
  241. JNIEnv *env = get_jni_env();
  242. ERR_FAIL_NULL_V(env, ERR_UNCONFIGURED);
  243. String path = get_absolute_path(p_name);
  244. jstring j_name = env->NewStringUTF(path.utf8().get_data());
  245. bool result = env->CallBooleanMethod(dir_access_handler, _remove, get_access_type(), j_name);
  246. env->DeleteLocalRef(j_name);
  247. if (result) {
  248. return OK;
  249. } else {
  250. return FAILED;
  251. }
  252. } else {
  253. return ERR_UNCONFIGURED;
  254. }
  255. }
  256. uint64_t DirAccessJAndroid::get_space_left() {
  257. if (_get_space_left) {
  258. JNIEnv *env = get_jni_env();
  259. ERR_FAIL_NULL_V(env, 0);
  260. return env->CallLongMethod(dir_access_handler, _get_space_left, get_access_type());
  261. } else {
  262. return 0;
  263. }
  264. }
  265. void DirAccessJAndroid::setup(jobject p_dir_access_handler) {
  266. JNIEnv *env = get_jni_env();
  267. dir_access_handler = env->NewGlobalRef(p_dir_access_handler);
  268. jclass c = env->GetObjectClass(dir_access_handler);
  269. cls = (jclass)env->NewGlobalRef(c);
  270. _dir_open = env->GetMethodID(cls, "dirOpen", "(ILjava/lang/String;)I");
  271. _dir_next = env->GetMethodID(cls, "dirNext", "(I)Ljava/lang/String;");
  272. _dir_close = env->GetMethodID(cls, "dirClose", "(I)V");
  273. _dir_is_dir = env->GetMethodID(cls, "dirIsDir", "(I)Z");
  274. _dir_exists = env->GetMethodID(cls, "dirExists", "(ILjava/lang/String;)Z");
  275. _file_exists = env->GetMethodID(cls, "fileExists", "(ILjava/lang/String;)Z");
  276. _get_drive_count = env->GetMethodID(cls, "getDriveCount", "(I)I");
  277. _get_drive = env->GetMethodID(cls, "getDrive", "(II)Ljava/lang/String;");
  278. _make_dir = env->GetMethodID(cls, "makeDir", "(ILjava/lang/String;)Z");
  279. _get_space_left = env->GetMethodID(cls, "getSpaceLeft", "(I)J");
  280. _rename = env->GetMethodID(cls, "rename", "(ILjava/lang/String;Ljava/lang/String;)Z");
  281. _remove = env->GetMethodID(cls, "remove", "(ILjava/lang/String;)Z");
  282. _current_is_hidden = env->GetMethodID(cls, "isCurrentHidden", "(I)Z");
  283. }
  284. void DirAccessJAndroid::terminate() {
  285. JNIEnv *env = get_jni_env();
  286. ERR_FAIL_NULL(env);
  287. env->DeleteGlobalRef(cls);
  288. env->DeleteGlobalRef(dir_access_handler);
  289. }
  290. DirAccessJAndroid::DirAccessJAndroid() {
  291. }
  292. DirAccessJAndroid::~DirAccessJAndroid() {
  293. list_dir_end();
  294. }
  295. int DirAccessJAndroid::dir_open(String p_path) {
  296. if (_dir_open) {
  297. JNIEnv *env = get_jni_env();
  298. ERR_FAIL_NULL_V(env, 0);
  299. String path = get_absolute_path(p_path);
  300. jstring js = env->NewStringUTF(path.utf8().get_data());
  301. int dirId = env->CallIntMethod(dir_access_handler, _dir_open, get_access_type(), js);
  302. env->DeleteLocalRef(js);
  303. return dirId;
  304. } else {
  305. return 0;
  306. }
  307. }
  308. void DirAccessJAndroid::dir_close(int p_id) {
  309. if (_dir_close) {
  310. JNIEnv *env = get_jni_env();
  311. ERR_FAIL_NULL(env);
  312. env->CallVoidMethod(dir_access_handler, _dir_close, p_id);
  313. }
  314. }