android.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Copyright (c) 2006-2014 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. namespace love
  25. {
  26. namespace android
  27. {
  28. double getScreenScale()
  29. {
  30. static double result = -1.;
  31. if (result == -1.) {
  32. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  33. jclass activity = env->FindClass("org/love2d/android/GameActivity");
  34. jmethodID getMetrics = env->GetStaticMethodID(activity, "getMetrics", "()Landroid/util/DisplayMetrics;");
  35. jobject metrics = env->CallStaticObjectMethod(activity, getMetrics);
  36. jclass metricsClass = env->GetObjectClass(metrics);
  37. result = env->GetFloatField(metrics, env->GetFieldID(metricsClass, "density", "F"));
  38. env->DeleteLocalRef (metricsClass);
  39. env->DeleteLocalRef (metrics);
  40. env->DeleteLocalRef (activity);
  41. }
  42. return result;
  43. }
  44. const char* getSelectedGameFile()
  45. {
  46. static const char *path = NULL;
  47. if (!path) {
  48. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  49. jclass activity = env->FindClass("org/love2d/android/GameActivity");
  50. jmethodID getGamePath = env->GetStaticMethodID(activity, "getGamePath", "()Ljava/lang/String;");
  51. jstring gamePath = (jstring) env->CallStaticObjectMethod(activity, getGamePath);
  52. const char *utf = env->GetStringUTFChars(gamePath, 0);
  53. if (utf)
  54. {
  55. path = SDL_strdup(utf);
  56. env->ReleaseStringUTFChars(gamePath, utf);
  57. }
  58. env->DeleteLocalRef (gamePath);
  59. env->DeleteLocalRef (activity);
  60. }
  61. return path;
  62. }
  63. bool openURL(const std::string &url)
  64. {
  65. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  66. jclass activity = env->FindClass("org/love2d/android/GameActivity");
  67. jmethodID openURL= env->GetStaticMethodID(activity, "openURL", "(Ljava/lang/String;)V");
  68. jstring url_jstring = (jstring) env->NewStringUTF (url.c_str());
  69. env->CallStaticVoidMethod (activity, openURL, url_jstring);
  70. env->DeleteLocalRef (url_jstring);
  71. env->DeleteLocalRef (activity);
  72. return true;
  73. }
  74. void vibrate (double seconds)
  75. {
  76. JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
  77. jclass activity = env->FindClass("org/love2d/android/GameActivity");
  78. jmethodID vibrate_method = env->GetStaticMethodID(activity, "vibrate", "(D)V");
  79. env->CallStaticVoidMethod(activity, vibrate_method, seconds);
  80. env->DeleteLocalRef (activity);
  81. }
  82. } // android
  83. } // love
  84. #endif // LOVE_ANDROID