1 /* 2 * Copyright (C) 2013 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.dreams.phototable; 17 18 import android.util.Log; 19 import android.view.KeyEvent; 20 import android.view.View; 21 22 /** 23 * Keyboard event dispatcher for Photo Table. 24 */ 25 public class KeyboardInterpreter { 26 private static final String TAG = "DPadInterpreter"; 27 private static final boolean DEBUG = false; 28 29 private final PhotoTable mTable; 30 private final long mBounce; 31 private long mLastDeckNavigation; 32 KeyboardInterpreter(PhotoTable table)33 public KeyboardInterpreter(PhotoTable table) { 34 mBounce = 2000; // TODO: remove this once latencies in lower layers are removed. 35 mTable = table; 36 } onKeyDown(int keyCode, KeyEvent event)37 public boolean onKeyDown(int keyCode, KeyEvent event) { 38 final View focus = mTable.getFocus(); 39 boolean consumed = true; 40 if (mTable.hasSelection()) { 41 switch (keyCode) { 42 case KeyEvent.KEYCODE_ENTER: 43 case KeyEvent.KEYCODE_DPAD_CENTER: 44 case KeyEvent.KEYCODE_ESCAPE: 45 mTable.setFocus(mTable.getSelection()); 46 mTable.clearSelection(); 47 break; 48 49 case KeyEvent.KEYCODE_DPAD_RIGHT: 50 case KeyEvent.KEYCODE_L: 51 if ((System.currentTimeMillis() - mLastDeckNavigation) > mBounce) { 52 mLastDeckNavigation = System.currentTimeMillis(); 53 mTable.selectPrevious(); 54 } 55 break; 56 57 case KeyEvent.KEYCODE_DPAD_LEFT: 58 case KeyEvent.KEYCODE_H: 59 if ((System.currentTimeMillis() - mLastDeckNavigation) > mBounce) { 60 mLastDeckNavigation = System.currentTimeMillis(); 61 mTable.selectNext(); 62 } 63 break; 64 65 default: 66 if (DEBUG) Log.d(TAG, "dropped unexpected: " + keyCode); 67 consumed = false; 68 // give the user some more time to figure it out 69 mTable.refreshSelection(); 70 break; 71 } 72 } else { 73 switch (keyCode) { 74 case KeyEvent.KEYCODE_ENTER: 75 case KeyEvent.KEYCODE_DPAD_CENTER: 76 if (mTable.hasFocus()) { 77 mTable.setSelection(mTable.getFocus()); 78 mTable.clearFocus(); 79 } else { 80 mTable.setDefaultFocus(); 81 } 82 break; 83 84 case KeyEvent.KEYCODE_DEL: 85 case KeyEvent.KEYCODE_X: 86 if (mTable.hasFocus()) { 87 mTable.fling(mTable.getFocus()); 88 } 89 break; 90 91 case KeyEvent.KEYCODE_DPAD_UP: 92 case KeyEvent.KEYCODE_K: 93 mTable.moveFocus(focus, 0f); 94 break; 95 96 case KeyEvent.KEYCODE_DPAD_RIGHT: 97 case KeyEvent.KEYCODE_L: 98 mTable.moveFocus(focus, 90f); 99 break; 100 101 case KeyEvent.KEYCODE_DPAD_DOWN: 102 case KeyEvent.KEYCODE_J: 103 mTable.moveFocus(focus, 180f); 104 break; 105 106 case KeyEvent.KEYCODE_DPAD_LEFT: 107 case KeyEvent.KEYCODE_H: 108 mTable.moveFocus(focus, 270f); 109 break; 110 111 default: 112 if (DEBUG) Log.d(TAG, "dropped unexpected: " + keyCode); 113 consumed = false; 114 // give the user some more time to figure it out 115 mTable.refreshFocus(); 116 break; 117 } 118 } 119 120 return consumed; 121 } 122 } 123