Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/snackbar/SnackbarCollection.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/snackbar/SnackbarCollection.java b/chrome/android/java/src/org/chromium/chrome/browser/snackbar/SnackbarCollection.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6a6d5176c026d29f66b70eeea57bfe8c4da24b10 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/snackbar/SnackbarCollection.java |
| @@ -0,0 +1,126 @@ |
| +// Copyright 2016 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.snackbar; |
| + |
| +import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; |
| + |
| +import java.util.Deque; |
| +import java.util.Iterator; |
| +import java.util.LinkedList; |
| +import java.util.Queue; |
| + |
| +/** |
| + * A collection of {@link Snackbar}s. |
|
newt (away)
2016/01/28 17:32:00
Explain why this class exists :)
The current comm
Ian Wen
2016/01/29 02:38:43
Done.
|
| + */ |
| +class SnackbarCollection { |
| + private Deque<Snackbar> mStack = new LinkedList<>(); |
| + private Queue<Snackbar> mQueue = new LinkedList<>(); |
| + |
| + /** |
| + * Adds a new snackbar to the collection. If the new snackbar is of |
| + * {@link Snackbar#TYPE_ACTION} and current snackbar is of |
| + * {@link Snackbar#TYPE_NOTIFICATION}, the current snackbar will be removed from the |
| + * collection immediately. |
| + */ |
| + public void add(Snackbar snackbar) { |
|
newt (away)
2016/01/28 17:32:00
My personal preference is to remove "public" on al
Ian Wen
2016/01/29 02:38:43
Done.
|
| + if (snackbar.isTypeAction()) { |
| + if (getCurrent() != null && !getCurrent().isTypeAction()) { |
| + removeCurrent(false); |
| + } |
| + mStack.push(snackbar); |
| + } else { |
| + mQueue.offer(snackbar); |
| + } |
| + } |
| + |
| + /** |
| + * Removes the current snackbar from the collection. |
| + * @param isAction Whether the removal is triggered by user clicking the action button. |
| + */ |
| + public Snackbar removeCurrent(boolean isAction) { |
|
newt (away)
2016/01/28 17:32:00
Since removeCurrent(false) shouldn't be called by
Ian Wen
2016/01/29 02:38:42
Ah good suggestion!! Done.
|
| + Snackbar current = !mStack.isEmpty() ? mStack.pop() : mQueue.poll(); |
| + if (current != null) { |
| + SnackbarController controller = current.getController(); |
| + if (isAction) controller.onAction(current.getActionData()); |
| + else controller.onDismissNoAction(current.getActionData()); |
| + } |
| + return current; |
| + } |
| + |
| + /** |
| + * @return The snackbar that is currently displayed. |
| + */ |
| + public Snackbar getCurrent() { |
| + return !mStack.isEmpty() ? mStack.peek() : mQueue.peek(); |
| + } |
| + |
| + public boolean isEmpty() { |
| + return mStack.isEmpty() && mQueue.isEmpty(); |
| + } |
| + |
| + public void clear() { |
| + while (!isEmpty()) { |
| + removeCurrent(false); |
| + } |
| + } |
| + |
| + public void removeCurrentDueToTimeout() { |
| + removeCurrent(false); |
| + Snackbar current; |
| + while ((current = getCurrent()) != null && current.isTypeAction()) { |
| + removeCurrent(false); |
| + } |
| + } |
| + |
| + public boolean removeMatchingSnackbars(SnackbarController controller) { |
| + boolean snackbarRemoved = false; |
| + Iterator<Snackbar> iter = mStack.iterator(); |
| + while (iter.hasNext()) { |
| + Snackbar snackbar = iter.next(); |
| + if (snackbar.getController() == controller) { |
| + iter.remove(); |
| + snackbarRemoved = true; |
| + } |
| + } |
| + iter = mQueue.iterator(); |
| + while (iter.hasNext()) { |
| + Snackbar snackbar = iter.next(); |
| + if (snackbar.getController() == controller) { |
| + iter.remove(); |
| + snackbarRemoved = true; |
| + } |
| + } |
| + return snackbarRemoved; |
| + } |
| + |
| + public boolean removeMatchingSnackbars(SnackbarController controller, Object data) { |
| + boolean snackbarRemoved = false; |
| + Iterator<Snackbar> iter = mStack.iterator(); |
| + while (iter.hasNext()) { |
| + Snackbar snackbar = iter.next(); |
| + if (snackbar.getController() == controller |
| + && objectsAreEqual(snackbar.getActionData(), data)) { |
| + iter.remove(); |
| + snackbarRemoved = true; |
| + } |
| + } |
| + iter = mQueue.iterator(); |
| + while (iter.hasNext()) { |
| + Snackbar snackbar = iter.next(); |
| + if (snackbar.getController() == controller |
| + && objectsAreEqual(snackbar.getActionData(), data)) { |
| + iter.remove(); |
| + snackbarRemoved = true; |
| + } |
| + } |
| + return snackbarRemoved; |
| + } |
| + |
| + private static boolean objectsAreEqual(Object a, Object b) { |
| + if (a == null && b == null) return true; |
| + if (a == null || b == null) return false; |
| + return a.equals(b); |
| + } |
| +} |