Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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,49 @@
package com.google.androidbrowserhelper.trusted.splashscreens;

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

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

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

import com.google.common.collect.ImmutableList;

public class EdgeToEdgeController {
private Activity mActivity;
private ColorProtection statusBarProtection;
private ColorProtection navigationBarProtection;

public EdgeToEdgeController(Activity activity, @ColorInt int defaultColor) {
mActivity = activity;
statusBarProtection = new ColorProtection(WindowInsetsCompat.Side.TOP, defaultColor);
navigationBarProtection = new ColorProtection(WindowInsetsCompat.Side.BOTTOM, defaultColor);
}

public FrameLayout getWrapperView() {
FrameLayout rootView = new FrameLayout(mActivity);
rootView.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));

ProtectionLayout protectionLayout = new ProtectionLayout(mActivity,
ImmutableList.of(statusBarProtection, navigationBarProtection));
rootView.addView(protectionLayout);
protectionLayout.setVisibility(View.VISIBLE);
protectionLayout.setElevation(1f);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is it needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The splash screen ImageView is added later to this FrameLayout and it draws views by recency, unless we set z-index manually

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed manual elevation setting

return rootView;
}

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

public void setNavigationBarColor(@ColorInt int color) {
navigationBarProtection.setColor(color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.google.androidbrowserhelper.trusted.Utils;
Expand Down Expand Up @@ -89,6 +90,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 +133,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 +162,9 @@ private void showSplashScreen() {
view.setImageMatrix(mTransformationMatrix);
}

mActivity.setContentView(view);
FrameLayout rootView = mEdgeToEdgeController.getWrapperView();
rootView.addView(view);
mActivity.setContentView(rootView);
}

/**
Expand All @@ -169,13 +176,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