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;
|
2011-07-11 01:09:15 +00:00
|
|
|
import android.content.res.TypedArray;
|
2011-04-27 05:14:45 +00:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.Gravity;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.PopupWindow;
|
|
|
|
|
2011-07-11 01:09:15 +00:00
|
|
|
import com.android.inputmethod.keyboard.PointerTracker.DrawingProxy;
|
|
|
|
import com.android.inputmethod.keyboard.PointerTracker.TimerProxy;
|
2011-05-25 06:22:03 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
|
|
|
|
2011-08-23 03:08:36 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2011-04-27 05:14:45 +00:00
|
|
|
/**
|
|
|
|
* A view that renders a virtual {@link MiniKeyboard}. It handles rendering of keys and detecting
|
|
|
|
* key presses and touch movements.
|
|
|
|
*/
|
2011-07-11 01:09:15 +00:00
|
|
|
public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel {
|
2011-04-27 05:14:45 +00:00
|
|
|
private final int[] mCoordinates = new int[2];
|
|
|
|
|
2011-07-11 01:09:15 +00:00
|
|
|
private final KeyDetector mKeyDetector;
|
|
|
|
private final int mVerticalCorrection;
|
|
|
|
|
2011-08-29 08:02:52 +00:00
|
|
|
private Controller mController;
|
|
|
|
private KeyboardActionListener mListener;
|
2011-04-27 05:14:45 +00:00
|
|
|
private int mOriginX;
|
|
|
|
private int mOriginY;
|
2011-07-11 01:09:15 +00:00
|
|
|
|
2011-08-23 03:08:36 +00:00
|
|
|
private static class MiniKeyboardKeyDetector extends KeyDetector {
|
|
|
|
private final int mSlideAllowanceSquare;
|
|
|
|
private final int mSlideAllowanceSquareTop;
|
|
|
|
|
|
|
|
public MiniKeyboardKeyDetector(float slideAllowance) {
|
|
|
|
super(/* keyHysteresisDistance */0);
|
|
|
|
mSlideAllowanceSquare = (int)(slideAllowance * slideAllowance);
|
|
|
|
// Top slide allowance is slightly longer (sqrt(2) times) than other edges.
|
|
|
|
mSlideAllowanceSquareTop = mSlideAllowanceSquare * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean alwaysAllowsSlidingInput() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected int getMaxNearbyKeys() {
|
|
|
|
// No nearby key will be returned.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getKeyIndexAndNearbyCodes(int x, int y, final int[] allCodes) {
|
|
|
|
final List<Key> keys = getKeyboard().mKeys;
|
|
|
|
final int touchX = getTouchX(x);
|
|
|
|
final int touchY = getTouchY(y);
|
|
|
|
|
|
|
|
int nearestIndex = NOT_A_KEY;
|
|
|
|
int nearestDist = (y < 0) ? mSlideAllowanceSquareTop : mSlideAllowanceSquare;
|
|
|
|
final int keyCount = keys.size();
|
|
|
|
for (int index = 0; index < keyCount; index++) {
|
|
|
|
final int dist = keys.get(index).squaredDistanceToEdge(touchX, touchY);
|
|
|
|
if (dist < nearestDist) {
|
|
|
|
nearestIndex = index;
|
|
|
|
nearestDist = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allCodes != null && nearestIndex != NOT_A_KEY)
|
|
|
|
allCodes[0] = keys.get(nearestIndex).mCode;
|
|
|
|
return nearestIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-11 01:09:15 +00:00
|
|
|
private static final TimerProxy EMPTY_TIMER_PROXY = new TimerProxy() {
|
|
|
|
@Override
|
|
|
|
public void startKeyRepeatTimer(long delay, int keyIndex, PointerTracker tracker) {}
|
|
|
|
@Override
|
|
|
|
public void startLongPressTimer(long delay, int keyIndex, PointerTracker tracker) {}
|
|
|
|
@Override
|
2011-07-17 21:37:30 +00:00
|
|
|
public void cancelLongPressTimer() {}
|
2011-07-11 01:09:15 +00:00
|
|
|
@Override
|
|
|
|
public void cancelKeyTimers() {}
|
|
|
|
};
|
|
|
|
|
2011-08-29 10:09:56 +00:00
|
|
|
private final KeyboardActionListener mMiniKeyboardListener =
|
|
|
|
new KeyboardActionListener.Adapter() {
|
2011-07-11 01:09:15 +00:00
|
|
|
@Override
|
|
|
|
public void onCodeInput(int primaryCode, int[] keyCodes, int x, int y) {
|
2011-08-29 08:02:52 +00:00
|
|
|
mListener.onCodeInput(primaryCode, keyCodes, x, y);
|
2011-07-11 01:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTextInput(CharSequence text) {
|
2011-08-29 08:02:52 +00:00
|
|
|
mListener.onTextInput(text);
|
2011-07-11 01:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCancelInput() {
|
2011-08-29 08:02:52 +00:00
|
|
|
mListener.onCancelInput();
|
2011-07-11 01:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPress(int primaryCode, boolean withSliding) {
|
2011-08-29 08:02:52 +00:00
|
|
|
mListener.onPress(primaryCode, withSliding);
|
2011-07-11 01:09:15 +00:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void onRelease(int primaryCode, boolean withSliding) {
|
2011-08-29 08:02:52 +00:00
|
|
|
mListener.onRelease(primaryCode, withSliding);
|
2011-07-11 01:09:15 +00:00
|
|
|
}
|
|
|
|
};
|
2011-04-27 05:14:45 +00:00
|
|
|
|
|
|
|
public PopupMiniKeyboardView(Context context, AttributeSet attrs) {
|
2011-05-24 12:08:20 +00:00
|
|
|
this(context, attrs, R.attr.popupMiniKeyboardViewStyle);
|
2011-04-27 05:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public PopupMiniKeyboardView(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
2011-07-11 01:09:15 +00:00
|
|
|
final TypedArray a = context.obtainStyledAttributes(
|
|
|
|
attrs, R.styleable.KeyboardView, defStyle, R.style.KeyboardView);
|
|
|
|
mVerticalCorrection = a.getDimensionPixelOffset(
|
|
|
|
R.styleable.KeyboardView_verticalCorrection, 0);
|
|
|
|
a.recycle();
|
|
|
|
|
2011-04-27 05:14:45 +00:00
|
|
|
final Resources res = context.getResources();
|
|
|
|
// Override default ProximityKeyDetector.
|
|
|
|
mKeyDetector = new MiniKeyboardKeyDetector(res.getDimension(
|
|
|
|
R.dimen.mini_keyboard_slide_allowance));
|
|
|
|
// Remove gesture detector on mini-keyboard
|
2011-05-11 11:51:07 +00:00
|
|
|
setKeyPreviewPopupEnabled(false, 0);
|
2011-04-27 05:14:45 +00:00
|
|
|
}
|
|
|
|
|
2011-07-21 06:57:00 +00:00
|
|
|
@Override
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
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
|
|
|
|
public void setKeyboard(Keyboard keyboard) {
|
|
|
|
super.setKeyboard(keyboard);
|
|
|
|
mKeyDetector.setKeyboard(keyboard, -getPaddingLeft(),
|
|
|
|
-getPaddingTop() + mVerticalCorrection);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public KeyDetector getKeyDetector() {
|
|
|
|
return mKeyDetector;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public KeyboardActionListener getKeyboardActionListener() {
|
2011-08-29 08:02:52 +00:00
|
|
|
return mMiniKeyboardListener;
|
2011-07-11 01:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DrawingProxy getDrawingProxy() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TimerProxy getTimerProxy() {
|
|
|
|
return EMPTY_TIMER_PROXY;
|
|
|
|
}
|
|
|
|
|
2011-04-27 05:14:45 +00:00
|
|
|
@Override
|
2011-05-11 11:51:07 +00:00
|
|
|
public void setKeyPreviewPopupEnabled(boolean previewEnabled, int delay) {
|
|
|
|
// Mini keyboard needs no pop-up key preview displayed, so we pass always false with a
|
|
|
|
// delay of 0. The delay does not matter actually since the popup is not shown anyway.
|
|
|
|
super.setKeyPreviewPopupEnabled(false, 0);
|
2011-04-27 05:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-08-29 08:02:52 +00:00
|
|
|
public void setShifted(boolean shifted) {
|
2011-08-30 09:56:27 +00:00
|
|
|
final Keyboard keyboard = getKeyboard();
|
|
|
|
if (keyboard.setShifted(shifted)) {
|
2011-08-29 08:02:52 +00:00
|
|
|
invalidateAllKeys();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void showPopupPanel(View parentView, Controller controller, int pointX, int pointY,
|
|
|
|
PopupWindow window, KeyboardActionListener listener) {
|
|
|
|
mController = controller;
|
|
|
|
mListener = listener;
|
2011-04-27 05:14:45 +00:00
|
|
|
final View container = (View)getParent();
|
|
|
|
final MiniKeyboard miniKeyboard = (MiniKeyboard)getKeyboard();
|
|
|
|
|
2011-08-29 08:02:52 +00:00
|
|
|
parentView.getLocationInWindow(mCoordinates);
|
2011-06-03 12:28:24 +00:00
|
|
|
final int miniKeyboardLeft = pointX - miniKeyboard.getDefaultCoordX()
|
2011-08-29 08:02:52 +00:00
|
|
|
+ parentView.getPaddingLeft();
|
2011-08-26 06:45:05 +00:00
|
|
|
final int x = wrapUp(Math.max(0, Math.min(miniKeyboardLeft,
|
2011-08-29 08:02:52 +00:00
|
|
|
parentView.getWidth() - miniKeyboard.mOccupiedWidth))
|
2011-08-26 06:45:05 +00:00
|
|
|
- container.getPaddingLeft() + mCoordinates[0],
|
2011-08-29 08:02:52 +00:00
|
|
|
container.getMeasuredWidth(), 0, parentView.getWidth());
|
|
|
|
final int y = pointY
|
2011-04-27 05:14:45 +00:00
|
|
|
- (container.getMeasuredHeight() - container.getPaddingBottom())
|
2011-08-29 08:02:52 +00:00
|
|
|
+ parentView.getPaddingTop() + mCoordinates[1];
|
2011-04-27 05:14:45 +00:00
|
|
|
|
|
|
|
window.setContentView(container);
|
|
|
|
window.setWidth(container.getMeasuredWidth());
|
|
|
|
window.setHeight(container.getMeasuredHeight());
|
2011-08-29 08:02:52 +00:00
|
|
|
window.showAtLocation(parentView, Gravity.NO_GRAVITY, x, y);
|
2011-04-27 05:14:45 +00:00
|
|
|
|
|
|
|
mOriginX = x + container.getPaddingLeft() - mCoordinates[0];
|
|
|
|
mOriginY = y + container.getPaddingTop() - mCoordinates[1];
|
|
|
|
}
|
|
|
|
|
2011-08-26 06:45:05 +00:00
|
|
|
private static int wrapUp(int x, int width, int left, int right) {
|
|
|
|
if (x < left)
|
|
|
|
return left;
|
|
|
|
if (x + width > right)
|
|
|
|
return right - width;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2011-07-23 08:16:56 +00:00
|
|
|
@Override
|
|
|
|
public boolean dismissPopupPanel() {
|
2011-08-29 08:02:52 +00:00
|
|
|
return mController.dismissPopupPanel();
|
2011-07-23 08:16:56 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 05:14:45 +00:00
|
|
|
@Override
|
2011-07-11 01:09:15 +00:00
|
|
|
public int translateX(int x) {
|
|
|
|
return x - mOriginX;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int translateY(int y) {
|
|
|
|
return y - mOriginY;
|
2011-04-27 05:14:45 +00:00
|
|
|
}
|
|
|
|
}
|