From 15815118a2ae6b60d877617910efc3222192574c Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 26 Aug 2014 16:53:37 +0800 Subject: [PATCH] finish android version --- cocos/Android.mk | 4 +- .../org/cocos2dx/lib/Cocos2dxActivity.java | 12 ++++-- .../src/org/cocos2dx/lib/Cocos2dxWebView.java | 8 ++-- cocos/ui/WebViewImpl_android.cpp | 41 ++++++++++++++----- cocos/ui/Webview-inl.h | 6 +-- tests/cpp-tests/Android.mk | 1 + .../UIWebViewTest/UIWebViewTest.cpp | 14 ++++++- .../UIWebViewTest/UIWebViewTest.h | 2 + 8 files changed, 64 insertions(+), 24 deletions(-) diff --git a/cocos/Android.mk b/cocos/Android.mk index 7b2e999f32..086c9d07cc 100644 --- a/cocos/Android.mk +++ b/cocos/Android.mk @@ -349,7 +349,9 @@ LOCAL_SRC_FILES += ui/UIWidget.cpp \ ui/UIRelativeBox.cpp \ ui/UIVideoPlayerAndroid.cpp \ ui/UIDeprecated.cpp \ - ui/UIScale9Sprite.cpp + ui/UIScale9Sprite.cpp \ + ui/WebView.cpp \ + ui/WebViewImpl_android.cpp \ #extension LOCAL_SRC_FILES += ../extensions/assets-manager/AssetsManager.cpp \ diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java index 1ad24349ab..53ea0418a3 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -33,11 +33,10 @@ import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.Message; -import android.view.ViewGroup; -import android.view.WindowManager; -import android.util.Log; -import android.widget.FrameLayout; import android.preference.PreferenceManager.OnActivityResultListener; +import android.util.Log; +import android.view.ViewGroup; +import android.widget.FrameLayout; public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener { // =========================================================== @@ -55,6 +54,7 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe private Cocos2dxHandler mHandler; private static Cocos2dxActivity sContext = null; private Cocos2dxVideoHelper mVideoHelper = null; + private Cocos2dxWebViewHelper mWebViewHelper = null; public static Context getContext() { return sContext; @@ -102,6 +102,10 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe if (mVideoHelper == null) { mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout); } + + if(mWebViewHelper == null){ + mWebViewHelper = new Cocos2dxWebViewHelper(mFrameLayout); + } } //native method,call GLViewImpl::getGLContextAttrs() to get the OpenGL ES context attributions diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java index 362cc4e6c4..4229a21e0b 100755 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java @@ -1,5 +1,8 @@ package org.cocos2dx.lib; +import java.lang.reflect.Method; +import java.net.URI; + import android.annotation.SuppressLint; import android.content.Context; import android.util.Log; @@ -7,9 +10,6 @@ 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(); @@ -37,7 +37,7 @@ public class Cocos2dxWebView extends WebView { try { Method method = this.getClass().getMethod("removeJavascriptInterface", new Class[]{String.class}); method.invoke(this, "searchBoxJavaBridge_"); - } catch (ReflectiveOperationException e) { + } catch (Exception e) { Log.d(TAG, "This API level do not support `removeJavascriptInterface`"); } diff --git a/cocos/ui/WebViewImpl_android.cpp b/cocos/ui/WebViewImpl_android.cpp index 4207090169..67c6bb30a0 100644 --- a/cocos/ui/WebViewImpl_android.cpp +++ b/cocos/ui/WebViewImpl_android.cpp @@ -24,20 +24,22 @@ #include "WebViewImpl_android.h" -#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) -#include "WebView.h" +#include +#include +#include #include "jni/JniHelper.h" #include + +#include "WebView.h" #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"{ + +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"",__VA_ARGS__) + +extern "C" { /* * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper * Method: shouldStartLoading @@ -56,6 +58,7 @@ extern "C"{ * Signature: (ILjava/lang/String;)V */ JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading(JNIEnv *env, jclass, jint index, jstring jurl) { + // LOGD("didFinishLoading"); auto charUrl = env->GetStringUTFChars(jurl, NULL); std::string url = charUrl; env->ReleaseStringUTFChars(jurl, charUrl); @@ -68,6 +71,7 @@ extern "C"{ * Signature: (ILjava/lang/String;)V */ JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading(JNIEnv *env, jclass, jint index, jstring jurl) { + // LOGD("didFailLoading"); auto charUrl = env->GetStringUTFChars(jurl, NULL); std::string url = charUrl; env->ReleaseStringUTFChars(jurl, charUrl); @@ -80,6 +84,7 @@ extern "C"{ * Signature: (ILjava/lang/String;)V */ JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback(JNIEnv *env, jclass, jint index, jstring jmessage) { + // LOGD("jsCallback"); auto charMessage = env->GetStringUTFChars(jmessage, NULL); std::string message = charMessage; env->ReleaseStringUTFChars(jmessage, charMessage); @@ -92,6 +97,7 @@ namespace { int createWebViewJNI() { cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "createWebView", "()I")) { + // LOGD("error: %s,%d",__func__,__LINE__); jint viewTag = t.env->CallStaticIntMethod(t.classID, t.methodID); t.env->DeleteLocalRef(t.classID); return viewTag; @@ -100,6 +106,7 @@ int createWebViewJNI() { } void removeWebViewJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "removeWebView", "(I)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index); @@ -108,6 +115,7 @@ void removeWebViewJNI(const int index) { } void setWebViewRectJNI(const int index, const int left, const int top, const int width, const int height) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setWebViewRect", "(IIIII)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index, left, top, width, height); @@ -116,6 +124,7 @@ void setWebViewRectJNI(const int index, const int left, const int top, const int } void setJavascriptInterfaceSchemeJNI(const int index, const std::string &scheme) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setJavascriptInterfaceScheme", "(ILjava/lang/String;)V")) { jstring jScheme = t.env->NewStringUTF(scheme.c_str()); @@ -127,6 +136,7 @@ void setJavascriptInterfaceSchemeJNI(const int index, const std::string &scheme) } void loadDataJNI(const int index, const std::string &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadData", "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")) { jstring jData = t.env->NewStringUTF(data.c_str()); @@ -144,6 +154,7 @@ void loadDataJNI(const int index, const std::string &data, const std::string &MI } void loadHTMLStringJNI(const int index, const std::string &string, const std::string &baseURL) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadHTMLString", "(ILjava/lang/String;Ljava/lang/String;)V")) { jstring jString = t.env->NewStringUTF(string.c_str()); @@ -168,6 +179,7 @@ void loadUrlJNI(const int index, const std::string &url) { } void loadFileJNI(const int index, const std::string &filePath) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadFile", "(ILjava/lang/String;)V")) { jstring jFilePath = t.env->NewStringUTF(filePath.c_str()); @@ -179,6 +191,7 @@ void loadFileJNI(const int index, const std::string &filePath) { } void stopLoadingJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "stopLoading", "(I)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index); @@ -187,6 +200,7 @@ void stopLoadingJNI(const int index) { } void reloadJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "reload", "(I)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index); @@ -195,6 +209,7 @@ void reloadJNI(const int index) { } bool canGoBackJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "canGoBack", "(I)Z")) { jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, index); @@ -205,6 +220,7 @@ bool canGoBackJNI(const int index) { } bool canGoForwardJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "canGoForward", "(I)Z")) { jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, index); @@ -215,6 +231,7 @@ bool canGoForwardJNI(const int index) { } void goBackJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "goBack", "(I)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index); @@ -223,6 +240,7 @@ void goBackJNI(const int index) { } void goForwardJNI(const int index) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "goForward", "(I)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index); @@ -231,6 +249,7 @@ void goForwardJNI(const int index) { } void evaluateJSJNI(const int index, const std::string &js) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "evaluateJS", "(ILjava/lang/String;)V")) { jstring jjs = t.env->NewStringUTF(js.c_str()); @@ -242,6 +261,7 @@ void evaluateJSJNI(const int index, const std::string &js) { } void setScalesPageToFitJNI(const int index, const bool scalesPageToFit) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setScalesPageToFit", "(IZ)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index, scalesPageToFit); @@ -250,6 +270,7 @@ void setScalesPageToFitJNI(const int index, const bool scalesPageToFit) { } void setWebViewVisibleJNI(const int index, const bool visible) { + // LOGD("error: %s,%d",__func__,__LINE__); cocos2d::JniMethodInfo t; if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setVisible", "(IZ)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, index, visible); @@ -258,6 +279,7 @@ void setWebViewVisibleJNI(const int index, const bool visible) { } std::string getUrlStringByFileName(const std::string &fileName) { + // LOGD("error: %s,%d",__func__,__LINE__); const std::string basePath("file:///android_asset/"); std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName); const std::string assetsPath("assets/"); @@ -407,8 +429,7 @@ namespace cocos2d { void WebViewImpl::setVisible(bool visible) { setWebViewVisibleJNI(_viewTag, visible); } - } // namespace cocos2d + } // namespace ui } // namespace experimental -} //namespace ui +} //namespace cocos2d -#endif diff --git a/cocos/ui/Webview-inl.h b/cocos/ui/Webview-inl.h index 8a76365b0c..835d6829bb 100644 --- a/cocos/ui/Webview-inl.h +++ b/cocos/ui/Webview-inl.h @@ -135,7 +135,7 @@ namespace experimental{ Node::setVisible(visible); _impl->setVisible(visible); } - } // namespace cocos2d -} // namespace ui -} //namespace experimental + } // namespace ui +} // namespace experimental +} //namespace cocos2d diff --git a/tests/cpp-tests/Android.mk b/tests/cpp-tests/Android.mk index a954ff93e1..71ff77954d 100644 --- a/tests/cpp-tests/Android.mk +++ b/tests/cpp-tests/Android.mk @@ -104,6 +104,7 @@ Classes/UITest/CocoStudioGUITest/UITextBMFontTest/UITextBMFontTest_Editor.cpp \ Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp \ Classes/UITest/CocoStudioGUITest/UIWidgetAddNodeTest/UIWidgetAddNodeTest_Editor.cpp \ Classes/UITest/CocoStudioGUITest/UIVideoPlayerTest/UIVideoPlayerTest.cpp \ +Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp \ Classes/UITest/CocoStudioGUITest/CustomWidget/CustomImageView.cpp \ Classes/UITest/CocoStudioGUITest/CustomWidget/CustomImageViewReader.cpp \ Classes/UITest/CocoStudioGUITest/CustomWidget/CustomParticleWidget.cpp \ diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp index 2dd49d058f..f7bec25f7d 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp @@ -24,10 +24,20 @@ #include "UIWebViewTest.h" +WebViewTest::WebViewTest() +{ + +} + +WebViewTest::~WebViewTest() +{ + +} + bool WebViewTest::init() { if (UIScene::init()) { - _webView = experimental::ui::WebView::create(); + _webView = cocos2d::experimental::ui::WebView::create(); _webView->setPosition(Director::getInstance()->getVisibleSize()/2); _webView->setContentSize(Director::getInstance()->getVisibleSize() * 0.5); _webView->loadUrl("http://www.baidu.com"); @@ -37,4 +47,4 @@ bool WebViewTest::init() return true; } return false; -} \ No newline at end of file +} diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h index 9365412eb0..865ed9f02c 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.h @@ -32,6 +32,8 @@ USING_NS_CC; class WebViewTest : public UIScene { public: + WebViewTest(); + virtual ~WebViewTest(); UI_SCENE_CREATE_FUNC(WebViewTest); virtual bool init();