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