blob: 8cc2bfb74475b6ffb774b94f6da9fe475e348134 [file] [log] [blame]
Steve McKay1fdd34b2017-09-19 16:07:33 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.documentsui.dirlist;
17
KOUSHIK PANUGANTI6ca7acc2018-04-17 16:00:10 -070018import static androidx.core.util.Preconditions.checkArgument;
Steve McKay1fdd34b2017-09-19 16:07:33 -070019
Riddle Hsu0c375982018-06-21 22:06:43 +080020import android.view.KeyEvent;
21
22import androidx.recyclerview.selection.SelectionTracker;
23import androidx.recyclerview.selection.SelectionTracker.SelectionPredicate;
KOUSHIK PANUGANTI6ca7acc2018-04-17 16:00:10 -070024import androidx.recyclerview.widget.RecyclerView;
25import androidx.recyclerview.widget.RecyclerView.ViewHolder;
Steve McKay1fdd34b2017-09-19 16:07:33 -070026
27import com.android.documentsui.ActionHandler;
Steve McKay1fdd34b2017-09-19 16:07:33 -070028
29/**
30 * Helper class dedicated to building gesture input handlers. The construction
31 * of the various input handlers is non trivial. To keep logic clear,
32 * code flexible, and DirectoryFragment small(er), the construction has been
33 * isolated here in a separate class.
34 */
35final class InputHandlers {
36
37 private ActionHandler mActions;
Riddle Hsu0c375982018-06-21 22:06:43 +080038 private SelectionTracker<String> mSelectionHelper;
39 private SelectionPredicate<String> mSelectionPredicate;
Steve McKay1fdd34b2017-09-19 16:07:33 -070040 private FocusHandler mFocusHandler;
41 private RecyclerView mRecView;
Steve McKay1fdd34b2017-09-19 16:07:33 -070042
43 InputHandlers(
44 ActionHandler actions,
Riddle Hsu0c375982018-06-21 22:06:43 +080045 SelectionTracker<String> selectionHelper,
46 SelectionPredicate<String> selectionPredicate,
Steve McKay1fdd34b2017-09-19 16:07:33 -070047 FocusHandler focusHandler,
Riddle Hsu0c375982018-06-21 22:06:43 +080048 RecyclerView recView) {
Steve McKay1fdd34b2017-09-19 16:07:33 -070049
50 checkArgument(actions != null);
51 checkArgument(selectionHelper != null);
52 checkArgument(selectionPredicate != null);
Steve McKay1fdd34b2017-09-19 16:07:33 -070053 checkArgument(focusHandler != null);
54 checkArgument(recView != null);
Steve McKay1fdd34b2017-09-19 16:07:33 -070055
56 mActions = actions;
57 mSelectionHelper = selectionHelper;
58 mSelectionPredicate = selectionPredicate;
Steve McKay1fdd34b2017-09-19 16:07:33 -070059 mFocusHandler = focusHandler;
60 mRecView = recView;
Steve McKay1fdd34b2017-09-19 16:07:33 -070061 }
62
63 KeyInputHandler createKeyHandler() {
Riddle Hsu0c375982018-06-21 22:06:43 +080064 KeyInputHandler.Callbacks<DocumentItemDetails> callbacks =
65 new KeyInputHandler.Callbacks<DocumentItemDetails>() {
Steve McKay1fdd34b2017-09-19 16:07:33 -070066 @Override
Riddle Hsu0c375982018-06-21 22:06:43 +080067 public boolean isInteractiveItem(DocumentItemDetails item, KeyEvent e) {
Steve McKay1fdd34b2017-09-19 16:07:33 -070068 switch (item.getItemViewType()) {
69 case DocumentsAdapter.ITEM_TYPE_HEADER_MESSAGE:
70 case DocumentsAdapter.ITEM_TYPE_INFLATED_MESSAGE:
71 case DocumentsAdapter.ITEM_TYPE_SECTION_BREAK:
72 return false;
73 case DocumentsAdapter.ITEM_TYPE_DOCUMENT:
74 case DocumentsAdapter.ITEM_TYPE_DIRECTORY:
75 return true;
76 default:
77 throw new RuntimeException(
78 "Unsupported item type: " + item.getItemViewType());
79 }
80 }
81
82 @Override
Riddle Hsu0c375982018-06-21 22:06:43 +080083 public boolean onItemActivated(DocumentItemDetails item, KeyEvent e) {
Steve McKay1fdd34b2017-09-19 16:07:33 -070084 // Handle enter key events
85 switch (e.getKeyCode()) {
86 case KeyEvent.KEYCODE_ENTER:
87 case KeyEvent.KEYCODE_DPAD_CENTER:
88 case KeyEvent.KEYCODE_BUTTON_A:
89 return mActions.openItem(
90 item,
91 ActionHandler.VIEW_TYPE_REGULAR,
92 ActionHandler.VIEW_TYPE_PREVIEW);
93 case KeyEvent.KEYCODE_SPACE:
94 return mActions.openItem(
95 item,
96 ActionHandler.VIEW_TYPE_PREVIEW,
97 ActionHandler.VIEW_TYPE_NONE);
98 }
99
100 return false;
101 }
102
103 @Override
Riddle Hsu0c375982018-06-21 22:06:43 +0800104 public boolean onFocusItem(DocumentItemDetails details, int keyCode, KeyEvent event) {
Steve McKay1fdd34b2017-09-19 16:07:33 -0700105 ViewHolder holder =
106 mRecView.findViewHolderForAdapterPosition(details.getPosition());
107 if (holder instanceof DocumentHolder) {
108 return mFocusHandler.handleKey((DocumentHolder) holder, keyCode, event);
109 }
110 return false;
111 }
Steve McKay1fdd34b2017-09-19 16:07:33 -0700112 };
113
114 return new KeyInputHandler(mSelectionHelper, mSelectionPredicate, callbacks);
115 }
Steve McKayb93a5942018-01-17 09:27:35 -0800116}