Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/suggestions/SuggestionsUma.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/suggestions/SuggestionsUma.java b/chrome/android/java/src/org/chromium/chrome/browser/suggestions/SuggestionsUma.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..51456bcf1b0e1d17625d3a59959c1d4292a85b55 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/suggestions/SuggestionsUma.java |
| @@ -0,0 +1,48 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.suggestions; |
| + |
| +import org.chromium.chrome.browser.ntp.cards.SuggestionsSection; |
| +import org.chromium.chrome.browser.ntp.snippets.CategoryInt; |
| +import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; |
| + |
| +import java.util.Map; |
| +import java.util.NoSuchElementException; |
| + |
| +/** |
| + * Exposes UMA related methods. |
| + */ |
| +public class SuggestionsUma { |
| + private final Map<Integer, SuggestionsSection> mSections; |
| + |
| + public SuggestionsUma(Map<Integer, SuggestionsSection> sections) { |
| + mSections = sections; |
| + } |
| + |
| + /** A suggestion's rank is the number of suggestions preceding it in its section. */ |
| + public int getSuggestionRank(SnippetArticle suggestion) { |
| + return mSections.get(suggestion.mCategory).getSuggestionRank(suggestion); |
| + } |
| + |
| + /** |
| + * A suggestion's global rank is the total number of suggestions preceding it on the current |
| + * page. |
| + * @param suggestion the suggestion to compute the global rank for. |
| + * @param localRank the rank of the suggestion in its section. |
| + */ |
| + public int getSuggestionGlobalRank(SnippetArticle suggestion, int localRank) { |
| + return getSectionStartingRank(suggestion.mCategory) + localRank; |
|
Bernhard Bauer
2017/01/11 13:53:38
Would it be simpler to just return an android.util
dgn
2017/01/12 14:29:58
Done.
|
| + } |
| + |
| + /** @return the global rank that the first suggestion of the section would have. */ |
| + private int getSectionStartingRank(@CategoryInt int category) { |
| + int index = 0; |
| + for (Map.Entry<Integer, SuggestionsSection> kvPair : mSections.entrySet()) { |
| + if (kvPair.getKey() == category) return index; |
| + index += kvPair.getValue().getSuggestionsCount(); |
| + } |
| + throw new NoSuchElementException(); // Or return -Inf? |
| + } |
| +} |