KeyboardInterpreter.java revision bcfd4439d730a4d783a02596c8ab444796323aad
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 */
16package com.android.dreams.phototable;
17
18import android.util.Log;
19import android.view.KeyEvent;
20import android.view.View;
21
22/**
23 * Keyboard event dispatcher for Photo Table.
24 */
25public 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
33    public KeyboardInterpreter(PhotoTable table) {
34        mBounce = 2000; // TODO: remove this once latencies in lower layers are removed.
35        mTable = table;
36    }
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                break;
69            }
70        } else {
71            switch (keyCode) {
72            case KeyEvent.KEYCODE_ENTER:
73            case KeyEvent.KEYCODE_DPAD_CENTER:
74                if (mTable.hasFocus()) {
75                    mTable.setSelection(mTable.getFocus());
76                    mTable.clearFocus();
77                } else {
78                    mTable.setDefaultFocus();
79                }
80                break;
81
82            case KeyEvent.KEYCODE_DEL:
83            case KeyEvent.KEYCODE_X:
84                if (mTable.hasFocus()) {
85                    mTable.fling(mTable.getFocus());
86                }
87                break;
88
89            case KeyEvent.KEYCODE_DPAD_UP:
90            case KeyEvent.KEYCODE_K:
91                mTable.moveFocus(focus, 0f);
92                break;
93
94            case KeyEvent.KEYCODE_DPAD_RIGHT:
95            case KeyEvent.KEYCODE_L:
96                mTable.moveFocus(focus, 90f);
97                break;
98
99            case KeyEvent.KEYCODE_DPAD_DOWN:
100            case KeyEvent.KEYCODE_J:
101                mTable.moveFocus(focus, 180f);
102                break;
103
104            case KeyEvent.KEYCODE_DPAD_LEFT:
105            case KeyEvent.KEYCODE_H:
106                mTable.moveFocus(focus, 270f);
107                break;
108
109            default:
110                if (DEBUG) Log.d(TAG, "dropped unexpected: " + keyCode);
111                consumed = false;
112                break;
113            }
114        }
115
116        return consumed;
117    }
118}
119