dir_access_jandroid.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*************************************************************************/
  2. /* dir_access_jandroid.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  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. #ifndef ANDROID_NATIVE_ACTIVITY
  31. #include "dir_access_jandroid.h"
  32. #include "file_access_jandroid.h"
  33. #include "print_string.h"
  34. #include "thread_jandroid.h"
  35. jobject DirAccessJAndroid::io = NULL;
  36. jclass DirAccessJAndroid::cls = NULL;
  37. jmethodID DirAccessJAndroid::_dir_open = NULL;
  38. jmethodID DirAccessJAndroid::_dir_next = NULL;
  39. jmethodID DirAccessJAndroid::_dir_close = NULL;
  40. jmethodID DirAccessJAndroid::_dir_is_dir = NULL;
  41. DirAccess *DirAccessJAndroid::create_fs() {
  42. return memnew(DirAccessJAndroid);
  43. }
  44. Error DirAccessJAndroid::list_dir_begin() {
  45. list_dir_end();
  46. JNIEnv *env = ThreadAndroid::get_env();
  47. jstring js = env->NewStringUTF(current_dir.utf8().get_data());
  48. int res = env->CallIntMethod(io, _dir_open, js);
  49. if (res <= 0)
  50. return ERR_CANT_OPEN;
  51. id = res;
  52. return OK;
  53. }
  54. String DirAccessJAndroid::get_next() {
  55. ERR_FAIL_COND_V(id == 0, "");
  56. JNIEnv *env = ThreadAndroid::get_env();
  57. jstring str = (jstring)env->CallObjectMethod(io, _dir_next, id);
  58. if (!str)
  59. return "";
  60. String ret = String::utf8(env->GetStringUTFChars((jstring)str, NULL));
  61. env->DeleteLocalRef((jobject)str);
  62. return ret;
  63. }
  64. bool DirAccessJAndroid::current_is_dir() const {
  65. JNIEnv *env = ThreadAndroid::get_env();
  66. return env->CallBooleanMethod(io, _dir_is_dir, id);
  67. }
  68. bool DirAccessJAndroid::current_is_hidden() const {
  69. return current != "." && current != ".." && current.begins_with(".");
  70. }
  71. void DirAccessJAndroid::list_dir_end() {
  72. if (id == 0)
  73. return;
  74. JNIEnv *env = ThreadAndroid::get_env();
  75. env->CallVoidMethod(io, _dir_close, id);
  76. id = 0;
  77. }
  78. int DirAccessJAndroid::get_drive_count() {
  79. return 0;
  80. }
  81. String DirAccessJAndroid::get_drive(int p_drive) {
  82. return "";
  83. }
  84. Error DirAccessJAndroid::change_dir(String p_dir) {
  85. JNIEnv *env = ThreadAndroid::get_env();
  86. p_dir = p_dir.simplify_path();
  87. if (p_dir == "" || p_dir == "." || (p_dir == ".." && current_dir == ""))
  88. return OK;
  89. String new_dir;
  90. if (p_dir != "res://" && p_dir.length() > 1 && p_dir.ends_with("/"))
  91. p_dir = p_dir.substr(0, p_dir.length() - 1);
  92. if (p_dir.begins_with("/"))
  93. new_dir = p_dir.substr(1, p_dir.length());
  94. else if (p_dir.begins_with("res://"))
  95. new_dir = p_dir.substr(6, p_dir.length());
  96. else if (current_dir == "")
  97. new_dir = p_dir;
  98. else
  99. new_dir = current_dir.plus_file(p_dir);
  100. //print_line("new dir is: "+new_dir);
  101. //test if newdir exists
  102. new_dir = new_dir.simplify_path();
  103. jstring js = env->NewStringUTF(new_dir.utf8().get_data());
  104. int res = env->CallIntMethod(io, _dir_open, js);
  105. env->DeleteLocalRef(js);
  106. if (res <= 0)
  107. return ERR_INVALID_PARAMETER;
  108. env->CallVoidMethod(io, _dir_close, res);
  109. current_dir = new_dir;
  110. return OK;
  111. }
  112. String DirAccessJAndroid::get_current_dir() {
  113. return "res://" + current_dir;
  114. }
  115. bool DirAccessJAndroid::file_exists(String p_file) {
  116. JNIEnv *env = ThreadAndroid::get_env();
  117. String sd;
  118. if (current_dir == "")
  119. sd = p_file;
  120. else
  121. sd = current_dir.plus_file(p_file);
  122. FileAccessJAndroid *f = memnew(FileAccessJAndroid);
  123. bool exists = f->file_exists(sd);
  124. memdelete(f);
  125. return exists;
  126. }
  127. bool DirAccessJAndroid::dir_exists(String p_dir) {
  128. JNIEnv *env = ThreadAndroid::get_env();
  129. String sd;
  130. if (current_dir == "")
  131. sd = p_dir;
  132. else {
  133. if (p_dir.is_rel_path())
  134. sd = current_dir.plus_file(p_dir);
  135. else
  136. sd = fix_path(p_dir);
  137. }
  138. String path = sd.simplify_path();
  139. if (path.begins_with("/"))
  140. path = path.substr(1, path.length());
  141. else if (path.begins_with("res://"))
  142. path = path.substr(6, path.length());
  143. jstring js = env->NewStringUTF(path.utf8().get_data());
  144. int res = env->CallIntMethod(io, _dir_open, js);
  145. env->DeleteLocalRef(js);
  146. if (res <= 0)
  147. return false;
  148. env->CallVoidMethod(io, _dir_close, res);
  149. return true;
  150. }
  151. Error DirAccessJAndroid::make_dir(String p_dir) {
  152. ERR_FAIL_V(ERR_UNAVAILABLE);
  153. }
  154. Error DirAccessJAndroid::rename(String p_from, String p_to) {
  155. ERR_FAIL_V(ERR_UNAVAILABLE);
  156. }
  157. Error DirAccessJAndroid::remove(String p_name) {
  158. ERR_FAIL_V(ERR_UNAVAILABLE);
  159. }
  160. //FileType get_file_type() const;
  161. size_t DirAccessJAndroid::get_space_left() {
  162. return 0;
  163. }
  164. void DirAccessJAndroid::setup(jobject p_io) {
  165. JNIEnv *env = ThreadAndroid::get_env();
  166. io = p_io;
  167. __android_log_print(ANDROID_LOG_INFO, "godot", "STEP7");
  168. jclass c = env->GetObjectClass(io);
  169. cls = (jclass)env->NewGlobalRef(c);
  170. __android_log_print(ANDROID_LOG_INFO, "godot", "STEP8");
  171. _dir_open = env->GetMethodID(cls, "dir_open", "(Ljava/lang/String;)I");
  172. if (_dir_open != 0) {
  173. __android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _dir_open ok!!");
  174. }
  175. _dir_next = env->GetMethodID(cls, "dir_next", "(I)Ljava/lang/String;");
  176. if (_dir_next != 0) {
  177. __android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _dir_next ok!!");
  178. }
  179. _dir_close = env->GetMethodID(cls, "dir_close", "(I)V");
  180. if (_dir_close != 0) {
  181. __android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _dir_close ok!!");
  182. }
  183. _dir_is_dir = env->GetMethodID(cls, "dir_is_dir", "(I)Z");
  184. if (_dir_is_dir != 0) {
  185. __android_log_print(ANDROID_LOG_INFO, "godot", "*******GOT METHOD _dir_is_dir ok!!");
  186. }
  187. //(*env)->CallVoidMethod(env,obj,aMethodID, myvar);
  188. }
  189. DirAccessJAndroid::DirAccessJAndroid() {
  190. id = 0;
  191. }
  192. DirAccessJAndroid::~DirAccessJAndroid() {
  193. list_dir_end();
  194. }
  195. #endif