Chromium Code Reviews| 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.cards.SuggestionsCategoryInfo; | 11 import org.chromium.chrome.browser.ntp.cards.SuggestionsCategoryInfo; |
| 12 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus.CategoryStatusEnu m; | 12 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus.CategoryStatusEnu m; |
| 13 import org.chromium.chrome.browser.profiles.Profile; | 13 import org.chromium.chrome.browser.profiles.Profile; |
| 14 import org.chromium.chrome.browser.suggestions.SuggestionsUma; | |
| 14 | 15 |
| 15 import java.util.ArrayList; | 16 import java.util.ArrayList; |
| 16 import java.util.List; | 17 import java.util.List; |
| 17 | 18 |
| 18 /** | 19 /** |
| 19 * Provides access to the snippets to display on the NTP using the C++ ContentSu ggestionsService. | 20 * Provides access to the snippets to display on the NTP using the C++ ContentSu ggestionsService. |
| 20 */ | 21 */ |
| 21 public class SnippetsBridge implements SuggestionsSource { | 22 public class SnippetsBridge implements SuggestionsSource { |
| 22 private static final String TAG = "SnippetsBridge"; | 23 private static final String TAG = "SnippetsBridge"; |
| 23 | 24 |
| 24 private long mNativeSnippetsBridge; | 25 private long mNativeSnippetsBridge; |
| 25 private SuggestionsSource.Observer mObserver; | 26 private SuggestionsSource.Observer mObserver; |
| 27 private SuggestionsUma mUmaHelper; | |
|
Bernhard Bauer
2017/01/11 13:53:38
I think I would make this an interface and have Se
dgn
2017/01/12 14:29:58
Done.
| |
| 26 | 28 |
| 27 public static boolean isCategoryStatusAvailable(@CategoryStatusEnum int stat us) { | 29 public static boolean isCategoryStatusAvailable(@CategoryStatusEnum int stat us) { |
| 28 // Note: This code is duplicated in content_suggestions_category_status. cc. | 30 // Note: This code is duplicated in content_suggestions_category_status. cc. |
| 29 return status == CategoryStatus.AVAILABLE_LOADING || status == CategoryS tatus.AVAILABLE; | 31 return status == CategoryStatus.AVAILABLE_LOADING || status == CategoryS tatus.AVAILABLE; |
| 30 } | 32 } |
| 31 | 33 |
| 32 /** Returns whether the category is considered "enabled", and can show conte nt suggestions. */ | 34 /** Returns whether the category is considered "enabled", and can show conte nt suggestions. */ |
| 33 public static boolean isCategoryEnabled(@CategoryStatusEnum int status) { | 35 public static boolean isCategoryEnabled(@CategoryStatusEnum int status) { |
| 34 switch (status) { | 36 switch (status) { |
| 35 case CategoryStatus.INITIALIZING: | 37 case CategoryStatus.INITIALIZING: |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 @Override | 114 @Override |
| 113 public void fetchSuggestionImage(SnippetArticle suggestion, Callback<Bitmap> callback) { | 115 public void fetchSuggestionImage(SnippetArticle suggestion, Callback<Bitmap> callback) { |
| 114 assert mNativeSnippetsBridge != 0; | 116 assert mNativeSnippetsBridge != 0; |
| 115 nativeFetchSuggestionImage(mNativeSnippetsBridge, suggestion.mCategory, | 117 nativeFetchSuggestionImage(mNativeSnippetsBridge, suggestion.mCategory, |
| 116 suggestion.mIdWithinCategory, callback); | 118 suggestion.mIdWithinCategory, callback); |
| 117 } | 119 } |
| 118 | 120 |
| 119 @Override | 121 @Override |
| 120 public void dismissSuggestion(SnippetArticle suggestion) { | 122 public void dismissSuggestion(SnippetArticle suggestion) { |
| 121 assert mNativeSnippetsBridge != 0; | 123 assert mNativeSnippetsBridge != 0; |
| 122 nativeDismissSuggestion(mNativeSnippetsBridge, suggestion.mUrl, suggesti on.mGlobalPosition, | 124 int suggestionRank = mUmaHelper.getSuggestionRank(suggestion); |
|
Bernhard Bauer
2017/01/11 13:53:38
If getSuggestionRank returns -1 when the suggestio
dgn
2017/01/12 14:29:58
Done.
| |
| 123 suggestion.mCategory, suggestion.mPosition, suggestion.mIdWithin Category); | 125 int suggestionGlobalRank = mUmaHelper.getSuggestionGlobalRank(suggestion , suggestionRank); |
| 126 nativeDismissSuggestion(mNativeSnippetsBridge, suggestion.mUrl, suggesti onGlobalRank, | |
| 127 suggestion.mCategory, suggestionRank, suggestion.mIdWithinCatego ry); | |
| 124 } | 128 } |
| 125 | 129 |
| 126 @Override | 130 @Override |
| 127 public void dismissCategory(@CategoryInt int category) { | 131 public void dismissCategory(@CategoryInt int category) { |
| 128 assert mNativeSnippetsBridge != 0; | 132 assert mNativeSnippetsBridge != 0; |
| 129 nativeDismissCategory(mNativeSnippetsBridge, category); | 133 nativeDismissCategory(mNativeSnippetsBridge, category); |
| 130 } | 134 } |
| 131 | 135 |
| 132 @Override | 136 @Override |
| 133 public void restoreDismissedCategories() { | 137 public void restoreDismissedCategories() { |
| 134 assert mNativeSnippetsBridge != 0; | 138 assert mNativeSnippetsBridge != 0; |
| 135 nativeRestoreDismissedCategories(mNativeSnippetsBridge); | 139 nativeRestoreDismissedCategories(mNativeSnippetsBridge); |
| 136 } | 140 } |
| 137 | 141 |
| 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 |
| 143 public void onSuggestionShown(SnippetArticle suggestion) { | 147 public void onSuggestionShown(SnippetArticle suggestion) { |
| 144 assert mNativeSnippetsBridge != 0; | 148 assert mNativeSnippetsBridge != 0; |
| 145 nativeOnSuggestionShown(mNativeSnippetsBridge, suggestion.mGlobalPositio n, | 149 int suggestionRank = mUmaHelper.getSuggestionRank(suggestion); |
| 146 suggestion.mCategory, suggestion.mPosition, | 150 int suggestionGlobalRank = mUmaHelper.getSuggestionGlobalRank(suggestion , suggestionRank); |
| 147 suggestion.mPublishTimestampMilliseconds, suggestion.mScore); | 151 nativeOnSuggestionShown(mNativeSnippetsBridge, suggestionGlobalRank, sug gestion.mCategory, |
| 152 suggestionRank, suggestion.mPublishTimestampMilliseconds, sugges tion.mScore); | |
| 148 } | 153 } |
| 149 | 154 |
| 150 public void onSuggestionOpened(SnippetArticle suggestion, int windowOpenDisp osition) { | 155 public void onSuggestionOpened(SnippetArticle suggestion, int windowOpenDisp osition) { |
| 151 assert mNativeSnippetsBridge != 0; | 156 assert mNativeSnippetsBridge != 0; |
| 152 nativeOnSuggestionOpened(mNativeSnippetsBridge, suggestion.mGlobalPositi on, | 157 int suggestionRank = mUmaHelper.getSuggestionRank(suggestion); |
| 153 suggestion.mCategory, suggestion.mPosition, | 158 int suggestionGlobalRank = mUmaHelper.getSuggestionGlobalRank(suggestion , suggestionRank); |
| 154 suggestion.mPublishTimestampMilliseconds, suggestion.mScore, win dowOpenDisposition); | 159 nativeOnSuggestionOpened(mNativeSnippetsBridge, suggestionGlobalRank, su ggestion.mCategory, |
| 160 suggestionRank, suggestion.mPublishTimestampMilliseconds, sugges tion.mScore, | |
| 161 windowOpenDisposition); | |
| 155 } | 162 } |
| 156 | 163 |
| 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 int suggestionRank = mUmaHelper.getSuggestionRank(suggestion); |
| 160 suggestion.mCategory, suggestion.mPosition, | 167 int suggestionGlobalRank = mUmaHelper.getSuggestionGlobalRank(suggestion , suggestionRank); |
| 161 suggestion.mPublishTimestampMilliseconds, suggestion.mScore); | 168 nativeOnSuggestionMenuOpened(mNativeSnippetsBridge, suggestionGlobalRank , |
| 169 suggestion.mCategory, suggestionRank, suggestion.mPublishTimesta mpMilliseconds, | |
| 170 suggestion.mScore); | |
| 162 } | 171 } |
| 163 | 172 |
| 164 public void onMoreButtonShown(int category, int position) { | 173 public void onMoreButtonShown(int category, int position) { |
| 165 assert mNativeSnippetsBridge != 0; | 174 assert mNativeSnippetsBridge != 0; |
| 166 nativeOnMoreButtonShown(mNativeSnippetsBridge, category, position); | 175 nativeOnMoreButtonShown(mNativeSnippetsBridge, category, position); |
| 167 } | 176 } |
| 168 | 177 |
| 169 public void onMoreButtonClicked(int category, int position) { | 178 public void onMoreButtonClicked(int category, int position) { |
| 170 assert mNativeSnippetsBridge != 0; | 179 assert mNativeSnippetsBridge != 0; |
| 171 nativeOnMoreButtonClicked(mNativeSnippetsBridge, category, position); | 180 nativeOnMoreButtonClicked(mNativeSnippetsBridge, category, position); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 186 | 195 |
| 187 public static void notifySchedulerAboutColdStart() { | 196 public static void notifySchedulerAboutColdStart() { |
| 188 SnippetsBridge snippetsBridge = new SnippetsBridge(Profile.getLastUsedPr ofile()); | 197 SnippetsBridge snippetsBridge = new SnippetsBridge(Profile.getLastUsedPr ofile()); |
| 189 snippetsBridge.onColdStart(); | 198 snippetsBridge.onColdStart(); |
| 190 } | 199 } |
| 191 | 200 |
| 192 public static void onSuggestionTargetVisited(int category, long visitTimeMs) { | 201 public static void onSuggestionTargetVisited(int category, long visitTimeMs) { |
| 193 nativeOnSuggestionTargetVisited(category, visitTimeMs); | 202 nativeOnSuggestionTargetVisited(category, visitTimeMs); |
| 194 } | 203 } |
| 195 | 204 |
| 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 | 205 @Override |
| 205 public void setObserver(SuggestionsSource.Observer observer) { | 206 public void init(SuggestionsSource.Observer observer, SuggestionsUma umaHelp er) { |
| 206 assert observer != null; | 207 assert observer != null; |
| 207 mObserver = observer; | 208 mObserver = observer; |
| 209 | |
| 210 assert mUmaHelper == null; | |
|
Bernhard Bauer
2017/01/11 13:53:38
TBH, I kind of like keeping simple set methods tha
dgn
2017/01/12 14:29:58
Done.
| |
| 211 mUmaHelper = umaHelper; | |
| 208 } | 212 } |
| 209 | 213 |
| 210 @Override | 214 @Override |
| 211 public void fetchSuggestions(@CategoryInt int category, String[] displayedSu ggestionIds) { | 215 public void fetchSuggestions(@CategoryInt int category, String[] displayedSu ggestionIds) { |
| 212 nativeFetch(mNativeSnippetsBridge, category, displayedSuggestionIds); | 216 nativeFetch(mNativeSnippetsBridge, category, displayedSuggestionIds); |
| 213 } | 217 } |
| 214 | 218 |
| 215 private void onActivityWarmResumed() { | 219 private void onActivityWarmResumed() { |
| 216 assert mNativeSnippetsBridge != 0; | 220 assert mNativeSnippetsBridge != 0; |
| 217 nativeOnActivityWarmResumed(mNativeSnippetsBridge); | 221 nativeOnActivityWarmResumed(mNativeSnippetsBridge); |
| 218 } | 222 } |
| 219 | 223 |
| 220 private void onColdStart() { | 224 private void onColdStart() { |
| 221 assert mNativeSnippetsBridge != 0; | 225 assert mNativeSnippetsBridge != 0; |
| 222 nativeOnColdStart(mNativeSnippetsBridge); | 226 nativeOnColdStart(mNativeSnippetsBridge); |
| 223 } | 227 } |
| 224 | 228 |
| 225 @CalledByNative | 229 @CalledByNative |
| 226 private static List<SnippetArticle> createSuggestionList() { | 230 private static List<SnippetArticle> createSuggestionList() { |
| 227 return new ArrayList<>(); | 231 return new ArrayList<>(); |
| 228 } | 232 } |
| 229 | 233 |
| 230 @CalledByNative | 234 @CalledByNative |
| 231 private static SnippetArticle addSuggestion(List<SnippetArticle> suggestions , int category, | 235 private static SnippetArticle addSuggestion(List<SnippetArticle> suggestions , int category, |
| 232 String id, String title, String publisher, String previewText, Strin g url, | 236 String id, String title, String publisher, String previewText, Strin g url, |
| 233 long timestamp, float score) { | 237 long timestamp, float score) { |
| 234 int position = suggestions.size(); | 238 int position = suggestions.size(); |
| 235 suggestions.add(new SnippetArticle( | 239 suggestions.add(new SnippetArticle( |
| 236 category, id, title, publisher, previewText, url, timestamp, sco re, position)); | 240 category, id, title, publisher, previewText, url, timestamp, sco re)); |
| 237 return suggestions.get(position); | 241 return suggestions.get(position); |
| 238 } | 242 } |
| 239 | 243 |
| 240 @CalledByNative | 244 @CalledByNative |
| 241 private static void setAssetDownloadDataForSuggestion( | 245 private static void setAssetDownloadDataForSuggestion( |
| 242 SnippetArticle suggestion, String filePath, String mimeType) { | 246 SnippetArticle suggestion, String filePath, String mimeType) { |
| 243 suggestion.setAssetDownloadData(filePath, mimeType); | 247 suggestion.setAssetDownloadData(filePath, mimeType); |
| 244 } | 248 } |
| 245 | 249 |
| 246 @CalledByNative | 250 @CalledByNative |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 float score); | 324 float score); |
| 321 private native void nativeOnMoreButtonShown( | 325 private native void nativeOnMoreButtonShown( |
| 322 long nativeNTPSnippetsBridge, int category, int position); | 326 long nativeNTPSnippetsBridge, int category, int position); |
| 323 private native void nativeOnMoreButtonClicked( | 327 private native void nativeOnMoreButtonClicked( |
| 324 long nativeNTPSnippetsBridge, int category, int position); | 328 long nativeNTPSnippetsBridge, int category, int position); |
| 325 private native void nativeOnActivityWarmResumed(long nativeNTPSnippetsBridge ); | 329 private native void nativeOnActivityWarmResumed(long nativeNTPSnippetsBridge ); |
| 326 private native void nativeOnColdStart(long nativeNTPSnippetsBridge); | 330 private native void nativeOnColdStart(long nativeNTPSnippetsBridge); |
| 327 private static native void nativeOnSuggestionTargetVisited(int category, lon g visitTimeMs); | 331 private static native void nativeOnSuggestionTargetVisited(int category, lon g visitTimeMs); |
| 328 private static native void nativeOnNTPInitialized(long nativeNTPSnippetsBrid ge); | 332 private static native void nativeOnNTPInitialized(long nativeNTPSnippetsBrid ge); |
| 329 } | 333 } |
| OLD | NEW |