-
Notifications
You must be signed in to change notification settings - Fork 337
Add edge-to-edge support for the splash screen #558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7b902d4
Add edge-to-edge support for the splash screen
orbital17 c924c35
Add comments, deprecate Utils methods and minor fixes
orbital17 d9a2833
Fix whitespaces
orbital17 e704a66
Use ProtectionLayout for drawing system bars background
orbital17 e2051d1
Remove black system bars drawn by DecorView
orbital17 07ece09
Add ImageView as a child to ProtectionLayout
orbital17 b0761e0
Change EdgeToEdgeController to init everything in constructor
orbital17 79c434d
Rename setOriginalView to addView
orbital17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...main/java/com/google/androidbrowserhelper/trusted/splashscreens/EdgeToEdgeController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package com.google.androidbrowserhelper.trusted.splashscreens; | ||
|
|
||
| import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; | ||
|
|
||
| import android.app.Activity; | ||
| import android.graphics.Color; | ||
| import android.os.Build; | ||
| import android.view.View; | ||
| import android.view.ViewGroup; | ||
| import android.widget.FrameLayout; | ||
|
|
||
| import androidx.annotation.ColorInt; | ||
| import androidx.core.graphics.Insets; | ||
| import androidx.core.view.ViewCompat; | ||
| import androidx.core.view.WindowCompat; | ||
| import androidx.core.view.WindowInsetsCompat; | ||
| import androidx.core.view.WindowInsetsControllerCompat; | ||
|
|
||
| public class EdgeToEdgeController { | ||
gstepniewski-google marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private Activity mActivity; | ||
|
|
||
| private SystemBarBackgroundDrawable mSystemBarBackgroundDrawable; | ||
|
|
||
| public EdgeToEdgeController(Activity activity) { | ||
| mActivity = activity; | ||
| } | ||
|
|
||
| public FrameLayout getWrapperView(@ColorInt int defaultColor) { | ||
| FrameLayout rootView = new FrameLayout(mActivity); | ||
ukaratkevich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| rootView.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); | ||
| mSystemBarBackgroundDrawable = new SystemBarBackgroundDrawable(defaultColor); | ||
| rootView.setBackground(mSystemBarBackgroundDrawable); | ||
|
|
||
| ViewCompat.setOnApplyWindowInsetsListener(rootView, (v, insets) -> { | ||
| Insets systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars()); | ||
| mSystemBarBackgroundDrawable.setSystemBarPaddings(systemBarInsets.top, systemBarInsets.bottom); | ||
| v.setPadding(0, systemBarInsets.top, 0, systemBarInsets.bottom); | ||
| v.invalidate(); | ||
ukaratkevich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return insets; | ||
ukaratkevich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| ViewCompat.setOnApplyWindowInsetsListener(mActivity.getWindow().getDecorView(), (v, insets) -> insets); | ||
ukaratkevich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return rootView; | ||
| } | ||
|
|
||
| public void setStatusBarColor(@ColorInt int color) { | ||
| if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { | ||
| mActivity.getWindow().setStatusBarColor(color); | ||
| } | ||
| mSystemBarBackgroundDrawable.setStatusBarColor(color); | ||
| if (shouldUseDarkIconsOnBackground(color)) { | ||
| addSystemUiVisibilityFlag(mActivity, View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); | ||
ukaratkevich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| WindowInsetsControllerCompat windowInsetsController = | ||
| WindowCompat.getInsetsController(mActivity.getWindow(), mActivity.getWindow().getDecorView()); | ||
| if (windowInsetsController != null) { | ||
| windowInsetsController.setAppearanceLightStatusBars(true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void setNavigationBarColor(@ColorInt int color) { | ||
| if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { | ||
| mActivity.getWindow().setNavigationBarColor(color); | ||
ukaratkevich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| mSystemBarBackgroundDrawable.setNavigationBarColor(color); | ||
| if (shouldUseDarkIconsOnBackground(color)) { | ||
ukaratkevich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| addSystemUiVisibilityFlag(mActivity, View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); | ||
| } | ||
| WindowInsetsControllerCompat windowInsetsController = | ||
| WindowCompat.getInsetsController(mActivity.getWindow(), mActivity.getWindow().getDecorView()); | ||
| if (windowInsetsController != null) { | ||
| windowInsetsController.setAppearanceLightNavigationBars(true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void addSystemUiVisibilityFlag(Activity activity, int flag) { | ||
| if (Build.VERSION.SDK_INT > Build.VERSION_CODES.UPSIDE_DOWN_CAKE) return; | ||
|
|
||
| View root = activity.getWindow().getDecorView().getRootView(); | ||
| int visibility = root.getSystemUiVisibility(); | ||
| visibility |= flag; | ||
| root.setSystemUiVisibility(visibility); | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether to use dark icons on a background with given color by comparing the | ||
| * contrast ratio (https://www.w3.org/TR/WCAG20/#contrast-ratiodef) to a threshold. | ||
| * This criterion matches the one used by Chrome: | ||
| * https://chromium.googlesource.com/chromium/src/+/90ac05ba6cb9ab5d5df75f0cef62c950be3716c3/chrome/android/java/src/org/chromium/chrome/browser/util/ColorUtils.java#215 | ||
| */ | ||
| private static boolean shouldUseDarkIconsOnBackground(@ColorInt int backgroundColor) { | ||
| float luminance = 0.2126f * luminanceOfColorComponent(Color.red(backgroundColor)) | ||
| + 0.7152f * luminanceOfColorComponent(Color.green(backgroundColor)) | ||
| + 0.0722f * luminanceOfColorComponent(Color.blue(backgroundColor)); | ||
| float contrast = Math.abs((1.05f) / (luminance + 0.05f)); | ||
| return contrast < 3; | ||
| } | ||
|
|
||
| private static float luminanceOfColorComponent(float c) { | ||
| c /= 255f; | ||
| return (c < 0.03928f) ? c / 12.92f : (float) Math.pow((c + 0.055f) / 1.055f, 2.4f); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...va/com/google/androidbrowserhelper/trusted/splashscreens/SystemBarBackgroundDrawable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
|
|
||
| package com.google.androidbrowserhelper.trusted.splashscreens; | ||
|
|
||
|
|
||
| import android.graphics.Canvas; | ||
| import android.graphics.ColorFilter; | ||
| import android.graphics.Paint; | ||
| import android.graphics.PixelFormat; | ||
| import android.graphics.Rect; | ||
| import android.graphics.drawable.Drawable; | ||
|
|
||
| import androidx.annotation.ColorInt; | ||
|
|
||
| class SystemBarBackgroundDrawable extends Drawable { | ||
|
|
||
| private int mStatusBarPadding; | ||
| private int mNavigationBarPadding; | ||
|
|
||
| private int mStatusBarColor; | ||
| private int mNavigationBarColor; | ||
|
|
||
| public SystemBarBackgroundDrawable(@ColorInt int defaultColor) { | ||
| mStatusBarColor = defaultColor; | ||
| mNavigationBarColor = defaultColor; | ||
| } | ||
|
|
||
| public void setSystemBarPaddings(int statusBarPadding, int navigationBarPadding) { | ||
| mStatusBarPadding = statusBarPadding; | ||
| mNavigationBarPadding = navigationBarPadding; | ||
| } | ||
| public void setNavigationBarColor(@ColorInt int navigationBarColor) { | ||
| this.mNavigationBarColor = navigationBarColor; | ||
| } | ||
|
|
||
| public void setStatusBarColor(@ColorInt int statusBarColor) { | ||
| this.mStatusBarColor = statusBarColor; | ||
| } | ||
|
|
||
| @Override | ||
| public void draw(Canvas canvas) { | ||
| Rect bounds = getBounds(); | ||
|
|
||
| Paint paint = new Paint(); | ||
| paint.setStyle(Paint.Style.FILL); | ||
| paint.setStrokeWidth(0); // Width of the rectangle's border | ||
|
|
||
| paint.setColor(mStatusBarColor); | ||
| Rect statusBarRect = new Rect(bounds.left, bounds.top, bounds.right, bounds.top + mStatusBarPadding); | ||
| canvas.drawRect(statusBarRect, paint); | ||
|
|
||
| paint.setColor(mNavigationBarColor); | ||
| Rect navigationBarRect = new Rect(bounds.left, bounds.bottom - mNavigationBarPadding, bounds.right, bounds.bottom); | ||
| canvas.drawRect(navigationBarRect, paint); | ||
| } | ||
|
|
||
| // These methods must be implemented | ||
| @Override | ||
| public void setAlpha(int alpha) {} | ||
|
|
||
| @Override | ||
| public void setColorFilter(ColorFilter colorFilter) {} | ||
|
|
||
| @Override | ||
| public int getOpacity() { | ||
| return PixelFormat.OPAQUE; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.