From 5e02d303c07d428970e25b315061c61287df153d Mon Sep 17 00:00:00 2001 From: Tomasz Mikolajewski Date: Mon, 11 Apr 2016 13:44:01 +0900 Subject: [PATCH] Do not allow to select non-selectable items via mouse. Bug: 28101625 Change-Id: I9f6cd4e259f7860bbc6c74c4d24c43e2c3ba1047 --- .../documentsui/dirlist/MultiSelectManager.java | 25 +++++++++++++++++++++- .../dirlist/MultiSelectManager_GridModelTest.java | 5 +++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/DocumentsUI/src/com/android/documentsui/dirlist/MultiSelectManager.java b/packages/DocumentsUI/src/com/android/documentsui/dirlist/MultiSelectManager.java index 758815371b80..35d8988244f6 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/dirlist/MultiSelectManager.java +++ b/packages/DocumentsUI/src/com/android/documentsui/dirlist/MultiSelectManager.java @@ -1268,6 +1268,11 @@ public final class MultiSelectManager { notifySelectionChanged(); } + @Override + public boolean onBeforeItemStateChange(String id, boolean nextState) { + return notifyBeforeItemStateChange(id, nextState); + } + private class ViewScroller implements Runnable { /** * The number of milliseconds of scrolling at which scroll speed continues to increase. @@ -1655,7 +1660,9 @@ public final class MultiSelectManager { if (id != null) { // The adapter inserts items for UI layout purposes that aren't associated // with files. Those will have a null model ID. Don't select them. - mSelection.add(id); + if (canSelect(id)) { + mSelection.add(id); + } } if (isPossiblePositionNearestOrigin(column, columnStartIndex, columnEndIndex, row, rowStartIndex, rowEndIndex)) { @@ -1669,6 +1676,21 @@ public final class MultiSelectManager { } /** + * @return True if the item is selectable. + */ + private boolean canSelect(String id) { + // TODO: Simplify the logic, so the check whether we can select is done in one place. + // Consider injecting FragmentTuner, or move the checks from MultiSelectManager to + // Selection. + for (OnSelectionChangedListener listener : mOnSelectionChangedListeners) { + if (!listener.onBeforeItemStateChange(id, true)) { + return false; + } + } + return true; + } + + /** * @return Returns true if the position is the nearest to the origin, or, in the case of the * lower-right corner, whether it is possible that the position is the nearest to the * origin. See comment below for reasoning for this special case. @@ -1700,6 +1722,7 @@ public final class MultiSelectManager { */ static interface OnSelectionChangedListener { public void onSelectionChanged(Set updatedSelection); + public boolean onBeforeItemStateChange(String id, boolean nextState); } void addOnSelectionChangedListener(OnSelectionChangedListener listener) { diff --git a/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/MultiSelectManager_GridModelTest.java b/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/MultiSelectManager_GridModelTest.java index 0c0e0b7133dd..cc119fec8267 100644 --- a/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/MultiSelectManager_GridModelTest.java +++ b/packages/DocumentsUI/tests/src/com/android/documentsui/dirlist/MultiSelectManager_GridModelTest.java @@ -76,6 +76,11 @@ public class MultiSelectManager_GridModelTest extends AndroidTestCase { public void onSelectionChanged(Set updatedSelection) { lastSelection = updatedSelection; } + + @Override + public boolean onBeforeItemStateChange(String id, boolean nextState) { + return true; + } }); } -- 2.11.0