UIPlatformWebViewAndroid.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. bool result = (bool) gCreateMethodInfo.GetEnv()->CallStaticBooleanMethod(gCreateMethodInfo.classID, gCreateMethodInfo.methodID, (jint) webViewHandle_, (jlong)0, (jlong)0);
  110. if (result && queuedRequest_.Length())
  111. {
  112. LoadURL(queuedRequest_);
  113. queuedRequest_.Clear();
  114. }
  115. return result;
  116. }
  117. void UIPlatformWebView::PlatformShowWebView(bool visible)
  118. {
  119. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  120. {
  121. ATOMIC_LOGWARNING("UIPlatformWebView::ShowPlatformWebView() - Invalid webview handle");
  122. return;
  123. }
  124. if (visible)
  125. {
  126. gShowMethodInfo.GetEnv()->CallStaticVoidMethod(gShowMethodInfo.classID, gShowMethodInfo.methodID, (jint)webViewHandle_);
  127. }
  128. else
  129. {
  130. gHideMethodInfo.GetEnv()->CallStaticVoidMethod(gHideMethodInfo.classID, gHideMethodInfo.methodID, (jint)webViewHandle_);
  131. }
  132. }
  133. void UIPlatformWebView::PlatformDestroyWebView()
  134. {
  135. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  136. {
  137. return;
  138. }
  139. webViewLookup_.Erase(webViewHandle_);
  140. gDestroyMethodInfo.GetEnv()->CallStaticVoidMethod(gDestroyMethodInfo.classID, gDestroyMethodInfo.methodID, (jint)webViewHandle_);
  141. webViewHandle_ = UI_PLATFORM_WEBVIEW_INVALID_HANDLE;
  142. }
  143. void UIPlatformWebView::PlatformPositionWebView()
  144. {
  145. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  146. {
  147. return;
  148. }
  149. IntVector2 rootPos = ConvertToRoot(IntVector2( 0, 0));
  150. int height = GetSubsystem<Graphics>()->GetHeight();
  151. int posY = height - GetHeight() - rootPos.y_;
  152. gSetDimensionsMethodInfo.GetEnv()->CallStaticVoidMethod(gSetDimensionsMethodInfo.classID, gSetDimensionsMethodInfo.methodID, (jint)webViewHandle_, (jint)rootPos.x_, (jint)posY, (jint)GetWidth(), (jint)GetHeight());
  153. }
  154. void UIPlatformWebView::DestroyAll()
  155. {
  156. android_webViewEnsureInitialized();
  157. gDestroyAllMethodInfo.GetEnv()->CallStaticVoidMethod(gDestroyAllMethodInfo.classID, gDestroyAllMethodInfo.methodID);
  158. }
  159. bool UIPlatformWebView::GoBack()
  160. {
  161. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  162. {
  163. return false;
  164. }
  165. jboolean result = gGoBackMethodInfo.GetEnv()->CallStaticBooleanMethod(gGoBackMethodInfo.classID, gGoBackMethodInfo.methodID, (jint)webViewHandle_);
  166. return (bool)result;
  167. }
  168. bool UIPlatformWebView::GoForward()
  169. {
  170. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  171. {
  172. return false;
  173. }
  174. jboolean result = gGoForwardMethodInfo.GetEnv()->CallStaticBooleanMethod(gGoForwardMethodInfo.classID, gGoForwardMethodInfo.methodID, (jint)webViewHandle_);
  175. return (bool)result;
  176. }
  177. bool UIPlatformWebView::CanGoBack() const
  178. {
  179. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  180. {
  181. return false;
  182. }
  183. jboolean result = gCanGoBackMethodInfo.GetEnv()->CallStaticBooleanMethod(gCanGoBackMethodInfo.classID, gCanGoBackMethodInfo.methodID, (jint)webViewHandle_);
  184. return (bool)result;
  185. }
  186. bool UIPlatformWebView::CanGoForward() const
  187. {
  188. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  189. {
  190. return false;
  191. }
  192. jboolean result = gCanGoForwardMethodInfo.GetEnv()->CallStaticBooleanMethod(gCanGoForwardMethodInfo.classID, gCanGoForwardMethodInfo.methodID, (jint)webViewHandle_);
  193. return (bool)result;
  194. }
  195. void UIPlatformWebView::PauseAll()
  196. {
  197. }
  198. void UIPlatformWebView::ResumeAll()
  199. {
  200. }
  201. void UIPlatformWebView::LoadURL(const String& url)
  202. {
  203. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  204. {
  205. queuedRequest_ = url;
  206. return;
  207. }
  208. jstring urlString = gRequestMethodInfo.GetEnv()->NewStringUTF(url.CString());
  209. gRequestMethodInfo.GetEnv()->CallStaticVoidMethod(gRequestMethodInfo.classID, gRequestMethodInfo.methodID, (jint)webViewHandle_, urlString);
  210. gRequestMethodInfo.GetEnv()->DeleteLocalRef(urlString);
  211. }
  212. void UIPlatformWebView::Reload()
  213. {
  214. if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
  215. {
  216. return;
  217. }
  218. }
  219. }
  220. extern "C"
  221. {
  222. void Java_com_atomicgameengine_player_AtomicWebView_nativeCallback(JNIEnv *env, jobject thiz, jstring data, jlong callback, jlong payload, jint type)
  223. {
  224. /*
  225. atomic_webViewCallback cb = (atomic_webViewCallback)callback;
  226. const char *dataString = env->GetStringUTFChars(data, 0);
  227. cb((void *)payload, (atomic_webViewCallbackType)type, dataString);
  228. env->ReleaseStringUTFChars(data, dataString);
  229. */
  230. }
  231. }
  232. #endif