-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7882 from andyque/addWebView
add WebView
- Loading branch information
Showing
24 changed files
with
2,040 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -865,6 +865,8 @@ | |
290E94B6196FC16900694919 /* CocostudioParserTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 290E94B3196FC16900694919 /* CocostudioParserTest.cpp */; }; | ||
295824591987415900F9746D /* UIScale9SpriteTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 295824571987415900F9746D /* UIScale9SpriteTest.cpp */; }; | ||
2958245A1987415900F9746D /* UIScale9SpriteTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 295824571987415900F9746D /* UIScale9SpriteTest.cpp */; }; | ||
298D7F6F19AC31F300FF096D /* UIWebViewTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 298D7F6D19AC31F300FF096D /* UIWebViewTest.cpp */; }; | ||
29AFEF6719ACCAA000F6B10A /* Test.html in Resources */ = {isa = PBXBuildFile; fileRef = 29AFEF6619ACCAA000F6B10A /* Test.html */; }; | ||
29FBBBFE196A9ECD00E65826 /* CocostudioParserJsonTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 29FBBBFC196A9ECD00E65826 /* CocostudioParserJsonTest.cpp */; }; | ||
29FBBBFF196A9ECD00E65826 /* CocostudioParserJsonTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 29FBBBFC196A9ECD00E65826 /* CocostudioParserJsonTest.cpp */; }; | ||
38FA2E73194AEBE100FF2BE4 /* ActionTimelineTestScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 38FA2E71194AEBE100FF2BE4 /* ActionTimelineTestScene.cpp */; }; | ||
|
@@ -2886,6 +2888,9 @@ | |
290E94B4196FC16900694919 /* CocostudioParserTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CocostudioParserTest.h; path = ../CocostudioParserTest.h; sourceTree = "<group>"; }; | ||
295824571987415900F9746D /* UIScale9SpriteTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UIScale9SpriteTest.cpp; sourceTree = "<group>"; }; | ||
295824581987415900F9746D /* UIScale9SpriteTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIScale9SpriteTest.h; sourceTree = "<group>"; }; | ||
298D7F6D19AC31F300FF096D /* UIWebViewTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UIWebViewTest.cpp; path = UIWebViewTest/UIWebViewTest.cpp; sourceTree = "<group>"; }; | ||
298D7F6E19AC31F300FF096D /* UIWebViewTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UIWebViewTest.h; path = UIWebViewTest/UIWebViewTest.h; sourceTree = "<group>"; }; | ||
29AFEF6619ACCAA000F6B10A /* Test.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = Test.html; path = "../tests/cpp-tests/Resources/Test.html"; sourceTree = "<group>"; }; | ||
29FBBBFC196A9ECD00E65826 /* CocostudioParserJsonTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CocostudioParserJsonTest.cpp; sourceTree = "<group>"; }; | ||
29FBBBFD196A9ECD00E65826 /* CocostudioParserJsonTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CocostudioParserJsonTest.h; sourceTree = "<group>"; }; | ||
38FA2E71194AEBE100FF2BE4 /* ActionTimelineTestScene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ActionTimelineTestScene.cpp; sourceTree = "<group>"; }; | ||
|
@@ -6495,6 +6500,7 @@ | |
1AC35CB518CED84500F37B72 /* effect1.raw */, | ||
1AC35CB618CED84500F37B72 /* effect1.wav */, | ||
1AC35CC418CED84500F37B72 /* pew-pew-lei.wav */, | ||
29AFEF6619ACCAA000F6B10A /* Test.html */, | ||
); | ||
name = Resources; | ||
sourceTree = "<group>"; | ||
|
@@ -6907,6 +6913,15 @@ | |
path = UIWidgetAddNodeTest; | ||
sourceTree = "<group>"; | ||
}; | ||
298D7F6C19AC31C000FF096D /* UIWebViewTest */ = { | ||
isa = PBXGroup; | ||
children = ( | ||
298D7F6D19AC31F300FF096D /* UIWebViewTest.cpp */, | ||
298D7F6E19AC31F300FF096D /* UIWebViewTest.h */, | ||
); | ||
name = UIWebViewTest; | ||
sourceTree = "<group>"; | ||
}; | ||
29B97314FDCFA39411CA2CEA /* CustomTemplate */ = { | ||
isa = PBXGroup; | ||
children = ( | ||
|
@@ -6988,6 +7003,7 @@ | |
29FBBC00196A9F0D00E65826 /* UIAndEditorTests */ = { | ||
isa = PBXGroup; | ||
children = ( | ||
298D7F6C19AC31C000FF096D /* UIWebViewTest */, | ||
295824571987415900F9746D /* UIScale9SpriteTest.cpp */, | ||
295824581987415900F9746D /* UIScale9SpriteTest.h */, | ||
29080D1F191B595E0066F8DF /* CocosGUIScene.cpp */, | ||
|
@@ -7816,6 +7832,7 @@ | |
1AC35D0918CED84500F37B72 /* TileMaps in Resources */, | ||
1AC35CFF18CED84500F37B72 /* Particles in Resources */, | ||
1AC35C8818CECF1400F37B72 /* [email protected] in Resources */, | ||
29AFEF6719ACCAA000F6B10A /* Test.html in Resources */, | ||
1AC35CD518CED84500F37B72 /* ccb in Resources */, | ||
1AC35CE118CED84500F37B72 /* configs in Resources */, | ||
1AC35CE918CED84500F37B72 /* extensions in Resources */, | ||
|
@@ -8316,6 +8333,7 @@ | |
1AC35B3E18CECF0C00F37B72 /* Bug-422.cpp in Sources */, | ||
29080DAC191B595E0066F8DF /* UIFocusTest.cpp in Sources */, | ||
1AC35BF618CECF0C00F37B72 /* HttpClientTest.cpp in Sources */, | ||
298D7F6F19AC31F300FF096D /* UIWebViewTest.cpp in Sources */, | ||
29080DA6191B595E0066F8DF /* UIButtonTest_Editor.cpp in Sources */, | ||
1AC35B5018CECF0C00F37B72 /* ClickAndMoveTest.cpp in Sources */, | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
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; | ||
import android.webkit.WebChromeClient; | ||
import android.webkit.WebView; | ||
import android.webkit.WebViewClient; | ||
import android.widget.FrameLayout; | ||
|
||
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 (Exception e) { | ||
Log.d(TAG, "This API level do not support `removeJavascriptInterface`"); | ||
} | ||
|
||
this.setWebViewClient(new Cocos2dxWebViewClient()); | ||
this.setWebChromeClient(new WebChromeClient()); | ||
} | ||
|
||
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); | ||
} | ||
} |
Oops, something went wrong.