From 0e6b84ff6391ca6019be4d3ee96cd61821b16e74 Mon Sep 17 00:00:00 2001 From: Tomasz Mikolajewski Date: Mon, 11 Apr 2016 13:09:53 +0900 Subject: [PATCH] Do not crash when trying to select unselectable items with keyboard. Bug: 28103071 Change-Id: I6e9bc918433eee4b1309ccf8ea597ea774e5559e --- .../dirlist/DirectoryFragment.java | 21 +++++++++++++++---- .../dirlist/MultiSelectManager.java | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java b/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java index bc2133e7c7ee2..f38b0fdc33ea8 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java +++ b/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java @@ -1386,7 +1386,7 @@ public class DirectoryFragment extends Fragment // Handle range selection adjustments. Extending the selection will adjust the // bounds of the in-progress range selection. Each time an unshifted navigation // event is received, the range selection is restarted. - if (shouldExtendSelection(event)) { + if (shouldExtendSelection(doc, event)) { if (!mSelectionManager.isRangeSelectionActive()) { // Start a range selection if one isn't active mSelectionManager.startRangeSelection(doc.getAdapterPosition()); @@ -1423,9 +1423,22 @@ public class DirectoryFragment extends Fragment return false; } - private boolean shouldExtendSelection(KeyEvent event) { - return Events.isNavigationKeyCode(event.getKeyCode()) && - event.isShiftPressed(); + private boolean shouldExtendSelection(DocumentHolder doc, KeyEvent event) { + if (!Events.isNavigationKeyCode(event.getKeyCode()) || !event.isShiftPressed()) { + return false; + } + + // TODO: Combine this method with onBeforeItemStateChange, as both of them are almost + // the same, and responsible for the same thing (whether to select or not). + final Cursor cursor = mModel.getItem(doc.modelId); + if (cursor == null) { + Log.w(TAG, "Couldn't obtain cursor for modelId: " + doc.modelId); + return false; + } + + final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); + final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS); + return mTuner.canSelectType(docMimeType, docFlags); } } diff --git a/packages/DocumentsUI/src/com/android/documentsui/dirlist/MultiSelectManager.java b/packages/DocumentsUI/src/com/android/documentsui/dirlist/MultiSelectManager.java index 1285b34ce313f..758815371b80f 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/dirlist/MultiSelectManager.java +++ b/packages/DocumentsUI/src/com/android/documentsui/dirlist/MultiSelectManager.java @@ -378,8 +378,8 @@ public final class MultiSelectManager { * @param pos The anchor position for the selection range. */ void startRangeSelection(int pos) { - attemptSelect(mAdapter.getModelId(pos)); - setSelectionRangeBegin(pos); + attemptSelect(mAdapter.getModelId(pos)); + setSelectionRangeBegin(pos); } /**