Skip to content

Commit

Permalink
add null checks for getWindow()
Browse files Browse the repository at this point in the history
  • Loading branch information
obfusk committed Oct 12, 2023
1 parent aef32d2 commit 665c777
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 43 deletions.
22 changes: 13 additions & 9 deletions app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -30,15 +31,18 @@ protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// material 3 designer does not consider status bar colors
// XXX changing this in onCreate causes issues with the splash screen activity, so doing this here
boolean darkMode = Utils.isDarkModeEnabled(this);
if (Build.VERSION.SDK_INT >= 23) {
View decorView = getWindow().getDecorView();
WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(getWindow(), decorView);
wic.setAppearanceLightStatusBars(!darkMode);
getWindow().setStatusBarColor(Color.TRANSPARENT);
} else {
// icons are always white back then
getWindow().setStatusBarColor(darkMode ? Color.TRANSPARENT : Color.argb(127, 0, 0, 0));
Window window = getWindow();
if (window != null) {
boolean darkMode = Utils.isDarkModeEnabled(this);
if (Build.VERSION.SDK_INT >= 23) {
View decorView = window.getDecorView();
WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(window, decorView);
wic.setAppearanceLightStatusBars(!darkMode);
window.setStatusBarColor(Color.TRANSPARENT);
} else {
// icons are always white back then
window.setStatusBarColor(darkMode ? Color.TRANSPARENT : Color.argb(127, 0, 0, 0));
}
}
// XXX android 9 and below has a nasty rendering bug if the theme was patched earlier
Utils.postPatchColors(this);
Expand Down
60 changes: 35 additions & 25 deletions app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.text.style.ForegroundColorSpan;
import android.text.util.Linkify;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -42,17 +41,14 @@
import androidx.annotation.StringRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.FileProvider;
import androidx.core.graphics.BlendModeColorFilterCompat;
import androidx.core.graphics.BlendModeCompat;
import androidx.core.graphics.ColorUtils;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import androidx.core.widget.TextViewCompat;

import com.google.android.material.color.MaterialColors;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
Expand Down Expand Up @@ -638,10 +634,10 @@ public void onResume() {

// Set bottomAppBar and system navigation bar color
binding.bottomAppBar.setBackgroundColor(darkenedColor);
if (Build.VERSION.SDK_INT >= 27) {
WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(getWindow(), binding.getRoot());
if (window != null && Build.VERSION.SDK_INT >= 27) {
WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(window, binding.getRoot());
wic.setAppearanceLightNavigationBars(Utils.needsDarkForeground(darkenedColor));
getWindow().setNavigationBarColor(darkenedColor);
window.setNavigationBarColor(darkenedColor);
}

int complementaryColor = Utils.getComplementaryColor(darkenedColor);
Expand Down Expand Up @@ -1066,10 +1062,14 @@ private void setFullscreen(boolean enabled) {

// Set Android to fullscreen mode
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getWindow().setDecorFitsSystemWindows(false);
if (getWindow().getInsetsController() != null) {
getWindow().getInsetsController().hide(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
getWindow().getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
Window window = getWindow();
if (window != null) {
window.setDecorFitsSystemWindows(false);
WindowInsetsController wic = window.getInsetsController();
if (wic != null) {
wic.hide(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
wic.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
}
}
} else {
setFullscreenModeSdkLessThan30();
Expand All @@ -1096,10 +1096,14 @@ private void setFullscreen(boolean enabled) {

// Unset fullscreen mode
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getWindow().setDecorFitsSystemWindows(true);
if (getWindow().getInsetsController() != null) {
getWindow().getInsetsController().show(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
getWindow().getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_DEFAULT);
Window window = getWindow();
if (window != null) {
window.setDecorFitsSystemWindows(true);
WindowInsetsController wic = window.getInsetsController();
if (wic != null) {
wic.show(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
wic.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_DEFAULT);
}
}
} else {
unsetFullscreenModeSdkLessThan30();
Expand All @@ -1111,19 +1115,25 @@ private void setFullscreen(boolean enabled) {

@SuppressWarnings("deprecation")
private void unsetFullscreenModeSdkLessThan30() {
getWindow().getDecorView().setSystemUiVisibility(
getWindow().getDecorView().getSystemUiVisibility()
& ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
& ~View.SYSTEM_UI_FLAG_FULLSCREEN
);
Window window = getWindow();
if (window != null) {
window.getDecorView().setSystemUiVisibility(
window.getDecorView().getSystemUiVisibility()
& ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
& ~View.SYSTEM_UI_FLAG_FULLSCREEN
);
}
}

@SuppressWarnings("deprecation")
private void setFullscreenModeSdkLessThan30() {
getWindow().getDecorView().setSystemUiVisibility(
getWindow().getDecorView().getSystemUiVisibility()
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN
);
Window window = getWindow();
if (window != null) {
window.getDecorView().setSystemUiVisibility(
window.getDecorView().getSystemUiVisibility()
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN
);
}
}
}
14 changes: 9 additions & 5 deletions app/src/main/java/protect/card_locker/UCropWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

Expand All @@ -27,15 +28,18 @@ public class UCropWrapper extends UCropActivity {
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
boolean darkMode = Utils.isDarkModeEnabled(this);
Window window = getWindow();
// setup status bar to look like the rest of the app
if (Build.VERSION.SDK_INT >= 23) {
View decorView = getWindow().getDecorView();
WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(getWindow(), decorView);
wic.setAppearanceLightStatusBars(!darkMode);
if (window != null) {
View decorView = window.getDecorView();
WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(window, decorView);
wic.setAppearanceLightStatusBars(!darkMode);
}
} else {
// icons are always white back then
if (!darkMode) {
getWindow().setStatusBarColor(ColorUtils.compositeColors(Color.argb(127, 0, 0, 0), getWindow().getStatusBarColor()));
if (window != null && !darkMode) {
window.setStatusBarColor(ColorUtils.compositeColors(Color.argb(127, 0, 0, 0), window.getStatusBarColor()));
}
}

Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/protect/card_locker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
Expand Down Expand Up @@ -672,11 +673,12 @@ public static void postPatchColors(AppCompatActivity activity) {
activity.getTheme().resolveAttribute(android.R.attr.colorBackground, typedValue, true);
activity.findViewById(android.R.id.content).setBackgroundColor(typedValue.data);

if (Build.VERSION.SDK_INT >= 27) {
View decorView = activity.getWindow().getDecorView();
WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(activity.getWindow(), decorView);
Window window = activity.getWindow();
if (window != null && Build.VERSION.SDK_INT >= 27) {
View decorView = window.getDecorView();
WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(window, decorView);
wic.setAppearanceLightNavigationBars(!isDarkModeEnabled(activity));
activity.getWindow().setNavigationBarColor(typedValue.data);
window.setNavigationBarColor(typedValue.data);
}
}

Expand Down

0 comments on commit 665c777

Please sign in to comment.