4444import android .webkit .CookieSyncManager ;
4545import android .widget .FrameLayout ;
4646
47+ import android .app .AlertDialog ;
48+ import android .content .DialogInterface ;
49+ import android .os .Message ;
50+ import android .view .WindowManager ;
51+ import android .util .Log ;
52+
4753import java .net .HttpURLConnection ;
4854import java .net .URL ;
4955import java .net .URLEncoder ;
@@ -80,6 +86,8 @@ public void call(final String method, final String message) {
8086public class CWebViewPlugin {
8187 private static FrameLayout layout = null ;
8288 private WebView mWebView ;
89+ private WebView mPopWebView ;
90+ private View mVideoView ;
8391 private CWebViewPluginInterface mWebViewPlugin ;
8492 private int progress ;
8593 private boolean canGoBack ;
@@ -94,6 +102,102 @@ public boolean IsInitialized() {
94102 return mWebView != null ;
95103 }
96104
105+ class UriChromeClient extends WebChromeClient {
106+ // cf. https://github.com/hwasiti/Android_Popup_Webview_handler_example
107+ @ Override
108+ public boolean onCreateWindow (WebView view , boolean isDialog , boolean isUserGesture , Message resultMsg ) {
109+ if (mPopWebView != null ) {
110+ return false ;
111+ }
112+ Activity a = UnityPlayer .currentActivity ;
113+ mPopWebView = new WebView (a );
114+ // mPopWebView.setVerticalScrollBarEnabled(false);
115+ // mPopWebView.setHorizontalScrollBarEnabled(false);
116+ mPopWebView .setWebViewClient (new WebViewClient ());
117+ mPopWebView .setWebChromeClient (new UriChromeClient ());
118+ WebSettings webSettings = mPopWebView .getSettings ();
119+ webSettings .setSupportZoom (true );
120+ webSettings .setBuiltInZoomControls (true );
121+ webSettings .setDisplayZoomControls (false );
122+ webSettings .setLoadWithOverviewMode (true );
123+ webSettings .setUseWideViewPort (true );
124+ webSettings .setJavaScriptEnabled (true );
125+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN ) {
126+ // Log.i("CWebViewPlugin", "Build.VERSION.SDK_INT = " + Build.VERSION.SDK_INT);
127+ webSettings .setAllowUniversalAccessFromFileURLs (true );
128+ }
129+ webSettings .setMediaPlaybackRequiresUserGesture (false );
130+ webSettings .setDatabaseEnabled (true );
131+ webSettings .setDomStorageEnabled (true );
132+ String databasePath = mPopWebView .getContext ().getDir ("databases" , Context .MODE_PRIVATE ).getPath ();
133+ webSettings .setDatabasePath (databasePath );
134+ // webSettings.setAppCacheEnabled(true);
135+ // webSettings.setSavePassword(true);
136+ // webSettings.setSaveFormData(true);
137+
138+ layout .addView (
139+ mPopWebView ,
140+ new FrameLayout .LayoutParams (
141+ LayoutParams .MATCH_PARENT ,
142+ LayoutParams .MATCH_PARENT ,
143+ Gravity .NO_GRAVITY ));
144+ mPopWebView .setLayoutParams (mWebView .getLayoutParams ());
145+ CookieManager cookieManager = CookieManager .getInstance ();
146+ cookieManager .setAcceptCookie (true );
147+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
148+ cookieManager .setAcceptThirdPartyCookies (mPopWebView , true );
149+ }
150+ WebView .WebViewTransport transport = (WebView .WebViewTransport )resultMsg .obj ;
151+ transport .setWebView (mPopWebView );
152+ resultMsg .sendToTarget ();
153+ return true ;
154+ }
155+
156+ @ Override
157+ public void onCloseWindow (WebView window ) {
158+ try {
159+ layout .removeView (mPopWebView );
160+ } catch (Exception e ) {
161+ }
162+ try {
163+ mPopWebView .destroy ();
164+ } catch (Exception e ) {
165+ }
166+ mPopWebView = null ;
167+ }
168+
169+ @ Override
170+ public void onProgressChanged (WebView view , int newProgress ) {
171+ progress = newProgress ;
172+ }
173+
174+ @ Override
175+ public void onShowCustomView (View view , CustomViewCallback callback ) {
176+ super .onShowCustomView (view , callback );
177+ if (layout != null ) {
178+ mVideoView = view ;
179+ layout .setBackgroundColor (0xff000000 );
180+ layout .addView (mVideoView );
181+ }
182+ }
183+
184+ @ Override
185+ public void onHideCustomView () {
186+ super .onHideCustomView ();
187+ if (layout != null ) {
188+ layout .removeView (mVideoView );
189+ layout .setBackgroundColor (0x00000000 );
190+ mVideoView = null ;
191+ }
192+ }
193+
194+ // @Override
195+ // public boolean onConsoleMessage(android.webkit.ConsoleMessage cm) {
196+ // Log.d("Webview", cm.message());
197+ // return true;
198+ // }
199+ }
200+
97201 public void Init (final String gameObject , final boolean transparent , final String ua ) {
98202 final CWebViewPlugin self = this ;
99203 final Activity a = UnityPlayer .currentActivity ;
@@ -108,40 +212,7 @@ public void Init(final String gameObject, final boolean transparent, final Strin
108212 webView .setFocusable (true );
109213 webView .setFocusableInTouchMode (true );
110214
111- // webView.setWebChromeClient(new WebChromeClient() {
112- // public boolean onConsoleMessage(android.webkit.ConsoleMessage cm) {
113- // Log.d("Webview", cm.message());
114- // return true;
115- // }
116- // });
117- webView .setWebChromeClient (new WebChromeClient () {
118- View videoView ;
119-
120- @ Override
121- public void onProgressChanged (WebView view , int newProgress ) {
122- progress = newProgress ;
123- }
124-
125- @ Override
126- public void onShowCustomView (View view , CustomViewCallback callback ) {
127- super .onShowCustomView (view , callback );
128- if (layout != null ) {
129- videoView = view ;
130- layout .setBackgroundColor (0xff000000 );
131- layout .addView (videoView );
132- }
133- }
134-
135- @ Override
136- public void onHideCustomView () {
137- super .onHideCustomView ();
138- if (layout != null ) {
139- layout .removeView (videoView );
140- layout .setBackgroundColor (0x00000000 );
141- videoView = null ;
142- }
143- }
144- });
215+ webView .setWebChromeClient (new UriChromeClient ());
145216
146217 mWebViewPlugin = new CWebViewPluginInterface (self , gameObject );
147218 webView .setWebViewClient (new WebViewClient () {
@@ -155,7 +226,7 @@ public void onReceivedError(WebView view, int errorCode, String description, Str
155226
156227 @ Override
157228 public void onReceivedHttpError (WebView view , WebResourceRequest request , WebResourceResponse errorResponse ) {
158- canGoBack = webView .canGoBack ();
229+ canGoBack = webView .canGoBack ();
159230 canGoForward = webView .canGoForward ();
160231 mWebViewPlugin .call ("CallOnHttpError" , Integer .toString (errorResponse .getStatusCode ()));
161232 }
@@ -253,6 +324,11 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
253324 webSettings .setDomStorageEnabled (true );
254325 String databasePath = webView .getContext ().getDir ("databases" , Context .MODE_PRIVATE ).getPath ();
255326 webSettings .setDatabasePath (databasePath );
327+ // webSettings.setAppCacheEnabled(true);
328+ // webSettings.setSavePassword(true);
329+ // webSettings.setSaveFormData(true);
330+ webSettings .setJavaScriptCanOpenWindowsAutomatically (true );
331+ webSettings .setSupportMultipleWindows (true );
256332
257333 if (transparent ) {
258334 webView .setBackgroundColor (0x00000000 );
0 commit comments