android.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * Copyright (c) 2006-2019 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "android.h"
  21. #ifdef LOVE_ANDROID
  22. #include "SDL.h"
  23. #include "jni.h"
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. namespace love
  29. {
  30. namespace android
  31. {
  32. void setImmersive(bool immersive_active)
  33. {
  34. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  35. jobject activity = (jobject) SDL_AndroidGetActivity();
  36. jclass clazz(env->GetObjectClass(activity));
  37. jmethodID method_id = env->GetMethodID(clazz, "setImmersiveMode", "(Z)V");
  38. env->CallVoidMethod(activity, method_id, immersive_active);
  39. env->DeleteLocalRef(activity);
  40. env->DeleteLocalRef(clazz);
  41. }
  42. bool getImmersive()
  43. {
  44. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  45. jobject activity = (jobject) SDL_AndroidGetActivity();
  46. jclass clazz(env->GetObjectClass(activity));
  47. jmethodID method_id = env->GetMethodID(clazz, "getImmersiveMode", "()Z");
  48. jboolean immersive_active = env->CallBooleanMethod(activity, method_id);
  49. env->DeleteLocalRef(activity);
  50. env->DeleteLocalRef(clazz);
  51. return immersive_active;
  52. }
  53. double getScreenScale()
  54. {
  55. static double result = -1.;
  56. if (result == -1.)
  57. {
  58. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  59. jclass activity = env->FindClass("org/love2d/android/GameActivity");
  60. jmethodID getMetrics = env->GetStaticMethodID(activity, "getMetrics", "()Landroid/util/DisplayMetrics;");
  61. jobject metrics = env->CallStaticObjectMethod(activity, getMetrics);
  62. jclass metricsClass = env->GetObjectClass(metrics);
  63. result = env->GetFloatField(metrics, env->GetFieldID(metricsClass, "density", "F"));
  64. env->DeleteLocalRef(metricsClass);
  65. env->DeleteLocalRef(metrics);
  66. env->DeleteLocalRef(activity);
  67. }
  68. return result;
  69. }
  70. bool getSafeArea(int &top, int &left, int &bottom, int &right)
  71. {
  72. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  73. jobject activity = (jobject) SDL_AndroidGetActivity();
  74. jclass clazz(env->GetObjectClass(activity));
  75. jmethodID methodID = env->GetMethodID(clazz, "initializeSafeArea", "()Z");
  76. bool hasSafeArea = false;
  77. if (methodID == nullptr)
  78. // NoSuchMethodException is thrown in case methodID is null
  79. env->ExceptionClear();
  80. else if ((hasSafeArea = env->CallBooleanMethod(activity, methodID)))
  81. {
  82. top = env->GetIntField(activity, env->GetFieldID(clazz, "safeAreaTop", "I"));
  83. left = env->GetIntField(activity, env->GetFieldID(clazz, "safeAreaLeft", "I"));
  84. bottom = env->GetIntField(activity, env->GetFieldID(clazz, "safeAreaBottom", "I"));
  85. right = env->GetIntField(activity, env->GetFieldID(clazz, "safeAreaRight", "I"));
  86. }
  87. env->DeleteLocalRef(clazz);
  88. env->DeleteLocalRef(activity);
  89. return hasSafeArea;
  90. }
  91. const char *getSelectedGameFile()
  92. {
  93. static const char *path = NULL;
  94. if (path)
  95. {
  96. delete path;
  97. path = NULL;
  98. }
  99. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  100. jclass activity = env->FindClass("org/love2d/android/GameActivity");
  101. jmethodID getGamePath = env->GetStaticMethodID(activity, "getGamePath", "()Ljava/lang/String;");
  102. jstring gamePath = (jstring) env->CallStaticObjectMethod(activity, getGamePath);
  103. const char *utf = env->GetStringUTFChars(gamePath, 0);
  104. if (utf)
  105. {
  106. path = SDL_strdup(utf);
  107. env->ReleaseStringUTFChars(gamePath, utf);
  108. }
  109. env->DeleteLocalRef(gamePath);
  110. env->DeleteLocalRef(activity);
  111. return path;
  112. }
  113. bool openURL(const std::string &url)
  114. {
  115. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  116. jclass activity = env->FindClass("org/love2d/android/GameActivity");
  117. jmethodID openURL = env->GetStaticMethodID(activity, "openURL", "(Ljava/lang/String;)Z");
  118. jstring url_jstring = (jstring) env->NewStringUTF(url.c_str());
  119. jboolean result = env->CallStaticBooleanMethod(activity, openURL, url_jstring);
  120. env->DeleteLocalRef(url_jstring);
  121. env->DeleteLocalRef(activity);
  122. return result;
  123. }
  124. void vibrate(double seconds)
  125. {
  126. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  127. jclass activity = env->FindClass("org/love2d/android/GameActivity");
  128. jmethodID vibrate_method = env->GetStaticMethodID(activity, "vibrate", "(D)V");
  129. env->CallStaticVoidMethod(activity, vibrate_method, seconds);
  130. env->DeleteLocalRef(activity);
  131. }
  132. /*
  133. * Helper functions for the filesystem module
  134. */
  135. void freeGameArchiveMemory(void *ptr)
  136. {
  137. char *game_love_data = static_cast<char*>(ptr);
  138. delete[] game_love_data;
  139. }
  140. bool loadGameArchiveToMemory(const char* filename, char **ptr, size_t *size)
  141. {
  142. SDL_RWops *asset_game_file = SDL_RWFromFile(filename, "rb");
  143. if (!asset_game_file) {
  144. SDL_Log("Could not find %s", filename);
  145. return false;
  146. }
  147. Sint64 file_size = asset_game_file->size(asset_game_file);
  148. if (file_size <= 0) {
  149. SDL_Log("Could not load game from %s. File has invalid file size: %d.", filename, (int) file_size);
  150. return false;
  151. }
  152. *ptr = new char[file_size];
  153. if (!*ptr) {
  154. SDL_Log("Could not allocate memory for in-memory game archive");
  155. return false;
  156. }
  157. size_t bytes_copied = asset_game_file->read(asset_game_file, (void*) *ptr, sizeof(char), (size_t) file_size);
  158. if (bytes_copied != file_size) {
  159. SDL_Log("Incomplete copy of in-memory game archive!");
  160. delete[] *ptr;
  161. return false;
  162. }
  163. *size = (size_t) file_size;
  164. return true;
  165. }
  166. bool directoryExists(const char *path)
  167. {
  168. struct stat s;
  169. int err = stat(path, &s);
  170. if (err == -1)
  171. {
  172. if (errno != ENOENT)
  173. SDL_Log("Error checking for directory %s errno = %d: %s", path, errno, strerror(errno));
  174. return false;
  175. }
  176. return S_ISDIR(s.st_mode);
  177. }
  178. bool mkdir(const char *path)
  179. {
  180. int err = ::mkdir(path, 0770);
  181. if (err == -1)
  182. {
  183. SDL_Log("Error: Could not create directory %s", path);
  184. return false;
  185. }
  186. return true;
  187. }
  188. bool createStorageDirectories()
  189. {
  190. std::string internal_storage_path = SDL_AndroidGetInternalStoragePath();
  191. std::string save_directory = internal_storage_path + "/save";
  192. if (!directoryExists(save_directory.c_str()) && !mkdir(save_directory.c_str()))
  193. return false;
  194. std::string game_directory = internal_storage_path + "/game";
  195. if (!directoryExists (game_directory.c_str()) && !mkdir(game_directory.c_str()))
  196. return false;
  197. return true;
  198. }
  199. bool hasBackgroundMusic()
  200. {
  201. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  202. jobject activity = (jobject) SDL_AndroidGetActivity();
  203. jclass clazz(env->GetObjectClass(activity));
  204. jmethodID method_id = env->GetMethodID(clazz, "hasBackgroundMusic", "()Z");
  205. jboolean result = env->CallBooleanMethod(activity, method_id);
  206. env->DeleteLocalRef(activity);
  207. env->DeleteLocalRef(clazz);
  208. return result;
  209. }
  210. } // android
  211. } // love
  212. #endif // LOVE_ANDROID