diff --git a/plugins/Android/src/net/gree/unitywebview/CWebViewPlugin.java b/plugins/Android/src/net/gree/unitywebview/CWebViewPlugin.java index 523305e7..b7129cf9 100644 --- a/plugins/Android/src/net/gree/unitywebview/CWebViewPlugin.java +++ b/plugins/Android/src/net/gree/unitywebview/CWebViewPlugin.java @@ -44,6 +44,12 @@ import android.webkit.CookieSyncManager; import android.widget.FrameLayout; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.os.Message; +import android.view.WindowManager; +import android.util.Log; + import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; @@ -80,6 +86,8 @@ public void call(final String method, final String message) { public class CWebViewPlugin { private static FrameLayout layout = null; private WebView mWebView; + private WebView mPopWebView; + private View mVideoView; private CWebViewPluginInterface mWebViewPlugin; private int progress; private boolean canGoBack; @@ -94,6 +102,102 @@ public boolean IsInitialized() { return mWebView != null; } + class UriChromeClient extends WebChromeClient { + // cf. https://github.com/hwasiti/Android_Popup_Webview_handler_example + @Override + public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) { + if (mPopWebView != null) { + return false; + } + Activity a = UnityPlayer.currentActivity; + mPopWebView = new WebView(a); + // mPopWebView.setVerticalScrollBarEnabled(false); + // mPopWebView.setHorizontalScrollBarEnabled(false); + mPopWebView.setWebViewClient(new WebViewClient()); + mPopWebView.setWebChromeClient(new UriChromeClient()); + WebSettings webSettings = mPopWebView.getSettings(); + webSettings.setSupportZoom(true); + webSettings.setBuiltInZoomControls(true); + webSettings.setDisplayZoomControls(false); + webSettings.setLoadWithOverviewMode(true); + webSettings.setUseWideViewPort(true); + webSettings.setJavaScriptEnabled(true); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + // Log.i("CWebViewPlugin", "Build.VERSION.SDK_INT = " + Build.VERSION.SDK_INT); + webSettings.setAllowUniversalAccessFromFileURLs(true); + } + webSettings.setMediaPlaybackRequiresUserGesture(false); + webSettings.setDatabaseEnabled(true); + webSettings.setDomStorageEnabled(true); + String databasePath = mPopWebView.getContext().getDir("databases", Context.MODE_PRIVATE).getPath(); + webSettings.setDatabasePath(databasePath); + // webSettings.setAppCacheEnabled(true); + // webSettings.setSavePassword(true); + // webSettings.setSaveFormData(true); + + layout.addView( + mPopWebView, + new FrameLayout.LayoutParams( + LayoutParams.MATCH_PARENT, + LayoutParams.MATCH_PARENT, + Gravity.NO_GRAVITY)); + mPopWebView.setLayoutParams(mWebView.getLayoutParams()); + CookieManager cookieManager = CookieManager.getInstance(); + cookieManager.setAcceptCookie(true); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + cookieManager.setAcceptThirdPartyCookies(mPopWebView, true); + } + WebView.WebViewTransport transport = (WebView.WebViewTransport)resultMsg.obj; + transport.setWebView(mPopWebView); + resultMsg.sendToTarget(); + return true; + } + + @Override + public void onCloseWindow(WebView window) { + try { + layout.removeView(mPopWebView); + } catch (Exception e) { + } + try { + mPopWebView.destroy(); + } catch (Exception e) { + } + mPopWebView = null; + } + + @Override + public void onProgressChanged(WebView view, int newProgress) { + progress = newProgress; + } + + @Override + public void onShowCustomView(View view, CustomViewCallback callback) { + super.onShowCustomView(view, callback); + if (layout != null) { + mVideoView = view; + layout.setBackgroundColor(0xff000000); + layout.addView(mVideoView); + } + } + + @Override + public void onHideCustomView() { + super.onHideCustomView(); + if (layout != null) { + layout.removeView(mVideoView); + layout.setBackgroundColor(0x00000000); + mVideoView = null; + } + } + + // @Override + // public boolean onConsoleMessage(android.webkit.ConsoleMessage cm) { + // Log.d("Webview", cm.message()); + // return true; + // } + } + public void Init(final String gameObject, final boolean transparent, final String ua) { final CWebViewPlugin self = this; final Activity a = UnityPlayer.currentActivity; @@ -108,40 +212,7 @@ public void Init(final String gameObject, final boolean transparent, final Strin webView.setFocusable(true); webView.setFocusableInTouchMode(true); - // webView.setWebChromeClient(new WebChromeClient() { - // public boolean onConsoleMessage(android.webkit.ConsoleMessage cm) { - // Log.d("Webview", cm.message()); - // return true; - // } - // }); - webView.setWebChromeClient(new WebChromeClient() { - View videoView; - - @Override - public void onProgressChanged(WebView view, int newProgress) { - progress = newProgress; - } - - @Override - public void onShowCustomView(View view, CustomViewCallback callback) { - super.onShowCustomView(view, callback); - if (layout != null) { - videoView = view; - layout.setBackgroundColor(0xff000000); - layout.addView(videoView); - } - } - - @Override - public void onHideCustomView() { - super.onHideCustomView(); - if (layout != null) { - layout.removeView(videoView); - layout.setBackgroundColor(0x00000000); - videoView = null; - } - } - }); + webView.setWebChromeClient(new UriChromeClient()); mWebViewPlugin = new CWebViewPluginInterface(self, gameObject); webView.setWebViewClient(new WebViewClient() { @@ -155,7 +226,7 @@ public void onReceivedError(WebView view, int errorCode, String description, Str @Override public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) { - canGoBack = webView.canGoBack(); + canGoBack = webView.canGoBack(); canGoForward = webView.canGoForward(); mWebViewPlugin.call("CallOnHttpError", Integer.toString(errorResponse.getStatusCode())); } @@ -253,6 +324,11 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) { webSettings.setDomStorageEnabled(true); String databasePath = webView.getContext().getDir("databases", Context.MODE_PRIVATE).getPath(); webSettings.setDatabasePath(databasePath); + // webSettings.setAppCacheEnabled(true); + // webSettings.setSavePassword(true); + // webSettings.setSaveFormData(true); + webSettings.setJavaScriptCanOpenWindowsAutomatically(true); + webSettings.setSupportMultipleWindows(true); if (transparent) { webView.setBackgroundColor(0x00000000); diff --git a/plugins/iOS/WebView.mm b/plugins/iOS/WebView.mm index 20023b79..57cbdd0f 100644 --- a/plugins/iOS/WebView.mm +++ b/plugins/iOS/WebView.mm @@ -120,6 +120,7 @@ - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void @interface CWebViewPlugin : NSObject { UIView *webView; + WKWebView *popWebView; NSString *gameObjectName; NSMutableDictionary *customRequestHeader; } @@ -297,6 +298,33 @@ - (BOOL)webView:(UIWebView *)uiWebView shouldStartLoadWithRequest:(NSURLRequest } } +- (WKWebView *)webView:(WKWebView *)wkWebView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures { + if (popWebView) { + return nil; + } + popWebView = [[WKWebView alloc] initWithFrame:webView.frame configuration:configuration]; + popWebView.frame + = CGRectMake( + webView.frame.origin.x, + webView.frame.origin.y, + webView.frame.size.width, + webView.frame.size.height); + popWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + popWebView.UIDelegate = self; + popWebView.navigationDelegate = self; + // popWebView.load(navigationAction.request); + UIView *view = UnityGetGLViewController().view; + [view addSubview:popWebView]; + return popWebView; +} + +- (void)webViewDidClose:(WKWebView *)webView { + if (webView == popWebView) { + [popWebView removeFromSuperview]; + popWebView = nil; + } +} + - (void)webView:(WKWebView *)wkWebView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { if (webView == nil) { @@ -329,8 +357,9 @@ - (void)webView:(WKWebView *)wkWebView decidePolicyForNavigationAction:(WKNaviga } else if (navigationAction.navigationType == WKNavigationTypeLinkActivated && (!navigationAction.targetFrame || !navigationAction.targetFrame.isMainFrame)) { // cf. for target="_blank", cf. http://qiita.com/ShingoFukuyama/items/b3a1441025a36ab7659c - [webView load:navigationAction.request]; - decisionHandler(WKNavigationActionPolicyCancel); + // [webView load:navigationAction.request]; + // decisionHandler(WKNavigationActionPolicyCancel); + decisionHandler(WKNavigationActionPolicyAllow); return; } else { if (navigationAction.targetFrame != nil && navigationAction.targetFrame.isMainFrame) {