Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | */ |
| 16 | package com.android.documentsui; |
| 17 | |
Felipe Leme | 9de5807 | 2018-01-19 16:40:04 -0800 | [diff] [blame] | 18 | import static com.android.documentsui.base.SharedMinimal.DEBUG; |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 19 | |
| 20 | import android.util.Log; |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 21 | import android.view.KeyEvent; |
| 22 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 23 | import androidx.recyclerview.selection.SelectionTracker; |
| 24 | |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 25 | import com.android.documentsui.base.Events; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 26 | import com.android.documentsui.base.Features; |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 27 | import com.android.documentsui.base.Procedure; |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 28 | import com.android.documentsui.dirlist.FocusHandler; |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 29 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 30 | /** |
| 31 | * Handle common input events. |
| 32 | */ |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 33 | public class SharedInputHandler { |
| 34 | |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 35 | private static final String TAG = "SharedInputHandler"; |
| 36 | |
| 37 | private final FocusHandler mFocusManager; |
| 38 | private final Procedure mSearchCanceler; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 39 | private final Procedure mDirPopper; |
Bill Lin | 35bfbef | 2019-08-05 16:09:53 +0800 | [diff] [blame] | 40 | private final Runnable mSearchExecutor; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 41 | private final Features mFeatures; |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 42 | private final SelectionTracker<String> mSelectionMgr; |
Rhed Jao | 76a9d71 | 2018-05-16 15:32:59 +0800 | [diff] [blame] | 43 | private final DrawerController mDrawer; |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 44 | |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 45 | public SharedInputHandler( |
| 46 | FocusHandler focusHandler, |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 47 | SelectionTracker<String> selectionMgr, |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 48 | Procedure searchCanceler, |
| 49 | Procedure dirPopper, |
Rhed Jao | 76a9d71 | 2018-05-16 15:32:59 +0800 | [diff] [blame] | 50 | Features features, |
Bill Lin | 35bfbef | 2019-08-05 16:09:53 +0800 | [diff] [blame] | 51 | DrawerController drawer, |
| 52 | Runnable searchExcutor) { |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 53 | mFocusManager = focusHandler; |
| 54 | mSearchCanceler = searchCanceler; |
| 55 | mSelectionMgr = selectionMgr; |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 56 | mDirPopper = dirPopper; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 57 | mFeatures = features; |
Rhed Jao | 76a9d71 | 2018-05-16 15:32:59 +0800 | [diff] [blame] | 58 | mDrawer = drawer; |
Bill Lin | 35bfbef | 2019-08-05 16:09:53 +0800 | [diff] [blame] | 59 | mSearchExecutor = searchExcutor; |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 63 | switch (keyCode) { |
| 64 | // Unhandled ESC keys end up being rethrown back at us as BACK keys. So by returning |
| 65 | // true, we make sure it always does no-op. |
| 66 | case KeyEvent.KEYCODE_ESCAPE: |
| 67 | return onEscape(); |
| 68 | |
| 69 | case KeyEvent.KEYCODE_DEL: |
| 70 | return onDelete(); |
| 71 | |
| 72 | // This is the Android back button, not backspace. |
| 73 | case KeyEvent.KEYCODE_BACK: |
| 74 | return onBack(); |
| 75 | |
| 76 | case KeyEvent.KEYCODE_TAB: |
| 77 | return onTab(); |
| 78 | |
Bill Lin | 35bfbef | 2019-08-05 16:09:53 +0800 | [diff] [blame] | 79 | case KeyEvent.KEYCODE_SEARCH: |
| 80 | mSearchExecutor.run(); |
| 81 | return true; |
| 82 | |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 83 | default: |
| 84 | // Instead of duplicating the switch-case in #isNavigationKeyCode, best just to |
| 85 | // leave it here. |
| 86 | if (Events.isNavigationKeyCode(keyCode)) { |
| 87 | // Forward all unclaimed navigation keystrokes to the directory list. |
| 88 | // This causes any stray navigation keystrokes to focus the content pane, |
| 89 | // which is probably what the user is trying to do. |
| 90 | mFocusManager.focusDirectoryList(); |
| 91 | return true; |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private boolean onTab() { |
| 98 | if (!mFeatures.isSystemKeyboardNavigationEnabled()) { |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 99 | // Tab toggles focus on the navigation drawer. |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 100 | // This should only be called in pre-O devices, since O has built-in keyboard |
| 101 | // navigation |
Ben Lin | 340ab17 | 2017-01-27 11:41:26 -0800 | [diff] [blame] | 102 | // support. |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 103 | mFocusManager.advanceFocusArea(); |
| 104 | return true; |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return false; |
| 108 | } |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 109 | |
| 110 | private boolean onDelete() { |
| 111 | mDirPopper.run(); |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | private boolean onBack() { |
Rhed Jao | 76a9d71 | 2018-05-16 15:32:59 +0800 | [diff] [blame] | 116 | if (mDrawer.isPresent() && mDrawer.isOpen()) { |
| 117 | mDrawer.setOpen(false); |
| 118 | return true; |
| 119 | } |
| 120 | |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 121 | if (mSearchCanceler.run()) { |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | if (mSelectionMgr.hasSelection()) { |
Jason Chang | 96f886b | 2019-03-29 17:59:02 +0800 | [diff] [blame] | 126 | if (DEBUG) { |
| 127 | Log.d(TAG, "Back pressed. Clearing existing selection."); |
| 128 | } |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 129 | mSelectionMgr.clearSelection(); |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | return mDirPopper.run(); |
| 134 | } |
| 135 | |
| 136 | private boolean onEscape() { |
| 137 | if (mSearchCanceler.run()) { |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | if (mSelectionMgr.hasSelection()) { |
Jason Chang | 96f886b | 2019-03-29 17:59:02 +0800 | [diff] [blame] | 142 | if (DEBUG) { |
| 143 | Log.d(TAG, "ESC pressed. Clearing existing selection."); |
| 144 | } |
Ben Lin | 0902f07 | 2017-03-13 18:25:29 -0700 | [diff] [blame] | 145 | mSelectionMgr.clearSelection(); |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | return true; |
| 150 | } |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 151 | } |