Commit 168554a7 authored by John Lin's avatar John Lin Committed by Commit Bot

Update top bar visibility against a list of eligible domains from intent extra.

Bug: 882404
Change-Id: Ib880be036386c007e5bbfa6012ef5712451440ee
Reviewed-on: https://chromium-review.googlesource.com/c/1290930Reviewed-by: default avatarMichael van Ouwerkerk <mvanouwerkerk@chromium.org>
Commit-Queue: John Lin <chuanl@google.com>
Cr-Commit-Position: refs/heads/master@{#605717}
parent d1012fcc
......@@ -114,6 +114,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
/**
* The activity for custom tabs. It will be launched on top of a client's task.
......@@ -404,7 +405,8 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
public void setTopBarContentView(View view) {
mTopBarDelegate.setTopBarContentView(view);
mTopBarDelegate.showTopBarIfNecessary();
mTopBarDelegate.showTopBarIfNecessary(
isModuleManagedUrl(mIntentDataProvider.getUrlToLoad()));
}
@Override
......@@ -787,6 +789,7 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
public void onUrlUpdated(Tab tab) {
// Update the color on every new URL.
updateColor(tab);
mTopBarDelegate.showTopBarIfNecessary(isModuleManagedUrl(tab.getUrl()));
}
/**
......@@ -1503,4 +1506,9 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
return ChromeApplication.getComponent().createCustomTabActivityComponent(
commonsModule, contextualSuggestionsModule, customTabsModule);
}
private boolean isModuleManagedUrl(String url) {
Pattern urlsPattern = mIntentDataProvider.getExtraModuleManagedUrlsPattern();
return !TextUtils.isEmpty(url) && urlsPattern != null && urlsPattern.matcher(url).matches();
}
}
......@@ -47,6 +47,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* A model class that parses the incoming intent for Custom Tabs specific customization data.
......@@ -128,6 +129,10 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider {
public static final String EXTRA_SEND_TO_EXTERNAL_DEFAULT_HANDLER =
"android.support.customtabs.extra.SEND_TO_EXTERNAL_HANDLER";
/** Extra that defines the module managed URLs regex. */
public static final String EXTRA_MODULE_MANAGED_URLS_REGEX =
"org.chromium.chrome.browser.customtabs.EXTRA_MODULE_MANAGED_URLS_REGEX";
/** The APK package to load the module from. */
private static final String EXTRA_MODULE_PACKAGE_NAME =
"org.chromium.chrome.browser.customtabs.EXTRA_MODULE_PACKAGE_NAME";
......@@ -177,6 +182,8 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider {
private PendingIntent mRemoteViewsPendingIntent;
// OnFinished listener for PendingIntents. Used for testing only.
private PendingIntent.OnFinished mOnFinished;
@Nullable
private Pattern mModuleManagedUrlsPattern;
/** Whether this CustomTabActivity was explicitly started by another Chrome Activity. */
private final boolean mIsOpenedByChrome;
......@@ -289,6 +296,13 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider {
} else {
mModuleComponentName = null;
}
String moduleManagedUrlsRegex =
IntentUtils.safeGetStringExtra(intent, EXTRA_MODULE_MANAGED_URLS_REGEX);
if (moduleManagedUrlsRegex != null) {
mModuleManagedUrlsPattern = Pattern.compile(moduleManagedUrlsRegex);
} else {
mModuleManagedUrlsPattern = null;
}
}
private boolean isIncognitoForPaymentsFlow(Intent intent) {
......@@ -762,4 +776,14 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider {
ComponentName getModuleComponentName() {
return mModuleComponentName;
}
/**
* See {@link #EXTRA_MODULE_MANAGED_URLS_REGEX}.
* @return The pattern compiled from the regex that defines the module managed URLs,
* or null if not specified.
*/
@Nullable
Pattern getExtraModuleManagedUrlsPattern() {
return mModuleManagedUrlsPattern;
}
}
......@@ -26,12 +26,15 @@ class CustomTabTopBarDelegate {
}
/**
* Makes the top bar area to show, if any.
* Adds the top bar, if any, to the view hierarchy and updates its visibility.
*/
public void showTopBarIfNecessary() {
public void showTopBarIfNecessary(boolean isVisible) {
if (mTopBarContentView != null && mTopBarContentView.getParent() == null) {
getTopBarView().addView(mTopBarContentView);
}
if (mTopBarContentView != null) {
mTopBarContentView.setVisibility(isVisible ? View.VISIBLE : View.GONE);
}
}
/**
......
......@@ -1118,6 +1118,7 @@ public class CustomTabActivityTest {
ViewGroup topBar = cctActivity.findViewById(R.id.topbar);
Assert.assertNotNull(topBar);
Assert.assertThat(anyView.getParent(), equalTo(topBar));
Assert.assertEquals(View.GONE, anyView.getVisibility());
});
}
......@@ -1137,6 +1138,28 @@ public class CustomTabActivityTest {
});
}
@Test
@SmallTest
@RetryOnFailure
public void testSetTopBarContentView_moduleManagedUrl_topBarVisible() throws Exception {
Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getTargetContext(),
"https://www.google.com/search?q=london");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_MANAGED_URLS_REGEX,
"^https://www.google.com/search.*");
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
ThreadUtils.runOnUiThread(() -> {
CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity();
View anyView = new View(cctActivity);
cctActivity.setTopBarContentView(anyView);
ViewGroup topBar = cctActivity.findViewById(R.id.topbar);
Assert.assertNotNull(topBar);
Assert.assertThat(anyView.getParent(), equalTo(topBar));
Assert.assertEquals(View.VISIBLE, anyView.getVisibility());
});
}
@Test
@SmallTest
@Feature({"UiCatalogue"})
......
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