dir_access_jandroid.cpp 6.2 KB

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