AndroidJNI.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
  3. // Copyright (c) 2011-2017, The Game Engine Company LLC (Apache License 2.0)
  4. // Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. // Copyright (c) 2010-2012 cocos2d-x.org
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #include <SDL.h>
  26. #include "../../IO/Log.h"
  27. #include "AndroidJNI.h"
  28. namespace Atomic
  29. {
  30. static bool GetJSEnv(JNIEnv **env)
  31. {
  32. if (!env)
  33. {
  34. return false;
  35. }
  36. *env = (JNIEnv*)SDL_AndroidGetJNIEnv();
  37. return *env != NULL;
  38. }
  39. JNIEnv* AtomicJNIMethodInfo::GetEnv()
  40. {
  41. JNIEnv *env = NULL;
  42. GetJSEnv(&env);
  43. return env;
  44. }
  45. jclass AtomicJNI::GetClassID(const String &className, JNIEnv *env)
  46. {
  47. JNIEnv *pEnv = env;
  48. jclass ret = 0;
  49. if (!pEnv)
  50. {
  51. if (!GetJSEnv(&pEnv))
  52. {
  53. return 0;
  54. }
  55. }
  56. ret = pEnv->FindClass(className.CString());
  57. if (!ret)
  58. {
  59. ATOMIC_LOGWARNINGF("AtomicJNI::GetClassID() - Failed to find class of %s", className.CString());
  60. return 0;
  61. }
  62. // ATOMIC_LOGINFOF("AtomicJNI::GetClassID() - Found class of %s", className.CString());
  63. return ret;
  64. }
  65. bool AtomicJNI::GetStaticMethodInfo(AtomicJNIMethodInfo& methodinfo, const String& className, const String& methodName, const String& paramCode)
  66. {
  67. jmethodID methodID = 0;
  68. JNIEnv *pEnv = 0;
  69. if (!GetJSEnv(&pEnv))
  70. {
  71. return false;
  72. }
  73. jclass classID = GetClassID(className, pEnv);
  74. if (!classID)
  75. {
  76. return false;
  77. }
  78. methodID = pEnv->GetStaticMethodID(classID, methodName.CString(), paramCode.CString());
  79. if (!methodID)
  80. {
  81. ATOMIC_LOGWARNINGF("AtomicJNI::GetStaticMethodInfo() - Failed to find method id of %s - %s", methodName.CString(), paramCode.CString());
  82. return false;
  83. }
  84. methodinfo.classID = classID;
  85. methodinfo.methodID = methodID;
  86. return true;
  87. }
  88. bool AtomicJNI::GetMethodInfo(AtomicJNIMethodInfo& methodinfo, const String& className, const String& methodName, const String& paramCode)
  89. {
  90. jmethodID methodID = 0;
  91. JNIEnv *pEnv = 0;
  92. if (!GetJSEnv(&pEnv))
  93. {
  94. return false;
  95. }
  96. jclass classID = GetClassID(className, pEnv);
  97. if (!classID)
  98. {
  99. return false;
  100. }
  101. methodID = pEnv->GetMethodID(classID, methodName.CString(), paramCode.CString());
  102. if (!methodID)
  103. {
  104. ATOMIC_LOGWARNINGF("AtomicJNI::GetMethodInfo() - Failed to find method id of %s", methodName.CString());
  105. return false;
  106. }
  107. methodinfo.classID = classID;
  108. methodinfo.methodID = methodID;
  109. return true;
  110. }
  111. String AtomicJNI::JavaStringToString(jstring jstr)
  112. {
  113. if (jstr == NULL)
  114. {
  115. return String::EMPTY;
  116. }
  117. JNIEnv *env = 0;
  118. if (!GetJSEnv(&env))
  119. {
  120. return String::EMPTY;
  121. }
  122. const char *chars = env->GetStringUTFChars(jstr, NULL);
  123. String ret(chars);
  124. env->ReleaseStringUTFChars(jstr, chars);
  125. return ret;
  126. }
  127. }