aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/EmojiAltPhysicalKeyDetector.java
blob: 8116a4983a7ea81a44d439c96453b12d2bb36ae3 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (C) 2014, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.inputmethod.latin;

import android.view.KeyEvent;

import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.latin.settings.Settings;

/**
 * A class for detecting Emoji-Alt physical key.
 */
final class EmojiAltPhysicalKeyDetector {
    // True if the Alt key has been used as a modifier. In this case the Alt key up isn't
    // recognized as an emoji key.
    private boolean mAltHasBeenUsedAsAModifier;

    /**
     * Record a down key event.
     * @param keyEvent a down key event.
     */
    public void onKeyDown(final KeyEvent keyEvent) {
        if (isAltKey(keyEvent)) {
            mAltHasBeenUsedAsAModifier = false;
        }
        if (containsAltModifier(keyEvent)) {
            mAltHasBeenUsedAsAModifier = true;
        }
    }

    /**
     * Determine whether an up key event is a special key up or not.
     * @param keyEvent an up key event.
     */
    public void onKeyUp(final KeyEvent keyEvent) {
        if (keyEvent.isCanceled()) {
            // This key up event was a part of key combinations and should be ignored.
            return;
        }
        if (!isAltKey(keyEvent)) {
            mAltHasBeenUsedAsAModifier |= containsAltModifier(keyEvent);
            return;
        }
        if (containsAltModifier(keyEvent)) {
            mAltHasBeenUsedAsAModifier = true;
            return;
        }
        if (!Settings.getInstance().getCurrent().mEnableEmojiAltPhysicalKey) {
            return;
        }
        if (!mAltHasBeenUsedAsAModifier) {
            onEmojiAltKeyDetected();
        }
    }

    private static void onEmojiAltKeyDetected() {
        KeyboardSwitcher.getInstance().onToggleEmojiKeyboard();
    }

    private static boolean isAltKey(final KeyEvent keyEvent) {
        final int keyCode = keyEvent.getKeyCode();
        return keyCode == KeyEvent.KEYCODE_ALT_LEFT || keyCode == KeyEvent.KEYCODE_ALT_RIGHT;
    }

    private static boolean containsAltModifier(final KeyEvent keyEvent) {
        final int metaState = keyEvent.getMetaState();
        // TODO: Support multiple keyboards. Take device id into account.
        switch (keyEvent.getKeyCode()) {
        case KeyEvent.KEYCODE_ALT_LEFT:
            // Return true if Left-Alt is pressed with Right-Alt pressed.
            return (metaState & KeyEvent.META_ALT_RIGHT_ON) != 0;
        case KeyEvent.KEYCODE_ALT_RIGHT:
            // Return true if Right-Alt is pressed with Left-Alt pressed.
            return (metaState & KeyEvent.META_ALT_LEFT_ON) != 0;
        default:
            return (metaState & (KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_RIGHT_ON)) != 0;
        }
    }
}