Skip to content

Commit

Permalink
Implement opt-in message for desktop site global setting on 10"+ tablets
Browse files Browse the repository at this point in the history
- Add Finch param to enable opt-in message trigger
- Add Finch params to set min and max screen size thresholds
- Add SharedPreferences key to track when the message is shown, to restrict showing at most once on a device
- Implement window-scoped message for opt-in

Screenshot: https://screenshot.googleplex.com/A3NedP9kAwqsBvK.png

Bug: 1351819
Change-Id: I79b6d7f36ee50177745eb61762b9ce91f6ff5852
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3830046
Reviewed-by: Theresa Sullivan <twellington@chromium.org>
Reviewed-by: Shu Yang <shuyng@google.com>
Commit-Queue: Aishwarya Rajesh <aishwaryarj@google.com>
Reviewed-by: Tomasz Wiszkowski <ender@google.com>
Cr-Commit-Position: refs/heads/main@{#1035199}
  • Loading branch information
Aishwarya Rajesh authored and Chromium LUCI CQ committed Aug 15, 2022
1 parent d0c9248 commit c0b23a1
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.TabUtils.LoadIfNeededCaller;
import org.chromium.components.browser_ui.site_settings.SingleCategorySettings;
import org.chromium.components.browser_ui.site_settings.SiteSettingsCategory;
import org.chromium.components.browser_ui.site_settings.WebsitePreferenceBridge;
Expand Down Expand Up @@ -52,6 +53,15 @@ public class RequestDesktopUtils {
static final String PARAM_SHOW_MESSAGE_ON_GLOBAL_SETTING_DEFAULT_ON =
"show_message_on_default_on";

static final String PARAM_GLOBAL_SETTING_OPT_IN_ENABLED = "show_opt_in_message";
static final String PARAM_GLOBAL_SETTING_OPT_IN_DISPLAY_SIZE_MIN_THRESHOLD_INCHES =
"opt_in_display_size_min_threshold_inches";
static final double DEFAULT_GLOBAL_SETTING_OPT_IN_DISPLAY_SIZE_MIN_THRESHOLD_INCHES = 10.0;
static final String PARAM_GLOBAL_SETTING_OPT_IN_DISPLAY_SIZE_MAX_THRESHOLD_INCHES =
"opt_in_display_size_max_threshold_inches";
static final double DEFAULT_GLOBAL_SETTING_OPT_IN_DISPLAY_SIZE_MAX_THRESHOLD_INCHES =
Double.MAX_VALUE;

// Note: these values must match the UserAgentRequestType enum in enums.xml.
@IntDef({UserAgentRequestType.REQUEST_DESKTOP, UserAgentRequestType.REQUEST_MOBILE})
@Retention(RetentionPolicy.SOURCE)
Expand Down Expand Up @@ -156,11 +166,18 @@ public static void setRequestDesktopSiteContentSettingsForUrl(
* @param displaySizeInInches The device primary display size, in inches.
* @return Whether the desktop site global setting should be default-enabled.
*/
public static boolean shouldDefaultEnableGlobalSetting(double displaySizeInInches) {
static boolean shouldDefaultEnableGlobalSetting(double displaySizeInInches) {
if (!ChromeFeatureList.isEnabled(ChromeFeatureList.REQUEST_DESKTOP_SITE_DEFAULTS)) {
return false;
}

// If the device is part of an opt-in experiment arm, avoid default-enabling the setting.
if (ChromeFeatureList.getFieldTrialParamByFeatureAsBoolean(
ChromeFeatureList.REQUEST_DESKTOP_SITE_DEFAULTS,
PARAM_GLOBAL_SETTING_OPT_IN_ENABLED, false)) {
return false;
}

// Check whether default-on for low end devices is disabled.
if (!ChromeFeatureList.getFieldTrialParamByFeatureAsBoolean(
ChromeFeatureList.REQUEST_DESKTOP_SITE_DEFAULTS,
Expand Down Expand Up @@ -267,4 +284,103 @@ public static boolean maybeShowDefaultEnableGlobalSettingMessage(
ChromePreferenceKeys.DEFAULT_ENABLED_DESKTOP_SITE_GLOBAL_SETTING_SHOW_MESSAGE);
return true;
}

/**
* @param displaySizeInInches The device primary display size, in inches.
* @param profile The current {@link Profile}.
* @return Whether the message to opt-in to the desktop site global setting should be shown.
*/
static boolean shouldShowGlobalSettingOptInMessage(
double displaySizeInInches, Profile profile) {
if (!ChromeFeatureList.isEnabled(ChromeFeatureList.REQUEST_DESKTOP_SITE_DEFAULTS)) {
return false;
}

// Present the message only if opt-in is enabled.
if (!ChromeFeatureList.getFieldTrialParamByFeatureAsBoolean(
ChromeFeatureList.REQUEST_DESKTOP_SITE_DEFAULTS,
PARAM_GLOBAL_SETTING_OPT_IN_ENABLED, false)) {
return false;
}

// Present the message at most once on a device.
if (SharedPreferencesManager.getInstance().contains(
ChromePreferenceKeys.DESKTOP_SITE_GLOBAL_SETTING_OPT_IN_MESSAGE_SHOWN)) {
return false;
}

// Present the message only if the user has not previously updated the global setting.
if (SharedPreferencesManager.getInstance().contains(
SingleCategorySettings
.USER_ENABLED_DESKTOP_SITE_GLOBAL_SETTING_PREFERENCE_KEY)) {
return false;
}

// Present the message only if the desktop site global setting is off.
if (WebsitePreferenceBridge.isCategoryEnabled(
profile, ContentSettingsType.REQUEST_DESKTOP_SITE)) {
return false;
}

// Present the message only if the device falls within the range of screen sizes for the
// opt-in.
return displaySizeInInches >= ChromeFeatureList.getFieldTrialParamByFeatureAsDouble(
ChromeFeatureList.REQUEST_DESKTOP_SITE_DEFAULTS,
PARAM_GLOBAL_SETTING_OPT_IN_DISPLAY_SIZE_MIN_THRESHOLD_INCHES,
DEFAULT_GLOBAL_SETTING_OPT_IN_DISPLAY_SIZE_MIN_THRESHOLD_INCHES)
&& displaySizeInInches < ChromeFeatureList.getFieldTrialParamByFeatureAsDouble(
ChromeFeatureList.REQUEST_DESKTOP_SITE_DEFAULTS,
PARAM_GLOBAL_SETTING_OPT_IN_DISPLAY_SIZE_MAX_THRESHOLD_INCHES,
DEFAULT_GLOBAL_SETTING_OPT_IN_DISPLAY_SIZE_MAX_THRESHOLD_INCHES);
}

/**
* Creates and shows a message to the user to opt-in to the desktop site global setting based on
* device conditions.
* @param displaySizeInInches The device primary display size, in inches.
* @param profile The current {@link Profile}.
* @param messageDispatcher The {@link MessageDispatcher} to enqueue the message.
* @param context The current context.
* @param tab The {@link Tab} where the message is shown.
* @return Whether the opt-in message was shown.
*/
public static boolean maybeShowGlobalSettingOptInMessage(double displaySizeInInches,
Profile profile, MessageDispatcher messageDispatcher, Context context, Tab tab) {
if (messageDispatcher == null) return false;

if (!shouldShowGlobalSettingOptInMessage(displaySizeInInches, profile)) {
return false;
}

Resources resources = context.getResources();
PropertyModel message =
new PropertyModel.Builder(MessageBannerProperties.ALL_KEYS)
.with(MessageBannerProperties.MESSAGE_IDENTIFIER,
MessageIdentifier.DESKTOP_SITE_GLOBAL_OPT_IN)
.with(MessageBannerProperties.TITLE,
resources.getString(R.string.rds_global_opt_in_message_title))
.with(MessageBannerProperties.ICON_RESOURCE_ID,
R.drawable.ic_desktop_windows)
.with(MessageBannerProperties.PRIMARY_BUTTON_TEXT,
resources.getString(R.string.yes))
.with(MessageBannerProperties.ON_PRIMARY_ACTION,
() -> {
WebsitePreferenceBridge.setCategoryEnabled(profile,
ContentSettingsType.REQUEST_DESKTOP_SITE, true);
// TODO(crbug.com/1350274): Remove this explicit load when this
// bug is addressed.
if (tab != null) {
tab.loadIfNeeded(
LoadIfNeededCaller
.MAYBE_SHOW_GLOBAL_SETTING_OPT_IN_MESSAGE);
}
return PrimaryActionClickBehavior.DISMISS_IMMEDIATELY;
})
.build();

messageDispatcher.enqueueWindowScopedMessage(message, false);
SharedPreferencesManager.getInstance().writeBoolean(
ChromePreferenceKeys.DESKTOP_SITE_GLOBAL_SETTING_OPT_IN_MESSAGE_SHOWN, true);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public class TabUtils {
@IntDef({LoadIfNeededCaller.SET_TAB, LoadIfNeededCaller.ON_ACTIVITY_SHOWN,
LoadIfNeededCaller.ON_ACTIVITY_SHOWN_THEN_SHOW, LoadIfNeededCaller.REQUEST_TO_SHOW_TAB,
LoadIfNeededCaller.REQUEST_TO_SHOW_TAB_THEN_SHOW,
LoadIfNeededCaller.ON_FINISH_NATIVE_INITIALIZATION, LoadIfNeededCaller.OTHER})
LoadIfNeededCaller.ON_FINISH_NATIVE_INITIALIZATION,
LoadIfNeededCaller.MAYBE_SHOW_GLOBAL_SETTING_OPT_IN_MESSAGE, LoadIfNeededCaller.OTHER})
@Retention(RetentionPolicy.SOURCE)
public @interface LoadIfNeededCaller {
int SET_TAB = 0;
Expand All @@ -75,7 +76,8 @@ public class TabUtils {
int REQUEST_TO_SHOW_TAB = 3;
int REQUEST_TO_SHOW_TAB_THEN_SHOW = 4;
int ON_FINISH_NATIVE_INITIALIZATION = 5;
int OTHER = 6;
int MAYBE_SHOW_GLOBAL_SETTING_OPT_IN_MESSAGE = 6;
int OTHER = 7;
}

// Do not instantiate this class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,12 @@ private void initializeIPH(boolean intentWithEffect) {
didTriggerPromo = triggerPromo(intentWithEffect);
}

if (!didTriggerPromo) {
didTriggerPromo = RequestDesktopUtils.maybeShowGlobalSettingOptInMessage(
getPrimaryDisplaySizeInInches(), Profile.getLastUsedRegularProfile(),
mMessageDispatcher, mActivity, mActivityTabProvider.get());
}

if (!didTriggerPromo) {
didTriggerPromo = RequestDesktopUtils.maybeShowDefaultEnableGlobalSettingMessage(
Profile.getLastUsedRegularProfile(), mMessageDispatcher, mActivity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ private void initIncognitoReauthController() {
/**
* @return The primary display size of the device, in inches.
*/
private double getPrimaryDisplaySizeInInches() {
protected double getPrimaryDisplaySizeInInches() {
DisplayAndroid display = DisplayAndroid.getNonMultiDisplay(mActivity);
double xInches = display.getDisplayWidth() / display.getXdpi();
double yInches = display.getDisplayHeight() / display.getYdpi();
Expand Down

0 comments on commit c0b23a1

Please sign in to comment.