1 // Copyright 2015 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.chrome.browser.toolbar; 6 7 import android.view.KeyEvent; 8 import android.view.View; 9 import android.view.View.OnKeyListener; 10 11 /** 12 * This is an abstract class to override keyboard navigation behavior for views. 13 */ 14 public abstract class KeyboardNavigationListener implements OnKeyListener { KeyboardNavigationListener()15 public KeyboardNavigationListener() { 16 super(); 17 } 18 19 @Override onKey(View v, int keyCode, KeyEvent event)20 public boolean onKey(View v, int keyCode, KeyEvent event) { 21 if (keyCode == KeyEvent.KEYCODE_TAB && event.getAction() == KeyEvent.ACTION_DOWN) { 22 if (event.hasNoModifiers()) { 23 View forward = getNextFocusForward(); 24 if (forward != null) return forward.requestFocus(); 25 } else if (event.isShiftPressed()) { 26 View backward = getNextFocusBackward(); 27 if (backward != null) return backward.requestFocus(); 28 } 29 } else if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { 30 return handleEnterKeyPress(); 31 } 32 return false; 33 } 34 35 /** 36 * Get the view to be focused on a TAB click. If you return null, the default key event 37 * processing will occur instead of attempting to focus. 38 * @return The view to gain focus. 39 */ getNextFocusForward()40 public View getNextFocusForward() { 41 return null; 42 } 43 44 /** 45 * Get the view to be focused on a Shift + TAB click. If you return null, the default key event 46 * processing will occur instead of attempting to focus. 47 * @return The view to gain focus. 48 */ getNextFocusBackward()49 public View getNextFocusBackward() { 50 return null; 51 } 52 53 /** 54 * Allows the extending class to special case the enter key press handling. 55 * @return Whether the enter key was handled 56 */ handleEnterKeyPress()57 protected boolean handleEnterKeyPress() { 58 return false; 59 } 60 } 61