private View mCustomView;
private int mOriginalSystemUiVisibility;
private WebChromeClient mWebChromeClient;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
mWebChromeClient = new WebChromeClient() {
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
onHideCustomView();
return;
}
// 1. Stash the current state
mCustomView = view;
mCustomView.setBackgroundColor(Color.WHITE);
mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
// 2. Stash the custom view callback
mCustomViewCallback = callback;
// 3. Add the custom view to the view hierarchy
FrameLayout decor = (FrameLayout)getWindow().getDecorView();
decor.addView(mCustomView, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
// 4. Change the state of the window
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE);
}
@Override
public void onHideCustomView() {
super.onHideCustomView();
// 1. Remove the custom view
FrameLayout decor = (FrameLayout)getWindow().getDecorView();
decor.removeView(mCustomView);
mCustomView = null;
// 2. Restore the state to it's original form
getWindow().getDecorView().setSystemUiVisibility(mOriginalSystemUiVisibility);
// 3. Call the custom view callback
mCustomViewCallback.onCustomViewHidden();
mCustomViewCallback = null;
}
};
mWebView.setWebChromeClient(mWebChromeClient);