Untitled

 avatar
unknown
java
a year ago
2.5 kB
2
Indexable
public class CustomWebChromeClient extends WebChromeClient {

    private View mCustomView;
    private CustomViewCallback mCustomViewCallback;
    private Activity mActivity;

    public CustomWebChromeClient(Activity activity) {
        this.mActivity = activity;
    }

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        // if a view already exists then immediately terminate the new one
        if (mCustomView != null) {
            callback.onCustomViewHidden();
            return;
        }

        // get the frame layout that holds the webview
        FrameLayout frameLayout = (FrameLayout) mActivity.findViewById(R.id.frame_container);

        // get the frame layout that will hold the video view
        FrameLayout fullscreenContainer = (FrameLayout) mActivity.findViewById(R.id.fullscreenContainer);

        // set the custom view and its callback
        mCustomView = view;
        mCustomViewCallback = callback;

        // hide the webview and show the video view
        frameLayout.setVisibility(View.GONE);
        fullscreenContainer.setVisibility(View.VISIBLE);
        fullscreenContainer.addView(mCustomView);

        // set the activity to full screen mode
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    public void onHideCustomView() {
        // if a view doesn't exist then do nothing
        if (mCustomView == null) {
            return;
        }

        // get the frame layout that holds the webview
        FrameLayout frameLayout = (FrameLayout) mActivity.findViewById(R.id.frame_container);

        // get the frame layout that holds the video view
        FrameLayout fullscreenContainer = (FrameLayout) mActivity.findViewById(R.id.fullscreenContainer);

        // remove the video view from its container
        fullscreenContainer.removeView(mCustomView);

        // show the webview and hide the video view
        frameLayout.setVisibility(View.VISIBLE);
        fullscreenContainer.setVisibility(View.GONE);

        // call the custom view callback
        mCustomViewCallback.onCustomViewHidden();

        // reset the custom view and its callback
        mCustomView = null;
        mCustomViewCallback = null;

        // restore the activity to normal mode
        mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}