1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.chrome.browser.suggestions;
6 
7 import android.content.res.Resources;
8 import android.text.TextUtils;
9 
10 import androidx.annotation.IntDef;
11 
12 import org.chromium.base.ApiCompatibilityUtils;
13 import org.chromium.chrome.R;
14 import org.chromium.chrome.browser.flags.ChromeFeatureList;
15 import org.chromium.chrome.browser.util.ChromeAccessibilityUtil;
16 import org.chromium.components.browser_ui.widget.displaystyle.UiConfig;
17 
18 import java.lang.annotation.Retention;
19 import java.lang.annotation.RetentionPolicy;
20 
21 /**
22  * Provides configuration details for suggestions.
23  */
24 public final class SuggestionsConfig {
25     @IntDef({TileStyle.MODERN, TileStyle.MODERN_CONDENSED})
26     @Retention(RetentionPolicy.SOURCE)
27     public @interface TileStyle {
28         int MODERN = 1;
29         int MODERN_CONDENSED = 2;
30     }
31 
32     /**
33      * Field trial parameter for referrer URL.
34      * It must be kept in sync with //components/ntp_suggestions/features.cc
35      */
36     private static final String REFERRER_URL_PARAM = "referrer_url";
37 
38     /**
39      * Default value of referrer URL for content suggestions.
40      * It must be kept in sync with //components/ntp_suggestions/features.cc
41      */
42     private static final String DEFAULT_CONTENT_SUGGESTIONS_REFERRER_URL =
43             "https://www.googleapis.com/auth/chrome-content-suggestions";
44 
SuggestionsConfig()45     private SuggestionsConfig() {}
46 
47     /**
48      * @return Whether scrolling to the bottom of suggestions triggers a load.
49      */
scrollToLoad()50     public static boolean scrollToLoad() {
51         // The scroll to load feature does not work well for users who require accessibility mode.
52         if (ChromeAccessibilityUtil.get().isAccessibilityEnabled()) return false;
53 
54         return ChromeFeatureList.isEnabled(ChromeFeatureList.CONTENT_SUGGESTIONS_SCROLL_TO_LOAD);
55     }
56 
57     /**
58      * @param resources The resources to fetch the color from.
59      * @return The background color for the suggestions sheet content.
60      */
getBackgroundColor(Resources resources)61     public static int getBackgroundColor(Resources resources) {
62         return ApiCompatibilityUtils.getColor(resources, R.color.suggestions_modern_bg);
63     }
64 
65     /**
66      * Returns the current tile style, that depends on the enabled features and the screen size.
67      */
68     @TileStyle
getTileStyle(UiConfig uiConfig)69     public static int getTileStyle(UiConfig uiConfig) {
70         return uiConfig.getCurrentDisplayStyle().isSmall() ? TileStyle.MODERN_CONDENSED
71                                                            : TileStyle.MODERN;
72     }
73 
useCondensedTileLayout(boolean isScreenSmall)74     private static boolean useCondensedTileLayout(boolean isScreenSmall) {
75         if (isScreenSmall) return true;
76 
77         return false;
78     }
79 
80     /**
81      * @param featureName The feature from {@link ChromeFeatureList}, which provides the referrer
82      *                    URL parameter.
83      * @return The value of referrer URL to use with content suggestions.
84      */
getReferrerUrl(String featureName)85     public static String getReferrerUrl(String featureName) {
86         assert ChromeFeatureList.INTEREST_FEED_CONTENT_SUGGESTIONS.equals(featureName)
87                 || ChromeFeatureList.INTEREST_FEED_V2.equals(featureName);
88 
89         return getReferrerUrlParamOrDefault(featureName, DEFAULT_CONTENT_SUGGESTIONS_REFERRER_URL);
90     }
91 
getReferrerUrlParamOrDefault(String featureName, String defaultValue)92     private static String getReferrerUrlParamOrDefault(String featureName, String defaultValue) {
93         String referrerParamValue =
94                 ChromeFeatureList.getFieldTrialParamByFeature(featureName, REFERRER_URL_PARAM);
95 
96         if (!TextUtils.isEmpty(referrerParamValue)) {
97             return referrerParamValue;
98         }
99 
100         return defaultValue;
101     }
102 }
103