Skip to content

Commit ca8e5b7

Browse files
committed
Fix navigation bar coloring on Android 11-14
1 parent 35aa009 commit ca8e5b7

File tree

2 files changed

+52
-37
lines changed

2 files changed

+52
-37
lines changed

app/src/main/java/org/thunderdog/challegram/BaseActivity.java

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -863,19 +863,11 @@ public void updateWindowContextTheme() {
863863
}
864864

865865
public void setWindowDecorSystemUiVisibility (int visibility, boolean remember) {
866-
View decorView = getWindow().getDecorView();
867-
int setVisibility = visibility;
868-
boolean isLight = false;
869-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && Config.USE_CUSTOM_NAVIGATION_COLOR && !Theme.isDark() && (visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
870-
setVisibility |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
871-
isLight = true;
872-
}
873-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Theme.needLightStatusBar()) {
874-
setVisibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
875-
}
876-
decorView.setSystemUiVisibility(setVisibility);
877-
if (this.isWindowLight != isLight) {
878-
this.isWindowLight = isLight;
866+
boolean lightNavigationBar = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && Config.USE_CUSTOM_NAVIGATION_COLOR && !Theme.isDark() && (visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0;
867+
boolean lightStatusBar = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Theme.needLightStatusBar();
868+
UI.setLightSystemBars(getWindow(), lightNavigationBar, lightStatusBar, visibility, true);
869+
if (this.isWindowLight != lightNavigationBar) {
870+
this.isWindowLight = lightNavigationBar;
879871
updateNavigationBarColor();
880872
}
881873
lastWindowVisibility = visibility;
@@ -3360,18 +3352,10 @@ private void updateNavigationBarColor () {
33603352
color = ColorUtils.fromToArgb(color, Theme.getColor(ColorId.passcode), passcodeFactor);
33613353
isLight = isLight && passcodeFactor < .5f;
33623354
}
3363-
getWindow().setNavigationBarColor(color);
3355+
UI.setNavigationBarColor(getWindow(), color);
33643356
if (this.isWindowLight != isLight) {
33653357
this.isWindowLight = isLight;
3366-
int visibility = lastWindowVisibility;
3367-
if (isLight) {
3368-
visibility |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
3369-
}
3370-
if (Theme.needLightStatusBar()) {
3371-
visibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
3372-
}
3373-
// TODO: rework to WindowInsetsControlle
3374-
getWindow().getDecorView().setSystemUiVisibility(visibility);
3358+
UI.setLightSystemBars(getWindow(), isLight, Theme.needLightStatusBar(), lastWindowVisibility, true);
33753359
}
33763360
}
33773361
}

app/src/main/java/org/thunderdog/challegram/tool/UI.java

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.content.Intent;
2121
import android.content.res.Configuration;
2222
import android.content.res.Resources;
23+
import android.graphics.Color;
2324
import android.os.Build;
2425
import android.os.CancellationSignal;
2526
import android.os.Handler;
@@ -28,15 +29,19 @@
2829
import android.text.format.DateFormat;
2930
import android.view.View;
3031
import android.view.Window;
32+
import android.view.WindowInsetsController;
3133
import android.view.WindowManager;
3234
import android.view.inputmethod.InputMethodManager;
3335
import android.view.inputmethod.InputMethodSubtype;
3436
import android.widget.Toast;
3537

38+
import androidx.activity.EdgeToEdge;
3639
import androidx.annotation.IntDef;
3740
import androidx.annotation.Nullable;
3841
import androidx.annotation.StringRes;
3942
import androidx.core.content.ContextCompat;
43+
import androidx.core.view.WindowCompat;
44+
import androidx.core.view.WindowInsetsControllerCompat;
4045

