Skip to content

Commit c924c35

Browse files
committed
Add comments, deprecate Utils methods and minor fixes
1 parent 7b902d4 commit c924c35

File tree

2 files changed

+64
-21
lines changed

2 files changed

+64
-21
lines changed

androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/Utils.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,62 @@
3333
*/
3434
public class Utils {
3535

36+
/**
37+
* Sets status bar color. Makes the icons dark if necessary.
38+
* Deprecated - use EdgeToEdgeController instead.
39+
*/
40+
@Deprecated
41+
public static void setStatusBarColor(Activity activity, @ColorInt int color) {
42+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
43+
activity.getWindow().setStatusBarColor(color);
44+
45+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
46+
&& shouldUseDarkIconsOnBackground(color)) {
47+
addSystemUiVisibilityFlag(activity, View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
48+
}
49+
}
50+
51+
/**
52+
* Sets navigation bar color. Makes the icons dark if necessary.
53+
* Deprecated - use EdgeToEdgeController instead.
54+
*/
55+
@Deprecated
56+
public static void setNavigationBarColor(Activity activity, @ColorInt int color) {
57+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
58+
59+
activity.getWindow().setNavigationBarColor(color);
60+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
61+
&& shouldUseDarkIconsOnBackground(color)) {
62+
addSystemUiVisibilityFlag(activity, View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
63+
}
64+
}
65+
66+
private static void addSystemUiVisibilityFlag(Activity activity, int flag) {
67+
View root = activity.getWindow().getDecorView().getRootView();
68+
int visibility = root.getSystemUiVisibility();
69+
visibility |= flag;
70+
root.setSystemUiVisibility(visibility);
71+
}
72+
73+
/**
74+
* Determines whether to use dark icons on a background with given color by comparing the
75+
* contrast ratio (https://www.w3.org/TR/WCAG20/#contrast-ratiodef) to a threshold.
76+
* This criterion matches the one used by Chrome:
77+
* https://chromium.googlesource.com/chromium/src/+/90ac05ba6cb9ab5d5df75f0cef62c950be3716c3/chrome/android/java/src/org/chromium/chrome/browser/util/ColorUtils.java#215
78+
*/
79+
private static boolean shouldUseDarkIconsOnBackground(@ColorInt int backgroundColor) {
80+
float luminance = 0.2126f * luminanceOfColorComponent(Color.red(backgroundColor))
81+
+ 0.7152f * luminanceOfColorComponent(Color.green(backgroundColor))
82+
+ 0.0722f * luminanceOfColorComponent(Color.blue(backgroundColor));
83+
float contrast = Math.abs((1.05f) / (luminance + 0.05f));
84+
return contrast < 3;
85+
}
86+
87+
private static float luminanceOfColorComponent(float c) {
88+
c /= 255f;
89+
return (c < 0.03928f) ? c / 12.92f : (float) Math.pow((c + 0.055f) / 1.055f, 2.4f);
90+
}
91+
3692
/**
3793
* Converts drawable located at given resource id into a Bitmap.
3894
*/

androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/splashscreens/EdgeToEdgeController.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,56 +35,43 @@ public FrameLayout getWrapperView(@ColorInt int defaultColor) {
3535
Insets systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars());
3636
mSystemBarBackgroundDrawable.setSystemBarPaddings(systemBarInsets.top, systemBarInsets.bottom);
3737
v.setPadding(0, systemBarInsets.top, 0, systemBarInsets.bottom);
38-
v.invalidate();
39-
return insets;
38+
return WindowInsetsCompat.CONSUMED;
4039
});
4140

41+
// This is required to make system bars transparent
4242
ViewCompat.setOnApplyWindowInsetsListener(mActivity.getWindow().getDecorView(), (v, insets) -> insets);
4343

4444
return rootView;
4545
}
4646

4747
public void setStatusBarColor(@ColorInt int color) {
4848
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
49+
// Call the old API for compatibility with older SDKs, in case edge to edge cannot be enabled
4950
mActivity.getWindow().setStatusBarColor(color);
5051
}
5152
mSystemBarBackgroundDrawable.setStatusBarColor(color);
52-
if (shouldUseDarkIconsOnBackground(color)) {
53-
addSystemUiVisibilityFlag(mActivity, View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
5453
WindowInsetsControllerCompat windowInsetsController =
5554
WindowCompat.getInsetsController(mActivity.getWindow(), mActivity.getWindow().getDecorView());
5655
if (windowInsetsController != null) {
57-
windowInsetsController.setAppearanceLightStatusBars(true);
58-
}
56+
boolean shouldUseDarkIcons = shouldUseDarkIconsOnBackground(color);
57+
windowInsetsController.setAppearanceLightStatusBars(shouldUseDarkIcons);
5958
}
6059
}
6160

6261
public void setNavigationBarColor(@ColorInt int color) {
6362
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
63+
// Call the old API for compatibility with older SDKs, in case edge to edge cannot be enabled
6464
mActivity.getWindow().setNavigationBarColor(color);
6565
}
6666
mSystemBarBackgroundDrawable.setNavigationBarColor(color);
67-
if (shouldUseDarkIconsOnBackground(color)) {
68-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
69-
addSystemUiVisibilityFlag(mActivity, View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
70-
}
7167
WindowInsetsControllerCompat windowInsetsController =
7268
WindowCompat.getInsetsController(mActivity.getWindow(), mActivity.getWindow().getDecorView());
7369
if (windowInsetsController != null) {
74-
windowInsetsController.setAppearanceLightNavigationBars(true);
75-
}
70+
boolean shouldUseDarkIcons = shouldUseDarkIconsOnBackground(color);
71+
windowInsetsController.setAppearanceLightNavigationBars(shouldUseDarkIcons);
7672
}
7773
}
7874

79-
private static void addSystemUiVisibilityFlag(Activity activity, int flag) {
80-
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.UPSIDE_DOWN_CAKE) return;
81-
82-
View root = activity.getWindow().getDecorView().getRootView();
83-
int visibility = root.getSystemUiVisibility();
84-
visibility |= flag;
85-
root.setSystemUiVisibility(visibility);
86-
}
87-
8875
/**
8976
* Determines whether to use dark icons on a background with given color by comparing the
9077
* contrast ratio (https://www.w3.org/TR/WCAG20/#contrast-ratiodef) to a threshold.

0 commit comments

Comments
 (0)