OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.ntp.cards; | 5 package org.chromium.chrome.browser.ntp.cards; |
6 | 6 |
7 import android.support.annotation.IntDef; | 7 import android.support.annotation.IntDef; |
8 import android.view.View; | 8 import android.view.View; |
9 | 9 |
10 import org.chromium.base.VisibleForTesting; | 10 import org.chromium.base.VisibleForTesting; |
11 import org.chromium.chrome.R; | 11 import org.chromium.chrome.R; |
12 import org.chromium.chrome.browser.ntp.ContextMenuManager; | 12 import org.chromium.chrome.browser.ntp.ContextMenuManager; |
13 import org.chromium.chrome.browser.ntp.ContextMenuManager.ContextMenuItemId; | 13 import org.chromium.chrome.browser.ntp.ContextMenuManager.ContextMenuItemId; |
14 import org.chromium.chrome.browser.ntp.ContextMenuManager.Delegate; | 14 import org.chromium.chrome.browser.ntp.ContextMenuManager.Delegate; |
15 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; | 15 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; |
16 import org.chromium.chrome.browser.ntp.UiConfig; | 16 import org.chromium.chrome.browser.ntp.UiConfig; |
| 17 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; |
17 import org.chromium.chrome.browser.ntp.snippets.SnippetsConfig; | 18 import org.chromium.chrome.browser.ntp.snippets.SnippetsConfig; |
| 19 import org.chromium.chrome.browser.suggestions.SuggestionsRanker; |
18 | 20 |
19 import java.lang.annotation.Retention; | 21 import java.lang.annotation.Retention; |
20 import java.lang.annotation.RetentionPolicy; | 22 import java.lang.annotation.RetentionPolicy; |
21 | 23 |
22 /** | 24 /** |
23 * Item that allows the user to perform an action on the NTP. | 25 * Item that allows the user to perform an action on the NTP. |
24 * Note: Use {@link #refreshVisibility()} to update the visibility of the button
instead of calling | 26 * Note: Use {@link #refreshVisibility()} to update the visibility of the button
instead of calling |
25 * {@link #setVisible(boolean)} directly. | 27 * {@link #setVisible(boolean)} directly. |
26 */ | 28 */ |
27 class ActionItem extends OptionalLeaf { | 29 public class ActionItem extends OptionalLeaf { |
28 @IntDef({ACTION_NONE, ACTION_VIEW_ALL, ACTION_FETCH_MORE, ACTION_RELOAD}) | 30 @IntDef({ACTION_NONE, ACTION_VIEW_ALL, ACTION_FETCH_MORE, ACTION_RELOAD}) |
29 @Retention(RetentionPolicy.SOURCE) | 31 @Retention(RetentionPolicy.SOURCE) |
30 public @interface Action {} | 32 public @interface Action {} |
31 public static final int ACTION_NONE = 0; | 33 public static final int ACTION_NONE = 0; |
32 public static final int ACTION_VIEW_ALL = 1; | 34 public static final int ACTION_VIEW_ALL = 1; |
33 public static final int ACTION_FETCH_MORE = 2; | 35 public static final int ACTION_FETCH_MORE = 2; |
34 public static final int ACTION_RELOAD = 3; | 36 public static final int ACTION_RELOAD = 3; |
35 | 37 |
36 private final SuggestionsCategoryInfo mCategoryInfo; | 38 private final SuggestionsCategoryInfo mCategoryInfo; |
37 private final SuggestionsSection mParentSection; | 39 private final SuggestionsSection mParentSection; |
| 40 private final SuggestionsRanker mSuggestionsRanker; |
38 | 41 |
39 @Action | 42 @Action |
40 private int mCurrentAction = ACTION_NONE; | 43 private int mCurrentAction = ACTION_NONE; |
41 private boolean mImpressionTracked; | 44 private boolean mImpressionTracked; |
| 45 private int mPerSectionRank = -1; |
42 | 46 |
43 public ActionItem(SuggestionsSection section) { | 47 public ActionItem(SuggestionsSection section, SuggestionsRanker ranker) { |
44 mCategoryInfo = section.getCategoryInfo(); | 48 mCategoryInfo = section.getCategoryInfo(); |
45 mParentSection = section; | 49 mParentSection = section; |
| 50 mSuggestionsRanker = ranker; |
46 } | 51 } |
47 | 52 |
48 /** Call this instead of {@link #setVisible(boolean)} to update the visibili
ty. */ | 53 /** Call this instead of {@link #setVisible(boolean)} to update the visibili
ty. */ |
49 public void refreshVisibility() { | 54 public void refreshVisibility() { |
50 mCurrentAction = findAppropriateAction(); | 55 mCurrentAction = findAppropriateAction(); |
51 setVisible(mCurrentAction != ACTION_NONE); | 56 setVisible(mCurrentAction != ACTION_NONE); |
52 } | 57 } |
53 | 58 |
54 @Override | 59 @Override |
55 public int getItemViewType() { | 60 public int getItemViewType() { |
56 return ItemViewType.ACTION; | 61 return ItemViewType.ACTION; |
57 } | 62 } |
58 | 63 |
59 @Override | 64 @Override |
60 protected void onBindViewHolder(NewTabPageViewHolder holder) { | 65 protected void onBindViewHolder(NewTabPageViewHolder holder) { |
61 assert holder instanceof ViewHolder; | 66 assert holder instanceof ViewHolder; |
| 67 mSuggestionsRanker.rankActionItem(this, mParentSection); |
62 ((ViewHolder) holder).onBindViewHolder(this); | 68 ((ViewHolder) holder).onBindViewHolder(this); |
63 } | 69 } |
64 | 70 |
65 private int getPosition() { | 71 @CategoryInt |
66 // TODO(dgn): looks dodgy. Confirm that's what we want. | 72 public int getCategory() { |
67 return mParentSection.getSuggestionsCount(); | 73 return mCategoryInfo.getCategory(); |
| 74 } |
| 75 |
| 76 public void setPerSectionRank(int perSectionRank) { |
| 77 mPerSectionRank = perSectionRank; |
| 78 } |
| 79 |
| 80 public int getPerSectionRank() { |
| 81 return mPerSectionRank; |
68 } | 82 } |
69 | 83 |
70 @VisibleForTesting | 84 @VisibleForTesting |
71 void performAction(NewTabPageManager manager) { | 85 void performAction(NewTabPageManager manager) { |
72 manager.trackSnippetCategoryActionClick(mCategoryInfo.getCategory(), get
Position()); | 86 manager.getSuggestionsMetricsReporter().onMoreButtonClicked(this); |
73 | 87 |
74 switch (mCurrentAction) { | 88 switch (mCurrentAction) { |
75 case ACTION_VIEW_ALL: | 89 case ACTION_VIEW_ALL: |
76 mCategoryInfo.performViewAllAction(manager); | 90 mCategoryInfo.performViewAllAction(manager); |
77 return; | 91 return; |
78 case ACTION_FETCH_MORE: | 92 case ACTION_FETCH_MORE: |
79 case ACTION_RELOAD: | 93 case ACTION_RELOAD: |
80 manager.getSuggestionsSource().fetchSuggestions( | 94 manager.getSuggestionsSource().fetchSuggestions( |
81 mCategoryInfo.getCategory(), mParentSection.getDisplayed
SuggestionIds()); | 95 mCategoryInfo.getCategory(), mParentSection.getDisplayed
SuggestionIds()); |
82 mParentSection.onFetchStarted(); | 96 mParentSection.onFetchStarted(); |
83 return; | 97 return; |
84 case ACTION_NONE: | 98 case ACTION_NONE: |
85 default: | 99 default: |
86 // Should never be reached. | 100 // Should never be reached. |
87 assert false; | 101 assert false; |
88 } | 102 } |
89 } | 103 } |
90 | 104 |
91 @Action | 105 @Action |
92 private int findAppropriateAction() { | 106 private int findAppropriateAction() { |
93 boolean hasSuggestions = mParentSection.hasSuggestions(); | 107 boolean hasSuggestions = mParentSection.hasSuggestions(); |
94 if (mCategoryInfo.hasViewAllAction()) return ACTION_VIEW_ALL; | 108 if (mCategoryInfo.hasViewAllAction()) return ACTION_VIEW_ALL; |
95 if (hasSuggestions && mCategoryInfo.hasFetchMoreAction()) return ACTION_
FETCH_MORE; | 109 if (hasSuggestions && mCategoryInfo.hasFetchMoreAction()) return ACTION_
FETCH_MORE; |
96 if (!hasSuggestions && mCategoryInfo.hasReloadAction()) return ACTION_RE
LOAD; | 110 if (!hasSuggestions && mCategoryInfo.hasReloadAction()) return ACTION_RE
LOAD; |
97 return ACTION_NONE; | 111 return ACTION_NONE; |
98 } | 112 } |
99 | 113 |
| 114 /** ViewHolder associated to {@link ItemViewType#ACTION}. */ |
100 public static class ViewHolder extends CardViewHolder implements ContextMenu
Manager.Delegate { | 115 public static class ViewHolder extends CardViewHolder implements ContextMenu
Manager.Delegate { |
101 private ActionItem mActionListItem; | 116 private ActionItem mActionListItem; |
102 | 117 |
103 public ViewHolder(final NewTabPageRecyclerView recyclerView, | 118 public ViewHolder(final NewTabPageRecyclerView recyclerView, |
104 final NewTabPageManager manager, UiConfig uiConfig) { | 119 final NewTabPageManager manager, UiConfig uiConfig) { |
105 super(R.layout.new_tab_page_action_card, recyclerView, uiConfig, man
ager); | 120 super(R.layout.new_tab_page_action_card, recyclerView, uiConfig, man
ager); |
106 | 121 |
107 itemView.findViewById(R.id.action_button) | 122 itemView.findViewById(R.id.action_button) |
108 .setOnClickListener(new View.OnClickListener() { | 123 .setOnClickListener(new View.OnClickListener() { |
109 @Override | 124 @Override |
110 public void onClick(View v) { | 125 public void onClick(View v) { |
111 mActionListItem.performAction(manager); | 126 mActionListItem.performAction(manager); |
112 } | 127 } |
113 }); | 128 }); |
114 | 129 |
115 new ImpressionTracker(itemView, new ImpressionTracker.Listener() { | 130 new ImpressionTracker(itemView, new ImpressionTracker.Listener() { |
116 @Override | 131 @Override |
117 public void onImpression() { | 132 public void onImpression() { |
118 if (mActionListItem != null && !mActionListItem.mImpressionT
racked) { | 133 if (mActionListItem != null && !mActionListItem.mImpressionT
racked) { |
119 mActionListItem.mImpressionTracked = true; | 134 mActionListItem.mImpressionTracked = true; |
120 manager.trackSnippetCategoryActionImpression( | 135 manager.getSuggestionsMetricsReporter().onMoreButtonShow
n(mActionListItem); |
121 mActionListItem.mCategoryInfo.getCategory(), | |
122 mActionListItem.getPosition()); | |
123 } | 136 } |
124 } | 137 } |
125 }); | 138 }); |
126 } | 139 } |
127 | 140 |
128 @Override | 141 @Override |
129 public boolean isDismissable() { | 142 public boolean isDismissable() { |
130 return SnippetsConfig.isSectionDismissalEnabled() | 143 return SnippetsConfig.isSectionDismissalEnabled() |
131 && !mActionListItem.mParentSection.hasSuggestions(); | 144 && !mActionListItem.mParentSection.hasSuggestions(); |
132 } | 145 } |
(...skipping 25 matching lines...) Expand all Loading... |
158 | 171 |
159 @Override | 172 @Override |
160 public void onContextMenuCreated() {} | 173 public void onContextMenuCreated() {} |
161 | 174 |
162 public void onBindViewHolder(ActionItem item) { | 175 public void onBindViewHolder(ActionItem item) { |
163 super.onBindViewHolder(); | 176 super.onBindViewHolder(); |
164 mActionListItem = item; | 177 mActionListItem = item; |
165 } | 178 } |
166 } | 179 } |
167 } | 180 } |
OLD | NEW |