UIWebViewImpl-android.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifdef __ANDROID__
  22. #include "ui/UIWebViewImpl-android.h"
  23. #include <unordered_map>
  24. #include <stdlib.h>
  25. #include <string>
  26. #include "platform/android/jni/JniHelper.h"
  27. #include "ui/UIWebView.h"
  28. #include "platform/CCGLView.h"
  29. #include "base/CCDirector.h"
  30. #include "platform/CCFileUtils.h"
  31. #include "ui/UIHelper.h"
  32. static const std::string className = "org/cocos2dx/lib/Cocos2dxWebViewHelper";
  33. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"",__VA_ARGS__)
  34. static const std::string s_defaultBaseUrl = "file:///android_asset/";
  35. static const std::string s_sdRootBaseUrl = "file://";
  36. static std::string getFixedBaseUrl(const std::string& baseUrl)
  37. {
  38. std::string fixedBaseUrl;
  39. if (baseUrl.empty())
  40. {
  41. fixedBaseUrl = s_defaultBaseUrl;
  42. }
  43. else if (baseUrl.find(s_sdRootBaseUrl) != std::string::npos)
  44. {
  45. fixedBaseUrl = baseUrl;
  46. }
  47. else if (baseUrl.c_str()[0] != '/') {
  48. if(baseUrl.find("assets/") == 0) {
  49. fixedBaseUrl = s_defaultBaseUrl + baseUrl.c_str()[7];
  50. }
  51. else {
  52. fixedBaseUrl = s_defaultBaseUrl + baseUrl;
  53. }
  54. }
  55. else {
  56. fixedBaseUrl = s_sdRootBaseUrl + baseUrl;
  57. }
  58. if (fixedBaseUrl.c_str()[fixedBaseUrl.length() - 1] != '/') {
  59. fixedBaseUrl += "/";
  60. }
  61. return fixedBaseUrl;
  62. }
  63. extern "C" {
  64. /*
  65. * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
  66. * Method: shouldStartLoading
  67. * Signature: (ILjava/lang/String;)Z
  68. */
  69. JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_shouldStartLoading(JNIEnv *env, jclass, jint index, jstring jurl) {
  70. auto charUrl = env->GetStringUTFChars(jurl, NULL);
  71. std::string url = charUrl;
  72. env->ReleaseStringUTFChars(jurl, charUrl);
  73. return cocos2d::experimental::ui::WebViewImpl::shouldStartLoading(index, url);
  74. }
  75. /*
  76. * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
  77. * Method: didFinishLoading
  78. * Signature: (ILjava/lang/String;)V
  79. */
  80. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFinishLoading(JNIEnv *env, jclass, jint index, jstring jurl) {
  81. // LOGD("didFinishLoading");
  82. auto charUrl = env->GetStringUTFChars(jurl, NULL);
  83. std::string url = charUrl;
  84. env->ReleaseStringUTFChars(jurl, charUrl);
  85. cocos2d::experimental::ui::WebViewImpl::didFinishLoading(index, url);
  86. }
  87. /*
  88. * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
  89. * Method: didFailLoading
  90. * Signature: (ILjava/lang/String;)V
  91. */
  92. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_didFailLoading(JNIEnv *env, jclass, jint index, jstring jurl) {
  93. // LOGD("didFailLoading");
  94. auto charUrl = env->GetStringUTFChars(jurl, NULL);
  95. std::string url = charUrl;
  96. env->ReleaseStringUTFChars(jurl, charUrl);
  97. cocos2d::experimental::ui::WebViewImpl::didFailLoading(index, url);
  98. }
  99. /*
  100. * Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
  101. * Method: onJsCallback
  102. * Signature: (ILjava/lang/String;)V
  103. */
  104. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxWebViewHelper_onJsCallback(JNIEnv *env, jclass, jint index, jstring jmessage) {
  105. // LOGD("jsCallback");
  106. auto charMessage = env->GetStringUTFChars(jmessage, NULL);
  107. std::string message = charMessage;
  108. env->ReleaseStringUTFChars(jmessage, charMessage);
  109. cocos2d::experimental::ui::WebViewImpl::onJsCallback(index, message);
  110. }
  111. }
  112. namespace {
  113. int createWebViewJNI() {
  114. cocos2d::JniMethodInfo t;
  115. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), "createWebView", "()I")) {
  116. // LOGD("error: %s,%d",__func__,__LINE__);
  117. jint viewTag = t.env->CallStaticIntMethod(t.classID, t.methodID);
  118. t.env->DeleteLocalRef(t.classID);
  119. return viewTag;
  120. }
  121. return -1;
  122. }
  123. std::string getUrlStringByFileName(const std::string &fileName) {
  124. // LOGD("error: %s,%d",__func__,__LINE__);
  125. const std::string basePath("file:///android_asset/");
  126. std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName);
  127. const std::string assetsPath("assets/");
  128. std::string urlString;
  129. if (fullPath.find(assetsPath) != std::string::npos) {
  130. urlString = fullPath.replace(fullPath.find_first_of(assetsPath), assetsPath.length(), basePath);
  131. } else {
  132. urlString = fullPath;
  133. }
  134. return urlString;
  135. }
  136. } // namespace
  137. namespace cocos2d {
  138. namespace experimental {
  139. namespace ui{
  140. static std::unordered_map<int, cocos2d::experimental::ui::WebViewImpl*> s_WebViewImpls;
  141. WebViewImpl::WebViewImpl(WebView *webView) : _viewTag(-1), _webView(webView) {
  142. _viewTag = createWebViewJNI();
  143. s_WebViewImpls[_viewTag] = this;
  144. }
  145. WebViewImpl::~WebViewImpl() {
  146. JniHelper::callStaticVoidMethod(className, "removeWebView", _viewTag);
  147. s_WebViewImpls.erase(_viewTag);
  148. }
  149. void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) {
  150. std::string dataString(reinterpret_cast<char *>(data.getBytes()), static_cast<unsigned int>(data.getSize()));
  151. JniHelper::callStaticVoidMethod(className, "setJavascriptInterfaceScheme", _viewTag, dataString, MIMEType, encoding, baseURL);
  152. }
  153. void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL) {
  154. JniHelper::callStaticVoidMethod(className, "loadHTMLString", _viewTag, string, getFixedBaseUrl(baseURL));
  155. }
  156. void WebViewImpl::loadURL(const std::string &url) {
  157. this->loadURL(url, false);
  158. }
  159. void WebViewImpl::loadURL(const std::string &url, bool cleanCachedData) {
  160. JniHelper::callStaticVoidMethod(className, "loadUrl", _viewTag, url, cleanCachedData);
  161. }
  162. void WebViewImpl::loadFile(const std::string &fileName) {
  163. auto fullPath = getUrlStringByFileName(fileName);
  164. JniHelper::callStaticVoidMethod(className, "loadFile", _viewTag, fullPath);
  165. }
  166. void WebViewImpl::PostURL(const std::string &url, const std::string &data)
  167. {
  168. JniHelper::callStaticVoidMethod(className,"postUrl", _viewTag, url, data);
  169. }
  170. void WebViewImpl::stopLoading() {
  171. JniHelper::callStaticVoidMethod(className, "stopLoading", _viewTag);
  172. }
  173. void WebViewImpl::reload() {
  174. JniHelper::callStaticVoidMethod(className, "reload", _viewTag);
  175. }
  176. bool WebViewImpl::canGoBack() {
  177. return JniHelper::callStaticBooleanMethod(className, "canGoBack", _viewTag);
  178. }
  179. bool WebViewImpl::canGoForward() {
  180. return JniHelper::callStaticBooleanMethod(className, "canGoForward", _viewTag);
  181. }
  182. void WebViewImpl::goBack() {
  183. JniHelper::callStaticVoidMethod(className, "goBack", _viewTag);
  184. }
  185. void WebViewImpl::goForward() {
  186. JniHelper::callStaticVoidMethod(className, "goForward", _viewTag);
  187. }
  188. void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme) {
  189. JniHelper::callStaticVoidMethod(className, "setJavascriptInterfaceScheme", _viewTag, scheme);
  190. }
  191. void WebViewImpl::evaluateJS(const std::string &js) {
  192. JniHelper::callStaticVoidMethod(className, "evaluateJS", _viewTag, js);
  193. }
  194. void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit) {
  195. JniHelper::callStaticVoidMethod(className, "setScalesPageToFit", _viewTag, scalesPageToFit);
  196. }
  197. bool WebViewImpl::shouldStartLoading(const int viewTag, const std::string &url) {
  198. bool allowLoad = true;
  199. auto it = s_WebViewImpls.find(viewTag);
  200. if (it != s_WebViewImpls.end()) {
  201. auto webView = it->second->_webView;
  202. if (webView->_onShouldStartLoading) {
  203. allowLoad = webView->_onShouldStartLoading(webView, url);
  204. }
  205. }
  206. return allowLoad;
  207. }
  208. void WebViewImpl::didFinishLoading(const int viewTag, const std::string &url){
  209. auto it = s_WebViewImpls.find(viewTag);
  210. if (it != s_WebViewImpls.end()) {
  211. auto webView = it->second->_webView;
  212. if (webView->_onDidFinishLoading) {
  213. webView->_onDidFinishLoading(webView, url);
  214. }
  215. }
  216. }
  217. void WebViewImpl::didFailLoading(const int viewTag, const std::string &url){
  218. auto it = s_WebViewImpls.find(viewTag);
  219. if (it != s_WebViewImpls.end()) {
  220. auto webView = it->second->_webView;
  221. if (webView->_onDidFailLoading) {
  222. webView->_onDidFailLoading(webView, url);
  223. }
  224. }
  225. }
  226. void WebViewImpl::onJsCallback(const int viewTag, const std::string &message){
  227. auto it = s_WebViewImpls.find(viewTag);
  228. if (it != s_WebViewImpls.end()) {
  229. auto webView = it->second->_webView;
  230. if (webView->_onJSCallback) {
  231. webView->_onJSCallback(webView, message);
  232. }
  233. }
  234. }
  235. void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) {
  236. if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) {
  237. auto uiRect = cocos2d::ui::Helper::convertBoundingBoxToScreen(_webView);
  238. JniHelper::callStaticVoidMethod(className, "setWebViewRect", _viewTag,
  239. (int)uiRect.origin.x, (int)uiRect.origin.y,
  240. (int)uiRect.size.width, (int)uiRect.size.height);
  241. }
  242. }
  243. void WebViewImpl::setVisible(bool visible) {
  244. JniHelper::callStaticVoidMethod(className, "setVisible", _viewTag, visible);
  245. }
  246. void WebViewImpl::setOpacityWebView(const float opacity){
  247. JniHelper::callStaticVoidMethod(className, "setOpacityWebView", _viewTag, opacity);
  248. };
  249. float WebViewImpl::getOpacityWebView()const{
  250. return JniHelper::callStaticFloatMethod(className, "getOpacityWebView", _viewTag);
  251. };
  252. void WebViewImpl::setBackgroundTransparent(){
  253. JniHelper::callStaticVoidMethod(className, "setBackgroundTransparent", _viewTag);
  254. };
  255. void WebViewImpl::setBounces(bool bounces) {
  256. // empty function as this was mainly a fix for iOS
  257. }
  258. } // namespace ui
  259. } // namespace experimental
  260. } //namespace cocos2d
  261. #endif // __ANDROID__