Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -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 | |
| 17 | package com.android.documentsui.dirlist; |
| 18 | |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.base.DocumentInfo.getCursorString; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 20 | |
Ben Lin | 81afd7f | 2016-08-22 18:24:03 -0700 | [diff] [blame] | 21 | import android.annotation.ColorRes; |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 23 | import android.database.Cursor; |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.Looper; |
| 26 | import android.os.SystemClock; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 27 | import android.provider.DocumentsContract.Document; |
Ben Kwa | a6c2f0a | 2016-02-10 11:54:55 -0800 | [diff] [blame] | 28 | import android.support.v7.widget.GridLayoutManager; |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 29 | import android.support.v7.widget.RecyclerView; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 30 | import android.text.Editable; |
| 31 | import android.text.Spannable; |
| 32 | import android.text.method.KeyListener; |
| 33 | import android.text.method.TextKeyListener; |
| 34 | import android.text.method.TextKeyListener.Capitalize; |
| 35 | import android.text.style.BackgroundColorSpan; |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | import android.view.KeyEvent; |
| 38 | import android.view.View; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 39 | import android.widget.TextView; |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 40 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 41 | import com.android.documentsui.base.EventListener; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 42 | import com.android.documentsui.base.Events; |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 43 | import com.android.documentsui.dirlist.Model.Update; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 44 | |
| 45 | import java.util.ArrayList; |
| 46 | import java.util.List; |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 47 | import java.util.Timer; |
| 48 | import java.util.TimerTask; |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * A class that handles navigation and focus within the DirectoryFragment. |
| 52 | */ |
Ben Lin | 81afd7f | 2016-08-22 18:24:03 -0700 | [diff] [blame] | 53 | public final class FocusManager implements FocusHandler { |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 54 | private static final String TAG = "FocusManager"; |
| 55 | |
| 56 | private RecyclerView mView; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 57 | private DocumentsAdapter mAdapter; |
Ben Kwa | a6c2f0a | 2016-02-10 11:54:55 -0800 | [diff] [blame] | 58 | private GridLayoutManager mLayout; |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 59 | |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 60 | private TitleSearchHelper mSearchHelper; |
| 61 | private Model mModel; |
Ben Lin | 81afd7f | 2016-08-22 18:24:03 -0700 | [diff] [blame] | 62 | private @Nullable String mPendingFocusId; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 63 | |
Ben Kwa | 74e5d41 | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 64 | private int mLastFocusPosition = RecyclerView.NO_POSITION; |
| 65 | |
Ben Lin | 81afd7f | 2016-08-22 18:24:03 -0700 | [diff] [blame] | 66 | public FocusManager(RecyclerView view, Model model, @ColorRes int color) { |
Ben Lin | 26819d7 | 2016-08-17 12:45:05 -0700 | [diff] [blame] | 67 | assert (view != null); |
| 68 | assert (model != null); |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 69 | mView = view; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 70 | mAdapter = (DocumentsAdapter) view.getAdapter(); |
Ben Kwa | a6c2f0a | 2016-02-10 11:54:55 -0800 | [diff] [blame] | 71 | mLayout = (GridLayoutManager) view.getLayoutManager(); |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 72 | mModel = model; |
| 73 | |
Ben Lin | 81afd7f | 2016-08-22 18:24:03 -0700 | [diff] [blame] | 74 | mSearchHelper = new TitleSearchHelper(color); |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 77 | @Override |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 78 | public boolean handleKey(DocumentHolder doc, int keyCode, KeyEvent event) { |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 79 | // Search helper gets first crack, for doing type-to-focus. |
| 80 | if (mSearchHelper.handleKey(doc, keyCode, event)) { |
| 81 | return true; |
| 82 | } |
| 83 | |
Ben Kwa | 92db0b3 | 2016-02-11 13:29:12 -0800 | [diff] [blame] | 84 | // Translate space/shift-space into PgDn/PgUp |
| 85 | if (keyCode == KeyEvent.KEYCODE_SPACE) { |
| 86 | if (event.isShiftPressed()) { |
| 87 | keyCode = KeyEvent.KEYCODE_PAGE_UP; |
| 88 | } else { |
| 89 | keyCode = KeyEvent.KEYCODE_PAGE_DOWN; |
| 90 | } |
Ben Kwa | 92db0b3 | 2016-02-11 13:29:12 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 93 | if (Events.isNavigationKeyCode(keyCode)) { |
| 94 | // Find the target item and focus it. |
| 95 | int endPos = findTargetPosition(doc.itemView, keyCode, event); |
| 96 | |
| 97 | if (endPos != RecyclerView.NO_POSITION) { |
| 98 | focusItem(endPos); |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 99 | } |
Ben Kwa | 74e5d41 | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 100 | // Swallow all navigation keystrokes. Otherwise they go to the app's global |
| 101 | // key-handler, which will route them back to the DF and cause focus to be reset. |
| 102 | return true; |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 103 | } |
Ben Kwa | 74e5d41 | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void onFocusChange(View v, boolean hasFocus) { |
| 109 | // Remember focus events on items. |
| 110 | if (hasFocus && v.getParent() == mView) { |
| 111 | mLastFocusPosition = mView.getChildAdapterPosition(v); |
| 112 | } |
| 113 | } |
| 114 | |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 115 | @Override |
Ben Kwa | 74e5d41 | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 116 | public void restoreLastFocus() { |
Ben Kwa | 237432e | 2016-02-24 10:11:10 -0800 | [diff] [blame] | 117 | if (mAdapter.getItemCount() == 0) { |
| 118 | // Nothing to focus. |
| 119 | return; |
| 120 | } |
| 121 | |
Ben Kwa | 74e5d41 | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 122 | if (mLastFocusPosition != RecyclerView.NO_POSITION) { |
| 123 | // The system takes care of situations when a view is no longer on screen, etc, |
| 124 | focusItem(mLastFocusPosition); |
| 125 | } else { |
| 126 | // Focus the first visible item |
| 127 | focusItem(mLayout.findFirstVisibleItemPosition()); |
| 128 | } |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Ben Lin | 81afd7f | 2016-08-22 18:24:03 -0700 | [diff] [blame] | 131 | /* |
| 132 | * Attempts to reset focus on the item corresponding to {@code mPendingFocusId} if it exists and |
| 133 | * has a valid position in the adapter. It then automatically resets {@code mPendingFocusId}. |
| 134 | */ |
| 135 | @Override |
| 136 | public void onLayoutCompleted() { |
| 137 | if (mPendingFocusId == null) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | int pos = mAdapter.getModelIds().indexOf(mPendingFocusId); |
| 142 | if (pos != -1) { |
| 143 | focusItem(pos); |
| 144 | } |
| 145 | mPendingFocusId = null; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Attempts to put focus on the document associated with the given modelId. If item does not |
| 150 | * exist yet in the layout, this sets a pending modelId to be used when |
| 151 | * {@code #applyPendingFocus()} is called next time. |
| 152 | */ |
| 153 | @Override |
| 154 | public void onDirectoryCreated(String modelId) { |
| 155 | int pos = mAdapter.getModelIds().indexOf(modelId); |
| 156 | if (pos != -1 && mView.findViewHolderForAdapterPosition(pos) != null) { |
| 157 | focusItem(pos); |
| 158 | } else { |
| 159 | mPendingFocusId = modelId; |
| 160 | } |
| 161 | } |
| 162 | |
Steve McKay | 74956af | 2016-06-30 21:03:06 -0700 | [diff] [blame] | 163 | @Override |
Ben Kwa | 9504d76 | 2016-02-10 14:01:19 -0800 | [diff] [blame] | 164 | public int getFocusPosition() { |
| 165 | return mLastFocusPosition; |
| 166 | } |
| 167 | |
| 168 | /** |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 169 | * Finds the destination position where the focus should land for a given navigation event. |
| 170 | * |
| 171 | * @param view The view that received the event. |
| 172 | * @param keyCode The key code for the event. |
| 173 | * @param event |
| 174 | * @return The adapter position of the destination item. Could be RecyclerView.NO_POSITION. |
| 175 | */ |
| 176 | private int findTargetPosition(View view, int keyCode, KeyEvent event) { |
| 177 | switch (keyCode) { |
| 178 | case KeyEvent.KEYCODE_MOVE_HOME: |
| 179 | return 0; |
| 180 | case KeyEvent.KEYCODE_MOVE_END: |
| 181 | return mAdapter.getItemCount() - 1; |
| 182 | case KeyEvent.KEYCODE_PAGE_UP: |
| 183 | case KeyEvent.KEYCODE_PAGE_DOWN: |
| 184 | return findPagedTargetPosition(view, keyCode, event); |
| 185 | } |
| 186 | |
| 187 | // Find a navigation target based on the arrow key that the user pressed. |
| 188 | int searchDir = -1; |
| 189 | switch (keyCode) { |
| 190 | case KeyEvent.KEYCODE_DPAD_UP: |
| 191 | searchDir = View.FOCUS_UP; |
| 192 | break; |
| 193 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 194 | searchDir = View.FOCUS_DOWN; |
| 195 | break; |
Ben Kwa | a6c2f0a | 2016-02-10 11:54:55 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (inGridMode()) { |
| 199 | int currentPosition = mView.getChildAdapterPosition(view); |
| 200 | // Left and right arrow keys only work in grid mode. |
| 201 | switch (keyCode) { |
| 202 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 203 | if (currentPosition > 0) { |
| 204 | // Stop backward focus search at the first item, otherwise focus will wrap |
| 205 | // around to the last visible item. |
| 206 | searchDir = View.FOCUS_BACKWARD; |
| 207 | } |
| 208 | break; |
| 209 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 210 | if (currentPosition < mAdapter.getItemCount() - 1) { |
| 211 | // Stop forward focus search at the last item, otherwise focus will wrap |
| 212 | // around to the first visible item. |
| 213 | searchDir = View.FOCUS_FORWARD; |
| 214 | } |
| 215 | break; |
| 216 | } |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (searchDir != -1) { |
Ben Kwa | 67f06a3 | 2016-02-17 14:08:52 -0800 | [diff] [blame] | 220 | // Focus search behaves badly if the parent RecyclerView is focused. However, focusable |
| 221 | // shouldn't be unset on RecyclerView, otherwise focus isn't properly restored after |
| 222 | // events that cause a UI rebuild (like rotating the device). Compromise: turn focusable |
| 223 | // off while performing the focus search. |
| 224 | // TODO: Revisit this when RV focus issues are resolved. |
| 225 | mView.setFocusable(false); |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 226 | View targetView = view.focusSearch(searchDir); |
Ben Kwa | 67f06a3 | 2016-02-17 14:08:52 -0800 | [diff] [blame] | 227 | mView.setFocusable(true); |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 228 | // TargetView can be null, for example, if the user pressed <down> at the bottom |
| 229 | // of the list. |
| 230 | if (targetView != null) { |
| 231 | // Ignore navigation targets that aren't items in the RecyclerView. |
| 232 | if (targetView.getParent() == mView) { |
| 233 | return mView.getChildAdapterPosition(targetView); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return RecyclerView.NO_POSITION; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Given a PgUp/PgDn event and the current view, find the position of the target view. |
| 243 | * This returns: |
| 244 | * <li>The position of the topmost (or bottom-most) visible item, if the current item is not |
| 245 | * the top- or bottom-most visible item. |
| 246 | * <li>The position of an item that is one page's worth of items up (or down) if the current |
| 247 | * item is the top- or bottom-most visible item. |
| 248 | * <li>The first (or last) item, if paging up (or down) would go past those limits. |
| 249 | * @param view The view that received the key event. |
| 250 | * @param keyCode Must be KEYCODE_PAGE_UP or KEYCODE_PAGE_DOWN. |
| 251 | * @param event |
| 252 | * @return The adapter position of the target item. |
| 253 | */ |
| 254 | private int findPagedTargetPosition(View view, int keyCode, KeyEvent event) { |
| 255 | int first = mLayout.findFirstVisibleItemPosition(); |
| 256 | int last = mLayout.findLastVisibleItemPosition(); |
| 257 | int current = mView.getChildAdapterPosition(view); |
| 258 | int pageSize = last - first + 1; |
| 259 | |
| 260 | if (keyCode == KeyEvent.KEYCODE_PAGE_UP) { |
| 261 | if (current > first) { |
| 262 | // If the current item isn't the first item, target the first item. |
| 263 | return first; |
| 264 | } else { |
| 265 | // If the current item is the first item, target the item one page up. |
| 266 | int target = current - pageSize; |
| 267 | return target < 0 ? 0 : target; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if (keyCode == KeyEvent.KEYCODE_PAGE_DOWN) { |
| 272 | if (current < last) { |
| 273 | // If the current item isn't the last item, target the last item. |
| 274 | return last; |
| 275 | } else { |
| 276 | // If the current item is the last item, target the item one page down. |
| 277 | int target = current + pageSize; |
| 278 | int max = mAdapter.getItemCount() - 1; |
| 279 | return target < max ? target : max; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | throw new IllegalArgumentException("Unsupported keyCode: " + keyCode); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Requests focus for the item in the given adapter position, scrolling the RecyclerView if |
| 288 | * necessary. |
| 289 | * |
| 290 | * @param pos |
| 291 | */ |
| 292 | private void focusItem(final int pos) { |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 293 | focusItem(pos, null); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Requests focus for the item in the given adapter position, scrolling the RecyclerView if |
| 298 | * necessary. |
| 299 | * |
| 300 | * @param pos |
| 301 | * @param callback A callback to call after the given item has been focused. |
| 302 | */ |
| 303 | private void focusItem(final int pos, @Nullable final FocusCallback callback) { |
Ben Lin | 81afd7f | 2016-08-22 18:24:03 -0700 | [diff] [blame] | 304 | if (mPendingFocusId != null) { |
| 305 | Log.v(TAG, "clearing pending focus id: " + mPendingFocusId); |
| 306 | mPendingFocusId = null; |
| 307 | } |
| 308 | |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 309 | // If the item is already in view, focus it; otherwise, scroll to it and focus it. |
| 310 | RecyclerView.ViewHolder vh = mView.findViewHolderForAdapterPosition(pos); |
| 311 | if (vh != null) { |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 312 | if (vh.itemView.requestFocus() && callback != null) { |
| 313 | callback.onFocus(vh.itemView); |
| 314 | } |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 315 | } else { |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 316 | // Set a one-time listener to request focus when the scroll has completed. |
| 317 | mView.addOnScrollListener( |
| 318 | new RecyclerView.OnScrollListener() { |
| 319 | @Override |
| 320 | public void onScrollStateChanged(RecyclerView view, int newState) { |
| 321 | if (newState == RecyclerView.SCROLL_STATE_IDLE) { |
| 322 | // When scrolling stops, find the item and focus it. |
| 323 | RecyclerView.ViewHolder vh = |
| 324 | view.findViewHolderForAdapterPosition(pos); |
| 325 | if (vh != null) { |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 326 | if (vh.itemView.requestFocus() && callback != null) { |
| 327 | callback.onFocus(vh.itemView); |
| 328 | } |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 329 | } else { |
| 330 | // This might happen in weird corner cases, e.g. if the user is |
| 331 | // scrolling while a delete operation is in progress. In that |
| 332 | // case, just don't attempt to focus the missing item. |
| 333 | Log.w(TAG, "Unable to focus position " + pos + " after scroll"); |
| 334 | } |
| 335 | view.removeOnScrollListener(this); |
| 336 | } |
| 337 | } |
| 338 | }); |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 339 | mView.smoothScrollToPosition(pos); |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 340 | } |
| 341 | } |
Ben Kwa | a6c2f0a | 2016-02-10 11:54:55 -0800 | [diff] [blame] | 342 | |
| 343 | /** |
| 344 | * @return Whether the layout manager is currently in a grid-configuration. |
| 345 | */ |
| 346 | private boolean inGridMode() { |
| 347 | return mLayout.getSpanCount() > 1; |
| 348 | } |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 349 | |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 350 | private interface FocusCallback { |
| 351 | public void onFocus(View view); |
| 352 | } |
| 353 | |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 354 | /** |
| 355 | * A helper class for handling type-to-focus. Instantiate this class, and pass it KeyEvents via |
| 356 | * the {@link #handleKey(DocumentHolder, int, KeyEvent)} method. The class internally will build |
| 357 | * up a string from individual key events, and perform searching based on that string. When an |
| 358 | * item is found that matches the search term, that item will be focused. This class also |
| 359 | * highlights instances of the search term found in the view. |
| 360 | */ |
| 361 | private class TitleSearchHelper { |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 362 | static private final int SEARCH_TIMEOUT = 500; // ms |
| 363 | |
| 364 | private final KeyListener mTextListener = new TextKeyListener(Capitalize.NONE, false); |
| 365 | private final Editable mSearchString = Editable.Factory.getInstance().newEditable(""); |
| 366 | private final Highlighter mHighlighter = new Highlighter(); |
| 367 | private final BackgroundColorSpan mSpan; |
| 368 | |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 369 | private List<String> mIndex; |
| 370 | private boolean mActive; |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 371 | private Timer mTimer; |
| 372 | private KeyEvent mLastEvent; |
| 373 | private Handler mUiRunner; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 374 | |
Ben Lin | 81afd7f | 2016-08-22 18:24:03 -0700 | [diff] [blame] | 375 | public TitleSearchHelper(@ColorRes int color) { |
| 376 | mSpan = new BackgroundColorSpan(color); |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 377 | // Handler for running things on the main UI thread. Needed for updating the UI from a |
| 378 | // timer (see #activate, below). |
| 379 | mUiRunner = new Handler(Looper.getMainLooper()); |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Handles alphanumeric keystrokes for type-to-focus. This method builds a search term out |
| 384 | * of individual key events, and then performs a search for the given string. |
| 385 | * |
| 386 | * @param doc The document holder receiving the key event. |
| 387 | * @param keyCode |
| 388 | * @param event |
| 389 | * @return Whether the event was handled. |
| 390 | */ |
| 391 | public boolean handleKey(DocumentHolder doc, int keyCode, KeyEvent event) { |
| 392 | switch (keyCode) { |
| 393 | case KeyEvent.KEYCODE_ESCAPE: |
| 394 | case KeyEvent.KEYCODE_ENTER: |
| 395 | if (mActive) { |
| 396 | // These keys end any active searches. |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 397 | endSearch(); |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 398 | return true; |
| 399 | } else { |
| 400 | // Don't handle these key events if there is no active search. |
| 401 | return false; |
| 402 | } |
| 403 | case KeyEvent.KEYCODE_SPACE: |
| 404 | // This allows users to search for files with spaces in their names, but ignores |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 405 | // spacebar events when a text search is not active. Ignoring the spacebar |
| 406 | // event is necessary because other handlers (see FocusManager#handleKey) also |
| 407 | // listen for and handle it. |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 408 | if (!mActive) { |
| 409 | return false; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // Navigation keys also end active searches. |
| 414 | if (Events.isNavigationKeyCode(keyCode)) { |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 415 | endSearch(); |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 416 | // Don't handle the keycode, so navigation still occurs. |
| 417 | return false; |
| 418 | } |
| 419 | |
| 420 | // Build up the search string, and perform the search. |
| 421 | boolean handled = mTextListener.onKeyDown(doc.itemView, mSearchString, keyCode, event); |
| 422 | |
| 423 | // Delete is processed by the text listener, but not "handled". Check separately for it. |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 424 | if (keyCode == KeyEvent.KEYCODE_DEL) { |
| 425 | handled = true; |
| 426 | } |
| 427 | |
| 428 | if (handled) { |
| 429 | mLastEvent = event; |
| 430 | if (mSearchString.length() == 0) { |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 431 | // Don't perform empty searches. |
| 432 | return false; |
| 433 | } |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 434 | search(); |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | return handled; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Activates the search helper, which changes its key handling and updates the search index |
| 442 | * and highlights if necessary. Call this each time the search term is updated. |
| 443 | */ |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 444 | private void search() { |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 445 | if (!mActive) { |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 446 | // The model listener invalidates the search index when the model changes. |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 447 | mModel.addUpdateListener(mModelListener); |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 448 | |
| 449 | // Used to keep the current search alive until the timeout expires. If the user |
| 450 | // presses another key within that time, that keystroke is added to the current |
| 451 | // search. Otherwise, the current search ends, and subsequent keystrokes start a new |
| 452 | // search. |
| 453 | mTimer = new Timer(); |
| 454 | mActive = true; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // If the search index was invalidated, rebuild it |
| 458 | if (mIndex == null) { |
| 459 | buildIndex(); |
| 460 | } |
| 461 | |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 462 | // Search for the current search term. |
| 463 | // Perform case-insensitive search. |
| 464 | String searchString = mSearchString.toString().toLowerCase(); |
| 465 | for (int pos = 0; pos < mIndex.size(); pos++) { |
| 466 | String title = mIndex.get(pos); |
| 467 | if (title != null && title.startsWith(searchString)) { |
| 468 | focusItem(pos, new FocusCallback() { |
| 469 | @Override |
| 470 | public void onFocus(View view) { |
| 471 | mHighlighter.applyHighlight(view); |
| 472 | // Using a timer repeat period of SEARCH_TIMEOUT/2 means the amount of |
| 473 | // time between the last keystroke and a search expiring is actually |
| 474 | // between 500 and 750 ms. A smaller timer period results in less |
| 475 | // variability but does more polling. |
| 476 | mTimer.schedule(new TimeoutTask(), 0, SEARCH_TIMEOUT / 2); |
| 477 | } |
| 478 | }); |
| 479 | break; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 485 | * Ends the current search (see {@link #search()}. |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 486 | */ |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 487 | private void endSearch() { |
| 488 | if (mActive) { |
| 489 | mModel.removeUpdateListener(mModelListener); |
| 490 | mTimer.cancel(); |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 493 | mHighlighter.removeHighlight(); |
| 494 | |
| 495 | mIndex = null; |
| 496 | mSearchString.clear(); |
| 497 | mActive = false; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Builds a search index for finding items by title. Queries the model and adapter, so both |
| 502 | * must be set up before calling this method. |
| 503 | */ |
| 504 | private void buildIndex() { |
| 505 | int itemCount = mAdapter.getItemCount(); |
| 506 | List<String> index = new ArrayList<>(itemCount); |
| 507 | for (int i = 0; i < itemCount; i++) { |
| 508 | String modelId = mAdapter.getModelId(i); |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 509 | Cursor cursor = mModel.getItem(modelId); |
| 510 | if (modelId != null && cursor != null) { |
| 511 | String title = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME); |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 512 | // Perform case-insensitive search. |
| 513 | index.add(title.toLowerCase()); |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 514 | } else { |
| 515 | index.add(""); |
| 516 | } |
| 517 | } |
| 518 | mIndex = index; |
| 519 | } |
| 520 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 521 | private EventListener<Model.Update> mModelListener = new EventListener<Model.Update>() { |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 522 | @Override |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 523 | public void accept(Update event) { |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 524 | // Invalidate the search index when the model updates. |
| 525 | mIndex = null; |
| 526 | } |
| 527 | }; |
| 528 | |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 529 | private class TimeoutTask extends TimerTask { |
| 530 | @Override |
| 531 | public void run() { |
| 532 | long last = mLastEvent.getEventTime(); |
| 533 | long now = SystemClock.uptimeMillis(); |
| 534 | if ((now - last) > SEARCH_TIMEOUT) { |
| 535 | // endSearch must run on the main thread because it does UI work |
| 536 | mUiRunner.post(new Runnable() { |
| 537 | @Override |
| 538 | public void run() { |
| 539 | endSearch(); |
| 540 | } |
| 541 | }); |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 542 | } |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 543 | } |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 544 | }; |
| 545 | |
| 546 | private class Highlighter { |
| 547 | private Spannable mCurrentHighlight; |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 548 | |
| 549 | /** |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 550 | * Applies title highlights to the given view. The view must have a title field that is a |
| 551 | * spannable text field. If this condition is not met, this function does nothing. |
| 552 | * |
| 553 | * @param view |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 554 | */ |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 555 | private void applyHighlight(View view) { |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 556 | TextView titleView = (TextView) view.findViewById(android.R.id.title); |
Ben Kwa | 6fd431e | 2016-02-23 23:00:01 -0800 | [diff] [blame] | 557 | if (titleView == null) { |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | CharSequence tmpText = titleView.getText(); |
| 562 | if (tmpText instanceof Spannable) { |
| 563 | if (mCurrentHighlight != null) { |
| 564 | mCurrentHighlight.removeSpan(mSpan); |
| 565 | } |
| 566 | mCurrentHighlight = (Spannable) tmpText; |
| 567 | mCurrentHighlight.setSpan( |
| 568 | mSpan, 0, mSearchString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * Removes title highlights from the given view. The view must have a title field that is a |
| 574 | * spannable text field. If this condition is not met, this function does nothing. |
| 575 | * |
| 576 | * @param view |
| 577 | */ |
| 578 | private void removeHighlight() { |
| 579 | if (mCurrentHighlight != null) { |
| 580 | mCurrentHighlight.removeSpan(mSpan); |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | }; |
| 584 | } |
Ben Kwa | 15de7f9 | 2016-02-09 11:27:45 -0800 | [diff] [blame] | 585 | } |