| Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/OptionalLeaf.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/Leaf.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/OptionalLeaf.java
|
| similarity index 56%
|
| copy from chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/Leaf.java
|
| copy to chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/OptionalLeaf.java
|
| index aef01d06007878e4cdc1f707723d0f21725c6dd9..0a9cc68e7c16a3ca3cf242e4de53d0e4c0b4766d 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/Leaf.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/OptionalLeaf.java
|
| @@ -7,36 +7,44 @@
|
| import org.chromium.chrome.browser.ntp.snippets.SnippetArticle;
|
|
|
| /**
|
| - * A leaf in the tree, i.e. a single item.
|
| + * An optional leaf (i.e. single item) in the tree. Depending on its internal state (see
|
| + * {@link #isShown()}), the item will be present or absent from the tree, by manipulating the values
|
| + * returned from {@link ChildNode} methods. This allows the parent node to not have to add or remove
|
| + * the optional leaf from its children manually.
|
| + *
|
| + * For a non optional leaf, see {@link Leaf}. They have similar interfaces.
|
| */
|
| -public abstract class Leaf implements TreeNode {
|
| +public abstract class OptionalLeaf extends ChildNode {
|
| + protected OptionalLeaf(NodeParent parent) {
|
| + super(parent);
|
| + }
|
| +
|
| @Override
|
| public int getItemCount() {
|
| - return 1;
|
| + return isShown() ? 1 : 0;
|
| }
|
|
|
| @Override
|
| - @ItemViewType
|
| public int getItemViewType(int position) {
|
| - if (position != 0) throw new IndexOutOfBoundsException();
|
| + checkIndex(position);
|
| return getItemViewType();
|
| }
|
|
|
| @Override
|
| public void onBindViewHolder(NewTabPageViewHolder holder, int position) {
|
| - if (position != 0) throw new IndexOutOfBoundsException();
|
| + checkIndex(position);
|
| onBindViewHolder(holder);
|
| }
|
|
|
| @Override
|
| public SnippetArticle getSuggestionAt(int position) {
|
| - if (position != 0) throw new IndexOutOfBoundsException();
|
| -
|
| + checkIndex(position);
|
| return null;
|
| }
|
|
|
| @Override
|
| public int getDismissSiblingPosDelta(int position) {
|
| + checkIndex(position);
|
| return 0;
|
| }
|
|
|
| @@ -54,4 +62,13 @@ public int getDismissSiblingPosDelta(int position) {
|
| */
|
| @ItemViewType
|
| protected abstract int getItemViewType();
|
| +
|
| + /** @return Whether the optional item is currently visible. */
|
| + public abstract boolean isShown();
|
| +
|
| + protected void checkIndex(int position) {
|
| + if (position < 0 || position >= getItemCount()) {
|
| + throw new IndexOutOfBoundsException(position + "/" + getItemCount());
|
| + }
|
| + }
|
| }
|
|
|