Index: chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadItemSelectionDelegate.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadItemSelectionDelegate.java b/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadItemSelectionDelegate.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5ae4314d804cbdcea63a563f5971378bc7c85a19 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadItemSelectionDelegate.java |
@@ -0,0 +1,75 @@ |
+// 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.download.ui; |
+ |
+import org.chromium.chrome.browser.download.ui.DownloadHistoryAdapter.SubsectionHeader; |
+import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
+ |
+import java.util.HashSet; |
+import java.util.Set; |
+ |
+/** |
+ * Separately maintains the selected state of the {@link SubsectionHeader}s in addition to the other |
+ * selected items. |
+ */ |
+public class DownloadItemSelectionDelegate extends SelectionDelegate<DownloadHistoryItemWrapper> { |
+ private final Set<SubsectionHeader> mSelectedHeaders = new HashSet<>(); |
+ |
+ @Override |
+ public boolean isSelectionEnabled() { |
shaktisahu
2017/03/09 06:03:33
Removing this as suggested in Theresa's prior comm
|
+ return !mSelectedHeaders.isEmpty() || super.isSelectionEnabled(); |
+ } |
+ |
+ /** |
+ * True if the header is currently selected. False otherwise. |
+ * @param header The given header. |
+ * @return Whether the header is selected. |
+ */ |
+ public boolean isHeaderSelected(SubsectionHeader header) { |
+ return mSelectedHeaders.contains(header); |
+ } |
+ |
+ @Override |
+ public void clearSelection() { |
+ mSelectedHeaders.clear(); |
+ super.clearSelection(); |
+ } |
+ |
+ /** |
+ * Toggles selection for a given subsection and sets the associated items to the correct |
+ * selection state. |
+ * @param header The header for the subsection being toggled. |
+ * @return True if the header is selected after the toggle, false otherwise. |
+ */ |
+ public boolean toggleSelectionForSubsection(SubsectionHeader header) { |
+ boolean newSelectedState = !isHeaderSelected(header); |
+ for (DownloadHistoryItemWrapper item : header.getItems()) { |
+ if (newSelectedState != isItemSelected(item)) { |
+ toggleSelectionForItem(item); |
+ } |
+ } |
+ |
+ return setSelectionForHeader(header, newSelectedState); |
+ } |
+ |
+ /** |
+ * Sets the selection state for a given header. Doesn't affect the associated items. This method |
+ * is supposed to be called if the header needs to be toggled as a result of selecting or |
+ * deselecting one of the associated items. In case of a long press on the header, don't call |
+ * this method, call toggleSelectionForSubsection instead directly. |
Theresa
2017/03/08 16:08:17
nit: the end of this should be "call {@link toggle
shaktisahu
2017/03/09 06:03:33
Done.
|
+ * @param header The given {@link SubsectionHeader}. |
+ * @param selected The new selected state. |
+ * @return Whether the header was successfully selected. |
Theresa
2017/03/08 16:08:17
nit: This returns whether the header is selected.
shaktisahu
2017/03/09 06:03:33
Done.
|
+ */ |
+ public boolean setSelectionForHeader(SubsectionHeader header, boolean selected) { |
+ if (selected) { |
+ mSelectedHeaders.add(header); |
+ } else { |
+ mSelectedHeaders.remove(header); |
+ } |
+ |
+ return isHeaderSelected(header); |
+ } |
+} |