2014-08-26 14:26:37 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2014-08-25 14:01:18 +08:00
|
|
|
|
2014-09-10 03:18:31 +08:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "ui/UIWebViewImpl-android.h"
|
2014-08-26 14:26:37 +08:00
|
|
|
|
2014-08-26 16:53:37 +08:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string>
|
2015-11-27 17:00:33 +08:00
|
|
|
#include "platform/android/jni/JniHelper.h"
|
2014-08-26 16:53:37 +08:00
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "ui/UIWebView.h"
|
2014-08-25 14:01:18 +08:00
|
|
|
#include "platform/CCGLView.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2015-08-07 17:27:15 +08:00
|
|
|
#include "ui/UIHelper.h"
|
2014-08-25 14:01:18 +08:00
|
|
|
|
2015-11-27 17:00:33 +08:00
|
|
|
static const std::string className = "org/cocos2dx/lib/Cocos2dxWebViewHelper";
|
2014-08-26 16:53:37 +08:00
|
|
|
|
|
|
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"",__VA_ARGS__)
|
|
|
|
|
2015-01-23 18:30:12 +08:00
|
|
|
static const std::string s_defaultBaseUrl = "file:///android_asset/";
|
|
|
|
static const std::string s_sdRootBaseUrl = "file://";
|
|
|
|
|
2015-10-15 21:43:53 +08:00
|
|
|
static std::string getFixedBaseUrl(const std::string& baseUrl)
|
2015-01-23 18:30:12 +08:00
|
|
|
{
|
|
|
|
std::string fixedBaseUrl;
|
|
|
|
if (baseUrl.empty())
|
|
|
|
{
|
|
|
|
fixedBaseUrl = s_defaultBaseUrl;
|
|
|
|
}
|
|
|
|
else if (baseUrl.find(s_sdRootBaseUrl) != std::string::npos)
|
|
|
|
{
|
|
|
|
fixedBaseUrl = baseUrl;
|
|
|
|
}
|
|
|
|
else if (baseUrl.c_str()[0] != '/') {
|
|
|
|
if(baseUrl.find("assets/") == 0) {
|
|
|
|
fixedBaseUrl = s_defaultBaseUrl + baseUrl.c_str()[7];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fixedBaseUrl = s_defaultBaseUrl + baseUrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fixedBaseUrl = s_sdRootBaseUrl + baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fixedBaseUrl.c_str()[fixedBaseUrl.length() - 1] != '/') {
|
|
|
|
fixedBaseUrl += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
return fixedBaseUrl;
|
|
|
|
}
|
|
|
|
|
2014-08-26 16:53:37 +08:00
|
|
|
extern "C" {
|
2014-08-26 14:26:37 +08:00
|
|
|
/*
|
|
|
|
* Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
|
|
|
|
* Method: shouldStartLoading
|
|
|
|
* Signature: (ILjava/lang/String;)Z
|
|
|
|
*/
|
|
|
|
JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_shouldStartLoading(JNIEnv *env, jclass, jint index, jstring jurl) {
|
|
|
|
auto charUrl = env->GetStringUTFChars(jurl, NULL);
|
|
|
|
std::string url = charUrl;
|
|
|
|
env->ReleaseStringUTFChars(jurl, charUrl);
|
|
|
|
return cocos2d::experimental::ui::WebViewImpl::shouldStartLoading(index, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
|
|
|
|
* Method: didFinishLoading
|
|
|
|
* Signature: (ILjava/lang/String;)V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading(JNIEnv *env, jclass, jint index, jstring jurl) {
|
2014-08-26 16:53:37 +08:00
|
|
|
// LOGD("didFinishLoading");
|
2014-08-26 14:26:37 +08:00
|
|
|
auto charUrl = env->GetStringUTFChars(jurl, NULL);
|
|
|
|
std::string url = charUrl;
|
|
|
|
env->ReleaseStringUTFChars(jurl, charUrl);
|
|
|
|
cocos2d::experimental::ui::WebViewImpl::didFinishLoading(index, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
|
|
|
|
* Method: didFailLoading
|
|
|
|
* Signature: (ILjava/lang/String;)V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading(JNIEnv *env, jclass, jint index, jstring jurl) {
|
2014-08-26 16:53:37 +08:00
|
|
|
// LOGD("didFailLoading");
|
2014-08-26 14:26:37 +08:00
|
|
|
auto charUrl = env->GetStringUTFChars(jurl, NULL);
|
|
|
|
std::string url = charUrl;
|
|
|
|
env->ReleaseStringUTFChars(jurl, charUrl);
|
|
|
|
cocos2d::experimental::ui::WebViewImpl::didFailLoading(index, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
|
|
|
|
* Method: onJsCallback
|
|
|
|
* Signature: (ILjava/lang/String;)V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback(JNIEnv *env, jclass, jint index, jstring jmessage) {
|
2014-08-26 16:53:37 +08:00
|
|
|
// LOGD("jsCallback");
|
2014-08-26 14:26:37 +08:00
|
|
|
auto charMessage = env->GetStringUTFChars(jmessage, NULL);
|
|
|
|
std::string message = charMessage;
|
|
|
|
env->ReleaseStringUTFChars(jmessage, charMessage);
|
|
|
|
cocos2d::experimental::ui::WebViewImpl::onJsCallback(index, message);
|
|
|
|
}
|
|
|
|
}
|
2014-08-25 14:01:18 +08:00
|
|
|
|
|
|
|
namespace {
|
2014-08-26 14:26:37 +08:00
|
|
|
|
2014-08-25 14:01:18 +08:00
|
|
|
int createWebViewJNI() {
|
|
|
|
cocos2d::JniMethodInfo t;
|
2015-11-27 17:00:33 +08:00
|
|
|
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), "createWebView", "()I")) {
|
2014-08-26 16:53:37 +08:00
|
|
|
// LOGD("error: %s,%d",__func__,__LINE__);
|
2014-08-25 14:01:18 +08:00
|
|
|
jint viewTag = t.env->CallStaticIntMethod(t.classID, t.methodID);
|
|
|
|
t.env->DeleteLocalRef(t.classID);
|
|
|
|
return viewTag;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getUrlStringByFileName(const std::string &fileName) {
|
2014-08-26 16:53:37 +08:00
|
|
|
// LOGD("error: %s,%d",__func__,__LINE__);
|
2014-08-25 14:01:18 +08:00
|
|
|
const std::string basePath("file:///android_asset/");
|
|
|
|
std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName);
|
|
|
|
const std::string assetsPath("assets/");
|
|
|
|
|
|
|
|
std::string urlString;
|
|
|
|
if (fullPath.find(assetsPath) != std::string::npos) {
|
|
|
|
urlString = fullPath.replace(fullPath.find_first_of(assetsPath), assetsPath.length(), basePath);
|
|
|
|
} else {
|
|
|
|
urlString = fullPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return urlString;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace cocos2d {
|
2014-08-26 14:26:37 +08:00
|
|
|
namespace experimental {
|
|
|
|
namespace ui{
|
|
|
|
|
|
|
|
static std::unordered_map<int, cocos2d::experimental::ui::WebViewImpl*> s_WebViewImpls;
|
|
|
|
|
|
|
|
WebViewImpl::WebViewImpl(WebView *webView) : _viewTag(-1), _webView(webView) {
|
|
|
|
_viewTag = createWebViewJNI();
|
|
|
|
s_WebViewImpls[_viewTag] = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
WebViewImpl::~WebViewImpl() {
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "removeWebView", _viewTag);
|
2014-08-26 14:26:37 +08:00
|
|
|
s_WebViewImpls.erase(_viewTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) {
|
|
|
|
std::string dataString(reinterpret_cast<char *>(data.getBytes()), static_cast<unsigned int>(data.getSize()));
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "setJavascriptInterfaceScheme", _viewTag, dataString, MIMEType, encoding, baseURL);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL) {
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "loadHTMLString", _viewTag, string, baseURL);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:14:04 +08:00
|
|
|
void WebViewImpl::loadURL(const std::string &url) {
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "loadUrl", _viewTag, url);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::loadFile(const std::string &fileName) {
|
|
|
|
auto fullPath = getUrlStringByFileName(fileName);
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "loadFile", _viewTag, fullPath);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::stopLoading() {
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "stopLoading", _viewTag);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::reload() {
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "reload", _viewTag);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebViewImpl::canGoBack() {
|
2015-11-27 17:00:33 +08:00
|
|
|
return JniHelper::callStaticBooleanMethod(className, "canGoBack", _viewTag);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebViewImpl::canGoForward() {
|
2015-11-27 17:00:33 +08:00
|
|
|
return JniHelper::callStaticBooleanMethod(className, "canGoForward", _viewTag);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::goBack() {
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "goBack", _viewTag);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::goForward() {
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "goForward", _viewTag);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme) {
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "setJavascriptInterfaceScheme", _viewTag, scheme);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::evaluateJS(const std::string &js) {
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "evaluateJS", _viewTag, js);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit) {
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "setScalesPageToFit", _viewTag, scalesPageToFit);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebViewImpl::shouldStartLoading(const int viewTag, const std::string &url) {
|
2015-10-15 21:43:53 +08:00
|
|
|
bool allowLoad = true;
|
2015-10-21 21:54:41 +08:00
|
|
|
auto it = s_WebViewImpls.find(viewTag);
|
|
|
|
if (it != s_WebViewImpls.end()) {
|
|
|
|
auto webView = it->second->_webView;
|
|
|
|
if (webView->_onShouldStartLoading) {
|
|
|
|
allowLoad = webView->_onShouldStartLoading(webView, url);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
2015-10-21 21:54:41 +08:00
|
|
|
}
|
2015-10-15 21:43:53 +08:00
|
|
|
return allowLoad;
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::didFinishLoading(const int viewTag, const std::string &url){
|
2015-10-21 21:54:41 +08:00
|
|
|
auto it = s_WebViewImpls.find(viewTag);
|
|
|
|
if (it != s_WebViewImpls.end()) {
|
|
|
|
auto webView = it->second->_webView;
|
|
|
|
if (webView->_onDidFinishLoading) {
|
|
|
|
webView->_onDidFinishLoading(webView, url);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
2015-10-21 21:54:41 +08:00
|
|
|
}
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::didFailLoading(const int viewTag, const std::string &url){
|
2015-10-21 21:54:41 +08:00
|
|
|
auto it = s_WebViewImpls.find(viewTag);
|
|
|
|
if (it != s_WebViewImpls.end()) {
|
|
|
|
auto webView = it->second->_webView;
|
|
|
|
if (webView->_onDidFailLoading) {
|
|
|
|
webView->_onDidFailLoading(webView, url);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
2015-10-21 21:54:41 +08:00
|
|
|
}
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::onJsCallback(const int viewTag, const std::string &message){
|
2015-10-21 21:54:41 +08:00
|
|
|
auto it = s_WebViewImpls.find(viewTag);
|
|
|
|
if (it != s_WebViewImpls.end()) {
|
|
|
|
auto webView = it->second->_webView;
|
|
|
|
if (webView->_onJSCallback) {
|
|
|
|
webView->_onJSCallback(webView, message);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
2015-10-21 21:54:41 +08:00
|
|
|
}
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) {
|
|
|
|
if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) {
|
2015-08-07 17:27:15 +08:00
|
|
|
auto uiRect = cocos2d::ui::Helper::convertBoundingBoxToScreen(_webView);
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "setWebViewRect", _viewTag,
|
|
|
|
(int)uiRect.origin.x, (int)uiRect.origin.y,
|
|
|
|
(int)uiRect.size.width, (int)uiRect.size.height);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::setVisible(bool visible) {
|
2015-11-27 17:00:33 +08:00
|
|
|
JniHelper::callStaticVoidMethod(className, "setVisible", _viewTag, visible);
|
2014-08-26 14:26:37 +08:00
|
|
|
}
|
2016-05-24 18:27:59 +08:00
|
|
|
|
|
|
|
void WebViewImpl::setBounces(bool bounces) {
|
|
|
|
// empty function as this was mainly a fix for iOS
|
|
|
|
}
|
2014-08-26 16:53:37 +08:00
|
|
|
} // namespace ui
|
2014-08-26 14:26:37 +08:00
|
|
|
} // namespace experimental
|
2014-08-26 16:53:37 +08:00
|
|
|
} //namespace cocos2d
|
2014-08-26 14:26:37 +08:00
|
|
|
|
2014-09-10 03:18:31 +08:00
|
|
|
#endif // __ANDROID__
|