android.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * Copyright (c) 2006-2020 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, "openURLFromLOVE", "(Ljava/lang/String;)Z");
  118. if (openURL == nullptr)
  119. {
  120. env->ExceptionClear();
  121. openURL = env->GetStaticMethodID(activity, "openURL", "(Ljava/lang/String;)Z");
  122. }
  123. jstring url_jstring = (jstring) env->NewStringUTF(url.c_str());
  124. jboolean result = env->CallStaticBooleanMethod(activity, openURL, url_jstring);
  125. env->DeleteLocalRef(url_jstring);
  126. env->DeleteLocalRef(activity);
  127. return result;
  128. }
  129. void vibrate(double seconds)
  130. {
  131. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  132. jclass activity = env->FindClass("org/love2d/android/GameActivity");
  133. jmethodID vibrate_method = env->GetStaticMethodID(activity, "vibrate", "(D)V");
  134. env->CallStaticVoidMethod(activity, vibrate_method, seconds);
  135. env->DeleteLocalRef(activity);
  136. }
  137. /*
  138. * Helper functions for the filesystem module
  139. */
  140. void freeGameArchiveMemory(void *ptr)
  141. {
  142. char *game_love_data = static_cast<char*>(ptr);
  143. delete[] game_love_data;
  144. }
  145. bool loadGameArchiveToMemory(const char* filename, char **ptr, size_t *size)
  146. {
  147. SDL_RWops *asset_game_file = SDL_RWFromFile(filename, "rb");
  148. if (!asset_game_file) {
  149. SDL_Log("Could not find %s", filename);
  150. return false;
  151. }
  152. Sint64 file_size = asset_game_file->size(asset_game_file);
  153. if (file_size <= 0) {
  154. SDL_Log("Could not load game from %s. File has invalid file size: %d.", filename, (int) file_size);
  155. return false;
  156. }
  157. *ptr = new char[file_size];
  158. if (!*ptr) {
  159. SDL_Log("Could not allocate memory for in-memory game archive");
  160. return false;
  161. }
  162. size_t bytes_copied = asset_game_file->read(asset_game_file, (void*) *ptr, sizeof(char), (size_t) file_size);
  163. if (bytes_copied != file_size) {
  164. SDL_Log("Incomplete copy of in-memory game archive!");
  165. delete[] *ptr;
  166. return false;
  167. }
  168. *size = (size_t) file_size;
  169. return true;
  170. }
  171. bool directoryExists(const char *path)
  172. {
  173. struct stat s;
  174. int err = stat(path, &s);
  175. if (err == -1)
  176. {
  177. if (errno != ENOENT)
  178. SDL_Log("Error checking for directory %s errno = %d: %s", path, errno, strerror(errno));
  179. return false;
  180. }
  181. return S_ISDIR(s.st_mode);
  182. }
  183. bool mkdir(const char *path)
  184. {
  185. int err = ::mkdir(path, 0770);
  186. if (err == -1)
  187. {
  188. SDL_Log("Error: Could not create directory %s", path);
  189. return false;
  190. }
  191. return true;
  192. }
  193. bool createStorageDirectories()
  194. {
  195. std::string internal_storage_path = SDL_AndroidGetInternalStoragePath();
  196. std::string save_directory = internal_storage_path + "/save";
  197. if (!directoryExists(save_directory.c_str()) && !mkdir(save_directory.c_str()))
  198. return false;
  199. std::string game_directory = internal_storage_path + "/game";
  200. if (!directoryExists (game_directory.c_str()) && !mkdir(game_directory.c_str()))
  201. return false;
  202. return true;
  203. }
  204. bool hasBackgroundMusic()
  205. {
  206. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  207. jobject activity = (jobject) SDL_AndroidGetActivity();
  208. jclass clazz(env->GetObjectClass(activity));
  209. jmethodID method_id = env->GetMethodID(clazz, "hasBackgroundMusic", "()Z");
  210. jboolean result = env->CallBooleanMethod(activity, method_id);
  211. env->DeleteLocalRef(activity);
  212. env->DeleteLocalRef(clazz);
  213. return result;
  214. }
  215. bool hasRecordingPermission()
  216. {
  217. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  218. jobject activity = (jobject) SDL_AndroidGetActivity();
  219. jclass clazz(env->GetObjectClass(activity));
  220. jmethodID methodID = env->GetMethodID(clazz, "hasRecordAudioPermission", "()Z");
  221. jboolean result = false;
  222. if (methodID == nullptr)
  223. env->ExceptionClear();
  224. else
  225. result = env->CallBooleanMethod(activity, methodID);
  226. env->DeleteLocalRef(activity);
  227. env->DeleteLocalRef(clazz);
  228. return result;
  229. }
  230. void requestRecordingPermission()
  231. {
  232. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  233. jobject activity = (jobject) SDL_AndroidGetActivity();
  234. jclass clazz(env->GetObjectClass(activity));
  235. jmethodID methodID = env->GetMethodID(clazz, "requestRecordAudioPermission", "()V");
  236. if (methodID == nullptr)
  237. env->ExceptionClear();
  238. else
  239. env->CallVoidMethod(activity, methodID);
  240. env->DeleteLocalRef(clazz);
  241. env->DeleteLocalRef(activity);
  242. }
  243. void showRecordingPermissionMissingDialog()
  244. {
  245. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  246. jobject activity = (jobject) SDL_AndroidGetActivity();
  247. jclass clazz(env->GetObjectClass(activity));
  248. jmethodID methodID = env->GetMethodID(clazz, "showRecordingAudioPermissionMissingDialog", "()V");
  249. if (methodID == nullptr)
  250. env->ExceptionClear();
  251. else
  252. env->CallVoidMethod(activity, methodID);
  253. env->DeleteLocalRef(clazz);
  254. env->DeleteLocalRef(activity);
  255. }
  256. } // android
  257. } // love
  258. #endif // LOVE_ANDROID