dir_access_jandroid.cpp 6.9 KB

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