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 crash when current activity is null #6022

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.swmansion.reanimated.keyboard;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
Expand All @@ -18,6 +19,8 @@ public class WindowsInsetsManager {
private final Keyboard mKeyboard;
private final NotifyAboutKeyboardChangeFunction mNotifyAboutKeyboardChange;

private final String MissingContextErrorMsg = "Unable to get reference to react activity";

public WindowsInsetsManager(
WeakReference<ReactApplicationContext> reactContext,
Keyboard keyboard,
Expand All @@ -27,33 +30,55 @@ public WindowsInsetsManager(
mNotifyAboutKeyboardChange = notifyAboutKeyboardChange;
}

private Window getWindow() {
return mReactContext.get().getCurrentActivity().getWindow();
}

private View getRootView() {
return getWindow().getDecorView();
private Activity getCurrentActivity() {
return mReactContext.get().getCurrentActivity();
}

public void startObservingChanges(
KeyboardAnimationCallback keyboardAnimationCallback, boolean isStatusBarTranslucent) {
mIsStatusBarTranslucent = isStatusBarTranslucent;
updateWindowDecor(false);
ViewCompat.setOnApplyWindowInsetsListener(getRootView(), this::onApplyWindowInsetsListener);
ViewCompat.setWindowInsetsAnimationCallback(getRootView(), keyboardAnimationCallback);

Activity currentActivity = getCurrentActivity();
if (currentActivity == null) {
Log.e("Reanimated", MissingContextErrorMsg);
return;
}

ViewCompat.setOnApplyWindowInsetsListener(
currentActivity.getWindow().getDecorView(), this::onApplyWindowInsetsListener);
ViewCompat.setWindowInsetsAnimationCallback(
currentActivity.getWindow().getDecorView(), keyboardAnimationCallback);
maciekstosio marked this conversation as resolved.
Show resolved Hide resolved
}

public void stopObservingChanges() {
updateWindowDecor(!mIsStatusBarTranslucent);
updateInsets(0, 0);
View rootView = getRootView();

Activity currentActivity = getCurrentActivity();
if (currentActivity == null) {
Log.e("Reanimated", MissingContextErrorMsg);
return;
}

View rootView = currentActivity.getWindow().getDecorView();
ViewCompat.setWindowInsetsAnimationCallback(rootView, null);
ViewCompat.setOnApplyWindowInsetsListener(rootView, null);
}

private void updateWindowDecor(boolean decorFitsSystemWindow) {
new Handler(Looper.getMainLooper())
.post(() -> WindowCompat.setDecorFitsSystemWindows(getWindow(), decorFitsSystemWindow));
.post(
() -> {
Activity currentActivity = getCurrentActivity();
if (currentActivity == null) {
Log.e("Reanimated", MissingContextErrorMsg);
return;
}

WindowCompat.setDecorFitsSystemWindows(
currentActivity.getWindow(), decorFitsSystemWindow);
});
}

private WindowInsetsCompat onApplyWindowInsetsListener(View view, WindowInsetsCompat insets) {
Expand All @@ -79,7 +104,15 @@ private void updateInsets(int paddingTop, int paddingBottom) {
() -> {
FrameLayout.LayoutParams params = getLayoutParams(paddingTop, paddingBottom);
int actionBarId = androidx.appcompat.R.id.action_bar_root;
View actionBarRootView = getRootView().findViewById(actionBarId);

Activity currentActivity = getCurrentActivity();
if (currentActivity == null) {
Log.e("Reanimated", MissingContextErrorMsg);
return;
}

View actionBarRootView =
currentActivity.getWindow().getDecorView().findViewById(actionBarId);
actionBarRootView.setLayoutParams(params);
});
}
Expand Down
4 changes: 3 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
"workletize",
"workletized",
"workletizes",
"worklets"
"worklets",
"androidx",
"appcompat"
maciekstosio marked this conversation as resolved.
Show resolved Hide resolved
],
"flagWords": [
"ere",
Expand Down
Loading