Skip to content

Commit

Permalink
Make StatusBarModule nullsafe (#44618)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44618

This change is a prerequisite to converting this file to Kotlin. It adds null checks to potentially nullable window and inset getters that were previously not there.

## Changelog:
[Android] [Changed] - Added null checks, marked null safety in StatusBarModule

Reviewed By: NickGerleman

Differential Revision: D57553395

fbshipit-source-id: 5293bb74a95d22bb82971c0a9d691c9e5e36d81f
  • Loading branch information
Abbondanzo authored and facebook-github-bot committed May 20, 2024
1 parent 9970480 commit 7349dab
Showing 1 changed file with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import com.facebook.common.logging.FLog;
import com.facebook.fbreact.specs.NativeStatusBarManagerAndroidSpec;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.GuardedRunnable;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -33,6 +35,7 @@

/** {@link NativeModule} that allows changing the appearance of the status bar. */
@ReactModule(name = NativeStatusBarManagerAndroidSpec.NAME)
@Nullsafe(Nullsafe.Mode.LOCAL)
public class StatusBarModule extends NativeStatusBarManagerAndroidSpec {

private static final String HEIGHT_KEY = "HEIGHT";
Expand All @@ -44,7 +47,7 @@ public StatusBarModule(ReactApplicationContext reactContext) {
}

@Override
public @Nullable Map<String, Object> getTypedExportedConstants() {
public Map<String, Object> getTypedExportedConstants() {
final Context context = getReactApplicationContext();
final Activity activity = getCurrentActivity();

Expand All @@ -57,8 +60,11 @@ public StatusBarModule(ReactApplicationContext reactContext) {
String statusBarColorString = "black";

if (activity != null) {
final int statusBarColor = activity.getWindow().getStatusBarColor();
statusBarColorString = String.format("#%06X", (0xFFFFFF & statusBarColor));
Window window = activity.getWindow();
if (window != null) {
final int statusBarColor = window.getStatusBarColor();
statusBarColorString = String.format("#%06X", (0xFFFFFF & statusBarColor));
}
}

return MapBuilder.<String, Object>of(
Expand All @@ -81,25 +87,30 @@ public void setColor(final double colorDouble, final boolean animated) {
new GuardedRunnable(getReactApplicationContext()) {
@Override
public void runGuarded() {
activity
.getWindow()
.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
Window window = activity.getWindow();
if (window == null) {
return;
}
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (animated) {
int curColor = activity.getWindow().getStatusBarColor();
int curColor = window.getStatusBarColor();
ValueAnimator colorAnimation =
ValueAnimator.ofObject(new ArgbEvaluator(), curColor, color);

colorAnimation.addUpdateListener(
new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
activity.getWindow().setStatusBarColor((Integer) animator.getAnimatedValue());
Window window = activity.getWindow();
if (window != null) {
window.setStatusBarColor((Integer) animator.getAnimatedValue());
}
}
});
colorAnimation.setDuration(300).setStartDelay(0);
colorAnimation.start();
} else {
activity.getWindow().setStatusBarColor(color);
window.setStatusBarColor(color);
}
}
});
Expand All @@ -121,7 +132,11 @@ public void setTranslucent(final boolean translucent) {
public void runGuarded() {
// If the status bar is translucent hook into the window insets calculations
// and consume all the top insets so no padding will be added under the status bar.
View decorView = activity.getWindow().getDecorView();
Window window = activity.getWindow();
if (window == null) {
return;
}
View decorView = window.getDecorView();
if (translucent) {
decorView.setOnApplyWindowInsetsListener(
new View.OnApplyWindowInsetsListener() {
Expand Down Expand Up @@ -157,12 +172,16 @@ public void setHidden(final boolean hidden) {
new Runnable() {
@Override
public void run() {
Window window = activity.getWindow();
if (window == null) {
return;
}
if (hidden) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
});
Expand All @@ -183,8 +202,15 @@ public void setStyle(@Nullable final String style) {
@TargetApi(Build.VERSION_CODES.R)
@Override
public void run() {
Window window = activity.getWindow();
if (window == null) {
return;
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
WindowInsetsController insetsController = activity.getWindow().getInsetsController();
WindowInsetsController insetsController = window.getInsetsController();
if (insetsController == null) {
return;
}
if ("dark-content".equals(style)) {
// dark-content means dark icons on a light status bar
insetsController.setSystemBarsAppearance(
Expand All @@ -195,7 +221,7 @@ public void run() {
0, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS);
}
} else {
View decorView = activity.getWindow().getDecorView();
View decorView = window.getDecorView();
int systemUiVisibilityFlags = decorView.getSystemUiVisibility();
if ("dark-content".equals(style)) {
systemUiVisibilityFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
Expand Down

0 comments on commit 7349dab

Please sign in to comment.