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;
|
|
|
|
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-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-08-31 06:26:32 +00:00
|
|
|
public class MiniKeyboardView extends KeyboardView implements MoreKeysPanel {
|
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;
|
|
|
|
|
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-24 05:44:46 +00:00
|
|
|
private static final TimerProxy EMPTY_TIMER_PROXY = new TimerProxy.Adapter();
|
2011-07-11 01:09:15 +00:00
|
|
|
|
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
|
2012-01-17 08:08:26 +00:00
|
|
|
public void onPressKey(int primaryCode) {
|
|
|
|
mListener.onPressKey(primaryCode);
|
2011-07-11 01:09:15 +00:00
|
|
|
}
|
2012-01-17 08:08:26 +00:00
|
|
|
|
2011-07-11 01:09:15 +00:00
|
|
|
@Override
|
2012-01-17 08:08:26 +00:00
|
|
|
public void onReleaseKey(int primaryCode, boolean withSliding) {
|
|
|
|
mListener.onReleaseKey(primaryCode, withSliding);
|
2011-07-11 01:09:15 +00:00
|
|
|
}
|
|
|
|
};
|
2011-04-27 05:14:45 +00:00
|
|
|
|
2011-08-31 05:22:53 +00:00
|
|
|
public MiniKeyboardView(Context context, AttributeSet attrs) {
|
|
|
|
this(context, attrs, R.attr.miniKeyboardViewStyle);
|
2011-04-27 05:14:45 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 05:22:53 +00:00
|
|
|
public MiniKeyboardView(Context context, AttributeSet attrs, 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(
|
|
|
|
res.getDimension(R.dimen.mini_keyboard_slide_allowance));
|
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() {
|
2011-10-26 08:03:41 +00:00
|
|
|
return this;
|
2011-07-11 01:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-08-29 08:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-08-31 06:26:32 +00:00
|
|
|
public void showMoreKeysPanel(View parentView, Controller controller, int pointX, int pointY,
|
2011-08-29 08:02:52 +00:00
|
|
|
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-10-26 08:03:41 +00:00
|
|
|
private boolean mIsDismissing;
|
|
|
|
|
2011-07-23 08:16:56 +00:00
|
|
|
@Override
|
2011-08-31 06:26:32 +00:00
|
|
|
public boolean dismissMoreKeysPanel() {
|
2011-10-26 08:03:41 +00:00
|
|
|
if (mIsDismissing) return false;
|
|
|
|
mIsDismissing = true;
|
|
|
|
final boolean dismissed = mController.dismissMoreKeysPanel();
|
|
|
|
mIsDismissing = false;
|
|
|
|
return dismissed;
|
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
|
|
|
}
|
|
|
|
}
|