blob: f3f26419d75184af0b64b2dc7adefc3db7c5dddb [file] [log] [blame]
Steve McKay04718262016-11-08 11:01:35 -08001/*
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 */
16package com.android.documentsui;
17
Ben Lin0902f072017-03-13 18:25:29 -070018import static com.android.documentsui.base.Shared.DEBUG;
19
20import android.util.Log;
Steve McKay04718262016-11-08 11:01:35 -080021import android.view.KeyEvent;
22
23import com.android.documentsui.base.Events;
Steve McKay98f8c5f2017-03-03 13:52:14 -080024import com.android.documentsui.base.Features;
Steve McKay04718262016-11-08 11:01:35 -080025import com.android.documentsui.base.Procedure;
Ben Lin0902f072017-03-13 18:25:29 -070026import com.android.documentsui.dirlist.FocusHandler;
27import com.android.documentsui.selection.SelectionManager;
Steve McKay04718262016-11-08 11:01:35 -080028
29public class SharedInputHandler {
30
Ben Lin0902f072017-03-13 18:25:29 -070031 private static final String TAG = "SharedInputHandler";
32
33 private final FocusHandler mFocusManager;
34 private final Procedure mSearchCanceler;
Steve McKay98f8c5f2017-03-03 13:52:14 -080035 private final Procedure mDirPopper;
36 private final Features mFeatures;
Ben Lin0902f072017-03-13 18:25:29 -070037 private final SelectionManager mSelectionMgr;
Steve McKay04718262016-11-08 11:01:35 -080038
Ben Lin0902f072017-03-13 18:25:29 -070039 public SharedInputHandler(
40 FocusHandler focusHandler,
41 SelectionManager selectionMgr,
42 Procedure searchCanceler,
43 Procedure dirPopper,
44 Features features) {
45 mFocusManager = focusHandler;
46 mSearchCanceler = searchCanceler;
47 mSelectionMgr = selectionMgr;
Steve McKay04718262016-11-08 11:01:35 -080048 mDirPopper = dirPopper;
Steve McKay98f8c5f2017-03-03 13:52:14 -080049 mFeatures = features;
Steve McKay04718262016-11-08 11:01:35 -080050 }
51
52 public boolean onKeyDown(int keyCode, KeyEvent event) {
Ben Lin0902f072017-03-13 18:25:29 -070053 switch (keyCode) {
54 // Unhandled ESC keys end up being rethrown back at us as BACK keys. So by returning
55 // true, we make sure it always does no-op.
56 case KeyEvent.KEYCODE_ESCAPE:
57 return onEscape();
58
59 case KeyEvent.KEYCODE_DEL:
60 return onDelete();
61
62 // This is the Android back button, not backspace.
63 case KeyEvent.KEYCODE_BACK:
64 return onBack();
65
66 case KeyEvent.KEYCODE_TAB:
67 return onTab();
68
69 default:
70 // Instead of duplicating the switch-case in #isNavigationKeyCode, best just to
71 // leave it here.
72 if (Events.isNavigationKeyCode(keyCode)) {
73 // Forward all unclaimed navigation keystrokes to the directory list.
74 // This causes any stray navigation keystrokes to focus the content pane,
75 // which is probably what the user is trying to do.
76 mFocusManager.focusDirectoryList();
77 return true;
78 }
79 return false;
80 }
81 }
82
83 private boolean onTab() {
84 if (!mFeatures.isSystemKeyboardNavigationEnabled()) {
Steve McKay04718262016-11-08 11:01:35 -080085 // Tab toggles focus on the navigation drawer.
Ben Lin0902f072017-03-13 18:25:29 -070086 // This should only be called in pre-O devices, since O has built-in keyboard
87 // navigation
Ben Lin340ab172017-01-27 11:41:26 -080088 // support.
Steve McKay04718262016-11-08 11:01:35 -080089 mFocusManager.advanceFocusArea();
90 return true;
Steve McKay04718262016-11-08 11:01:35 -080091 }
92
93 return false;
94 }
Ben Lin0902f072017-03-13 18:25:29 -070095
96 private boolean onDelete() {
97 mDirPopper.run();
98 return true;
99 }
100
101 private boolean onBack() {
102 if (mSearchCanceler.run()) {
103 return true;
104 }
105
106 if (mSelectionMgr.hasSelection()) {
107 if (DEBUG) Log.d(TAG, "Back pressed. Clearing existing selection.");
108 mSelectionMgr.clearSelection();
109 return true;
110 }
111
112 return mDirPopper.run();
113 }
114
115 private boolean onEscape() {
116 if (mSearchCanceler.run()) {
117 return true;
118 }
119
120 if (mSelectionMgr.hasSelection()) {
121 if (DEBUG) Log.d(TAG, "ESC pressed. Clearing existing selection.");
122 mSelectionMgr.clearSelection();
123 return true;
124 }
125
126 return true;
127 }
Steve McKay04718262016-11-08 11:01:35 -0800128}