Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
Expand All @@ -36,6 +37,7 @@
import androidx.browser.trusted.sharing.ShareData;
import androidx.browser.trusted.sharing.ShareTarget;
import androidx.core.content.ContextCompat;
import androidx.core.view.WindowCompat;

import com.google.androidbrowserhelper.trusted.splashscreens.PwaWrapperSplashScreenStrategy;

Expand Down Expand Up @@ -132,6 +134,9 @@ public class LauncherActivity extends Activity {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

WindowCompat.enableEdgeToEdge(getWindow());
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to move this to splash coordinator, otherwise we have a chance of getting obscured status bar icons.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed offline, status bar icons are not getting obscured

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a fresh look, let's move it to the splash controller and apply when we definitely set a color to system bars.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case the bug that we have now with the behavior without the splash screen will not be fixed.
Right now the transparent activity without splash screen shows black system bars, instead of being transparent. Setting this flag fixes it.


mStartupUptimeMillis = SystemClock.uptimeMillis();
sLauncherActivitiesAlive++;
boolean twaAlreadyRunning = sLauncherActivitiesAlive > 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
*/
public class Utils {

/** Sets status bar color. Makes the icons dark if necessary. */
/**
* Sets status bar color. Makes the icons dark if necessary.
* Deprecated - use EdgeToEdgeController instead.
*/
@Deprecated
public static void setStatusBarColor(Activity activity, @ColorInt int color) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
activity.getWindow().setStatusBarColor(color);
Expand All @@ -44,7 +48,11 @@ && shouldUseDarkIconsOnBackground(color)) {
}
}

/** Sets navigation bar color. Makes the icons dark if necessary */
/**
* Sets navigation bar color. Makes the icons dark if necessary.
* Deprecated - use EdgeToEdgeController instead.
*/
@Deprecated
public static void setNavigationBarColor(Activity activity, @ColorInt int color) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.google.androidbrowserhelper.trusted.splashscreens;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.ColorInt;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.insets.ColorProtection;
import androidx.core.view.insets.ProtectionLayout;

import com.google.common.collect.ImmutableList;

/**
* A helper class to control the color of system bars in edge-to-edge mode.
* It sets up a {@link ProtectionLayout} which manages the drawables behind
* the status and navigation bars.
*/
public class EdgeToEdgeController {
private FrameLayout mRootView;
private ProtectionLayout mProtectionLayout;
private ColorProtection mStatusBarProtection;
private ColorProtection mNavigationBarProtection;

public EdgeToEdgeController(Activity activity, @ColorInt int defaultColor) {
mStatusBarProtection = new ColorProtection(WindowInsetsCompat.Side.TOP, defaultColor);
mNavigationBarProtection = new ColorProtection(WindowInsetsCompat.Side.BOTTOM, defaultColor);

mRootView = new FrameLayout(activity);
mRootView.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));

mProtectionLayout = new ProtectionLayout(activity,
ImmutableList.of(mStatusBarProtection, mNavigationBarProtection));
mRootView.addView(mProtectionLayout);
mProtectionLayout.setVisibility(View.VISIBLE);
}

public FrameLayout getWrapperView() {
return mRootView;
}

public void setOriginalView(View originalView) {
mProtectionLayout.addView(originalView);
}

public void setStatusBarColor(@ColorInt int color) {
mStatusBarProtection.setColor(color);
}

public void setNavigationBarColor(@ColorInt int color) {
mNavigationBarProtection.setColor(color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public class PwaWrapperSplashScreenStrategy implements SplashScreenStrategy {

private boolean mStartChromeBeforeAnimationComplete;

private EdgeToEdgeController mEdgeToEdgeController;

/**
* @param activity {@link Activity} on top of which a TWA is going to be launched.
* @param drawableId Resource id of the Drawable of an image (e.g. logo) displayed in the
Expand Down Expand Up @@ -130,6 +132,8 @@ public void onTwaLaunchInitiated(String providerPackage, TrustedWebActivityInten
return;
}

mEdgeToEdgeController = new EdgeToEdgeController(mActivity, mBackgroundColor);

showSplashScreen();
if (mSplashImage != null) {
customizeStatusAndNavBarDuringSplashScreen(providerPackage, builder);
Expand Down Expand Up @@ -157,7 +161,8 @@ private void showSplashScreen() {
view.setImageMatrix(mTransformationMatrix);
}

mActivity.setContentView(view);
mEdgeToEdgeController.setOriginalView(view);
mActivity.setContentView(mEdgeToEdgeController.getWrapperView());
}

/**
Expand All @@ -169,13 +174,13 @@ private void customizeStatusAndNavBarDuringSplashScreen(
Integer navbarColor = sSystemBarColorPredictor.getExpectedNavbarColor(mActivity,
providerPackage, builder);
if (navbarColor != null) {
Utils.setNavigationBarColor(mActivity, navbarColor);
mEdgeToEdgeController.setNavigationBarColor(navbarColor);
}

Integer statusBarColor = sSystemBarColorPredictor.getExpectedStatusBarColor(mActivity,
providerPackage, builder);
if (statusBarColor != null) {
Utils.setStatusBarColor(mActivity, statusBarColor);
mEdgeToEdgeController.setStatusBarColor(statusBarColor);
}
}

Expand Down
7 changes: 7 additions & 0 deletions demos/twa-basic/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:manageSpaceActivity="com.google.androidbrowserhelper.trusted.ManageDataLauncherActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar">

<meta-data
android:name="asset_statements"
android:resource="@string/asset_statements" />

<activity android:name="com.google.androidbrowserhelper.trusted.ManageDataLauncherActivity">
<meta-data
android:name="android.support.customtabs.trusted.MANAGE_SPACE_URL"
android:value="https://airhorner.com" />
</activity>

<activity android:name="com.google.androidbrowserhelper.trusted.LauncherActivity"
android:label="@string/app_name"
android:exported="true">
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ androidx-annotation = "1.9.1"
androidx-appcompat = "1.7.0"
androidx-browser = "1.10.0-alpha01"
androidx-constraintlayout = "2.2.0"
androidx-core = "1.10.1"
androidx-core = "1.17.0"
androidx-recyclerview = "1.1.0"
androidx-test-espresso-core = "3.2.0"
androidx-test-core = "1.4.0"
Expand Down