Skip to content
This repository has been archived by the owner on May 18, 2020. It is now read-only.

add code like this maybe better #1

Open
mmorcc opened this issue Dec 19, 2017 · 1 comment
Open

add code like this maybe better #1

mmorcc opened this issue Dec 19, 2017 · 1 comment

Comments

@mmorcc
Copy link

mmorcc commented Dec 19, 2017

package com.akulyk.react.androidwebview;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.FrameLayout;

import static android.view.ViewGroup.LayoutParams;

/**

  • Provides support for full-screen video on Android
    */
    public class VideoEnabledWebChromeClient extends WebChromeClient {

    private final FrameLayout.LayoutParams FULLSCREEN_LAYOUT_PARAMS = new FrameLayout.LayoutParams(
    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.CENTER);

    private WebChromeClient.CustomViewCallback mCustomViewCallback;

    private Activity mActivity;
    private View mWebView;
    private View mVideoView;

    public VideoEnabledWebChromeClient(Activity activity, WebView webView) {
    mWebView = webView;
    mActivity = activity;
    }

    @OverRide
    public void onShowCustomView(View view, CustomViewCallback callback) {
    if (mVideoView != null) {
    callback.onCustomViewHidden();
    return;
    }

     // Store the view and it's callback for later, so we can dispose of them correctly
     mVideoView = view;
     mCustomViewCallback = callback;
    
     view.setBackgroundColor(Color.BLACK);
    
     getRootView().addView(view, FULLSCREEN_LAYOUT_PARAMS);
    
     mWebView.setVisibility(View.GONE);
     if(mActivity!=null){
         mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
         WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
         params.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
         mActivity.  getWindow().setAttributes(params);
         mActivity.  getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
     }
    

    }

    @OverRide
    public void onHideCustomView() {
    if (mVideoView == null) {
    return;
    }

     mVideoView.setVisibility(View.GONE);
    
     // Remove the custom view from its container.
     getRootView().removeView(mVideoView);
     mVideoView = null;
     mCustomViewCallback.onCustomViewHidden();
    
     mWebView.setVisibility(View.VISIBLE);
     if(mActivity!=null){
         mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
         final WindowManager.LayoutParams attrs =mActivity.getWindow().getAttributes();
         attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
         mActivity.getWindow().setAttributes(attrs);
         mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
     }
    

    }

    private ViewGroup getRootView() {
    return ((ViewGroup) mActivity.findViewById(android.R.id.content));
    }
    }

@mmorcc
Copy link
Author

mmorcc commented Dec 19, 2017

add code in mainfest.xml
application
android:hardwareAccelerated="true"
or else it don't work properly

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant