| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- //
- // Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
- // Copyright (c) 2011-2017, The Game Engine Company LLC (Apache License 2.0)
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #ifdef ATOMIC_PLATFORM_ANDROID
- /* Notes
- 1) This is based on the Android webview from the LoomSDK: https://github.com/LoomSDK/LoomSDK/blob/master/loom/common/platform/platformWebViewAndroid.cpp
- */
- #include "../../IO/Log.h"
- #include "../../Graphics/Graphics.h"
- #include "UIPlatformWebView.h"
- #include "../../Platform/Android/AndroidJNI.h"
- namespace Atomic
- {
- //_________________________________________________________________________
- // JNI Helpers
- //_________________________________________________________________________
- static AtomicJNIMethodInfo gCreateMethodInfo;
- static AtomicJNIMethodInfo gDestroyMethodInfo;
- static AtomicJNIMethodInfo gDestroyAllMethodInfo;
- static AtomicJNIMethodInfo gShowMethodInfo;
- static AtomicJNIMethodInfo gHideMethodInfo;
- static AtomicJNIMethodInfo gRequestMethodInfo;
- static AtomicJNIMethodInfo gGoBackMethodInfo;
- static AtomicJNIMethodInfo gGoForwardMethodInfo;
- static AtomicJNIMethodInfo gCanGoBackMethodInfo;
- static AtomicJNIMethodInfo gCanGoForwardMethodInfo;
- static AtomicJNIMethodInfo gSetDimensionsMethodInfo;
- static void android_webViewEnsureInitialized()
- {
- static bool initialized = false;
- if (!initialized)
- {
- // initialize all of our jni method infos
- AtomicJNI::GetStaticMethodInfo(gCreateMethodInfo,
- "com/atomicgameengine/player/AtomicWebView",
- "create",
- "(IJJ)Z");
- AtomicJNI::GetStaticMethodInfo(gDestroyMethodInfo,
- "com/atomicgameengine/player/AtomicWebView",
- "destroy",
- "(I)V");
- AtomicJNI::GetStaticMethodInfo(gDestroyAllMethodInfo,
- "com/atomicgameengine/player/AtomicWebView",
- "destroyAll",
- "()V");
- AtomicJNI::GetStaticMethodInfo(gShowMethodInfo,
- "com/atomicgameengine/player/AtomicWebView",
- "show",
- "(I)V");
- AtomicJNI::GetStaticMethodInfo(gHideMethodInfo,
- "com/atomicgameengine/player/AtomicWebView",
- "hide",
- "(I)V");
- AtomicJNI::GetStaticMethodInfo(gRequestMethodInfo,
- "com/atomicgameengine/player/AtomicWebView",
- "request",
- "(ILjava/lang/String;)V");
- AtomicJNI::GetStaticMethodInfo(gGoBackMethodInfo,
- "com/atomicgameengine/player/AtomicWebView",
- "goBack",
- "(I)Z");
- AtomicJNI::GetStaticMethodInfo(gGoForwardMethodInfo,
- "com/atomicgameengine/player/AtomicWebView",
- "goForward",
- "(I)Z");
- AtomicJNI::GetStaticMethodInfo(gCanGoBackMethodInfo,
- "com/atomicgameengine/player/AtomicWebView",
- "canGoBack",
- "(I)Z");
- AtomicJNI::GetStaticMethodInfo(gCanGoForwardMethodInfo,
- "com/atomicgameengine/player/AtomicWebView",
- "canGoForward",
- "(I)Z");
- AtomicJNI::GetStaticMethodInfo(gSetDimensionsMethodInfo,
- "com/atomicgameengine/player/AtomicWebView",
- "setDimensions",
- "(IIIII)V");
- initialized = true;
- }
- }
- bool UIPlatformWebView::PlatformCreateWebView()
- {
- if (webViewHandle_ != UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
- {
- ATOMIC_LOGWARNING("UIPlatformWebView::CreatePlatformWebView() - Attempting to create platform webview with valid handle");
- return false;
- }
- webViewHandle_ = webViewHandleCounter_++;
- android_webViewEnsureInitialized();
- bool result = (bool) gCreateMethodInfo.GetEnv()->CallStaticBooleanMethod(gCreateMethodInfo.classID, gCreateMethodInfo.methodID, (jint) webViewHandle_, (jlong)0, (jlong)0);
- if (result && queuedRequest_.Length())
- {
- LoadURL(queuedRequest_);
- queuedRequest_.Clear();
- }
- return result;
- }
- void UIPlatformWebView::PlatformShowWebView(bool visible)
- {
- if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
- {
- ATOMIC_LOGWARNING("UIPlatformWebView::ShowPlatformWebView() - Invalid webview handle");
- return;
- }
- if (visible)
- {
- gShowMethodInfo.GetEnv()->CallStaticVoidMethod(gShowMethodInfo.classID, gShowMethodInfo.methodID, (jint)webViewHandle_);
- }
- else
- {
- gHideMethodInfo.GetEnv()->CallStaticVoidMethod(gHideMethodInfo.classID, gHideMethodInfo.methodID, (jint)webViewHandle_);
- }
- }
- void UIPlatformWebView::PlatformDestroyWebView()
- {
- if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
- {
- return;
- }
- webViewLookup_.Erase(webViewHandle_);
- gDestroyMethodInfo.GetEnv()->CallStaticVoidMethod(gDestroyMethodInfo.classID, gDestroyMethodInfo.methodID, (jint)webViewHandle_);
- webViewHandle_ = UI_PLATFORM_WEBVIEW_INVALID_HANDLE;
- }
- void UIPlatformWebView::PlatformPositionWebView()
- {
- if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
- {
- return;
- }
- IntVector2 rootPos = ConvertToRoot(IntVector2( 0, 0));
- int height = GetSubsystem<Graphics>()->GetHeight();
- int posY = height - GetHeight() - rootPos.y_;
- gSetDimensionsMethodInfo.GetEnv()->CallStaticVoidMethod(gSetDimensionsMethodInfo.classID, gSetDimensionsMethodInfo.methodID, (jint)webViewHandle_, (jint)rootPos.x_, (jint)posY, (jint)GetWidth(), (jint)GetHeight());
- }
- void UIPlatformWebView::DestroyAll()
- {
- android_webViewEnsureInitialized();
- gDestroyAllMethodInfo.GetEnv()->CallStaticVoidMethod(gDestroyAllMethodInfo.classID, gDestroyAllMethodInfo.methodID);
- }
- bool UIPlatformWebView::GoBack()
- {
- if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
- {
- return false;
- }
- jboolean result = gGoBackMethodInfo.GetEnv()->CallStaticBooleanMethod(gGoBackMethodInfo.classID, gGoBackMethodInfo.methodID, (jint)webViewHandle_);
- return (bool)result;
- }
- bool UIPlatformWebView::GoForward()
- {
- if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
- {
- return false;
- }
- jboolean result = gGoForwardMethodInfo.GetEnv()->CallStaticBooleanMethod(gGoForwardMethodInfo.classID, gGoForwardMethodInfo.methodID, (jint)webViewHandle_);
- return (bool)result;
- }
- bool UIPlatformWebView::CanGoBack() const
- {
- if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
- {
- return false;
- }
- jboolean result = gCanGoBackMethodInfo.GetEnv()->CallStaticBooleanMethod(gCanGoBackMethodInfo.classID, gCanGoBackMethodInfo.methodID, (jint)webViewHandle_);
- return (bool)result;
- }
- bool UIPlatformWebView::CanGoForward() const
- {
- if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
- {
- return false;
- }
- jboolean result = gCanGoForwardMethodInfo.GetEnv()->CallStaticBooleanMethod(gCanGoForwardMethodInfo.classID, gCanGoForwardMethodInfo.methodID, (jint)webViewHandle_);
- return (bool)result;
- }
- void UIPlatformWebView::PauseAll()
- {
- }
- void UIPlatformWebView::ResumeAll()
- {
- }
- void UIPlatformWebView::LoadURL(const String& url)
- {
- if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
- {
- queuedRequest_ = url;
- return;
- }
- jstring urlString = gRequestMethodInfo.GetEnv()->NewStringUTF(url.CString());
- gRequestMethodInfo.GetEnv()->CallStaticVoidMethod(gRequestMethodInfo.classID, gRequestMethodInfo.methodID, (jint)webViewHandle_, urlString);
- gRequestMethodInfo.GetEnv()->DeleteLocalRef(urlString);
- }
- void UIPlatformWebView::Reload()
- {
- if (webViewHandle_ == UI_PLATFORM_WEBVIEW_INVALID_HANDLE)
- {
- return;
- }
- }
- }
- extern "C"
- {
- void Java_com_atomicgameengine_player_AtomicWebView_nativeCallback(JNIEnv *env, jobject thiz, jstring data, jlong callback, jlong payload, jint type)
- {
- /*
- atomic_webViewCallback cb = (atomic_webViewCallback)callback;
- const char *dataString = env->GetStringUTFChars(data, 0);
- cb((void *)payload, (atomic_webViewCallbackType)type, dataString);
- env->ReleaseStringUTFChars(data, dataString);
- */
- }
- }
- #endif
|