Commit 1724b086 authored by chuanl's avatar chuanl Committed by Commit Bot

Enable progress bar top margin customization for module-managed URLs.

This is guarded by the CCTModuleCustomHeader Finch feature.

Bug: 882404
Change-Id: I2a7675b95d2135b602f89b26a5d9220677d9079a
Reviewed-on: https://chromium-review.googlesource.com/c/1386428Reviewed-by: default avatarMichael van Ouwerkerk <mvanouwerkerk@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Commit-Queue: John Lin <chuanl@google.com>
Cr-Commit-Position: refs/heads/master@{#619546}
parent 1076d92a
...@@ -66,6 +66,14 @@ public class CustomTabTopBarDelegate { ...@@ -66,6 +66,14 @@ public class CustomTabTopBarDelegate {
return mTopBarHeight; return mTopBarHeight;
} }
/**
* Gets the top bar content view, or null if it is not specified.
*/
@Nullable
public View getTopBarContentView() {
return mTopBarContentView;
}
/** /**
* Gets the {@link ViewGroup} of the top bar. If it has not been inflated, inflate it first. * Gets the {@link ViewGroup} of the top bar. If it has not been inflated, inflate it first.
*/ */
......
...@@ -88,8 +88,6 @@ public class DynamicModuleCoordinator implements NativeInitObserver, Destroyable ...@@ -88,8 +88,6 @@ public class DynamicModuleCoordinator implements NativeInitObserver, Destroyable
@ToolbarVisibility @ToolbarVisibility
private int mDefaultToolbarShadowVisibility; private int mDefaultToolbarShadowVisibility;
// Whether the progress bar is enabled prior to any header customization.
private boolean mDefaultIsProgressBarEnabled;
// Default height of the top control container prior to any header customization. // Default height of the top control container prior to any header customization.
private int mDefaultTopControlContainerHeight; private int mDefaultTopControlContainerHeight;
private boolean mHasSetOverlayView; private boolean mHasSetOverlayView;
...@@ -280,7 +278,6 @@ public class DynamicModuleCoordinator implements NativeInitObserver, Destroyable ...@@ -280,7 +278,6 @@ public class DynamicModuleCoordinator implements NativeInitObserver, Destroyable
mDefaultToolbarVisibility = mActivity.getToolbarManager().getToolbarVisibility(); mDefaultToolbarVisibility = mActivity.getToolbarManager().getToolbarVisibility();
mDefaultToolbarShadowVisibility = mDefaultToolbarShadowVisibility =
mActivity.getToolbarManager().getToolbarShadowVisibility(); mActivity.getToolbarManager().getToolbarShadowVisibility();
mDefaultIsProgressBarEnabled = mActivity.getToolbarManager().isProgressBarEnabled();
mDefaultTopControlContainerHeight = mFullscreenManager.get().getTopControlsHeight(); mDefaultTopControlContainerHeight = mFullscreenManager.get().getTopControlsHeight();
mModuleCallback = null; mModuleCallback = null;
...@@ -379,10 +376,11 @@ public class DynamicModuleCoordinator implements NativeInitObserver, Destroyable ...@@ -379,10 +376,11 @@ public class DynamicModuleCoordinator implements NativeInitObserver, Destroyable
isModuleManagedUrl ? View.GONE : mDefaultToolbarVisibility); isModuleManagedUrl ? View.GONE : mDefaultToolbarVisibility);
mActivity.getToolbarManager().setToolbarShadowVisibility( mActivity.getToolbarManager().setToolbarShadowVisibility(
isModuleManagedUrl ? View.GONE : mDefaultToolbarShadowVisibility); isModuleManagedUrl ? View.GONE : mDefaultToolbarShadowVisibility);
mActivity.getToolbarManager().setProgressBarEnabled(
isModuleManagedUrl ? false : mDefaultIsProgressBarEnabled);
mFullscreenManager.get().setTopControlsHeight( mFullscreenManager.get().setTopControlsHeight(
isModuleManagedUrl ? getTopBarHeight() : mDefaultTopControlContainerHeight); isModuleManagedUrl ? getTopBarHeight() : mDefaultTopControlContainerHeight);
mActivity.getToolbarManager().setProgressBarAnchorView(isModuleManagedUrl
? mTopBarDelegate.get().getTopBarContentView()
: mActivity.getToolbarManager().getToolbarView());
} }
} }
......
...@@ -211,7 +211,6 @@ public class ToolbarManager implements ScrimObserver, ToolbarTabController, UrlF ...@@ -211,7 +211,6 @@ public class ToolbarManager implements ScrimObserver, ToolbarTabController, UrlF
private boolean mNativeLibraryReady; private boolean mNativeLibraryReady;
private boolean mTabRestoreCompleted; private boolean mTabRestoreCompleted;
private boolean mProgressBarEnabled;
private AppMenuButtonHelper mAppMenuButtonHelper; private AppMenuButtonHelper mAppMenuButtonHelper;
...@@ -1472,7 +1471,7 @@ public class ToolbarManager implements ScrimObserver, ToolbarTabController, UrlF ...@@ -1472,7 +1471,7 @@ public class ToolbarManager implements ScrimObserver, ToolbarTabController, UrlF
* @return One of View.VISIBLE, View.INVISIBLE, or View.GONE. * @return One of View.VISIBLE, View.INVISIBLE, or View.GONE.
*/ */
public int getToolbarVisibility() { public int getToolbarVisibility() {
View toolbar = mControlContainer.findViewById(R.id.toolbar); View toolbar = getToolbarView();
return (toolbar != null) ? toolbar.getVisibility() : View.GONE; return (toolbar != null) ? toolbar.getVisibility() : View.GONE;
} }
...@@ -1480,10 +1479,18 @@ public class ToolbarManager implements ScrimObserver, ToolbarTabController, UrlF ...@@ -1480,10 +1479,18 @@ public class ToolbarManager implements ScrimObserver, ToolbarTabController, UrlF
* Sets the visibility of the Toolbar. * Sets the visibility of the Toolbar.
*/ */
public void setToolbarVisibility(int visibility) { public void setToolbarVisibility(int visibility) {
View toolbar = mControlContainer.findViewById(R.id.toolbar); View toolbar = getToolbarView();
if (toolbar != null) toolbar.setVisibility(visibility); if (toolbar != null) toolbar.setVisibility(visibility);
} }
/**
* Gets the Toolbar view.
*/
@Nullable
public View getToolbarView() {
return mControlContainer.findViewById(R.id.toolbar);
}
/** /**
* Sets the drawable that the close button shows, or hides it if {@code drawable} is * Sets the drawable that the close button shows, or hides it if {@code drawable} is
* {@code null}. * {@code null}.
...@@ -1757,18 +1764,17 @@ public class ToolbarManager implements ScrimObserver, ToolbarTabController, UrlF ...@@ -1757,18 +1764,17 @@ public class ToolbarManager implements ScrimObserver, ToolbarTabController, UrlF
} }
/** /**
* @return Whether the progress bar is enabled. * @param enabled Whether the progress bar is enabled.
*/ */
public boolean isProgressBarEnabled() { public void setProgressBarEnabled(boolean enabled) {
return mProgressBarEnabled; mToolbar.setProgressBarEnabled(enabled);
} }
/** /**
* @param enabled Whether the progress bar is enabled. * @param anchor The view to use as an anchor.
*/ */
public void setProgressBarEnabled(boolean enabled) { public void setProgressBarAnchorView(@Nullable View anchor) {
mProgressBarEnabled = enabled; mToolbar.setProgressBarAnchorView(anchor);
mToolbar.setProgressBarEnabled(enabled);
} }
private boolean shouldShowCursorInLocationBar() { private boolean shouldShowCursorInLocationBar() {
......
...@@ -109,7 +109,6 @@ public abstract class ToolbarLayout extends FrameLayout { ...@@ -109,7 +109,6 @@ public abstract class ToolbarLayout extends FrameLayout {
if (isNativeLibraryReady() && mProgressBar.getParent() != null) { if (isNativeLibraryReady() && mProgressBar.getParent() != null) {
mProgressBar.initializeAnimation(); mProgressBar.initializeAnimation();
} }
mProgressBar.setTopMargin(getProgressBarTopMargin());
// Since this only needs to happen once, remove this listener from the view. // Since this only needs to happen once, remove this listener from the view.
removeOnLayoutChangeListener(this); removeOnLayoutChangeListener(this);
...@@ -141,16 +140,6 @@ public abstract class ToolbarLayout extends FrameLayout { ...@@ -141,16 +140,6 @@ public abstract class ToolbarLayout extends FrameLayout {
*/ */
void destroy() {} void destroy() {}
/**
* Get the top margin of the progress bar relative to the toolbar layout. This is used to set
* the position of the progress bar (either top or bottom of the toolbar).
* @return The top margin of the progress bar.
*/
int getProgressBarTopMargin() {
return getHeight()
- getResources().getDimensionPixelSize(R.dimen.toolbar_progress_bar_height);
}
/** /**
* Set the height that the progress bar should be. * Set the height that the progress bar should be.
* @return The progress bar height in px. * @return The progress bar height in px.
...@@ -163,8 +152,7 @@ public abstract class ToolbarLayout extends FrameLayout { ...@@ -163,8 +152,7 @@ public abstract class ToolbarLayout extends FrameLayout {
* @return A progress bar for Chrome to use. * @return A progress bar for Chrome to use.
*/ */
ToolbarProgressBar createProgressBar() { ToolbarProgressBar createProgressBar() {
return new ToolbarProgressBar( return new ToolbarProgressBar(getContext(), getProgressBarHeight(), this, false);
getContext(), getProgressBarHeight(), getProgressBarTopMargin(), false);
} }
/** /**
......
...@@ -134,12 +134,6 @@ public class ToolbarTablet extends ToolbarLayout ...@@ -134,12 +134,6 @@ public class ToolbarTablet extends ToolbarLayout
mToolbarButtons = new ImageButton[] {mBackButton, mForwardButton, mReloadButton}; mToolbarButtons = new ImageButton[] {mBackButton, mForwardButton, mReloadButton};
} }
@Override
protected int getProgressBarTopMargin() {
int tabStripHeight = getResources().getDimensionPixelSize(R.dimen.tab_strip_height);
return super.getProgressBarTopMargin() + tabStripHeight;
}
/** /**
* Sets up key listeners after native initialization is complete, so that we can invoke * Sets up key listeners after native initialization is complete, so that we can invoke
* native functions. * native functions.
......
...@@ -469,6 +469,13 @@ public class TopToolbarCoordinator implements Toolbar { ...@@ -469,6 +469,13 @@ public class TopToolbarCoordinator implements Toolbar {
(toolbar) -> getProgressBar().setVisibility(enabled ? View.VISIBLE : View.GONE)); (toolbar) -> getProgressBar().setVisibility(enabled ? View.VISIBLE : View.GONE));
} }
/**
* @param anchor The view to use as an anchor.
*/
public void setProgressBarAnchorView(@Nullable View anchor) {
mToolbarProvider.whenLoaded(toolbar -> getProgressBar().setAnchorView(anchor));
}
/** /**
* Starts load progress. * Starts load progress.
*/ */
......
...@@ -12,7 +12,9 @@ import android.animation.TimeInterpolator; ...@@ -12,7 +12,9 @@ import android.animation.TimeInterpolator;
import android.content.Context; import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
import android.os.Build; import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityEvent;
import android.view.animation.AccelerateInterpolator; import android.view.animation.AccelerateInterpolator;
...@@ -116,6 +118,16 @@ public class ToolbarProgressBar extends ClipDrawableProgressBar { ...@@ -116,6 +118,16 @@ public class ToolbarProgressBar extends ClipDrawableProgressBar {
/** Whether or not the progress bar is attached to the window. */ /** Whether or not the progress bar is attached to the window. */
private boolean mIsAttachedToWindow; private boolean mIsAttachedToWindow;
/** The progress bar's anchor view. */
@Nullable
private View mAnchorView;
/** The progress bar's height. */
private final int mProgressBarHeight;
private final OnLayoutChangeListener mOnLayoutChangeListener = (view, left, top, right, bottom,
oldLeft, oldTop, oldRight, oldBottom) -> updateTopMargin();
private final Runnable mStartSmoothIndeterminate = new Runnable() { private final Runnable mStartSmoothIndeterminate = new Runnable() {
@Override @Override
public void run() { public void run() {
...@@ -220,15 +232,16 @@ public class ToolbarProgressBar extends ClipDrawableProgressBar { ...@@ -220,15 +232,16 @@ public class ToolbarProgressBar extends ClipDrawableProgressBar {
* *
* @param context The application environment. * @param context The application environment.
* @param height The height of the progress bar in px. * @param height The height of the progress bar in px.
* @param topMargin The top margin of the progress bar. * @param anchor The view to use as an anchor.
* @param useStatusBarColorAsBackground Whether or not to use the status bar color as the * @param useStatusBarColorAsBackground Whether or not to use the status bar color as the
* background of the toolbar. * background of the toolbar.
*/ */
public ToolbarProgressBar( public ToolbarProgressBar(
Context context, int height, int topMargin, boolean useStatusBarColorAsBackground) { Context context, int height, View anchor, boolean useStatusBarColorAsBackground) {
super(context, height); super(context, height);
mProgressBarHeight = height;
setAlpha(0.0f); setAlpha(0.0f);
mMarginTop = topMargin; setAnchorView(anchor);
mUseStatusBarColorAsBackground = useStatusBarColorAsBackground; mUseStatusBarColorAsBackground = useStatusBarColorAsBackground;
mAnimationLogic = new ProgressAnimationSmooth(); mAnimationLogic = new ProgressAnimationSmooth();
...@@ -241,12 +254,16 @@ public class ToolbarProgressBar extends ClipDrawableProgressBar { ...@@ -241,12 +254,16 @@ public class ToolbarProgressBar extends ClipDrawableProgressBar {
* Set the top progress bar's top margin. * Set the top progress bar's top margin.
* @param topMargin The top margin of the progress bar in px. * @param topMargin The top margin of the progress bar in px.
*/ */
public void setTopMargin(int topMargin) { private void setTopMargin(int topMargin) {
mMarginTop = topMargin; mMarginTop = topMargin;
if (mIsAttachedToWindow) { if (mIsAttachedToWindow) {
assert getLayoutParams() != null; assert getLayoutParams() != null;
((ViewGroup.MarginLayoutParams) getLayoutParams()).topMargin = mMarginTop; ((ViewGroup.MarginLayoutParams) getLayoutParams()).topMargin = mMarginTop;
if (mAnimatingView != null && mAnimatingView.getLayoutParams() != null) {
((ViewGroup.MarginLayoutParams) mAnimatingView.getLayoutParams()).topMargin =
mMarginTop;
}
} }
} }
...@@ -575,4 +592,34 @@ public class ToolbarProgressBar extends ClipDrawableProgressBar { ...@@ -575,4 +592,34 @@ public class ToolbarProgressBar extends ClipDrawableProgressBar {
public Animator getIndeterminateAnimatorForTesting() { public Animator getIndeterminateAnimatorForTesting() {
return mSmoothProgressAnimator; return mSmoothProgressAnimator;
} }
/**
* Sets the progress bar's anchor view. This progress bar will always be positioned relative to
* the anchor view when shown.
*
* @param anchor The view to use as an anchor.
*/
public void setAnchorView(@Nullable View anchor) {
if (mAnchorView == anchor) return;
if (mAnchorView != null) {
mAnchorView.removeOnLayoutChangeListener(mOnLayoutChangeListener);
}
mAnchorView = anchor;
updateTopMargin();
if (mAnchorView != null) {
mAnchorView.addOnLayoutChangeListener(mOnLayoutChangeListener);
}
}
/**
* Updates the progress bar's top margin. The only time this is a NOOP is if the margin remains
* the same.
*/
private void updateTopMargin() {
int topMargin = (mAnchorView != null ? mAnchorView.getBottom() : 0) - mProgressBarHeight;
if (mMarginTop != topMargin) {
setTopMargin(topMargin);
}
}
} }
...@@ -17,6 +17,7 @@ import android.support.test.filters.SmallTest; ...@@ -17,6 +17,7 @@ import android.support.test.filters.SmallTest;
import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiDevice;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
...@@ -455,11 +456,55 @@ public class CustomTabsDynamicModuleUITest { ...@@ -455,11 +456,55 @@ public class CustomTabsDynamicModuleUITest {
CustomTabActivity cctActivity = getActivity(); CustomTabActivity cctActivity = getActivity();
int defaultHeight = cctActivity.getFullscreenManager().getTopControlsHeight(); int defaultHeight = cctActivity.getFullscreenManager().getTopControlsHeight();
int newHeight = defaultHeight + 10; int newHeight = defaultHeight + 10;
cctActivity.getComponent().resolveDynamicModuleCoordinator().setTopBarHeight(newHeight); getModuleCoordinator().setTopBarHeight(newHeight);
assertEquals(newHeight, cctActivity.getFullscreenManager().getTopControlsHeight()); assertEquals(newHeight, cctActivity.getFullscreenManager().getTopControlsHeight());
}); });
} }
@Test
@SmallTest
@Features.DisableFeatures(ChromeFeatureList.CCT_MODULE_CUSTOM_HEADER)
public void testSetTopBarContentView_featureDisabled_progressBarNoChange() throws Exception {
Intent intent = new IntentBuilder(mModuleManagedPage)
.setModuleHostList(getServerHostsList())
.setModuleManagedUrlRegex(getModuleManagedRegex())
.setHideCCTHeader(true)
.build();
mActivityRule.startCustomTabActivityWithIntent(intent);
runOnUiThread(() -> Assert.assertFalse(canChangeProgressBarTopMargin()));
}
@Test
@SmallTest
@Features.
EnableFeatures({ChromeFeatureList.CCT_MODULE, ChromeFeatureList.CCT_MODULE_CUSTOM_HEADER})
public void testSetTopBarContentView_cctHeaderNotHidden_progressBarNoChange() throws Exception {
Intent intent = new IntentBuilder(mModuleManagedPage)
.setModuleHostList(getServerHostsList())
.setModuleManagedUrlRegex(getModuleManagedRegex())
.setHideCCTHeader(false)
.build();
mActivityRule.startCustomTabActivityWithIntent(intent);
runOnUiThread(() -> Assert.assertFalse(canChangeProgressBarTopMargin()));
}
@Test
@SmallTest
@Features.
EnableFeatures({ChromeFeatureList.CCT_MODULE, ChromeFeatureList.CCT_MODULE_CUSTOM_HEADER})
public void testSetTopBarContentView_withModuleAndExtras_progressBarChanged() throws Exception {
Intent intent = new IntentBuilder(mModuleManagedPage)
.setModuleHostList(getServerHostsList())
.setModuleManagedUrlRegex(getModuleManagedRegex())
.setHideCCTHeader(true)
.build();
mActivityRule.startCustomTabActivityWithIntent(intent);
runOnUiThread(() -> Assert.assertTrue(canChangeProgressBarTopMargin()));
}
private void assertNoTopBar() { private void assertNoTopBar() {
runOnUiThread(() -> { runOnUiThread(() -> {
ViewGroup topBar = getActivity().findViewById(R.id.topbar); ViewGroup topBar = getActivity().findViewById(R.id.topbar);
...@@ -521,4 +566,36 @@ public class CustomTabsDynamicModuleUITest { ...@@ -521,4 +566,36 @@ public class CustomTabsDynamicModuleUITest {
private DynamicModuleCoordinator getModuleCoordinator() { private DynamicModuleCoordinator getModuleCoordinator() {
return getActivity().getComponent().resolveDynamicModuleCoordinator(); return getActivity().getComponent().resolveDynamicModuleCoordinator();
} }
private boolean canChangeProgressBarTopMargin() {
CustomTabActivity cctActivity = getActivity();
ViewGroup controlContainerView = cctActivity.findViewById(R.id.control_container);
int progressBarHeight = 0;
for (int index = 0; index < controlContainerView.getChildCount(); index++) {
View childView = controlContainerView.getChildAt(index);
if (childView.getId() != R.id.toolbar_container) {
// Either ToolbarProgressBar or ToolbarProgressBarAnimatingView
progressBarHeight = childView.getHeight();
break;
}
}
Assert.assertNotEquals(progressBarHeight, 0);
View anyView = new View(cctActivity);
getModuleCoordinator().setTopBarContentView(anyView);
int newMargin = anyView.getBottom() - progressBarHeight;
boolean canChange = false;
for (int index = 0; index < controlContainerView.getChildCount(); index++) {
View childView = controlContainerView.getChildAt(index);
if (childView.getId() != R.id.toolbar_container) {
if (((MarginLayoutParams) childView.getLayoutParams()).topMargin != newMargin) {
return false;
} else {
canChange = true;
}
}
}
return canChange;
}
} }
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment