Browse Source

Allow video players in Android WebView to maximize from Widget mode

Maximilian Schneider 6 years ago
parent
commit
f993de9833
1 changed files with 49 additions and 0 deletions
  1. 49 0
      android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

+ 49 - 0
android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

@@ -243,6 +243,55 @@ class WebviewManager {
                 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;
+            }
         });
     }