Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,11 @@ public static RNCWebViewModule getModule(ReactContext reactContext) {
}

protected void setupWebChromeClient(ReactContext reactContext, WebView webView) {
if (mAllowsFullscreenVideo) {
int initialRequestedOrientation = reactContext.getCurrentActivity().getRequestedOrientation();
Activity activity = reactContext.getCurrentActivity();

if (mAllowsFullscreenVideo && activity != null) {
int initialRequestedOrientation = activity.getRequestedOrientation();

mWebChromeClient = new RNCWebChromeClient(reactContext, webView) {
@Override
public Bitmap getDefaultVideoPoster() {
Expand All @@ -777,31 +780,32 @@ public void onShowCustomView(View view, CustomViewCallback callback) {
mVideoView = view;
mCustomViewCallback = callback;

mReactContext.getCurrentActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
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);

// since RN's Modals interfere with the View hierarchy
// we will decide which View to Hide if the hierarchy
// does not match (i.e., the webview is within a Modal)
// NOTE: We could use mWebView.getRootView() instead of getRootView()
// Since RN's Modals interfere with the View hierarchy
// we will decide which View to hide if the hierarchy
// does not match (i.e., the WebView is within a Modal)
// NOTE: We could use `mWebView.getRootView()` instead of `getRootView()`
// 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();
rootView.addView(mVideoView, FULLSCREEN_LAYOUT_PARAMS);

// Different root views, we are in a Modal
if(rootView.getRootView() != mWebView.getRootView()){
if (rootView.getRootView() != mWebView.getRootView()) {
mWebView.getRootView().setVisibility(View.GONE);
}

// Same view hierarchy (no Modal), just hide the webview then
else{
} else {
// Same view hierarchy (no Modal), just hide the WebView then
mWebView.setVisibility(View.GONE);
}

Expand All @@ -814,20 +818,18 @@ public void onHideCustomView() {
return;
}

// same logic as above
// Same logic as above
ViewGroup rootView = getRootView();

if(rootView.getRootView() != mWebView.getRootView()){
if (rootView.getRootView() != mWebView.getRootView()) {
mWebView.getRootView().setVisibility(View.VISIBLE);
}

// Same view hierarchy (no Modal)
else{
} else {
// Same view hierarchy (no Modal)
mWebView.setVisibility(View.VISIBLE);
}

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);
Expand All @@ -836,22 +838,25 @@ public void onHideCustomView() {
mVideoView = null;
mCustomViewCallback = null;

mReactContext.getCurrentActivity().setRequestedOrientation(initialRequestedOrientation);
activity.setRequestedOrientation(initialRequestedOrientation);

mReactContext.removeLifecycleEventListener(this);
}
};

webView.setWebChromeClient(mWebChromeClient);
} else {
if (mWebChromeClient != null) {
mWebChromeClient.onHideCustomView();
}

mWebChromeClient = new RNCWebChromeClient(reactContext, webView) {
@Override
public Bitmap getDefaultVideoPoster() {
return Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
}
};

webView.setWebChromeClient(mWebChromeClient);
}
}
Expand Down