2011-04-27 05:14:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 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.keyboard;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.util.AttributeSet;
|
2012-12-03 02:47:52 +00:00
|
|
|
import android.view.MotionEvent;
|
2011-04-27 05:14:45 +00:00
|
|
|
import android.view.View;
|
|
|
|
|
2012-08-21 05:05:57 +00:00
|
|
|
import com.android.inputmethod.latin.Constants;
|
2011-05-25 06:22:03 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CoordinateUtils;
|
2011-05-25 06:22:03 +00:00
|
|
|
|
2011-04-27 05:14:45 +00:00
|
|
|
/**
|
2012-02-08 07:12:11 +00:00
|
|
|
* A view that renders a virtual {@link MoreKeysKeyboard}. It handles rendering of keys and
|
|
|
|
* detecting key presses and touch movements.
|
2011-04-27 05:14:45 +00:00
|
|
|
*/
|
2012-12-03 02:47:52 +00:00
|
|
|
public class MoreKeysKeyboardView extends KeyboardView implements MoreKeysPanel {
|
2012-11-28 08:25:23 +00:00
|
|
|
private final int[] mCoordinates = CoordinateUtils.newInstance();
|
2011-04-27 05:14:45 +00:00
|
|
|
|
2013-04-15 03:57:10 +00:00
|
|
|
protected final KeyDetector mKeyDetector;
|
2013-07-25 08:03:16 +00:00
|
|
|
private Controller mController = EMPTY_CONTROLLER;
|
2012-12-03 02:47:52 +00:00
|
|
|
protected KeyboardActionListener mListener;
|
2011-04-27 05:14:45 +00:00
|
|
|
private int mOriginX;
|
|
|
|
private int mOriginY;
|
2012-12-03 02:47:52 +00:00
|
|
|
private Key mCurrentKey;
|
2011-07-11 01:09:15 +00:00
|
|
|
|
2012-12-03 02:47:52 +00:00
|
|
|
private int mActivePointerId;
|
2011-04-27 05:14:45 +00:00
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
public MoreKeysKeyboardView(final Context context, final AttributeSet attrs) {
|
2012-02-08 07:12:11 +00:00
|
|
|
this(context, attrs, R.attr.moreKeysKeyboardViewStyle);
|
2011-04-27 05:14:45 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
public MoreKeysKeyboardView(final Context context, final AttributeSet attrs,
|
|
|
|
final int defStyle) {
|
2011-04-27 05:14:45 +00:00
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
|
|
|
final Resources res = context.getResources();
|
2011-09-06 02:37:51 +00:00
|
|
|
mKeyDetector = new MoreKeysDetector(
|
2012-02-08 07:12:11 +00:00
|
|
|
res.getDimension(R.dimen.more_keys_keyboard_slide_allowance));
|
2011-04-27 05:14:45 +00:00
|
|
|
}
|
|
|
|
|
2011-07-21 06:57:00 +00:00
|
|
|
@Override
|
2012-10-03 06:19:43 +00:00
|
|
|
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
|
2011-07-21 06:57:00 +00:00
|
|
|
final Keyboard keyboard = getKeyboard();
|
|
|
|
if (keyboard != null) {
|
2011-07-29 00:05:40 +00:00
|
|
|
final int width = keyboard.mOccupiedWidth + getPaddingLeft() + getPaddingRight();
|
|
|
|
final int height = keyboard.mOccupiedHeight + getPaddingTop() + getPaddingBottom();
|
2011-07-21 06:57:00 +00:00
|
|
|
setMeasuredDimension(width, height);
|
|
|
|
} else {
|
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-11 01:09:15 +00:00
|
|
|
@Override
|
2012-10-03 06:19:43 +00:00
|
|
|
public void setKeyboard(final Keyboard keyboard) {
|
2011-07-11 01:09:15 +00:00
|
|
|
super.setKeyboard(keyboard);
|
|
|
|
mKeyDetector.setKeyboard(keyboard, -getPaddingLeft(),
|
2013-04-11 03:08:36 +00:00
|
|
|
-getPaddingTop() + getVerticalCorrection());
|
2011-07-11 01:09:15 +00:00
|
|
|
}
|
|
|
|
|
2011-08-29 08:02:52 +00:00
|
|
|
@Override
|
2012-10-03 06:19:43 +00:00
|
|
|
public void showMoreKeysPanel(final View parentView, final Controller controller,
|
2012-12-03 06:49:10 +00:00
|
|
|
final int pointX, final int pointY, final KeyboardActionListener listener) {
|
2011-08-29 08:02:52 +00:00
|
|
|
mController = controller;
|
|
|
|
mListener = listener;
|
2012-12-03 06:49:10 +00:00
|
|
|
final View container = getContainerView();
|
2012-03-07 10:09:36 +00:00
|
|
|
// The coordinates of panel's left-top corner in parentView's coordinate system.
|
2012-12-03 02:47:52 +00:00
|
|
|
final int x = pointX - getDefaultCoordX() - container.getPaddingLeft();
|
2012-04-27 14:44:28 +00:00
|
|
|
final int y = pointY - container.getMeasuredHeight() + container.getPaddingBottom();
|
2011-04-27 05:14:45 +00:00
|
|
|
|
2012-03-07 10:09:36 +00:00
|
|
|
parentView.getLocationInWindow(mCoordinates);
|
2012-12-03 06:49:10 +00:00
|
|
|
// Ensure the horizontal position of the panel does not extend past the screen edges.
|
|
|
|
final int maxX = parentView.getMeasuredWidth() - container.getMeasuredWidth();
|
2012-12-14 04:57:50 +00:00
|
|
|
final int panelX = Math.max(0, Math.min(maxX, x)) + CoordinateUtils.x(mCoordinates);
|
2012-12-03 06:49:10 +00:00
|
|
|
final int panelY = y + CoordinateUtils.y(mCoordinates);
|
|
|
|
container.setX(panelX);
|
|
|
|
container.setY(panelY);
|
2011-04-27 05:14:45 +00:00
|
|
|
|
2012-03-07 10:09:36 +00:00
|
|
|
mOriginX = x + container.getPaddingLeft();
|
|
|
|
mOriginY = y + container.getPaddingTop();
|
2012-12-03 06:49:10 +00:00
|
|
|
controller.onShowMoreKeysPanel(this);
|
2011-08-26 06:45:05 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* Returns the default x coordinate for showing this panel.
|
|
|
|
*/
|
|
|
|
protected int getDefaultCoordX() {
|
|
|
|
return ((MoreKeysKeyboard)getKeyboard()).getDefaultCoordX();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDownEvent(final int x, final int y, final int pointerId, final long eventTime) {
|
|
|
|
mActivePointerId = pointerId;
|
|
|
|
onMoveKeyInternal(x, y, pointerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMoveEvent(int x, int y, final int pointerId, long eventTime) {
|
2012-12-10 20:38:29 +00:00
|
|
|
if (mActivePointerId != pointerId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final boolean hasOldKey = (mCurrentKey != null);
|
2012-12-03 02:47:52 +00:00
|
|
|
onMoveKeyInternal(x, y, pointerId);
|
2012-12-10 20:38:29 +00:00
|
|
|
if (hasOldKey && mCurrentKey == null) {
|
|
|
|
// If the pointer has moved too far away from any target then cancel the panel.
|
2013-07-25 09:07:46 +00:00
|
|
|
mController.onCancelMoreKeysPanel(this);
|
2012-12-10 20:38:29 +00:00
|
|
|
}
|
2012-12-03 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpEvent(final int x, final int y, final int pointerId, final long eventTime) {
|
|
|
|
if (mCurrentKey != null && mActivePointerId == pointerId) {
|
|
|
|
updateReleaseKeyGraphics(mCurrentKey);
|
2013-08-12 09:05:11 +00:00
|
|
|
onCodeInput(mCurrentKey.getCode(), x, y);
|
2012-12-03 02:47:52 +00:00
|
|
|
mCurrentKey = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs the specific action for this panel when the user presses a key on the panel.
|
|
|
|
*/
|
|
|
|
protected void onCodeInput(final int code, final int x, final int y) {
|
|
|
|
if (code == Constants.CODE_OUTPUT_TEXT) {
|
|
|
|
mListener.onTextInput(mCurrentKey.getOutputText());
|
|
|
|
} else if (code != Constants.CODE_UNSPECIFIED) {
|
|
|
|
mListener.onCodeInput(code, x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onMoveKeyInternal(int x, int y, int pointerId) {
|
|
|
|
if (mActivePointerId != pointerId) {
|
|
|
|
// Ignore old pointers when newer pointer is active.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final Key oldKey = mCurrentKey;
|
|
|
|
final Key newKey = mKeyDetector.detectHitKey(x, y);
|
|
|
|
if (newKey != oldKey) {
|
|
|
|
mCurrentKey = newKey;
|
|
|
|
invalidateKey(mCurrentKey);
|
|
|
|
if (oldKey != null) {
|
|
|
|
updateReleaseKeyGraphics(oldKey);
|
|
|
|
}
|
|
|
|
if (newKey != null) {
|
|
|
|
updatePressKeyGraphics(newKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateReleaseKeyGraphics(final Key key) {
|
|
|
|
key.onReleased();
|
|
|
|
invalidateKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updatePressKeyGraphics(final Key key) {
|
|
|
|
key.onPressed();
|
|
|
|
invalidateKey(key);
|
|
|
|
}
|
|
|
|
|
2011-07-23 08:16:56 +00:00
|
|
|
@Override
|
2013-07-25 08:03:16 +00:00
|
|
|
public void dismissMoreKeysPanel() {
|
|
|
|
if (!isShowingInParent()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-07-25 09:07:46 +00:00
|
|
|
mController.onDismissMoreKeysPanel(this);
|
2011-07-23 08:16:56 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 05:14:45 +00:00
|
|
|
@Override
|
2012-10-03 06:19:43 +00:00
|
|
|
public int translateX(final int x) {
|
2011-07-11 01:09:15 +00:00
|
|
|
return x - mOriginX;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-10-03 06:19:43 +00:00
|
|
|
public int translateY(final int y) {
|
2011-07-11 01:09:15 +00:00
|
|
|
return y - mOriginY;
|
2011-04-27 05:14:45 +00:00
|
|
|
}
|
2012-12-03 06:49:10 +00:00
|
|
|
|
2012-12-03 02:47:52 +00:00
|
|
|
@Override
|
|
|
|
public boolean onTouchEvent(final MotionEvent me) {
|
|
|
|
final int action = me.getActionMasked();
|
|
|
|
final long eventTime = me.getEventTime();
|
|
|
|
final int index = me.getActionIndex();
|
|
|
|
final int x = (int)me.getX(index);
|
|
|
|
final int y = (int)me.getY(index);
|
|
|
|
final int pointerId = me.getPointerId(index);
|
|
|
|
switch (action) {
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
case MotionEvent.ACTION_POINTER_DOWN:
|
|
|
|
onDownEvent(x, y, pointerId, eventTime);
|
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
case MotionEvent.ACTION_POINTER_UP:
|
|
|
|
onUpEvent(x, y, pointerId, eventTime);
|
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
|
onMoveEvent(x, y, pointerId, eventTime);
|
|
|
|
break;
|
|
|
|
}
|
2013-08-01 07:55:55 +00:00
|
|
|
return true;
|
2012-12-03 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 06:49:10 +00:00
|
|
|
@Override
|
|
|
|
public View getContainerView() {
|
|
|
|
return (View)getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isShowingInParent() {
|
|
|
|
return (getContainerView().getParent() != null);
|
|
|
|
}
|
2011-04-27 05:14:45 +00:00
|
|
|
}
|