| 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 org.chromium.base.Log; | 7 import org.chromium.base.Log; |
| 8 import org.chromium.base.VisibleForTesting; | 8 import org.chromium.base.VisibleForTesting; |
| 9 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; | 9 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; |
| 10 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; | 10 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; |
| 11 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus.CategoryStatusEnu
m; | |
| 12 import org.chromium.chrome.browser.ntp.snippets.KnownCategories; | 11 import org.chromium.chrome.browser.ntp.snippets.KnownCategories; |
| 13 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; | 12 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; |
| 14 import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge; | 13 import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge; |
| 15 import org.chromium.chrome.browser.ntp.snippets.SuggestionsSource; | 14 import org.chromium.chrome.browser.ntp.snippets.SuggestionsSource; |
| 16 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; | 15 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; |
| 17 import org.chromium.chrome.browser.suggestions.DestructionObserver; | 16 import org.chromium.chrome.browser.suggestions.DestructionObserver; |
| 18 import org.chromium.chrome.browser.suggestions.SuggestionsRanker; | 17 import org.chromium.chrome.browser.suggestions.SuggestionsRanker; |
| 19 import org.chromium.chrome.browser.suggestions.SuggestionsUiDelegate; | 18 import org.chromium.chrome.browser.suggestions.SuggestionsUiDelegate; |
| 20 | 19 |
| 21 import java.util.LinkedHashMap; | 20 import java.util.LinkedHashMap; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 /** | 81 /** |
| 83 * Resets the section for {@code category}. Removes the section if there are
no suggestions for | 82 * Resets the section for {@code category}. Removes the section if there are
no suggestions for |
| 84 * it and it is not allowed to be empty. Otherwise, creates the section if i
t is not present | 83 * it and it is not allowed to be empty. Otherwise, creates the section if i
t is not present |
| 85 * yet. Sets the available suggestions on the section. | 84 * yet. Sets the available suggestions on the section. |
| 86 * @param category The category for which the section must be reset. | 85 * @param category The category for which the section must be reset. |
| 87 * @param categoryStatus The category status. | 86 * @param categoryStatus The category status. |
| 88 * @param alwaysAllowEmptySections Whether sections are always allowed to be
displayed when | 87 * @param alwaysAllowEmptySections Whether sections are always allowed to be
displayed when |
| 89 * they are empty, even when they are normally not. | 88 * they are empty, even when they are normally not. |
| 90 * @return The number of suggestions for the section. | 89 * @return The number of suggestions for the section. |
| 91 */ | 90 */ |
| 92 private int resetSection(@CategoryInt int category, @CategoryStatusEnum int
categoryStatus, | 91 private int resetSection(@CategoryInt int category, @CategoryStatus int cate
goryStatus, |
| 93 boolean alwaysAllowEmptySections) { | 92 boolean alwaysAllowEmptySections) { |
| 94 SuggestionsSource suggestionsSource = mUiDelegate.getSuggestionsSource()
; | 93 SuggestionsSource suggestionsSource = mUiDelegate.getSuggestionsSource()
; |
| 95 List<SnippetArticle> suggestions = suggestionsSource.getSuggestionsForCa
tegory(category); | 94 List<SnippetArticle> suggestions = suggestionsSource.getSuggestionsForCa
tegory(category); |
| 96 SuggestionsCategoryInfo info = suggestionsSource.getCategoryInfo(categor
y); | 95 SuggestionsCategoryInfo info = suggestionsSource.getCategoryInfo(categor
y); |
| 97 | 96 |
| 98 SuggestionsSection section = mSections.get(category); | 97 SuggestionsSection section = mSections.get(category); |
| 99 | 98 |
| 100 // Do not show an empty section if not allowed. | 99 // Do not show an empty section if not allowed. |
| 101 if (suggestions.isEmpty() && !info.showIfEmpty() && !alwaysAllowEmptySec
tions) { | 100 if (suggestions.isEmpty() && !info.showIfEmpty() && !alwaysAllowEmptySec
tions) { |
| 102 if (section != null) removeSection(section); | 101 if (section != null) removeSection(section); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 113 } | 112 } |
| 114 | 113 |
| 115 // Set the new suggestions. | 114 // Set the new suggestions. |
| 116 setSuggestions(category, suggestions, categoryStatus, /* replaceExisting
= */ true); | 115 setSuggestions(category, suggestions, categoryStatus, /* replaceExisting
= */ true); |
| 117 | 116 |
| 118 return suggestions.size(); | 117 return suggestions.size(); |
| 119 } | 118 } |
| 120 | 119 |
| 121 @Override | 120 @Override |
| 122 public void onNewSuggestions(@CategoryInt int category) { | 121 public void onNewSuggestions(@CategoryInt int category) { |
| 123 @CategoryStatusEnum | 122 @CategoryStatus |
| 124 int status = mUiDelegate.getSuggestionsSource().getCategoryStatus(catego
ry); | 123 int status = mUiDelegate.getSuggestionsSource().getCategoryStatus(catego
ry); |
| 125 | 124 |
| 126 if (!canLoadSuggestions(category, status)) return; | 125 if (!canLoadSuggestions(category, status)) return; |
| 127 | 126 |
| 128 List<SnippetArticle> suggestions = | 127 List<SnippetArticle> suggestions = |
| 129 mUiDelegate.getSuggestionsSource().getSuggestionsForCategory(cat
egory); | 128 mUiDelegate.getSuggestionsSource().getSuggestionsForCategory(cat
egory); |
| 130 | 129 |
| 131 Log.d(TAG, "Received %d new suggestions for category %d.", suggestions.s
ize(), category); | 130 Log.d(TAG, "Received %d new suggestions for category %d.", suggestions.s
ize(), category); |
| 132 | 131 |
| 133 // At first, there might be no suggestions available, we wait until they
have been fetched. | 132 // At first, there might be no suggestions available, we wait until they
have been fetched. |
| 134 if (suggestions.isEmpty()) return; | 133 if (suggestions.isEmpty()) return; |
| 135 | 134 |
| 136 setSuggestions(category, suggestions, status, /* replaceExisting = */ tr
ue); | 135 setSuggestions(category, suggestions, status, /* replaceExisting = */ tr
ue); |
| 137 } | 136 } |
| 138 | 137 |
| 139 @Override | 138 @Override |
| 140 public void onMoreSuggestions(@CategoryInt int category, List<SnippetArticle
> suggestions) { | 139 public void onMoreSuggestions(@CategoryInt int category, List<SnippetArticle
> suggestions) { |
| 141 @CategoryStatusEnum | 140 @CategoryStatus |
| 142 int status = mUiDelegate.getSuggestionsSource().getCategoryStatus(catego
ry); | 141 int status = mUiDelegate.getSuggestionsSource().getCategoryStatus(catego
ry); |
| 143 if (!canLoadSuggestions(category, status)) return; | 142 if (!canLoadSuggestions(category, status)) return; |
| 144 | 143 |
| 145 setSuggestions(category, suggestions, status, /* replaceExisting = */ fa
lse); | 144 setSuggestions(category, suggestions, status, /* replaceExisting = */ fa
lse); |
| 146 } | 145 } |
| 147 | 146 |
| 148 @Override | 147 @Override |
| 149 public void onCategoryStatusChanged(@CategoryInt int category, @CategoryStat
usEnum int status) { | 148 public void onCategoryStatusChanged(@CategoryInt int category, @CategoryStat
us int status) { |
| 150 // Observers should not be registered for this state. | 149 // Observers should not be registered for this state. |
| 151 assert status != CategoryStatus.ALL_SUGGESTIONS_EXPLICITLY_DISABLED; | 150 assert status != CategoryStatus.ALL_SUGGESTIONS_EXPLICITLY_DISABLED; |
| 152 | 151 |
| 153 // If there is no section for this category there is nothing to do. | 152 // If there is no section for this category there is nothing to do. |
| 154 if (!mSections.containsKey(category)) return; | 153 if (!mSections.containsKey(category)) return; |
| 155 | 154 |
| 156 switch (status) { | 155 switch (status) { |
| 157 case CategoryStatus.NOT_PROVIDED: | 156 case CategoryStatus.NOT_PROVIDED: |
| 158 // The section provider has gone away. Keep open UIs as they are
. | 157 // The section provider has gone away. Keep open UIs as they are
. |
| 159 return; | 158 return; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 * suggestions with the new ones or append the new suggestions at the end of
the list. This call | 193 * suggestions with the new ones or append the new suggestions at the end of
the list. This call |
| 195 * may have no or only partial effect if changing the list of suggestions is
not allowed (e.g. | 194 * may have no or only partial effect if changing the list of suggestions is
not allowed (e.g. |
| 196 * because the user has already seen the suggestions). | 195 * because the user has already seen the suggestions). |
| 197 * @param category The category for which the suggestions should be set. | 196 * @param category The category for which the suggestions should be set. |
| 198 * @param suggestions The new list of suggestions for the given category. | 197 * @param suggestions The new list of suggestions for the given category. |
| 199 * @param status The new category status. | 198 * @param status The new category status. |
| 200 * @param replaceExisting If true, {@code suggestions} replace the current l
ist of suggestions. | 199 * @param replaceExisting If true, {@code suggestions} replace the current l
ist of suggestions. |
| 201 * If false, {@code suggestions} are appended to current list of suggestions
. | 200 * If false, {@code suggestions} are appended to current list of suggestions
. |
| 202 */ | 201 */ |
| 203 private void setSuggestions(@CategoryInt int category, List<SnippetArticle>
suggestions, | 202 private void setSuggestions(@CategoryInt int category, List<SnippetArticle>
suggestions, |
| 204 @CategoryStatusEnum int status, boolean replaceExisting) { | 203 @CategoryStatus int status, boolean replaceExisting) { |
| 205 mSections.get(category).setSuggestions(suggestions, status, replaceExist
ing); | 204 mSections.get(category).setSuggestions(suggestions, status, replaceExist
ing); |
| 206 } | 205 } |
| 207 | 206 |
| 208 private boolean canLoadSuggestions(@CategoryInt int category, @CategoryStatu
sEnum int status) { | 207 private boolean canLoadSuggestions(@CategoryInt int category, @CategoryStatu
s int status) { |
| 209 // We never want to add suggestions from unknown categories. | 208 // We never want to add suggestions from unknown categories. |
| 210 if (!mSections.containsKey(category)) return false; | 209 if (!mSections.containsKey(category)) return false; |
| 211 | 210 |
| 212 // The status may have changed while the suggestions were loading, perha
ps they should not | 211 // The status may have changed while the suggestions were loading, perha
ps they should not |
| 213 // be displayed any more. | 212 // be displayed any more. |
| 214 if (!SnippetsBridge.isCategoryEnabled(status)) { | 213 if (!SnippetsBridge.isCategoryEnabled(status)) { |
| 215 Log.w(TAG, "Received suggestions for a disabled category (id=%d, sta
tus=%d)", category, | 214 Log.w(TAG, "Received suggestions for a disabled category (id=%d, sta
tus=%d)", category, |
| 216 status); | 215 status); |
| 217 return false; | 216 return false; |
| 218 } | 217 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 * @return Whether the list of sections is empty. | 264 * @return Whether the list of sections is empty. |
| 266 */ | 265 */ |
| 267 public boolean isEmpty() { | 266 public boolean isEmpty() { |
| 268 return mSections.isEmpty(); | 267 return mSections.isEmpty(); |
| 269 } | 268 } |
| 270 | 269 |
| 271 SuggestionsSection getSectionForTesting(@CategoryInt int categoryId) { | 270 SuggestionsSection getSectionForTesting(@CategoryInt int categoryId) { |
| 272 return mSections.get(categoryId); | 271 return mSections.get(categoryId); |
| 273 } | 272 } |
| 274 } | 273 } |
| OLD | NEW |