android.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * Copyright (c) 2006-2016 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. const char *getSelectedGameFile()
  71. {
  72. static const char *path = NULL;
  73. if (path)
  74. {
  75. delete path;
  76. path = NULL;
  77. }
  78. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  79. jclass activity = env->FindClass("org/love2d/android/GameActivity");
  80. jmethodID getGamePath = env->GetStaticMethodID(activity, "getGamePath", "()Ljava/lang/String;");
  81. jstring gamePath = (jstring) env->CallStaticObjectMethod(activity, getGamePath);
  82. const char *utf = env->GetStringUTFChars(gamePath, 0);
  83. if (utf)
  84. {
  85. path = SDL_strdup(utf);
  86. env->ReleaseStringUTFChars(gamePath, utf);
  87. }
  88. env->DeleteLocalRef(gamePath);
  89. env->DeleteLocalRef(activity);
  90. return path;
  91. }
  92. bool openURL(const std::string &url)
  93. {
  94. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  95. jclass activity = env->FindClass("org/love2d/android/GameActivity");
  96. jmethodID openURL= env->GetStaticMethodID(activity, "openURL", "(Ljava/lang/String;)V");
  97. jstring url_jstring = (jstring) env->NewStringUTF(url.c_str());
  98. env->CallStaticVoidMethod(activity, openURL, url_jstring);
  99. env->DeleteLocalRef(url_jstring);
  100. env->DeleteLocalRef(activity);
  101. return true;
  102. }
  103. void vibrate(double seconds)
  104. {
  105. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  106. jclass activity = env->FindClass("org/love2d/android/GameActivity");
  107. jmethodID vibrate_method = env->GetStaticMethodID(activity, "vibrate", "(D)V");
  108. env->CallStaticVoidMethod(activity, vibrate_method, seconds);
  109. env->DeleteLocalRef(activity);
  110. }
  111. /*
  112. * Helper functions for the filesystem module
  113. */
  114. void freeGameArchiveMemory(void *ptr)
  115. {
  116. char *game_love_data = static_cast<char*>(ptr);
  117. delete[] game_love_data;
  118. }
  119. bool loadGameArchiveToMemory(const char* filename, char **ptr, size_t *size)
  120. {
  121. SDL_RWops *asset_game_file = SDL_RWFromFile(filename, "rb");
  122. if (!asset_game_file) {
  123. SDL_Log("Could not find %s", filename);
  124. return false;
  125. }
  126. Sint64 file_size = asset_game_file->size(asset_game_file);
  127. if (file_size <= 0) {
  128. SDL_Log("Could not load game from %s. File has invalid file size: %d.", filename, (int) file_size);
  129. return false;
  130. }
  131. *ptr = new char[file_size];
  132. if (!*ptr) {
  133. SDL_Log("Could not allocate memory for in-memory game archive");
  134. return false;
  135. }
  136. size_t bytes_copied = asset_game_file->read(asset_game_file, (void*) *ptr, sizeof(char), (size_t) file_size);
  137. if (bytes_copied != file_size) {
  138. SDL_Log("Incomplete copy of in-memory game archive!");
  139. delete[] *ptr;
  140. return false;
  141. }
  142. *size = (size_t) file_size;
  143. return true;
  144. }
  145. bool directoryExists(const char *path)
  146. {
  147. struct stat s;
  148. int err = stat(path, &s);
  149. if (err == -1)
  150. {
  151. if (errno != ENOENT)
  152. SDL_Log("Error checking for directory %s errno = %d: %s", path, errno, strerror(errno));
  153. return false;
  154. }
  155. return S_ISDIR(s.st_mode);
  156. }
  157. bool mkdir(const char *path)
  158. {
  159. int err = ::mkdir(path, 0770);
  160. if (err == -1)
  161. {
  162. SDL_Log("Error: Could not create directory %s", path);
  163. return false;
  164. }
  165. return true;
  166. }
  167. bool createStorageDirectories()
  168. {
  169. std::string internal_storage_path = SDL_AndroidGetInternalStoragePath();
  170. std::string save_directory = internal_storage_path + "/save";
  171. if (!directoryExists(save_directory.c_str()) && !mkdir(save_directory.c_str()))
  172. return false;
  173. std::string game_directory = internal_storage_path + "/game";
  174. if (!directoryExists (game_directory.c_str()) && !mkdir(game_directory.c_str()))
  175. return false;
  176. return true;
  177. }
  178. } // android
  179. } // love
  180. #endif // LOVE_ANDROID