fix(android): add null-pointer check for `activity` before creating `RNCWebChromeClient` (closes #1406, #2190) (#2240)

* fix(android): add null-pointer check for activity before creating web chrome client with full-screen video support

* fix(android): reference error

* fix(android): add null-pointer check for activity before creating web chrome client with full-screen video support

* fix(android): reference error
This commit is contained in:
Jacob Müller 2021-11-26 13:50:04 +01:00 committed by GitHub
parent 3d7b9cac6e
commit 9b14584b78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 22 deletions

View File

@ -759,8 +759,11 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
} }
protected void setupWebChromeClient(ReactContext reactContext, WebView webView) { protected void setupWebChromeClient(ReactContext reactContext, WebView webView) {
if (mAllowsFullscreenVideo) { Activity activity = reactContext.getCurrentActivity();
int initialRequestedOrientation = reactContext.getCurrentActivity().getRequestedOrientation();
if (mAllowsFullscreenVideo && activity != null) {
int initialRequestedOrientation = activity.getRequestedOrientation();
mWebChromeClient = new RNCWebChromeClient(reactContext, webView) { mWebChromeClient = new RNCWebChromeClient(reactContext, webView) {
@Override @Override
public Bitmap getDefaultVideoPoster() { public Bitmap getDefaultVideoPoster() {
@ -777,31 +780,32 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
mVideoView = view; mVideoView = view;
mCustomViewCallback = callback; mCustomViewCallback = callback;
mReactContext.getCurrentActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mVideoView.setSystemUiVisibility(FULLSCREEN_SYSTEM_UI_VISIBILITY); mVideoView.setSystemUiVisibility(FULLSCREEN_SYSTEM_UI_VISIBILITY);
mReactContext.getCurrentActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); activity.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
);
} }
mVideoView.setBackgroundColor(Color.BLACK); mVideoView.setBackgroundColor(Color.BLACK);
// since RN's Modals interfere with the View hierarchy // Since RN's Modals interfere with the View hierarchy
// we will decide which View to Hide if the hierarchy // we will decide which View to hide if the hierarchy
// does not match (i.e., the webview is within a Modal) // does not match (i.e., the WebView is within a Modal)
// NOTE: We could use mWebView.getRootView() instead of getRootView() // NOTE: We could use `mWebView.getRootView()` instead of `getRootView()`
// but that breaks the Modal's styles and layout, so we need this to render // but that breaks the Modal's styles and layout, so we need this to render
// in the main View hierarchy regardless. // in the main View hierarchy regardless
ViewGroup rootView = getRootView(); ViewGroup rootView = getRootView();
rootView.addView(mVideoView, FULLSCREEN_LAYOUT_PARAMS); rootView.addView(mVideoView, FULLSCREEN_LAYOUT_PARAMS);
// Different root views, we are in a Modal // Different root views, we are in a Modal
if(rootView.getRootView() != mWebView.getRootView()){ if (rootView.getRootView() != mWebView.getRootView()) {
mWebView.getRootView().setVisibility(View.GONE); mWebView.getRootView().setVisibility(View.GONE);
} } else {
// Same view hierarchy (no Modal), just hide the WebView then
// Same view hierarchy (no Modal), just hide the webview then
else{
mWebView.setVisibility(View.GONE); mWebView.setVisibility(View.GONE);
} }
@ -814,20 +818,18 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
return; return;
} }
// same logic as above // Same logic as above
ViewGroup rootView = getRootView(); ViewGroup rootView = getRootView();
if(rootView.getRootView() != mWebView.getRootView()){ if (rootView.getRootView() != mWebView.getRootView()) {
mWebView.getRootView().setVisibility(View.VISIBLE); mWebView.getRootView().setVisibility(View.VISIBLE);
} } else {
// Same view hierarchy (no Modal)
// Same view hierarchy (no Modal)
else{
mWebView.setVisibility(View.VISIBLE); mWebView.setVisibility(View.VISIBLE);
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mReactContext.getCurrentActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
} }
rootView.removeView(mVideoView); rootView.removeView(mVideoView);
@ -836,22 +838,25 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
mVideoView = null; mVideoView = null;
mCustomViewCallback = null; mCustomViewCallback = null;
mReactContext.getCurrentActivity().setRequestedOrientation(initialRequestedOrientation); activity.setRequestedOrientation(initialRequestedOrientation);
mReactContext.removeLifecycleEventListener(this); mReactContext.removeLifecycleEventListener(this);
} }
}; };
webView.setWebChromeClient(mWebChromeClient); webView.setWebChromeClient(mWebChromeClient);
} else { } else {
if (mWebChromeClient != null) { if (mWebChromeClient != null) {
mWebChromeClient.onHideCustomView(); mWebChromeClient.onHideCustomView();
} }
mWebChromeClient = new RNCWebChromeClient(reactContext, webView) { mWebChromeClient = new RNCWebChromeClient(reactContext, webView) {
@Override @Override
public Bitmap getDefaultVideoPoster() { public Bitmap getDefaultVideoPoster() {
return Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); return Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
} }
}; };
webView.setWebChromeClient(mWebChromeClient); webView.setWebChromeClient(mWebChromeClient);
} }
} }