From 4ecc3b006ae2c1192159e6116d2d15110ee0265b Mon Sep 17 00:00:00 2001 From: Maximilian Schneider Date: Thu, 8 Aug 2019 18:33:30 +0200 Subject: [PATCH] Allow video players in Android WebView to maximize from Widget mode --- .../WebviewManager.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java b/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java index 36268d17..127e22df 100644 --- a/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java +++ b/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java @@ -243,6 +243,55 @@ public void onProgressChanged(WebView view, int progress) { args.put("progress", progress / 100.0); FlutterWebviewPlugin.channel.invokeMethod("onProgressChanged", args); } + + // Allow switching to fullscreen e.g when playing a video + + private View customView; + private CustomViewCallback customViewCallback; + + @Override + public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) { + onShowCustomView(view, callback); + } + + @Override + public void onShowCustomView(View view, CustomViewCallback callback) { + // if a view already exists then immediately terminate the new one + if (customView != null) { + callback.onCustomViewHidden(); + return; + } + + // add custom view to container and save reference + customView = view; + customViewCallback = callback; + + FrameLayout rootView = (FrameLayout)webView.getRootView(); + rootView.addView(customView); + + // hide webview and show custom view + customView.setVisibility(View.VISIBLE); + FrameLayout contentView = (FrameLayout)rootView.findViewById(android.R.id.content); + contentView.setVisibility(View.GONE); + } + + @Override + public void onHideCustomView() { + super.onHideCustomView(); + if (customView == null) + return; + + // Hide the custom view and show Webview + FrameLayout rootView = (FrameLayout)webView.getRootView(); + FrameLayout contentView = (FrameLayout)rootView.findViewById(android.R.id.content); + contentView.setVisibility(View.VISIBLE); + customView.setVisibility(View.GONE); + + // Remove the custom view from its container and clear reference + rootView.removeView(customView); + customViewCallback.onCustomViewHidden(); + customView = null; + } }); }