Commit 55cb2cc4 authored by Pavel Shmakov's avatar Pavel Shmakov Committed by Commit Bot

Support CustomTabColorSchemeParams

Support the api added here:
https://android-review.googlesource.com/c/platform/frameworks/support/+/958733

Since CustomTabActivity is recreated when system setting is changed,
it's enough to pick correct scheme parameters when activity is launched
and new instance of CustomTabIntentDataProvider is created.

Bug: 960453
Change-Id: I975682a8f6dd57a5ab6fa0bc12dc769e80631bb2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1599591
Commit-Queue: Pavel Shmakov <pshmakov@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarPeter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#660957}
parent b6bc5bf8
...@@ -822,7 +822,7 @@ deps = { ...@@ -822,7 +822,7 @@ deps = {
}, },
'src/third_party/custom_tabs_client/src': { 'src/third_party/custom_tabs_client/src': {
'url': Var('chromium_git') + '/custom-tabs-client.git' + '@' + 'b0d6c6e86d8046fa62135cebb7bc59d9feac9f89', 'url': Var('chromium_git') + '/custom-tabs-client.git' + '@' + '1663ae63d21b24f9de7e07c46013a6ff1c03f49c',
'condition': 'checkout_android', 'condition': 'checkout_android',
}, },
......
...@@ -49,6 +49,7 @@ chrome_junit_test_java_sources = [ ...@@ -49,6 +49,7 @@ chrome_junit_test_java_sources = [
"junit/src/org/chromium/chrome/browser/cookies/CanonicalCookieTest.java", "junit/src/org/chromium/chrome/browser/cookies/CanonicalCookieTest.java",
"junit/src/org/chromium/chrome/browser/crash/LogcatExtractionRunnableUnitTest.java", "junit/src/org/chromium/chrome/browser/crash/LogcatExtractionRunnableUnitTest.java",
"junit/src/org/chromium/chrome/browser/customtabs/CloseButtonNavigatorTest.java", "junit/src/org/chromium/chrome/browser/customtabs/CloseButtonNavigatorTest.java",
"junit/src/org/chromium/chrome/browser/customtabs/CustomTabIntentDataProviderTest.java",
"junit/src/org/chromium/chrome/browser/customtabs/NavigationInfoCaptureTriggerTest.java", "junit/src/org/chromium/chrome/browser/customtabs/NavigationInfoCaptureTriggerTest.java",
"junit/src/org/chromium/chrome/browser/customtabs/content/CustomTabActivityContentTestEnvironment.java", "junit/src/org/chromium/chrome/browser/customtabs/content/CustomTabActivityContentTestEnvironment.java",
"junit/src/org/chromium/chrome/browser/customtabs/content/CustomTabActivityNavigationControllerTest.java", "junit/src/org/chromium/chrome/browser/customtabs/content/CustomTabActivityNavigationControllerTest.java",
......
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
package org.chromium.chrome.browser.customtabs; package org.chromium.chrome.browser.customtabs;
import static android.support.customtabs.CustomTabsIntent.COLOR_SCHEME_DARK;
import static android.support.customtabs.CustomTabsIntent.COLOR_SCHEME_LIGHT;
import static org.chromium.chrome.browser.customtabs.content.CustomTabActivityNavigationController.FinishReason.USER_NAVIGATION; import static org.chromium.chrome.browser.customtabs.content.CustomTabActivityNavigationController.FinishReason.USER_NAVIGATION;
import android.app.Activity; import android.app.Activity;
...@@ -83,7 +86,6 @@ import org.chromium.chrome.browser.util.ColorUtils; ...@@ -83,7 +86,6 @@ import org.chromium.chrome.browser.util.ColorUtils;
import org.chromium.chrome.browser.util.FeatureUtilities; import org.chromium.chrome.browser.util.FeatureUtilities;
import org.chromium.chrome.browser.util.IntentUtils; import org.chromium.chrome.browser.util.IntentUtils;
import org.chromium.content_public.browser.LoadUrlParams; import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_public.browser.NavigationController;
import org.chromium.content_public.browser.NavigationEntry; import org.chromium.content_public.browser.NavigationEntry;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
...@@ -212,7 +214,9 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent ...@@ -212,7 +214,9 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
public void performPreInflationStartup() { public void performPreInflationStartup() {
// Parse the data from the Intent before calling super to allow the Intent to customize // Parse the data from the Intent before calling super to allow the Intent to customize
// the Activity parameters, including the background of the page. // the Activity parameters, including the background of the page.
mIntentDataProvider = new CustomTabIntentDataProvider(getIntent(), this); // Note that color scheme is fixed for the lifetime of Activity: if the system setting
// changes, we recreate the activity.
mIntentDataProvider = new CustomTabIntentDataProvider(getIntent(), this, getColorScheme());
super.performPreInflationStartup(); super.performPreInflationStartup();
mTabProvider.addObserver(mTabChangeObserver); mTabProvider.addObserver(mTabChangeObserver);
...@@ -228,6 +232,15 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent ...@@ -228,6 +232,15 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
initalizePreviewsObserver(); initalizePreviewsObserver();
} }
private int getColorScheme() {
if (mNightModeStateController != null) {
return mNightModeStateController.isInNightMode() ? COLOR_SCHEME_DARK :
COLOR_SCHEME_LIGHT;
}
assert false : "NightModeStateController should have been already created";
return COLOR_SCHEME_LIGHT;
}
private void initializeIncognito() { private void initializeIncognito() {
mIncognitoTabHost = new IncognitoCustomTabHost(); mIncognitoTabHost = new IncognitoCustomTabHost();
IncognitoTabHostRegistry.getInstance().register(mIncognitoTabHost); IncognitoTabHostRegistry.getInstance().register(mIncognitoTabHost);
...@@ -239,14 +252,6 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent ...@@ -239,14 +252,6 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
} }
} }
@Nullable
private NavigationController getNavigationController() {
if (getActivityTab() == null) return null;
WebContents webContents = getActivityTab().getWebContents();
return webContents == null ? null : webContents.getNavigationController();
}
@Override @Override
public boolean shouldAllocateChildConnection() { public boolean shouldAllocateChildConnection() {
return mTabController.shouldAllocateChildConnection(); return mTabController.shouldAllocateChildConnection();
......
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
package org.chromium.chrome.browser.customtabs; package org.chromium.chrome.browser.customtabs;
import static android.support.customtabs.CustomTabsIntent.COLOR_SCHEME_LIGHT;
import static android.support.customtabs.CustomTabsIntent.COLOR_SCHEME_SYSTEM;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException; import android.app.PendingIntent.CanceledException;
import android.content.ComponentName; import android.content.ComponentName;
...@@ -16,7 +19,9 @@ import android.graphics.drawable.Drawable; ...@@ -16,7 +19,9 @@ import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.IntDef; import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.customtabs.CustomTabColorSchemeParams;
import android.support.customtabs.CustomTabsIntent; import android.support.customtabs.CustomTabsIntent;
import android.support.customtabs.CustomTabsSessionToken; import android.support.customtabs.CustomTabsSessionToken;
import android.support.customtabs.TrustedWebUtils; import android.support.customtabs.TrustedWebUtils;
...@@ -53,6 +58,10 @@ import java.util.regex.Pattern; ...@@ -53,6 +58,10 @@ import java.util.regex.Pattern;
/** /**
* A model class that parses the incoming intent for Custom Tabs specific customization data. * A model class that parses the incoming intent for Custom Tabs specific customization data.
*
* Lifecycle: is activity-scoped, i.e. one instance per CustomTabActivity instance. Must be
* re-created when color scheme changes, which happens automatically since color scheme change leads
* to activity re-creation.
*/ */
public class CustomTabIntentDataProvider extends BrowserSessionDataProvider { public class CustomTabIntentDataProvider extends BrowserSessionDataProvider {
private static final String TAG = "CustomTabIntentData"; private static final String TAG = "CustomTabIntentData";
...@@ -245,9 +254,25 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider { ...@@ -245,9 +254,25 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider {
} }
/** /**
* Constructs a {@link CustomTabIntentDataProvider}. * Deprecated: use the constructor below.
* TODO(pshmakov): remove once no longer used downstream.
*/ */
@Deprecated
public CustomTabIntentDataProvider(Intent intent, Context context) { public CustomTabIntentDataProvider(Intent intent, Context context) {
this(intent, context, COLOR_SCHEME_LIGHT);
}
/**
* Constructs a {@link CustomTabIntentDataProvider}.
*
* The colorScheme parameter specifies which color scheme the Custom Tab should use.
* It can currently be either {@link CustomTabsIntent#COLOR_SCHEME_LIGHT} or
* {@link CustomTabsIntent#COLOR_SCHEME_DARK}.
* If Custom Tab was launched with {@link CustomTabsIntent#COLOR_SCHEME_SYSTEM}, colorScheme
* must reflect the current system setting. When the system setting changes, a new
* CustomTabIntentDataProvider object must be created.
*/
public CustomTabIntentDataProvider(Intent intent, Context context, int colorScheme) {
super(intent); super(intent);
if (intent == null) assert false; if (intent == null) assert false;
...@@ -261,9 +286,10 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider { ...@@ -261,9 +286,10 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider {
mIsIncognito = isIncognitoForPaymentsFlow(intent) || isValidExternalIncognitoIntent(intent); mIsIncognito = isIncognitoForPaymentsFlow(intent) || isValidExternalIncognitoIntent(intent);
CustomTabColorSchemeParams params = getColorSchemeParams(intent, colorScheme);
retrieveCustomButtons(intent, context); retrieveCustomButtons(intent, context);
retrieveToolbarColor(intent, context); retrieveToolbarColor(params, context);
retrieveBottomBarColor(intent); retrieveBottomBarColor(params);
mInitialBackgroundColor = retrieveInitialBackgroundColor(intent); mInitialBackgroundColor = retrieveInitialBackgroundColor(intent);
mEnableUrlBarHiding = IntentUtils.safeGetBooleanExtra( mEnableUrlBarHiding = IntentUtils.safeGetBooleanExtra(
...@@ -355,6 +381,22 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider { ...@@ -355,6 +381,22 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider {
} }
} }
@NonNull
private CustomTabColorSchemeParams getColorSchemeParams(Intent intent, int colorScheme) {
if (colorScheme == COLOR_SCHEME_SYSTEM) {
assert false : "Color scheme passed to IntentDataProvider should not be "
+ "COLOR_SCHEME_SYSTEM";
colorScheme = COLOR_SCHEME_LIGHT;
}
try {
return CustomTabsIntent.getColorSchemeParams(intent, colorScheme);
} catch (Throwable e) {
// Catch any un-parceling exceptions, like in IntentUtils#safe* methods
Log.e(TAG, "Failed to parse CustomTabColorSchemeParams");
return new CustomTabColorSchemeParams.Builder().build(); // Empty params
}
}
private boolean isIncognitoForPaymentsFlow(Intent intent) { private boolean isIncognitoForPaymentsFlow(Intent intent) {
return incognitoRequested(intent) && isTrustedIntent() && isOpenedByChrome() return incognitoRequested(intent) && isTrustedIntent() && isOpenedByChrome()
&& isForPaymentRequest(); && isForPaymentRequest();
...@@ -435,28 +477,27 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider { ...@@ -435,28 +477,27 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider {
/** /**
* Processes the color passed from the client app and updates {@link #mToolbarColor}. * Processes the color passed from the client app and updates {@link #mToolbarColor}.
*/ */
private void retrieveToolbarColor(Intent intent, Context context) { private void retrieveToolbarColor(CustomTabColorSchemeParams schemeParams, Context context) {
int defaultColor = ColorUtils.getDefaultThemeColor(context.getResources(), isIncognito()); int defaultColor = ColorUtils.getDefaultThemeColor(context.getResources(), isIncognito());
if (isIncognito()) { if (isIncognito()) {
mToolbarColor = defaultColor; mToolbarColor = defaultColor;
return; // Don't allow toolbar color customization for incognito tabs. return; // Don't allow toolbar color customization for incognito tabs.
} }
int color = IntentUtils.safeGetIntExtra( int color = schemeParams.toolbarColor != null ? schemeParams.toolbarColor : defaultColor;
intent, CustomTabsIntent.EXTRA_TOOLBAR_COLOR, defaultColor);
mToolbarColor = removeTransparencyFromColor(color); mToolbarColor = removeTransparencyFromColor(color);
} }
/** /**
* Must be called after calling {@link #retrieveToolbarColor(Intent, Context)}. * Must be called after calling {@link #retrieveToolbarColor}.
*/ */
private void retrieveBottomBarColor(Intent intent) { private void retrieveBottomBarColor(CustomTabColorSchemeParams schemeParams) {
if (isIncognito()) { if (isIncognito()) {
mBottomBarColor = mToolbarColor; mBottomBarColor = mToolbarColor;
return; return;
} }
int defaultColor = mToolbarColor; int defaultColor = mToolbarColor;
int color = IntentUtils.safeGetIntExtra( int color = schemeParams.secondaryToolbarColor != null ? schemeParams.secondaryToolbarColor
intent, CustomTabsIntent.EXTRA_SECONDARY_TOOLBAR_COLOR, defaultColor); : defaultColor;
mBottomBarColor = removeTransparencyFromColor(color); mBottomBarColor = removeTransparencyFromColor(color);
} }
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.customtabs;
import static android.support.customtabs.CustomTabsIntent.COLOR_SCHEME_DARK;
import static android.support.customtabs.CustomTabsIntent.COLOR_SCHEME_LIGHT;
import static org.junit.Assert.assertEquals;
import android.content.Intent;
import android.support.customtabs.CustomTabColorSchemeParams;
import android.support.customtabs.CustomTabsIntent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
/** Tests for {@link CustomTabIntentDataProvider}. */
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class CustomTabIntentDataProviderTest {
@Test
public void colorSchemeParametersAreRetrieved() {
CustomTabColorSchemeParams lightParams = new CustomTabColorSchemeParams.Builder()
.setToolbarColor(0xff0000ff)
.setSecondaryToolbarColor(0xff00aaff)
.build();
CustomTabColorSchemeParams darkParams = new CustomTabColorSchemeParams.Builder()
.setToolbarColor(0xffff0000)
.setSecondaryToolbarColor(0xffff8800)
.build();
Intent intent = new CustomTabsIntent.Builder()
.setColorSchemeParams(COLOR_SCHEME_LIGHT, lightParams)
.setColorSchemeParams(COLOR_SCHEME_DARK, darkParams)
.build()
.intent;
CustomTabIntentDataProvider lightProvider = new CustomTabIntentDataProvider(intent,
RuntimeEnvironment.application, COLOR_SCHEME_LIGHT);
CustomTabIntentDataProvider darkProvider = new CustomTabIntentDataProvider(intent,
RuntimeEnvironment.application, COLOR_SCHEME_DARK);
assertEquals((int) lightParams.toolbarColor, lightProvider.getToolbarColor());
assertEquals((int) darkParams.toolbarColor, darkProvider.getToolbarColor());
assertEquals((int) lightParams.secondaryToolbarColor, lightProvider.getBottomBarColor());
assertEquals((int) darkParams.secondaryToolbarColor, darkProvider.getBottomBarColor());
}
}
...@@ -74,6 +74,7 @@ android_library("custom_tabs_support_java") { ...@@ -74,6 +74,7 @@ android_library("custom_tabs_support_java") {
"src/customtabs/src/android/support/customtabs/CustomTabsServiceConnection.java", "src/customtabs/src/android/support/customtabs/CustomTabsServiceConnection.java",
"src/customtabs/src/android/support/customtabs/CustomTabsSession.java", "src/customtabs/src/android/support/customtabs/CustomTabsSession.java",
"src/customtabs/src/android/support/customtabs/CustomTabsSessionToken.java", "src/customtabs/src/android/support/customtabs/CustomTabsSessionToken.java",
"src/customtabs/src/android/support/customtabs/CustomTabColorSchemeParams.java",
"src/customtabs/src/android/support/customtabs/PostMessageBackend.java", "src/customtabs/src/android/support/customtabs/PostMessageBackend.java",
"src/customtabs/src/android/support/customtabs/PostMessageService.java", "src/customtabs/src/android/support/customtabs/PostMessageService.java",
"src/customtabs/src/android/support/customtabs/PostMessageServiceConnection.java", "src/customtabs/src/android/support/customtabs/PostMessageServiceConnection.java",
......
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