dir_access_jandroid.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*************************************************************************/
  2. /* dir_access_jandroid.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef ANDROID_NATIVE_ACTIVITY
  30. #include "dir_access_jandroid.h"
  31. #include "file_access_jandroid.h"
  32. #include "thread_jandroid.h"
  33. #include "print_string.h"
  34. jobject DirAccessJAndroid::io=NULL;
  35. jclass DirAccessJAndroid::cls=NULL;
  36. jmethodID DirAccessJAndroid::_dir_open=NULL;
  37. jmethodID DirAccessJAndroid::_dir_next=NULL;
  38. jmethodID DirAccessJAndroid::_dir_close=NULL;
  39. jmethodID DirAccessJAndroid::_dir_is_dir=NULL;
  40. DirAccess *DirAccessJAndroid::create_fs() {
  41. return memnew(DirAccessJAndroid);
  42. }
  43. bool DirAccessJAndroid::list_dir_begin() {
  44. list_dir_end();
  45. JNIEnv *env = ThreadAndroid::get_env();
  46. jstring js = env->NewStringUTF(current_dir.utf8().get_data());
  47. int res = env->CallIntMethod(io,_dir_open,js);
  48. if (res<=0)
  49. return true;
  50. id=res;
  51. return false;
  52. }
  53. String DirAccessJAndroid::get_next(){
  54. ERR_FAIL_COND_V(id==0,"");
  55. JNIEnv *env = ThreadAndroid::get_env();
  56. jstring str= (jstring)env->CallObjectMethod(io,_dir_next,id);
  57. if (!str)
  58. return "";
  59. String ret = String::utf8(env->GetStringUTFChars( (jstring)str, NULL ));
  60. env->DeleteLocalRef((jobject)str);
  61. return ret;
  62. }
  63. bool DirAccessJAndroid::current_is_dir() const{
  64. JNIEnv *env = ThreadAndroid::get_env();
  65. return env->CallBooleanMethod(io,_dir_is_dir,id);
  66. }
  67. bool DirAccessJAndroid::current_is_hidden() const {
  68. return current!="." && current!=".." && current.begins_with(".");
  69. }
  70. void DirAccessJAndroid::list_dir_end(){
  71. if (id==0)
  72. return;
  73. JNIEnv *env = ThreadAndroid::get_env();
  74. env->CallObjectMethod(io,_dir_close,id);
  75. id=0;
  76. }
  77. int DirAccessJAndroid::get_drive_count(){
  78. return 0;
  79. }
  80. String DirAccessJAndroid::get_drive(int p_drive){
  81. return "";
  82. }
  83. Error DirAccessJAndroid::change_dir(String p_dir){
  84. JNIEnv *env = ThreadAndroid::get_env();
  85. p_dir=p_dir.simplify_path();
  86. if (p_dir=="" || p_dir=="." || (p_dir==".." && current_dir==""))
  87. return OK;
  88. String new_dir;
  89. if (p_dir!="res://" && p_dir.length()>1 && p_dir.ends_with("/"))
  90. p_dir=p_dir.substr(0,p_dir.length()-1);
  91. if (p_dir.begins_with("/"))
  92. new_dir=p_dir.substr(1,p_dir.length());
  93. else if (p_dir.begins_with("res://"))
  94. new_dir=p_dir.substr(6,p_dir.length());
  95. else if (current_dir=="")
  96. new_dir=p_dir;
  97. else
  98. new_dir=current_dir.plus_file(p_dir);
  99. //print_line("new dir is: "+new_dir);
  100. //test if newdir exists
  101. new_dir=new_dir.simplify_path();
  102. jstring js = env->NewStringUTF(new_dir.utf8().get_data());
  103. int res = env->CallIntMethod(io,_dir_open,js);
  104. env->DeleteLocalRef(js);
  105. if (res<=0)
  106. return ERR_INVALID_PARAMETER;
  107. env->CallObjectMethod(io,_dir_close,res);
  108. current_dir=new_dir;
  109. return OK;
  110. }
  111. String DirAccessJAndroid::get_current_dir(){
  112. return "/"+current_dir;
  113. }
  114. bool DirAccessJAndroid::file_exists(String p_file){
  115. JNIEnv *env = ThreadAndroid::get_env();
  116. String sd;
  117. if (current_dir=="")
  118. sd=p_file;
  119. else
  120. sd=current_dir.plus_file(p_file);
  121. FileAccessJAndroid *f = memnew(FileAccessJAndroid);
  122. bool exists = f->file_exists(sd);
  123. memdelete(f);
  124. return exists;
  125. }
  126. bool DirAccessJAndroid::dir_exists(String p_dir) {
  127. JNIEnv *env = ThreadAndroid::get_env();
  128. String sd;
  129. if (current_dir=="")
  130. sd=p_dir;
  131. else {
  132. if (p_dir.is_rel_path())
  133. sd=current_dir.plus_file(p_dir);
  134. else
  135. sd=fix_path(p_dir);
  136. }
  137. String path=sd.simplify_path();
  138. if (path.begins_with("/"))
  139. path=path.substr(1,path.length());
  140. else if (path.begins_with("res://"))
  141. path=path.substr(6,path.length());
  142. jstring js = env->NewStringUTF(path.utf8().get_data());
  143. int res = env->CallIntMethod(io,_dir_open,js);
  144. env->DeleteLocalRef(js);
  145. if (res<=0)
  146. return false;
  147. env->CallVoidMethod(io,_dir_close,res);
  148. env->DeleteLocalRef(js);
  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