UIPlatformWebViewAndroid.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifdef ATOMIC_PLATFORM_ANDROID
  24. /* Notes
  25. 1) This is based on the Android webview from the LoomSDK: https://github.com/LoomSDK/LoomSDK/blob/master/loom/common/platform/platformWebViewAndroid.cpp
  26. */
  27. #include "../../IO/Log.h"
  28. #include "../../Graphics/Graphics.h"
  29. #include "UIPlatformWebView.h"
  30. #include "../../Platform/Android/AndroidJNI.h"
  31. namespace Atomic
  32. {
  33. //_________________________________________________________________________
  34. // JNI Helpers
  35. //_________________________________________________________________________
  36. static AtomicJNIMethodInfo gCreateMethodInfo;
  37. static AtomicJNIMethodInfo gDestroyMethodInfo;
  38. static AtomicJNIMethodInfo gDestroyAllMethodInfo;
  39. static AtomicJNIMethodInfo gShowMethodInfo;
  40. static AtomicJNIMethodInfo gHideMethodInfo;
  41. static AtomicJNIMethodInfo gRequestMethodInfo;
  42. static AtomicJNIMethodInfo gGoBackMethodInfo;
  43. static AtomicJNIMethodInfo gGoForwardMethodInfo;
  44. static AtomicJNIMethodInfo gCanGoBackMethodInfo;
  45. static AtomicJNIMethodInfo gCanGoForwardMethodInfo;
  46. static AtomicJNIMethodInfo gSetDimensionsMethodInfo;
  47. static void android_webViewEnsureInitialized()
  48. {
  49. static bool initialized = false;
  50. if (!initialized)
  51. {
  52. // initialize all of our jni method infos
  53. AtomicJNI::GetStaticMethodInfo(gCreateMethodInfo,
  54. "com/atomicgameengine/player/AtomicWebView",
  55. "create",
  56. "(IJJ)Z");
  57. AtomicJNI::GetStaticMethodInfo(gDestroyMethodInfo,
  58. "com/atomicgameengine/player/AtomicWebView",
  59. "destroy",
  60. "(I)V");
  61. AtomicJNI::GetStaticMethodInfo(gDestroyAllMethodInfo,
  62. "com/atomicgameengine/player/AtomicWebView",
  63. "destroyAll",
  64. "()V");
  65. AtomicJNI::GetStaticMethodInfo(gShowMethodInfo,
  66. "com/atomicgameengine/player/AtomicWebView",
  67. "show",
  68. "(I)V");
  69. AtomicJNI::GetStaticMethodInfo(gHideMethodInfo,
  70. "com/atomicgameengine/player/AtomicWebView",
  71. "hide",
  72. "(I)V");
  73. AtomicJNI::GetStaticMethodInfo(gRequestMethodInfo,
  74. "com/atomicgameengine/player/AtomicWebView",
  75. "request",
  76. "(ILjava/lang/String;)V");
  77. AtomicJNI::GetStaticMethodInfo(gGoBackMethodInfo,
  78. "com/atomicgameengine/player/AtomicWebView",
  79. "goBack",
  80. "(I)Z");
  81. AtomicJNI::GetStaticMethodInfo(gGoForwardMethodInfo,
  82. "com/atomicgameengine/player/AtomicWebView",
  83. "goForward",
  84. "(I)Z");
  85. AtomicJNI::GetStaticMethodInfo(gCanGoBackMethodInfo,
  86. "com/atomicgameengine/player/AtomicWebView",
  87. "canGoBack",
  88. "(I)Z");
  89. AtomicJNI::GetStaticMethodInfo(gCanGoForwardMethodInfo,
  90. "com/atomicgameengine/player/AtomicWebView",
  91. "canGoForward",
  92. "(I)Z");
  93. AtomicJNI::GetStaticMethodInfo(gSetDimensionsMethodInfo,
  94. "com/atomicgameengine/player/AtomicWebView",
  95. "setDimensions",
  96. "(IIIII)V");
  97. initialized = true;
  98. }
  99. }
  100. bool UIPlatformWebView::PlatformCreateWebView()
  101. {
  102. if (webViewHandle_ != UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  103. {
  104. ATOMIC_LOGWARNING("UIPlatformWebView::CreatePlatformWebView() - Attempting to create platform webview with valid handle");
  105. return false;
  106. }
  107. webViewHandle_ = webViewHandleCounter_++;
  108. android_webViewEnsureInitialized();
  109. return (bool) gCreateMethodInfo.GetEnv()->CallStaticBooleanMethod(gCreateMethodInfo.classID, gCreateMethodInfo.methodID, (jint) webViewHandle_, (jlong)0, (jlong)0);
  110. }
  111. void UIPlatformWebView::PlatformShowWebView(bool visible)
  112. {
  113. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  114. {
  115. ATOMIC_LOGWARNING("UIPlatformWebView::ShowPlatformWebView() - Invalid webview handle");
  116. return;
  117. }
  118. if (visible)
  119. {
  120. gShowMethodInfo.GetEnv()->CallStaticVoidMethod(gShowMethodInfo.classID, gShowMethodInfo.methodID, (jint)webViewHandle_);
  121. }
  122. else
  123. {
  124. gHideMethodInfo.GetEnv()->CallStaticVoidMethod(gHideMethodInfo.classID, gHideMethodInfo.methodID, (jint)webViewHandle_);
  125. }
  126. }
  127. void UIPlatformWebView::PlatformDestroyWebView()
  128. {
  129. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  130. {
  131. return;
  132. }
  133. webViewLookup_.Erase(webViewHandle_);
  134. gDestroyMethodInfo.GetEnv()->CallStaticVoidMethod(gDestroyMethodInfo.classID, gDestroyMethodInfo.methodID, (jint)webViewHandle_);
  135. webViewHandle_ = UI_PLATFORM_WEBVIEW_INVALID_HANDLE;
  136. }
  137. void UIPlatformWebView::PlatformPositionWebView()
  138. {
  139. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  140. {
  141. return;
  142. }
  143. IntVector2 rootPos = ConvertToRoot(IntVector2( 0, 0));
  144. int height = GetSubsystem<Graphics>()->GetHeight();
  145. int posY = height - GetHeight() - rootPos.y_;
  146. gSetDimensionsMethodInfo.GetEnv()->CallStaticVoidMethod(gSetDimensionsMethodInfo.classID, gSetDimensionsMethodInfo.methodID, (jint)webViewHandle_, (jint)rootPos.x_, (jint)posY, (jint)GetWidth(), (jint)GetHeight());
  147. }
  148. void UIPlatformWebView::DestroyAll()
  149. {
  150. android_webViewEnsureInitialized();
  151. gDestroyAllMethodInfo.GetEnv()->CallStaticVoidMethod(gDestroyAllMethodInfo.classID, gDestroyAllMethodInfo.methodID);
  152. }
  153. bool UIPlatformWebView::GoBack()
  154. {
  155. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  156. {
  157. return false;
  158. }
  159. jboolean result = gGoBackMethodInfo.GetEnv()->CallStaticBooleanMethod(gGoBackMethodInfo.classID, gGoBackMethodInfo.methodID, (jint)webViewHandle_);
  160. return (bool)result;
  161. }
  162. bool UIPlatformWebView::GoForward()
  163. {
  164. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  165. {
  166. return false;
  167. }
  168. jboolean result = gGoForwardMethodInfo.GetEnv()->CallStaticBooleanMethod(gGoForwardMethodInfo.classID, gGoForwardMethodInfo.methodID, (jint)webViewHandle_);
  169. return (bool)result;
  170. }
  171. bool UIPlatformWebView::CanGoBack() const
  172. {
  173. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  174. {
  175. return false;
  176. }
  177. jboolean result = gCanGoBackMethodInfo.GetEnv()->CallStaticBooleanMethod(gCanGoBackMethodInfo.classID, gCanGoBackMethodInfo.methodID, (jint)webViewHandle_);
  178. return (bool)result;
  179. }
  180. bool UIPlatformWebView::CanGoForward() const
  181. {
  182. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  183. {
  184. return false;
  185. }
  186. jboolean result = gCanGoForwardMethodInfo.GetEnv()->CallStaticBooleanMethod(gCanGoForwardMethodInfo.classID, gCanGoForwardMethodInfo.methodID, (jint)webViewHandle_);
  187. return (bool)result;
  188. }
  189. void UIPlatformWebView::PauseAll()
  190. {
  191. }
  192. void UIPlatformWebView::ResumeAll()
  193. {
  194. }
  195. void UIPlatformWebView::LoadURL(const String& url)
  196. {
  197. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  198. {
  199. return;
  200. }
  201. jstring urlString = gRequestMethodInfo.GetEnv()->NewStringUTF(url.CString());
  202. gRequestMethodInfo.GetEnv()->CallStaticVoidMethod(gRequestMethodInfo.classID, gRequestMethodInfo.methodID, (jint)webViewHandle_, urlString);
  203. gRequestMethodInfo.GetEnv()->DeleteLocalRef(urlString);
  204. }
  205. void UIPlatformWebView::Reload()
  206. {
  207. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  208. {
  209. return;
  210. }
  211. }
  212. }
  213. extern "C"
  214. {
  215. void Java_com_atomicgameengine_player_AtomicWebView_nativeCallback(JNIEnv *env, jobject thiz, jstring data, jlong callback, jlong payload, jint type)
  216. {
  217. /*
  218. atomic_webViewCallback cb = (atomic_webViewCallback)callback;
  219. const char *dataString = env->GetStringUTFChars(data, 0);
  220. cb((void *)payload, (atomic_webViewCallbackType)type, dataString);
  221. env->ReleaseStringUTFChars(data, dataString);
  222. */
  223. }
  224. }
  225. #endif