14
14
import android .content .Context ;
15
15
import android .os .Build ;
16
16
import android .view .View ;
17
+ import android .view .Window ;
17
18
import android .view .WindowInsets ;
18
19
import android .view .WindowInsetsController ;
19
20
import android .view .WindowManager ;
20
21
import androidx .annotation .Nullable ;
21
22
import androidx .core .view .ViewCompat ;
22
23
import com .facebook .common .logging .FLog ;
23
24
import com .facebook .fbreact .specs .NativeStatusBarManagerAndroidSpec ;
25
+ import com .facebook .infer .annotation .Nullsafe ;
24
26
import com .facebook .react .bridge .GuardedRunnable ;
25
27
import com .facebook .react .bridge .NativeModule ;
26
28
import com .facebook .react .bridge .ReactApplicationContext ;
33
35
34
36
/** {@link NativeModule} that allows changing the appearance of the status bar. */
35
37
@ ReactModule (name = NativeStatusBarManagerAndroidSpec .NAME )
38
+ @ Nullsafe (Nullsafe .Mode .LOCAL )
36
39
public class StatusBarModule extends NativeStatusBarManagerAndroidSpec {
37
40
38
41
private static final String HEIGHT_KEY = "HEIGHT" ;
@@ -44,7 +47,7 @@ public StatusBarModule(ReactApplicationContext reactContext) {
44
47
}
45
48
46
49
@ Override
47
- public @ Nullable Map <String , Object > getTypedExportedConstants () {
50
+ public Map <String , Object > getTypedExportedConstants () {
48
51
final Context context = getReactApplicationContext ();
49
52
final Activity activity = getCurrentActivity ();
50
53
@@ -57,8 +60,11 @@ public StatusBarModule(ReactApplicationContext reactContext) {
57
60
String statusBarColorString = "black" ;
58
61
59
62
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
+ }
62
68
}
63
69
64
70
return MapBuilder .<String , Object >of (
@@ -81,25 +87,30 @@ public void setColor(final double colorDouble, final boolean animated) {
81
87
new GuardedRunnable (getReactApplicationContext ()) {
82
88
@ Override
83
89
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 );
87
95
if (animated ) {
88
- int curColor = activity . getWindow () .getStatusBarColor ();
96
+ int curColor = window .getStatusBarColor ();
89
97
ValueAnimator colorAnimation =
90
98
ValueAnimator .ofObject (new ArgbEvaluator (), curColor , color );
91
99
92
100
colorAnimation .addUpdateListener (
93
101
new ValueAnimator .AnimatorUpdateListener () {
94
102
@ Override
95
103
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
+ }
97
108
}
98
109
});
99
110
colorAnimation .setDuration (300 ).setStartDelay (0 );
100
111
colorAnimation .start ();
101
112
} else {
102
- activity . getWindow () .setStatusBarColor (color );
113
+ window .setStatusBarColor (color );
103
114
}
104
115
}
105
116
});
@@ -121,7 +132,11 @@ public void setTranslucent(final boolean translucent) {
121
132
public void runGuarded () {
122
133
// If the status bar is translucent hook into the window insets calculations
123
134
// 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 ();
125
140
if (translucent ) {
126
141
decorView .setOnApplyWindowInsetsListener (
127
142
new View .OnApplyWindowInsetsListener () {
@@ -157,12 +172,16 @@ public void setHidden(final boolean hidden) {
157
172
new Runnable () {
158
173
@ Override
159
174
public void run () {
175
+ Window window = activity .getWindow ();
176
+ if (window == null ) {
177
+ return ;
178
+ }
160
179
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 );
163
182
} 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 );
166
185
}
167
186
}
168
187
});
@@ -183,8 +202,15 @@ public void setStyle(@Nullable final String style) {
183
202
@ TargetApi (Build .VERSION_CODES .R )
184
203
@ Override
185
204
public void run () {
205
+ Window window = activity .getWindow ();
206
+ if (window == null ) {
207
+ return ;
208
+ }
186
209
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
+ }
188
214
if ("dark-content" .equals (style )) {
189
215
// dark-content means dark icons on a light status bar
190
216
insetsController .setSystemBarsAppearance (
@@ -195,7 +221,7 @@ public void run() {
195
221
0 , WindowInsetsController .APPEARANCE_LIGHT_STATUS_BARS );
196
222
}
197
223
} else {
198
- View decorView = activity . getWindow () .getDecorView ();
224
+ View decorView = window .getDecorView ();
199
225
int systemUiVisibilityFlags = decorView .getSystemUiVisibility ();
200
226
if ("dark-content" .equals (style )) {
201
227
systemUiVisibilityFlags |= View .SYSTEM_UI_FLAG_LIGHT_STATUS_BAR ;
0 commit comments