1 /*
2  * Copyright (C) 2014 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.inputmethod.accessibility;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.Message;
22 
23 import com.android.inputmethod.keyboard.Key;
24 import com.android.inputmethod.latin.R;
25 
26 // Handling long press timer to show a more keys keyboard.
27 final class AccessibilityLongPressTimer extends Handler {
28     public interface LongPressTimerCallback {
performLongClickOn(Key key)29         public void performLongClickOn(Key key);
30     }
31 
32     private static final int MSG_LONG_PRESS = 1;
33 
34     private final LongPressTimerCallback mCallback;
35     private final long mConfigAccessibilityLongPressTimeout;
36 
AccessibilityLongPressTimer(final LongPressTimerCallback callback, final Context context)37     public AccessibilityLongPressTimer(final LongPressTimerCallback callback,
38             final Context context) {
39         super();
40         mCallback = callback;
41         mConfigAccessibilityLongPressTimeout = context.getResources().getInteger(
42                 R.integer.config_accessibility_long_press_key_timeout);
43     }
44 
45     @Override
handleMessage(final Message msg)46     public void handleMessage(final Message msg) {
47         switch (msg.what) {
48         case MSG_LONG_PRESS:
49             cancelLongPress();
50             mCallback.performLongClickOn((Key)msg.obj);
51             return;
52         default:
53             super.handleMessage(msg);
54             return;
55         }
56     }
57 
startLongPress(final Key key)58     public void startLongPress(final Key key) {
59         cancelLongPress();
60         final Message longPressMessage = obtainMessage(MSG_LONG_PRESS, key);
61         sendMessageDelayed(longPressMessage, mConfigAccessibilityLongPressTimeout);
62     }
63 
cancelLongPress()64     public void cancelLongPress() {
65         removeMessages(MSG_LONG_PRESS);
66     }
67 }
68