Skip to content

Commit 7349dab

Browse files
Abbondanzofacebook-github-bot
authored andcommitted
Make StatusBarModule nullsafe (facebook#44618)
Summary: Pull Request resolved: facebook#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
1 parent 9970480 commit 7349dab

File tree

1 file changed

+42
-16
lines changed
  • packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/statusbar

1 file changed

+42
-16
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/statusbar/StatusBarModule.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
import android.content.Context;
1515
import android.os.Build;
1616
import android.view.View;
17+
import android.view.Window;
1718
import android.view.WindowInsets;
1819
import android.view.WindowInsetsController;
1920
import android.view.WindowManager;
2021
import androidx.annotation.Nullable;
2122
import androidx.core.view.ViewCompat;
2223
import com.facebook.common.logging.FLog;
2324
import com.facebook.fbreact.specs.NativeStatusBarManagerAndroidSpec;
25+
import com.facebook.infer.annotation.Nullsafe;
2426
import com.facebook.react.bridge.GuardedRunnable;
2527
import com.facebook.react.bridge.NativeModule;
2628
import com.facebook.react.bridge.ReactApplicationContext;
@@ -33,6 +35,7 @@
3335

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

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

4649
@Override
47-
public @Nullable Map<String, Object> getTypedExportedConstants() {
50+
public Map<String, Object> getTypedExportedConstants() {
4851
final Context context = getReactApplicationContext();
4952
final Activity activity = getCurrentActivity();
5053

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

5962
if (activity != null) {
60-
final int statusBarColor = activity.getWindow().getStatusBarColor();
61-
statusBarColorString = String.format("#%06X", (0xFFFFFF & statusBarColor));
63+
Window window = activity.getWindow();
64+
if (window != null) {
65+
final int statusBarColor = window.getStatusBarColor();
66+
statusBarColorString = String.format("#%06X", (0xFFFFFF & statusBarColor));
67+
}
6268
}
6369

6470
return MapBuilder.<String, Object>of(
@@ -81,25 +87,30 @@ public void setColor(final double colorDouble, final boolean animated) {
8187
new GuardedRunnable(getReactApplicationContext()) {
8288
@Override
8389
public void runGuarded() {
84-
activity
85-
.getWindow()
86-
.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
90+
Window window = activity.getWindow();
91+
if (window == null) {
92+
return;
93+
}
94+
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
8795
if (animated) {
88-
int curColor = activity.getWindow().getStatusBarColor();
96+
int curColor = window.getStatusBarColor();
8997
ValueAnimator colorAnimation =
9098
ValueAnimator.ofObject(new ArgbEvaluator(), curColor, color);
9199

92100
colorAnimation.addUpdateListener(
93101
new ValueAnimator.AnimatorUpdateListener() {
94102
@Override
95103
public void onAnimationUpdate(ValueAnimator animator) {
96-
activity.getWindow().setStatusBarColor((Integer) animator.getAnimatedValue());
104+
Window window = activity.getWindow();
105+
if (window != null) {
106+
window.setStatusBarColor((Integer) animator.getAnimatedValue());
107+
}
97108
}
98109
});
99110
colorAnimation.setDuration(300).setStartDelay(0);
100111
colorAnimation.start();
101112
} else {
102-
activity.getWindow().setStatusBarColor(color);
113+
window.setStatusBarColor(color);
103114
}
104115
}
105116
});
@@ -121,7 +132,11 @@ public void setTranslucent(final boolean translucent) {
121132
public void runGuarded() {
122133
// If the status bar is translucent hook into the window insets calculations
123134
// and consume all the top insets so no padding will be added under the status bar.
124-
View decorView = activity.getWindow().getDecorView();
135+
Window window = activity.getWindow();
136+
if (window == null) {
137+
return;
138+
}
139+
View decorView = window.getDecorView();
125140
if (translucent) {
126141
decorView.setOnApplyWindowInsetsListener(
127142
new View.OnApplyWindowInsetsListener() {
@@ -157,12 +172,16 @@ public void setHidden(final boolean hidden) {
157172
new Runnable() {
158173
@Override
159174
public void run() {
175+
Window window = activity.getWindow();
176+
if (window == null) {
177+
return;
178+
}
160179
if (hidden) {
161-
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
162-
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
180+
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
181+
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
163182
} else {
164-
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
165-
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
183+
window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
184+
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
166185
}
167186
}
168187
});
@@ -183,8 +202,15 @@ public void setStyle(@Nullable final String style) {
183202
@TargetApi(Build.VERSION_CODES.R)
184203
@Override
185204
public void run() {
205+
Window window = activity.getWindow();
206+
if (window == null) {
207+
return;
208+
}
186209
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
187-
WindowInsetsController insetsController = activity.getWindow().getInsetsController();
210+
WindowInsetsController insetsController = window.getInsetsController();
211+
if (insetsController == null) {
212+
return;
213+
}
188214
if ("dark-content".equals(style)) {
189215
// dark-content means dark icons on a light status bar
190216
insetsController.setSystemBarsAppearance(
@@ -195,7 +221,7 @@ public void run() {
195221
0, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS);
196222
}
197223
} else {
198-
View decorView = activity.getWindow().getDecorView();
224+
View decorView = window.getDecorView();
199225
int systemUiVisibilityFlags = decorView.getSystemUiVisibility();
200226
if ("dark-content".equals(style)) {
201227
systemUiVisibilityFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;

0 commit comments

Comments
 (0)