dir_access_jandroid.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 DirAccessJAndroid::current_is_hidden() const {
  86. return current!="." && current!=".." && current.begins_with(".");
  87. }
  88. bool DirAccessAndroid::current_is_hidden() const{
  89. return current!="." && current!=".." && current.begins_with(".");
  90. }
  91. void DirAccessJAndroid::list_dir_end(){
  92. if (id==0)
  93. return;
  94. JNIEnv *env = ThreadAndroid::get_env();
  95. env->CallObjectMethod(io,_dir_close,id);
  96. id=0;
  97. }
  98. int DirAccessJAndroid::get_drive_count(){
  99. return 0;
  100. }
  101. String DirAccessJAndroid::get_drive(int p_drive){
  102. return "";
  103. }
  104. Error DirAccessJAndroid::change_dir(String p_dir){
  105. JNIEnv *env = ThreadAndroid::get_env();
  106. p_dir=p_dir.simplify_path();
  107. if (p_dir=="" || p_dir=="." || (p_dir==".." && current_dir==""))
  108. return OK;
  109. String new_dir;
  110. if (p_dir.begins_with("/"))
  111. new_dir=p_dir.substr(1,p_dir.length());
  112. else if (p_dir.begins_with("res://"))
  113. new_dir=p_dir.substr(6,p_dir.length());
  114. else //relative
  115. new_dir=new_dir+"/"+p_dir;
  116. //test if newdir exists
  117. new_dir=new_dir.simplify_path();
  118. jstring js = env->NewStringUTF(new_dir.utf8().get_data());
  119. int res = env->CallIntMethod(io,_dir_open,js);
  120. if (res<=0)
  121. return ERR_INVALID_PARAMETER;
  122. env->CallObjectMethod(io,_dir_close,res);
  123. return OK;
  124. }
  125. String DirAccessJAndroid::get_current_dir(){
  126. return "/"+current_dir;
  127. }
  128. bool DirAccessJAndroid::file_exists(String p_file){
  129. JNIEnv *env = ThreadAndroid::get_env();
  130. String sd;
  131. if (current_dir=="")
  132. sd=p_file;
  133. else
  134. sd=current_dir+"/"+p_file;
  135. FileAccessJAndroid *f = memnew(FileAccessJAndroid);
  136. bool exists = f->file_exists(sd);
  137. memdelete(f);
  138. return exists;
  139. }
  140. bool DirAccessJAndroid::dir_exists(String p_dir) {
  141. JNIEnv *env = ThreadAndroid::get_env();
  142. String sd;
  143. if (current_dir=="")
  144. sd=p_dir;
  145. else
  146. sd=current_dir+"/"+p_dir;
  147. String path=fix_path(sd).simplify_path();
  148. if (path.begins_with("/"))
  149. path=path.substr(1,path.length());
  150. else if (path.begins_with("res://"))
  151. path=path.substr(6,path.length());
  152. jstring js = env->NewStringUTF(path.utf8().get_data());
  153. int res = env->CallIntMethod(io,_dir_open,js);
  154. if (res<=0)
  155. return false;
  156. env->CallVoidMethod(io,_dir_close,res);
  157. env->DeleteLocalRef(js);
  158. return true;
  159. }
  160. Error DirAccessJAndroid::make_dir(String p_dir){
  161. ERR_FAIL_V(ERR_UNAVAILABLE);
  162. }
  163. Error DirAccessJAndroid::rename(String p_from, String p_to){
  164. ERR_FAIL_V(ERR_UNAVAILABLE);
  165. }
  166. Error DirAccessJAndroid::remove(String p_name){
  167. ERR_FAIL_V(ERR_UNAVAILABLE);
  168. }
  169. //FileType get_file_type() const;
  170. size_t DirAccessJAndroid::get_space_left() {
  171. return 0;
  172. }
  173. void DirAccessJAndroid::setup( jobject p_io) {
  174. JNIEnv *env = ThreadAndroid::get_env();
  175. io=p_io;
  176. __android_log_print(ANDROID_LOG_INFO,"godot","STEP7");
  177. jclass c = env->GetObjectClass(io);
  178. cls = (jclass)env->NewGlobalRef(c);
  179. __android_log_print(ANDROID_LOG_INFO,"godot","STEP8");
  180. _dir_open = env->GetMethodID(cls, "dir_open", "(Ljava/lang/String;)I");
  181. if(_dir_open != 0) {
  182. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _dir_open ok!!");
  183. }
  184. _dir_next = env->GetMethodID(cls, "dir_next", "(I)Ljava/lang/String;");
  185. if(_dir_next != 0) {
  186. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _dir_next ok!!");
  187. }
  188. _dir_close = env->GetMethodID(cls, "dir_close", "(I)V");
  189. if(_dir_close != 0) {
  190. __android_log_print(ANDROID_LOG_INFO,"godot","*******GOT METHOD _dir_close ok!!");
  191. }
  192. // (*env)->CallVoidMethod(env,obj,aMethodID, myvar);
  193. }
  194. DirAccessJAndroid::DirAccessJAndroid() {
  195. id=0;
  196. }
  197. DirAccessJAndroid::~DirAccessJAndroid() {
  198. list_dir_end();;
  199. }
  200. #endif