Skip to content

Commit a2a9af0

Browse files
authored
Added null check to fix NullPointerException being thrown (#931)
* Added null check to fix `NullPointerException` being thrown * getRootWindowInsets() from `Activity` `DecorView` is null some cases (most likely when opening the app from cold start) * null check will now return early with the decorView height instead of the sum of the height, top inset, and bottom inset * Assigning `WindowInsets` to variable first and then assessing
1 parent 645a699 commit a2a9af0

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSViewUtils.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.util.DisplayMetrics;
1212
import android.view.View;
1313
import android.view.Window;
14+
import android.view.WindowInsets;
1415

1516
import java.lang.ref.WeakReference;
1617

@@ -91,11 +92,13 @@ else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
9192
private static int getWindowHeightAPI23Plus(@NonNull Activity activity) {
9293
View decorView = activity.getWindow().getDecorView();
9394
// Use use stable heights as SystemWindowInset subtracts the keyboard
94-
int heightWithoutKeyboard =
95-
decorView.getHeight() -
96-
decorView.getRootWindowInsets().getStableInsetBottom() -
97-
decorView.getRootWindowInsets().getStableInsetTop();
98-
return heightWithoutKeyboard;
95+
WindowInsets windowInsets = decorView.getRootWindowInsets();
96+
if (windowInsets == null)
97+
return decorView.getHeight();
98+
99+
return decorView.getHeight() -
100+
windowInsets.getStableInsetBottom() -
101+
windowInsets.getStableInsetTop();
99102
}
100103

101104
private static int getWindowHeightLollipop(@NonNull Activity activity) {

0 commit comments

Comments
 (0)