aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/EmojiAltPhysicalKeyDetector.java
diff options
context:
space:
mode:
authorTadashi G. Takaoka <takaoka@google.com>2014-10-27 11:59:57 +0900
committerTadashi G. Takaoka <takaoka@google.com>2014-10-27 16:26:22 +0900
commitf7e01e866ffa89b4dd7e66c471ed9fc275a637a2 (patch)
tree94a28eeb180206044d517533053835646414cb56 /java/src/com/android/inputmethod/latin/EmojiAltPhysicalKeyDetector.java
parent53b010384e7e883b532d3f65a5e90a0889d315e0 (diff)
downloadlatinime-f7e01e866ffa89b4dd7e66c471ed9fc275a637a2.tar.gz
latinime-f7e01e866ffa89b4dd7e66c471ed9fc275a637a2.tar.xz
latinime-f7e01e866ffa89b4dd7e66c471ed9fc275a637a2.zip
Remove device checking of Emoji physical key
This CL also adds a settings option to enable/disable an Emoji-ALT physical key. Bug: 18122464 Change-Id: Iee1d97efec979a902b0492071d5e511ca1792ff0
Diffstat (limited to 'java/src/com/android/inputmethod/latin/EmojiAltPhysicalKeyDetector.java')
-rw-r--r--java/src/com/android/inputmethod/latin/EmojiAltPhysicalKeyDetector.java93
1 files changed, 93 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/latin/EmojiAltPhysicalKeyDetector.java b/java/src/com/android/inputmethod/latin/EmojiAltPhysicalKeyDetector.java
new file mode 100644
index 000000000..8116a4983
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/EmojiAltPhysicalKeyDetector.java
@@ -0,0 +1,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;
+ }
+ }
+}