4146
import org.drinkless.tdlib.TdApi;
4247
import org.thunderdog.challegram.BaseActivity;
@@ -77,6 +82,8 @@
7782
import me.vkryl.android.ViewUtils;
7883
import me.vkryl.android.util.InvalidateDelegate;
7984
import me.vkryl.android.util.LayoutDelegate;
85+
import me.vkryl.core.BitwiseUtils;
86+
import me.vkryl.core.ColorUtils;
8087
import me.vkryl.core.StringUtils;
8188
import me.vkryl.core.reference.ReferenceList;
8289

@@ -585,6 +592,36 @@ public static ViewController<?> getCurrentStackItem (Context context) {
585592

586593
public static final int NAVIGATION_BAR_COLOR = false && Device.NEED_LIGHT_NAVIGATION_COLOR ? 0xfff0f0f0 : 0xff000000;
587594

595+
public static void setLightSystemBars (Window w, boolean lightNavigationBar, boolean lightStatusBar, int newVisibility, boolean forceNewVisibility) {
596+
if (Settings.instance().useEdgeToEdge() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
597+
android.view.WindowInsetsController insetsController = w.getInsetsController();
598+
if (insetsController != null) {
599+
int flags =
600+
android.view.WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS |
601+
android.view.WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS;
602+
int setFlags =
603+
BitwiseUtils.optional(android.view.WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS, lightNavigationBar) |
604+
BitwiseUtils.optional(android.view.WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS, lightStatusBar);
605+
insetsController.setSystemBarsAppearance(setFlags, flags);
606+
}
607+
}
608+
int visibility = forceNewVisibility ? newVisibility : w.getDecorView().getSystemUiVisibility();
609+
visibility = BitwiseUtils.setFlag(visibility, View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR, lightNavigationBar);
610+
visibility = BitwiseUtils.setFlag(visibility, View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR, lightStatusBar);
611+
w.getDecorView().setSystemUiVisibility(visibility);
612+
}
613+
614+
public static void setNavigationBarColor (Window w, int color) {
615+
if (Config.USE_CUSTOM_NAVIGATION_COLOR) {
616+
if (Settings.instance().useEdgeToEdge()) {
617+
int transparentColor = Color.alpha(color) == 255 ? ColorUtils.alphaColor(.75f , color) : color;
618+
w.setNavigationBarColor(transparentColor);
619+
} else {
620+
w.setNavigationBarColor(color);
621+
}
622+
}
623+
}
624+
588625
public static void clearActivity (BaseActivity a) {
589626
a.requestWindowFeature(Window.FEATURE_NO_TITLE);
590627
Window w = a.getWindow();
@@ -593,27 +630,21 @@ public static void clearActivity (BaseActivity a) {
593630
} else {
594631
w.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
595632
}
633+
if (Settings.instance().useEdgeToEdge()) {
634+
EdgeToEdge.enable(a);
635+
if (Config.EDGE_TO_EDGE_CUSTOMIZABLE) {
636+
w.setNavigationBarContrastEnforced(false);
637+
}
638+
}
596639
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
597640
w.setBackgroundDrawableResource(R.drawable.transparent);
598641
} else {
599-
int visibility = 0;
600642
if (Config.USE_CUSTOM_NAVIGATION_COLOR) {
601-
w.setNavigationBarColor(Theme.backgroundColor());
602-
if (!Theme.isDark()) {
603-
visibility |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
604-
}
643+
setNavigationBarColor(w, Theme.backgroundColor());
605644
} else {
606645
w.setNavigationBarColor(NAVIGATION_BAR_COLOR);
607646
}
608-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
609-
if (Theme.needLightStatusBar()) {
610-
visibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
611-
}
612-
}
613-
if (visibility != 0) {
614-
// TODO: rework to WindowInsetsController
615-
w.getDecorView().setSystemUiVisibility(visibility);
616-
}
647+
setLightSystemBars(w, !Theme.isDark(), Theme.needLightStatusBar(), 0, false);
617648
RootDrawable d = new RootDrawable(a);
618649
w.setBackgroundDrawable(d);
619650
a.setRootDrawable(d);

0 commit comments

Comments
 (0)