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