The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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.browser; |
| 18 | |
| 19 | import android.view.KeyEvent; |
| 20 | import android.view.ViewConfiguration; |
| 21 | |
| 22 | class KeyTracker { |
| 23 | |
| 24 | public enum Stage { |
| 25 | DOWN, //!< the key has just been pressed |
| 26 | SHORT_REPEAT, //!< repeated key, but duration is under the long-press threshold |
| 27 | LONG_REPEAT, //!< repeated key, but duration is over the long-press threshold |
| 28 | UP //!< the key is being released |
| 29 | } |
| 30 | |
| 31 | public enum State { |
| 32 | KEEP_TRACKING, //!< return this to continue to track the key |
| 33 | DONE_TRACKING, //!< return this if you handled the key, but need not track it anymore |
| 34 | NOT_TRACKING //!< return this if you will not handle this key |
| 35 | } |
| 36 | |
| 37 | public interface OnKeyTracker { |
| 38 | |
| 39 | /** Called whenever there is a key event [down, short/long repeat, up] |
| 40 | @param keyCode The current keyCode (see KeyEvent class) |
| 41 | @param msg The message associated with the keyCode |
| 42 | @maram stage The state the key press is in [down, short/long repeat, up] |
| 43 | @param duration The number of milliseconds since this key was initially pressed |
| 44 | @return your state after seeing the key. If you return DONE_TRACKING or NOT_TRACKING, |
| 45 | you will not be called again for the lifetime of this key event. |
| 46 | */ |
| 47 | public State onKeyTracker(int keyCode, KeyEvent event, Stage stage, int duration); |
| 48 | } |
| 49 | |
| 50 | public KeyTracker(OnKeyTracker tracker) { |
| 51 | mTracker = tracker; |
| 52 | } |
| 53 | |
| 54 | public boolean doKeyDown(int keyCode, KeyEvent event) { |
| 55 | long now = System.currentTimeMillis(); |
| 56 | Stage stage = null; |
| 57 | |
| 58 | // check if its a new/different key |
| 59 | if (mKeyCode != keyCode || event.getRepeatCount() == 0) { |
| 60 | mKeyCode = keyCode; |
| 61 | mStartMS = now; |
| 62 | stage = Stage.DOWN; |
| 63 | } |
| 64 | else if (mState == State.KEEP_TRACKING) { |
| 65 | stage = (now - mStartMS) >= LONG_PRESS_DURATION_MS ? Stage.LONG_REPEAT : Stage.SHORT_REPEAT; |
| 66 | } |
| 67 | |
| 68 | if (stage != null) { |
| 69 | mEvent = event; |
| 70 | callTracker(stage, now); |
| 71 | } |
| 72 | |
| 73 | return mState != State.NOT_TRACKING; |
| 74 | } |
| 75 | |
| 76 | public boolean doKeyUp(int keyCode, KeyEvent event) { |
| 77 | boolean handled = false; |
| 78 | |
| 79 | if (mState == State.KEEP_TRACKING && mKeyCode == keyCode) { |
| 80 | mEvent = event; |
| 81 | callTracker(Stage.UP, System.currentTimeMillis()); |
| 82 | handled = mState != State.NOT_TRACKING; |
| 83 | } |
| 84 | mKeyCode = NOT_A_KEYCODE; |
| 85 | return handled; |
| 86 | } |
| 87 | |
| 88 | private void callTracker(Stage stage, long now) { |
| 89 | mState = mTracker.onKeyTracker(mKeyCode, mEvent, stage, (int)(now - mStartMS)); |
| 90 | } |
| 91 | |
| 92 | private void dump() { |
| 93 | System.out.println(" key=" + mKeyCode + " dur=" + (System.currentTimeMillis() - mStartMS) + |
| 94 | " state=" + mState); |
| 95 | } |
| 96 | |
| 97 | private int mKeyCode = NOT_A_KEYCODE; |
| 98 | private KeyEvent mEvent; |
| 99 | private long mStartMS; |
| 100 | private State mState; |
| 101 | private OnKeyTracker mTracker; |
| 102 | |
| 103 | private static final int LONG_PRESS_DURATION_MS = |
| 104 | ViewConfiguration.getLongPressTimeout(); |
| 105 | private static final int NOT_A_KEYCODE = -123456; |
| 106 | } |
| 107 | |