2014-10-27 02:59:57 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2015-02-26 19:10:37 +00:00
|
|
|
import android.util.Log;
|
2014-10-27 02:59:57 +00:00
|
|
|
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 {
|
2015-02-26 19:10:37 +00:00
|
|
|
private static final String TAG = "EmojiAltPhysicalKeyDetector";
|
|
|
|
|
|
|
|
private final RichInputConnection mRichInputConnection;
|
|
|
|
|
2014-10-27 02:59:57 +00:00
|
|
|
// 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;
|
|
|
|
|
2015-02-26 19:10:37 +00:00
|
|
|
public EmojiAltPhysicalKeyDetector(final RichInputConnection richInputConnection) {
|
|
|
|
mRichInputConnection = richInputConnection;
|
|
|
|
}
|
|
|
|
|
2014-10-27 02:59:57 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2015-02-26 19:10:37 +00:00
|
|
|
if (mAltHasBeenUsedAsAModifier) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!mRichInputConnection.isConnected()) {
|
|
|
|
Log.w(TAG, "onKeyUp() : No connection to text view");
|
|
|
|
return;
|
2014-10-27 02:59:57 +00:00
|
|
|
}
|
2015-02-26 19:10:37 +00:00
|
|
|
onEmojiAltKeyDetected();
|
2014-10-27 02:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|