diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java new file mode 100755 index 0000000000..362cc4e6c4 --- /dev/null +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java @@ -0,0 +1,92 @@ +package org.cocos2dx.lib; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.util.Log; +import android.webkit.WebView; +import android.webkit.WebViewClient; +import android.widget.FrameLayout; + +import java.lang.reflect.Method; +import java.net.URI; + +public class Cocos2dxWebView extends WebView { + private static final String TAG = Cocos2dxWebViewHelper.class.getSimpleName(); + + private int viewTag; + private String jsScheme; + + public Cocos2dxWebView(Context context) { + this(context, -1); + } + + @SuppressLint("SetJavaScriptEnabled") + public Cocos2dxWebView(Context context, int viewTag) { + super(context); + this.viewTag = viewTag; + this.jsScheme = ""; + + this.setFocusable(true); + this.setFocusableInTouchMode(true); + + this.getSettings().setSupportZoom(false); + + this.getSettings().setJavaScriptEnabled(true); + + // `searchBoxJavaBridge_` has big security risk. http://jvn.jp/en/jp/JVN53768697 + try { + Method method = this.getClass().getMethod("removeJavascriptInterface", new Class[]{String.class}); + method.invoke(this, "searchBoxJavaBridge_"); + } catch (ReflectiveOperationException e) { + Log.d(TAG, "This API level do not support `removeJavascriptInterface`"); + } + + this.setWebViewClient(new Cocos2dxWebViewClient()); + } + + public void setJavascriptInterfaceScheme(String scheme) { + this.jsScheme = scheme != null ? scheme : ""; + } + + public void setScalesPageToFit(boolean scalesPageToFit) { + this.getSettings().setSupportZoom(scalesPageToFit); + } + + class Cocos2dxWebViewClient extends WebViewClient { + @Override + public boolean shouldOverrideUrlLoading(WebView view, String urlString) { + URI uri = URI.create(urlString); + if (uri != null && uri.getScheme().equals(jsScheme)) { + Cocos2dxWebViewHelper._onJsCallback(viewTag, urlString); + return true; + } + return Cocos2dxWebViewHelper._shouldStartLoading(viewTag, urlString); + } + + @Override + public void onPageFinished(WebView view, String url) { + super.onPageFinished(view, url); + Cocos2dxWebViewHelper._didFinishLoading(viewTag, url); + } + + @Override + public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { + super.onReceivedError(view, errorCode, description, failingUrl); + Cocos2dxWebViewHelper._didFailLoading(viewTag, failingUrl); + } + } + + public void setWebViewRect(int left, int top, int maxWidth, int maxHeight) { + fixSize(left, top, maxWidth, maxHeight); + } + + private void fixSize(int left, int top, int width, int height) { + FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, + FrameLayout.LayoutParams.WRAP_CONTENT); + layoutParams.leftMargin = left; + layoutParams.topMargin = top; + layoutParams.width = width; + layoutParams.height = height; + this.setLayoutParams(layoutParams); + } +} diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java new file mode 100755 index 0000000000..e8d45b787e --- /dev/null +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java @@ -0,0 +1,296 @@ +package org.cocos2dx.lib; + +import android.os.Handler; +import android.os.Looper; +import android.util.SparseArray; +import android.view.View; +import android.widget.FrameLayout; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; + + +public class Cocos2dxWebViewHelper { + private static final String TAG = Cocos2dxWebViewHelper.class.getSimpleName(); + private static Handler handler; + private static Cocos2dxActivity cocos2dxActivity; + private static FrameLayout layout; + + private static SparseArray webViews; + private static int viewTag = 0; + + public Cocos2dxWebViewHelper(FrameLayout layout) { + Cocos2dxWebViewHelper.layout = layout; + Cocos2dxWebViewHelper.handler = new Handler(Looper.myLooper()); + + Cocos2dxWebViewHelper.cocos2dxActivity = (Cocos2dxActivity) Cocos2dxActivity.getContext(); + Cocos2dxWebViewHelper.webViews = new SparseArray(); + } + + private static native boolean shouldStartLoading(int index, String message); + + public static boolean _shouldStartLoading(int index, String message) { + return !shouldStartLoading(index, message); + } + + private static native void didFinishLoading(int index, String message); + + public static void _didFinishLoading(int index, String message) { + didFinishLoading(index, message); + } + + private static native void didFailLoading(int index, String message); + + public static void _didFailLoading(int index, String message) { + didFailLoading(index, message); + } + + private static native void onJsCallback(int index, String message); + + public static void _onJsCallback(int index, String message) { + onJsCallback(index, message); + } + + @SuppressWarnings("unused") + public static int createWebView() { + final int index = viewTag; + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = new Cocos2dxWebView(cocos2dxActivity, index); + FrameLayout.LayoutParams lParams = new FrameLayout.LayoutParams( + FrameLayout.LayoutParams.WRAP_CONTENT, + FrameLayout.LayoutParams.WRAP_CONTENT); + layout.addView(webView, lParams); + + webViews.put(index, webView); + } + }); + return viewTag++; + } + + @SuppressWarnings("unused") + public static void removeWebView(final int index) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webViews.remove(index); + layout.removeView(webView); + } + } + }); + } + + @SuppressWarnings("unused") + public static void setVisible(final int index, final boolean visible) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.setVisibility(visible ? View.VISIBLE : View.GONE); + } + } + }); + } + + @SuppressWarnings("unused") + public static void setWebViewRect(final int index, final int left, final int top, final int maxWidth, final int maxHeight) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.setWebViewRect(left, top, maxWidth, maxHeight); + } + } + }); + } + + @SuppressWarnings("unused") + public static void setJavascriptInterfaceScheme(final int index, final String scheme) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.setJavascriptInterfaceScheme(scheme); + } + } + }); + } + + @SuppressWarnings("unused") + public static void loadData(final int index, final String data, final String mimeType, final String encoding, final String baseURL) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.loadDataWithBaseURL(baseURL, data, mimeType, encoding, null); + } + } + }); + } + + @SuppressWarnings("unused") + public static void loadHTMLString(final int index, final String htmlString, final String mimeType, final String encoding) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.loadData(htmlString, mimeType, encoding); + } + } + }); + } + + @SuppressWarnings("unused") + public static void loadUrl(final int index, final String url) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.loadUrl(url); + } + } + }); + } + + @SuppressWarnings("unused") + public static void loadFile(final int index, final String filePath) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.loadUrl(filePath); + } + } + }); + } + + public static void stopLoading(final int index) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.stopLoading(); + } + } + }); + + } + + public static void reload(final int index) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.reload(); + } + } + }); + } + + public static T callInMainThread(Callable call) throws ExecutionException, InterruptedException { + FutureTask task = new FutureTask(call); + handler.post(task); + return task.get(); + } + + @SuppressWarnings("unused") + public static boolean canGoBack(final int index) { + Callable callable = new Callable() { + @Override + public Boolean call() throws Exception { + Cocos2dxWebView webView = webViews.get(index); + return webView != null && webView.canGoBack(); + } + }; + try { + return callInMainThread(callable); + } catch (ExecutionException e) { + return false; + } catch (InterruptedException e) { + return false; + } + } + + @SuppressWarnings("unused") + public static boolean canGoForward(final int index) { + Callable callable = new Callable() { + @Override + public Boolean call() throws Exception { + Cocos2dxWebView webView = webViews.get(index); + return webView != null && webView.canGoForward(); + } + }; + try { + return callInMainThread(callable); + } catch (ExecutionException e) { + return false; + } catch (InterruptedException e) { + return false; + } + } + + @SuppressWarnings("unused") + public static void goBack(final int index) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.goBack(); + } + } + }); + } + + @SuppressWarnings("unused") + public static void goForward(final int index) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.goForward(); + } + } + }); + } + + @SuppressWarnings("unused") + public static void evaluateJS(final int index, final String js) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.loadUrl("javascript:" + js); + } + } + }); + } + + @SuppressWarnings("unused") + public static void setScalesPageToFit(final int index, final boolean scalesPageToFit) { + cocos2dxActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebView webView = webViews.get(index); + if (webView != null) { + webView.setScalesPageToFit(scalesPageToFit); + } + } + }); + } +} diff --git a/cocos/ui/Android.mk b/cocos/ui/Android.mk index 5dc94fe248..e2b875b237 100644 --- a/cocos/ui/Android.mk +++ b/cocos/ui/Android.mk @@ -33,7 +33,6 @@ UIDeprecated.cpp \ UIScale9Sprite.cpp \ WebView.cpp \ WebViewImpl_android.cpp \ -org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp \ LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/.. \ $(LOCAL_PATH)/../editor-support diff --git a/cocos/ui/WebView.cpp b/cocos/ui/WebView.cpp index 43772dc2c1..c8d4441d75 100644 --- a/cocos/ui/WebView.cpp +++ b/cocos/ui/WebView.cpp @@ -1,6 +1,30 @@ -// -// Created by gin0606 on 2014/08/05. -// +/**************************************************************************** + 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. + ****************************************************************************/ +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) -#include "WebViewImpl_iOS.h" +#include "WebViewImpl_android.h" #include "WebView-inl.h" + + +#endif diff --git a/cocos/ui/WebView.mm b/cocos/ui/WebView.mm index 3be09fdb80..c2a6b0c9da 100644 --- a/cocos/ui/WebView.mm +++ b/cocos/ui/WebView.mm @@ -23,122 +23,8 @@ ****************************************************************************/ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) - -#include "WebView.h" -#include "platform/CCGLView.h" -#include "base/CCDirector.h" -#include "platform/CCFileUtils.h" - #include "WebViewImpl_iOS.h" +#include "WebView-inl.h" -NS_CC_BEGIN -namespace experimental{ - namespace ui{ - - WebView::WebView() - : _impl(new WebViewImpl(this)) - { - } - - WebView::~WebView() - { - CC_SAFE_DELETE(_impl); - } - - WebView *WebView::create() - { - auto webView = new(std::nothrow) WebView(); - if (webView && webView->init()) - { - webView->autorelease(); - return webView; - } - CC_SAFE_DELETE(webView); - return nullptr; - } - - void WebView::setJavascriptInterfaceScheme(const std::string &scheme) - { - _impl->setJavascriptInterfaceScheme(scheme); - } - - void WebView::loadData(const cocos2d::Data &data, - const std::string &MIMEType, - const std::string &encoding, - const std::string &baseURL) - { - _impl->loadData(data, MIMEType, encoding, baseURL); - } - - void WebView::loadHTMLString(const std::string &string, const std::string &baseURL) - { - _impl->loadHTMLString(string, baseURL); - } - - void WebView::loadUrl(const std::string &url) - { - _impl->loadUrl(url); - } - - void WebView::loadFile(const std::string &fileName) - { - _impl->loadFile(fileName); - } - - void WebView::stopLoading() - { - _impl->stopLoading(); - } - - void WebView::reload() - { - _impl->reload(); - } - - bool WebView::canGoBack() - { - return _impl->canGoBack(); - } - - bool WebView::canGoForward() - { - return _impl->canGoForward(); - } - - void WebView::goBack() - { - _impl->goBack(); - } - - void WebView::goForward() - { - _impl->goForward(); - } - - void WebView::evaluateJS(const std::string &js) - { - _impl->evaluateJS(js); - } - - void WebView::setScalesPageToFit(bool const scalesPageToFit) - { - _impl->setScalesPageToFit(scalesPageToFit); - } - - void WebView::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) - { - cocos2d::ui::Widget::draw(renderer, transform, flags); - _impl->draw(renderer, transform, flags); - } - - void WebView::setVisible(bool visible) - { - Node::setVisible(visible); - _impl->setVisible(visible); - } - } // namespace cocos2d -} // namespace ui -} //namespace experimental - -#endif \ No newline at end of file +#endif diff --git a/cocos/ui/WebViewImpl_android.cpp b/cocos/ui/WebViewImpl_android.cpp index 261db76781..4207090169 100644 --- a/cocos/ui/WebViewImpl_android.cpp +++ b/cocos/ui/WebViewImpl_android.cpp @@ -1,19 +1,94 @@ -// -// Created by gin0606 on 2014/07/30. -// +/**************************************************************************** + 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. + ****************************************************************************/ #include "WebViewImpl_android.h" + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include "WebView.h" -#include "org_cocos2dx_lib_Cocos2dxWebViewHelper.h" #include "jni/JniHelper.h" +#include #include "platform/CCGLView.h" #include "base/CCDirector.h" #include "platform/CCFileUtils.h" #include +#include +#include +#include #define CLASS_NAME "org/cocos2dx/lib/Cocos2dxWebViewHelper" +extern "C"{ + /* + * 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) { + 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) { + 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) { + auto charMessage = env->GetStringUTFChars(jmessage, NULL); + std::string message = charMessage; + env->ReleaseStringUTFChars(jmessage, charMessage); + cocos2d::experimental::ui::WebViewImpl::onJsCallback(index, message); + } +} namespace { + int createWebViewJNI() { cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "createWebView", "()I")) { @@ -199,136 +274,141 @@ std::string getUrlStringByFileName(const std::string &fileName) { } // namespace namespace cocos2d { -namespace plugin { -static std::unordered_map s_WebViewImpls; + namespace experimental { + namespace ui{ -WebViewImpl::WebViewImpl(WebView *webView) : _viewTag(-1), _webView(webView) { - _viewTag = createWebViewJNI(); - s_WebViewImpls[_viewTag] = this; -} + static std::unordered_map s_WebViewImpls; -WebViewImpl::~WebViewImpl() { - removeWebViewJNI(_viewTag); - s_WebViewImpls.erase(_viewTag); -} + WebViewImpl::WebViewImpl(WebView *webView) : _viewTag(-1), _webView(webView) { + _viewTag = createWebViewJNI(); + s_WebViewImpls[_viewTag] = this; + } -void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { - std::string dataString(reinterpret_cast(data.getBytes()), static_cast(data.getSize())); - loadDataJNI(_viewTag, dataString, MIMEType, encoding, baseURL); -} + WebViewImpl::~WebViewImpl() { + removeWebViewJNI(_viewTag); + s_WebViewImpls.erase(_viewTag); + } -void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL) { - loadHTMLStringJNI(_viewTag, string, baseURL); -} + void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { + std::string dataString(reinterpret_cast(data.getBytes()), static_cast(data.getSize())); + loadDataJNI(_viewTag, dataString, MIMEType, encoding, baseURL); + } -void WebViewImpl::loadUrl(const std::string &url) { - loadUrlJNI(_viewTag, url); -} + void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL) { + loadHTMLStringJNI(_viewTag, string, baseURL); + } -void WebViewImpl::loadFile(const std::string &fileName) { - auto fullPath = getUrlStringByFileName(fileName); - loadFileJNI(_viewTag, fullPath); -} + void WebViewImpl::loadUrl(const std::string &url) { + loadUrlJNI(_viewTag, url); + } -void WebViewImpl::stopLoading() { - stopLoadingJNI(_viewTag); -} + void WebViewImpl::loadFile(const std::string &fileName) { + auto fullPath = getUrlStringByFileName(fileName); + loadFileJNI(_viewTag, fullPath); + } -void WebViewImpl::reload() { - reloadJNI(_viewTag); -} + void WebViewImpl::stopLoading() { + stopLoadingJNI(_viewTag); + } -bool WebViewImpl::canGoBack() { - return canGoBackJNI(_viewTag); -} + void WebViewImpl::reload() { + reloadJNI(_viewTag); + } -bool WebViewImpl::canGoForward() { - return canGoForwardJNI(_viewTag); -} + bool WebViewImpl::canGoBack() { + return canGoBackJNI(_viewTag); + } -void WebViewImpl::goBack() { - goBackJNI(_viewTag); -} + bool WebViewImpl::canGoForward() { + return canGoForwardJNI(_viewTag); + } -void WebViewImpl::goForward() { - goForwardJNI(_viewTag); -} + void WebViewImpl::goBack() { + goBackJNI(_viewTag); + } -void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme) { - setJavascriptInterfaceSchemeJNI(_viewTag, scheme); -} + void WebViewImpl::goForward() { + goForwardJNI(_viewTag); + } -void WebViewImpl::evaluateJS(const std::string &js) { - evaluateJSJNI(_viewTag, js); -} + void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme) { + setJavascriptInterfaceSchemeJNI(_viewTag, scheme); + } -void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit) { - setScalesPageToFitJNI(_viewTag, scalesPageToFit); -} + void WebViewImpl::evaluateJS(const std::string &js) { + evaluateJSJNI(_viewTag, js); + } -bool WebViewImpl::shouldStartLoading(const int viewTag, const std::string &url) { - auto it = s_WebViewImpls.find(viewTag); - if (it != s_WebViewImpls.end()) { - auto webView = s_WebViewImpls[viewTag]->_webView; - if (webView->shouldStartLoading) { - return webView->shouldStartLoading(webView, url); - } - } - return true; -} + void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit) { + setScalesPageToFitJNI(_viewTag, scalesPageToFit); + } -void WebViewImpl::didFinishLoading(const int viewTag, const std::string &url){ - auto it = s_WebViewImpls.find(viewTag); - if (it != s_WebViewImpls.end()) { - auto webView = s_WebViewImpls[viewTag]->_webView; - if (webView->didFinishLoading) { - webView->didFinishLoading(webView, url); - } - } -} + bool WebViewImpl::shouldStartLoading(const int viewTag, const std::string &url) { + auto it = s_WebViewImpls.find(viewTag); + if (it != s_WebViewImpls.end()) { + auto webView = s_WebViewImpls[viewTag]->_webView; + if (webView->shouldStartLoading) { + return webView->shouldStartLoading(webView, url); + } + } + return true; + } -void WebViewImpl::didFailLoading(const int viewTag, const std::string &url){ - auto it = s_WebViewImpls.find(viewTag); - if (it != s_WebViewImpls.end()) { - auto webView = s_WebViewImpls[viewTag]->_webView; - if (webView->didFailLoading) { - webView->didFailLoading(webView, url); - } - } -} + void WebViewImpl::didFinishLoading(const int viewTag, const std::string &url){ + auto it = s_WebViewImpls.find(viewTag); + if (it != s_WebViewImpls.end()) { + auto webView = s_WebViewImpls[viewTag]->_webView; + if (webView->didFinishLoading) { + webView->didFinishLoading(webView, url); + } + } + } -void WebViewImpl::onJsCallback(const int viewTag, const std::string &message){ - auto it = s_WebViewImpls.find(viewTag); - if (it != s_WebViewImpls.end()) { - auto webView = s_WebViewImpls[viewTag]->_webView; - if (webView->onJsCallback) { - webView->onJsCallback(webView, message); - } - } -} + void WebViewImpl::didFailLoading(const int viewTag, const std::string &url){ + auto it = s_WebViewImpls.find(viewTag); + if (it != s_WebViewImpls.end()) { + auto webView = s_WebViewImpls[viewTag]->_webView; + if (webView->didFailLoading) { + webView->didFailLoading(webView, url); + } + } + } -void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) { - if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) { - auto directorInstance = cocos2d::Director::getInstance(); - auto glView = directorInstance->getOpenGLView(); - auto frameSize = glView->getFrameSize(); + void WebViewImpl::onJsCallback(const int viewTag, const std::string &message){ + auto it = s_WebViewImpls.find(viewTag); + if (it != s_WebViewImpls.end()) { + auto webView = s_WebViewImpls[viewTag]->_webView; + if (webView->onJsCallback) { + webView->onJsCallback(webView, message); + } + } + } - auto winSize = directorInstance->getWinSize(); + void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) { + if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) { + auto directorInstance = cocos2d::Director::getInstance(); + auto glView = directorInstance->getOpenGLView(); + auto frameSize = glView->getFrameSize(); - auto leftBottom = this->_webView->convertToWorldSpace(cocos2d::Point::ZERO); - auto rightTop = this->_webView->convertToWorldSpace(cocos2d::Point(this->_webView->getContentSize().width,this->_webView->getContentSize().height)); + auto winSize = directorInstance->getWinSize(); - auto uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX(); - auto uiTop = frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY(); + auto leftBottom = this->_webView->convertToWorldSpace(cocos2d::Point::ZERO); + auto rightTop = this->_webView->convertToWorldSpace(cocos2d::Point(this->_webView->getContentSize().width,this->_webView->getContentSize().height)); - setWebViewRectJNI(_viewTag,uiLeft,uiTop, - (rightTop.x - leftBottom.x) * glView->getScaleX(), - (rightTop.y - leftBottom.y) * glView->getScaleY()); - } -} + auto uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX(); + auto uiTop = frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY(); -void WebViewImpl::setVisible(bool visible) { - setWebViewVisibleJNI(_viewTag, visible); -} -} // namespace cocos2d -} // namespace plugin + setWebViewRectJNI(_viewTag,uiLeft,uiTop, + (rightTop.x - leftBottom.x) * glView->getScaleX(), + (rightTop.y - leftBottom.y) * glView->getScaleY()); + } + } + + void WebViewImpl::setVisible(bool visible) { + setWebViewVisibleJNI(_viewTag, visible); + } + } // namespace cocos2d + } // namespace experimental +} //namespace ui + +#endif diff --git a/cocos/ui/WebViewImpl_android.h b/cocos/ui/WebViewImpl_android.h index bc43f72282..01a3fd86ef 100644 --- a/cocos/ui/WebViewImpl_android.h +++ b/cocos/ui/WebViewImpl_android.h @@ -1,6 +1,26 @@ -// -// Created by gin0606 on 2014/07/30. -// +/**************************************************************************** + 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. + ****************************************************************************/ #ifndef __cocos2d_plugin_WebViewImpl_android_H_ #define __cocos2d_plugin_WebViewImpl_android_H_ @@ -8,65 +28,69 @@ #include namespace cocos2d { -class Data; -class Renderer; -class Mat4; + class Data; + class Renderer; + class Mat4; -namespace plugin { -class WebView; -} + namespace experimental { + namespace ui{ + class WebView; + } + } } namespace cocos2d { -namespace plugin { + namespace experimental { + namespace ui{ -class WebViewImpl { -public: - WebViewImpl(cocos2d::plugin::WebView *webView); + class WebViewImpl { + public: + WebViewImpl(cocos2d::experimental::ui::WebView *webView); - virtual ~WebViewImpl(); + virtual ~WebViewImpl(); - void setJavascriptInterfaceScheme(const std::string &scheme); + void setJavascriptInterfaceScheme(const std::string &scheme); - void loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL); + void loadData(const cocos2d::Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL); - void loadHTMLString(const std::string &string, const std::string &baseURL); + void loadHTMLString(const std::string &string, const std::string &baseURL); - void loadUrl(const std::string &url); + void loadUrl(const std::string &url); - void loadFile(const std::string &fileName); + void loadFile(const std::string &fileName); - void stopLoading(); + void stopLoading(); - void reload(); + void reload(); - bool canGoBack(); + bool canGoBack(); - bool canGoForward(); + bool canGoForward(); - void goBack(); + void goBack(); - void goForward(); + void goForward(); - void evaluateJS(const std::string &js); + void evaluateJS(const std::string &js); - void setScalesPageToFit(const bool scalesPageToFit); + void setScalesPageToFit(const bool scalesPageToFit); - virtual void draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags); + virtual void draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags); - virtual void setVisible(bool visible); + virtual void setVisible(bool visible); - static bool shouldStartLoading(const int viewTag, const std::string &url); - static void didFinishLoading(const int viewTag, const std::string &url); - static void didFailLoading(const int viewTag, const std::string &url); - static void onJsCallback(const int viewTag, const std::string &message); + static bool shouldStartLoading(const int viewTag, const std::string &url); + static void didFinishLoading(const int viewTag, const std::string &url); + static void didFailLoading(const int viewTag, const std::string &url); + static void onJsCallback(const int viewTag, const std::string &message); -private: - int _viewTag; - WebView *_webView; -}; + private: + int _viewTag; + WebView *_webView; + }; -} // namespace cocos2d -} // namespace plugin + } // namespace ui + } // namespace experimental +} //cocos2d #endif //__cocos2d_plugin_WebViewImpl_android_H_ diff --git a/cocos/ui/Webview-inl.h b/cocos/ui/Webview-inl.h new file mode 100644 index 0000000000..8a76365b0c --- /dev/null +++ b/cocos/ui/Webview-inl.h @@ -0,0 +1,141 @@ +/**************************************************************************** + 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. + ****************************************************************************/ + + +#include "WebView.h" +#include "platform/CCGLView.h" +#include "base/CCDirector.h" +#include "platform/CCFileUtils.h" + + + +NS_CC_BEGIN +namespace experimental{ + namespace ui{ + + WebView::WebView() + : _impl(new WebViewImpl(this)) + { + } + + WebView::~WebView() + { + CC_SAFE_DELETE(_impl); + } + + WebView *WebView::create() + { + auto webView = new(std::nothrow) WebView(); + if (webView && webView->init()) + { + webView->autorelease(); + return webView; + } + CC_SAFE_DELETE(webView); + return nullptr; + } + + void WebView::setJavascriptInterfaceScheme(const std::string &scheme) + { + _impl->setJavascriptInterfaceScheme(scheme); + } + + void WebView::loadData(const cocos2d::Data &data, + const std::string &MIMEType, + const std::string &encoding, + const std::string &baseURL) + { + _impl->loadData(data, MIMEType, encoding, baseURL); + } + + void WebView::loadHTMLString(const std::string &string, const std::string &baseURL) + { + _impl->loadHTMLString(string, baseURL); + } + + void WebView::loadUrl(const std::string &url) + { + _impl->loadUrl(url); + } + + void WebView::loadFile(const std::string &fileName) + { + _impl->loadFile(fileName); + } + + void WebView::stopLoading() + { + _impl->stopLoading(); + } + + void WebView::reload() + { + _impl->reload(); + } + + bool WebView::canGoBack() + { + return _impl->canGoBack(); + } + + bool WebView::canGoForward() + { + return _impl->canGoForward(); + } + + void WebView::goBack() + { + _impl->goBack(); + } + + void WebView::goForward() + { + _impl->goForward(); + } + + void WebView::evaluateJS(const std::string &js) + { + _impl->evaluateJS(js); + } + + void WebView::setScalesPageToFit(bool const scalesPageToFit) + { + _impl->setScalesPageToFit(scalesPageToFit); + } + + void WebView::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) + { + cocos2d::ui::Widget::draw(renderer, transform, flags); + _impl->draw(renderer, transform, flags); + } + + void WebView::setVisible(bool visible) + { + Node::setVisible(visible); + _impl->setVisible(visible); + } + } // namespace cocos2d +} // namespace ui +} //namespace experimental + diff --git a/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp b/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp deleted file mode 100644 index 203bd43987..0000000000 --- a/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "org_cocos2dx_lib_Cocos2dxWebViewHelper.h" -#include "WebViewImpl_android.h" -#include "WebView.h" -#include -#include -#include - - -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::plugin::WebViewImpl::shouldStartLoading(index, url); -} - -JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading(JNIEnv *env, jclass, jint index, jstring jurl) { - auto charUrl = env->GetStringUTFChars(jurl, NULL); - std::string url = charUrl; - env->ReleaseStringUTFChars(jurl, charUrl); - cocos2d::plugin::WebViewImpl::didFinishLoading(index, url); -} - -JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading(JNIEnv *env, jclass, jint index, jstring jurl) { - auto charUrl = env->GetStringUTFChars(jurl, NULL); - std::string url = charUrl; - env->ReleaseStringUTFChars(jurl, charUrl); - cocos2d::plugin::WebViewImpl::didFailLoading(index, url); -} - -JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback(JNIEnv *env, jclass, jint index, jstring jmessage) { - auto charMessage = env->GetStringUTFChars(jmessage, NULL); - std::string message = charMessage; - env->ReleaseStringUTFChars(jmessage, charMessage); - cocos2d::plugin::WebViewImpl::onJsCallback(index, message); -} diff --git a/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.h b/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.h deleted file mode 100644 index 288bf79765..0000000000 --- a/cocos/ui/org_cocos2dx_lib_Cocos2dxWebViewHelper.h +++ /dev/null @@ -1,59 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class org_cocos2dx_lib_Cocos2dxWebViewHelper */ - -#ifndef _Included_org_cocos2dx_lib_Cocos2dxWebViewHelper -#define _Included_org_cocos2dx_lib_Cocos2dxWebViewHelper -#ifdef __cplusplus -extern "C" { -#endif -/* Inaccessible static: handler */ -/* Inaccessible static: viewTag */ -#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskCreate -#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskCreate 0L -#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskRemove -#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskRemove 1L -#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskSetRect -#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskSetRect 2L -#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskLoadURI -#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskLoadURI 3L -#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskEvaluateJS -#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskEvaluateJS 4L -#undef org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskSetVisible -#define org_cocos2dx_lib_Cocos2dxWebViewHelper_kWebViewTaskSetVisible 5L -/* - * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper - * Method: shouldStartLoading - * Signature: (ILjava/lang/String;)Z - */ -JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_shouldStartLoading - (JNIEnv *, jclass, jint, jstring); - -/* - * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper - * Method: didFinishLoading - * Signature: (ILjava/lang/String;)V - */ -JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading - (JNIEnv *, jclass, jint, jstring); - -/* - * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper - * Method: didFailLoading - * Signature: (ILjava/lang/String;)V - */ -JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading - (JNIEnv *, jclass, jint, jstring); - -/* - * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper - * Method: onJsCallback - * Signature: (ILjava/lang/String;)V - */ -JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback - (JNIEnv *, jclass, jint, jstring); - -#ifdef __cplusplus -} -#endif -#endif