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.snippets; | 5 package org.chromium.chrome.browser.ntp.snippets; |
6 | 6 |
7 import android.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
8 | 8 |
9 import org.chromium.base.Callback; | 9 import org.chromium.base.Callback; |
10 import org.chromium.base.annotations.CalledByNative; | 10 import org.chromium.base.annotations.CalledByNative; |
| 11 import org.chromium.chrome.browser.ntp.NewTabPageUma; |
| 12 import org.chromium.chrome.browser.ntp.cards.ActionItem; |
11 import org.chromium.chrome.browser.ntp.cards.SuggestionsCategoryInfo; | 13 import org.chromium.chrome.browser.ntp.cards.SuggestionsCategoryInfo; |
12 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus.CategoryStatusEnu
m; | 14 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus.CategoryStatusEnu
m; |
13 import org.chromium.chrome.browser.profiles.Profile; | 15 import org.chromium.chrome.browser.profiles.Profile; |
| 16 import org.chromium.chrome.browser.suggestions.SuggestionsMetricsReporter; |
14 | 17 |
15 import java.util.ArrayList; | 18 import java.util.ArrayList; |
16 import java.util.List; | 19 import java.util.List; |
17 | 20 |
18 /** | 21 /** |
19 * Provides access to the snippets to display on the NTP using the C++ ContentSu
ggestionsService. | 22 * Provides access to the snippets to display on the NTP using the C++ ContentSu
ggestionsService. |
20 */ | 23 */ |
21 public class SnippetsBridge implements SuggestionsSource { | 24 public class SnippetsBridge implements SuggestionsSource, SuggestionsMetricsRepo
rter { |
22 private static final String TAG = "SnippetsBridge"; | 25 private static final String TAG = "SnippetsBridge"; |
23 | 26 |
24 private long mNativeSnippetsBridge; | 27 private long mNativeSnippetsBridge; |
25 private SuggestionsSource.Observer mObserver; | 28 private SuggestionsSource.Observer mObserver; |
26 | 29 |
27 public static boolean isCategoryStatusAvailable(@CategoryStatusEnum int stat
us) { | 30 public static boolean isCategoryStatusAvailable(@CategoryStatusEnum int stat
us) { |
28 // Note: This code is duplicated in content_suggestions_category_status.
cc. | 31 // Note: This code is duplicated in content_suggestions_category_status.
cc. |
29 return status == CategoryStatus.AVAILABLE_LOADING || status == CategoryS
tatus.AVAILABLE; | 32 return status == CategoryStatus.AVAILABLE_LOADING || status == CategoryS
tatus.AVAILABLE; |
30 } | 33 } |
31 | 34 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 @Override | 115 @Override |
113 public void fetchSuggestionImage(SnippetArticle suggestion, Callback<Bitmap>
callback) { | 116 public void fetchSuggestionImage(SnippetArticle suggestion, Callback<Bitmap>
callback) { |
114 assert mNativeSnippetsBridge != 0; | 117 assert mNativeSnippetsBridge != 0; |
115 nativeFetchSuggestionImage(mNativeSnippetsBridge, suggestion.mCategory, | 118 nativeFetchSuggestionImage(mNativeSnippetsBridge, suggestion.mCategory, |
116 suggestion.mIdWithinCategory, callback); | 119 suggestion.mIdWithinCategory, callback); |
117 } | 120 } |
118 | 121 |
119 @Override | 122 @Override |
120 public void dismissSuggestion(SnippetArticle suggestion) { | 123 public void dismissSuggestion(SnippetArticle suggestion) { |
121 assert mNativeSnippetsBridge != 0; | 124 assert mNativeSnippetsBridge != 0; |
122 nativeDismissSuggestion(mNativeSnippetsBridge, suggestion.mUrl, suggesti
on.mGlobalPosition, | 125 nativeDismissSuggestion(mNativeSnippetsBridge, suggestion.mUrl, suggesti
on.getGlobalRank(), |
123 suggestion.mCategory, suggestion.mPosition, suggestion.mIdWithin
Category); | 126 suggestion.mCategory, suggestion.getPerSectionRank(), suggestion
.mIdWithinCategory); |
124 } | 127 } |
125 | 128 |
126 @Override | 129 @Override |
127 public void dismissCategory(@CategoryInt int category) { | 130 public void dismissCategory(@CategoryInt int category) { |
128 assert mNativeSnippetsBridge != 0; | 131 assert mNativeSnippetsBridge != 0; |
129 nativeDismissCategory(mNativeSnippetsBridge, category); | 132 nativeDismissCategory(mNativeSnippetsBridge, category); |
130 } | 133 } |
131 | 134 |
132 @Override | 135 @Override |
133 public void restoreDismissedCategories() { | 136 public void restoreDismissedCategories() { |
134 assert mNativeSnippetsBridge != 0; | 137 assert mNativeSnippetsBridge != 0; |
135 nativeRestoreDismissedCategories(mNativeSnippetsBridge); | 138 nativeRestoreDismissedCategories(mNativeSnippetsBridge); |
136 } | 139 } |
137 | 140 |
| 141 @Override |
138 public void onPageShown(int[] categories, int[] suggestionsPerCategory) { | 142 public void onPageShown(int[] categories, int[] suggestionsPerCategory) { |
139 assert mNativeSnippetsBridge != 0; | 143 assert mNativeSnippetsBridge != 0; |
140 nativeOnPageShown(mNativeSnippetsBridge, categories, suggestionsPerCateg
ory); | 144 nativeOnPageShown(mNativeSnippetsBridge, categories, suggestionsPerCateg
ory); |
141 } | 145 } |
142 | 146 |
| 147 @Override |
143 public void onSuggestionShown(SnippetArticle suggestion) { | 148 public void onSuggestionShown(SnippetArticle suggestion) { |
144 assert mNativeSnippetsBridge != 0; | 149 assert mNativeSnippetsBridge != 0; |
145 nativeOnSuggestionShown(mNativeSnippetsBridge, suggestion.mGlobalPositio
n, | 150 nativeOnSuggestionShown(mNativeSnippetsBridge, suggestion.getGlobalRank(
), |
146 suggestion.mCategory, suggestion.mPosition, | 151 suggestion.mCategory, suggestion.getPerSectionRank(), |
147 suggestion.mPublishTimestampMilliseconds, suggestion.mScore); | 152 suggestion.mPublishTimestampMilliseconds, suggestion.mScore); |
148 } | 153 } |
149 | 154 |
| 155 @Override |
150 public void onSuggestionOpened(SnippetArticle suggestion, int windowOpenDisp
osition) { | 156 public void onSuggestionOpened(SnippetArticle suggestion, int windowOpenDisp
osition) { |
151 assert mNativeSnippetsBridge != 0; | 157 assert mNativeSnippetsBridge != 0; |
152 nativeOnSuggestionOpened(mNativeSnippetsBridge, suggestion.mGlobalPositi
on, | 158 nativeOnSuggestionOpened(mNativeSnippetsBridge, suggestion.getGlobalRank
(), |
153 suggestion.mCategory, suggestion.mPosition, | 159 suggestion.mCategory, suggestion.getPerSectionRank(), |
154 suggestion.mPublishTimestampMilliseconds, suggestion.mScore, win
dowOpenDisposition); | 160 suggestion.mPublishTimestampMilliseconds, suggestion.mScore, win
dowOpenDisposition); |
155 } | 161 } |
156 | 162 |
| 163 @Override |
157 public void onSuggestionMenuOpened(SnippetArticle suggestion) { | 164 public void onSuggestionMenuOpened(SnippetArticle suggestion) { |
158 assert mNativeSnippetsBridge != 0; | 165 assert mNativeSnippetsBridge != 0; |
159 nativeOnSuggestionMenuOpened(mNativeSnippetsBridge, suggestion.mGlobalPo
sition, | 166 nativeOnSuggestionMenuOpened(mNativeSnippetsBridge, suggestion.getGlobal
Rank(), |
160 suggestion.mCategory, suggestion.mPosition, | 167 suggestion.mCategory, suggestion.getPerSectionRank(), |
161 suggestion.mPublishTimestampMilliseconds, suggestion.mScore); | 168 suggestion.mPublishTimestampMilliseconds, suggestion.mScore); |
162 } | 169 } |
163 | 170 |
164 public void onMoreButtonShown(int category, int position) { | 171 @Override |
| 172 public void onMoreButtonShown(ActionItem actionItem) { |
165 assert mNativeSnippetsBridge != 0; | 173 assert mNativeSnippetsBridge != 0; |
166 nativeOnMoreButtonShown(mNativeSnippetsBridge, category, position); | 174 nativeOnMoreButtonShown( |
| 175 mNativeSnippetsBridge, actionItem.getCategory(), actionItem.getP
erSectionRank()); |
167 } | 176 } |
168 | 177 |
169 public void onMoreButtonClicked(int category, int position) { | 178 @Override |
| 179 public void onMoreButtonClicked(ActionItem actionItem) { |
170 assert mNativeSnippetsBridge != 0; | 180 assert mNativeSnippetsBridge != 0; |
171 nativeOnMoreButtonClicked(mNativeSnippetsBridge, category, position); | 181 @CategoryInt |
| 182 int category = actionItem.getCategory(); |
| 183 nativeOnMoreButtonClicked(mNativeSnippetsBridge, category, actionItem.ge
tPerSectionRank()); |
| 184 switch (category) { |
| 185 case KnownCategories.BOOKMARKS: |
| 186 NewTabPageUma.recordAction(NewTabPageUma.ACTION_OPENED_BOOKMARKS
_MANAGER); |
| 187 break; |
| 188 // MORE button in both categories leads to the recent tabs manager |
| 189 case KnownCategories.FOREIGN_TABS: |
| 190 case KnownCategories.RECENT_TABS: |
| 191 NewTabPageUma.recordAction(NewTabPageUma.ACTION_OPENED_RECENT_TA
BS_MANAGER); |
| 192 break; |
| 193 case KnownCategories.DOWNLOADS: |
| 194 NewTabPageUma.recordAction(NewTabPageUma.ACTION_OPENED_DOWNLOADS
_MANAGER); |
| 195 break; |
| 196 default: |
| 197 // No action associated |
| 198 break; |
| 199 } |
172 } | 200 } |
173 | 201 |
174 /** | 202 /** |
175 * Notifies the scheduler to adjust the plan due to a newly opened NTP. | 203 * Notifies the scheduler to adjust the plan due to a newly opened NTP. |
176 */ | 204 */ |
177 public void onNtpInitialized() { | 205 public void onNtpInitialized() { |
178 assert mNativeSnippetsBridge != 0; | 206 assert mNativeSnippetsBridge != 0; |
179 nativeOnNTPInitialized(mNativeSnippetsBridge); | 207 nativeOnNTPInitialized(mNativeSnippetsBridge); |
180 } | 208 } |
181 | 209 |
182 public static void notifySchedulerAboutWarmResume() { | 210 public static void notifySchedulerAboutWarmResume() { |
183 SnippetsBridge snippetsBridge = new SnippetsBridge(Profile.getLastUsedPr
ofile()); | 211 SnippetsBridge snippetsBridge = new SnippetsBridge(Profile.getLastUsedPr
ofile()); |
184 snippetsBridge.onActivityWarmResumed(); | 212 snippetsBridge.onActivityWarmResumed(); |
185 } | 213 } |
186 | 214 |
187 public static void notifySchedulerAboutColdStart() { | 215 public static void notifySchedulerAboutColdStart() { |
188 SnippetsBridge snippetsBridge = new SnippetsBridge(Profile.getLastUsedPr
ofile()); | 216 SnippetsBridge snippetsBridge = new SnippetsBridge(Profile.getLastUsedPr
ofile()); |
189 snippetsBridge.onColdStart(); | 217 snippetsBridge.onColdStart(); |
190 } | 218 } |
191 | 219 |
192 public static void onSuggestionTargetVisited(int category, long visitTimeMs)
{ | 220 public static void onSuggestionTargetVisited(int category, long visitTimeMs)
{ |
193 nativeOnSuggestionTargetVisited(category, visitTimeMs); | 221 nativeOnSuggestionTargetVisited(category, visitTimeMs); |
194 } | 222 } |
195 | 223 |
196 /** | |
197 * Sets the recipient for the fetched snippets. | |
198 * | |
199 * An observer needs to be set before the native code attempts to transmit s
nippets them to | |
200 * java. Upon registration, the observer will be notified of already fetched
snippets. | |
201 * | |
202 * @param observer object to notify when snippets are received. | |
203 */ | |
204 @Override | 224 @Override |
205 public void setObserver(SuggestionsSource.Observer observer) { | 225 public void setObserver(Observer observer) { |
206 assert observer != null; | 226 assert observer != null; |
207 mObserver = observer; | 227 mObserver = observer; |
208 } | 228 } |
209 | 229 |
210 @Override | 230 @Override |
211 public void fetchSuggestions(@CategoryInt int category, String[] displayedSu
ggestionIds) { | 231 public void fetchSuggestions(@CategoryInt int category, String[] displayedSu
ggestionIds) { |
212 nativeFetch(mNativeSnippetsBridge, category, displayedSuggestionIds); | 232 nativeFetch(mNativeSnippetsBridge, category, displayedSuggestionIds); |
213 } | 233 } |
214 | 234 |
215 private void onActivityWarmResumed() { | 235 private void onActivityWarmResumed() { |
(...skipping 10 matching lines...) Expand all Loading... |
226 private static List<SnippetArticle> createSuggestionList() { | 246 private static List<SnippetArticle> createSuggestionList() { |
227 return new ArrayList<>(); | 247 return new ArrayList<>(); |
228 } | 248 } |
229 | 249 |
230 @CalledByNative | 250 @CalledByNative |
231 private static SnippetArticle addSuggestion(List<SnippetArticle> suggestions
, int category, | 251 private static SnippetArticle addSuggestion(List<SnippetArticle> suggestions
, int category, |
232 String id, String title, String publisher, String previewText, Strin
g url, | 252 String id, String title, String publisher, String previewText, Strin
g url, |
233 long timestamp, float score) { | 253 long timestamp, float score) { |
234 int position = suggestions.size(); | 254 int position = suggestions.size(); |
235 suggestions.add(new SnippetArticle( | 255 suggestions.add(new SnippetArticle( |
236 category, id, title, publisher, previewText, url, timestamp, sco
re, position)); | 256 category, id, title, publisher, previewText, url, timestamp, sco
re)); |
237 return suggestions.get(position); | 257 return suggestions.get(position); |
238 } | 258 } |
239 | 259 |
240 @CalledByNative | 260 @CalledByNative |
241 private static void setAssetDownloadDataForSuggestion( | 261 private static void setAssetDownloadDataForSuggestion( |
242 SnippetArticle suggestion, String filePath, String mimeType) { | 262 SnippetArticle suggestion, String filePath, String mimeType) { |
243 suggestion.setAssetDownloadData(filePath, mimeType); | 263 suggestion.setAssetDownloadData(filePath, mimeType); |
244 } | 264 } |
245 | 265 |
246 @CalledByNative | 266 @CalledByNative |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 float score); | 340 float score); |
321 private native void nativeOnMoreButtonShown( | 341 private native void nativeOnMoreButtonShown( |
322 long nativeNTPSnippetsBridge, int category, int position); | 342 long nativeNTPSnippetsBridge, int category, int position); |
323 private native void nativeOnMoreButtonClicked( | 343 private native void nativeOnMoreButtonClicked( |
324 long nativeNTPSnippetsBridge, int category, int position); | 344 long nativeNTPSnippetsBridge, int category, int position); |
325 private native void nativeOnActivityWarmResumed(long nativeNTPSnippetsBridge
); | 345 private native void nativeOnActivityWarmResumed(long nativeNTPSnippetsBridge
); |
326 private native void nativeOnColdStart(long nativeNTPSnippetsBridge); | 346 private native void nativeOnColdStart(long nativeNTPSnippetsBridge); |
327 private static native void nativeOnSuggestionTargetVisited(int category, lon
g visitTimeMs); | 347 private static native void nativeOnSuggestionTargetVisited(int category, lon
g visitTimeMs); |
328 private static native void nativeOnNTPInitialized(long nativeNTPSnippetsBrid
ge); | 348 private static native void nativeOnNTPInitialized(long nativeNTPSnippetsBrid
ge); |
329 } | 349 } |
OLD | NEW |