diff options
| author | Tadashi G. Takaoka <takaoka@google.com> | 2011-04-27 14:14:45 +0900 |
|---|---|---|
| committer | Tadashi G. Takaoka <takaoka@google.com> | 2011-05-09 19:17:39 +0900 |
| commit | ff082d081f3ea18ff0b9b22126ee4a86504cf83c (patch) | |
| tree | b770b3c3025b9f0c544598e12b8437fb091a975e /java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java | |
| parent | aac2d87dcf8f199d942c61a1115746d61f181675 (diff) | |
Refactor KeyboardView and create MiniKeyboardView
Change-Id: I8d68b944762ccde05020978f20b3742eb6ab945b
Diffstat (limited to 'java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java')
| -rw-r--r-- | java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java new file mode 100644 index 000000000..12031f1ea --- /dev/null +++ b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java @@ -0,0 +1,124 @@ +/* + * 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 com.android.inputmethod.latin.R; + +import android.content.Context; +import android.content.res.Resources; +import android.os.SystemClock; +import android.util.AttributeSet; +import android.view.Gravity; +import android.view.MotionEvent; +import android.view.View; +import android.widget.PopupWindow; + +/** + * A view that renders a virtual {@link MiniKeyboard}. It handles rendering of keys and detecting + * key presses and touch movements. + */ +public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel { + private final int[] mCoordinates = new int[2]; + private final boolean mConfigShowMiniKeyboardAtTouchedPoint; + + private int mOriginX; + private int mOriginY; + private int mTrackerId; + private long mDownTime; + + public PopupMiniKeyboardView(Context context, AttributeSet attrs) { + this(context, attrs, R.attr.keyboardViewStyle); + } + + public PopupMiniKeyboardView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + + final Resources res = context.getResources(); + mConfigShowMiniKeyboardAtTouchedPoint = res.getBoolean( + R.bool.config_show_mini_keyboard_at_touched_point); + // Override default ProximityKeyDetector. + mKeyDetector = new MiniKeyboardKeyDetector(res.getDimension( + R.dimen.mini_keyboard_slide_allowance)); + // Remove gesture detector on mini-keyboard + mGestureDetector = null; + setKeyPreviewEnabled(false); + } + + @Override + public void setKeyPreviewEnabled(boolean previewEnabled) { + // Mini keyboard needs no pop-up key preview displayed. + super.setKeyPreviewEnabled(false); + } + + @Override + public void showPanel(KeyboardView parentKeyboardView, Key parentKey, + PointerTracker tracker, int keyPreviewY, PopupWindow window) { + final View container = (View)getParent(); + final MiniKeyboard miniKeyboard = (MiniKeyboard)getKeyboard(); + final Keyboard parentKeyboard = parentKeyboardView.getKeyboard(); + + parentKeyboardView.getLocationInWindow(mCoordinates); + final int pointX = (mConfigShowMiniKeyboardAtTouchedPoint) ? tracker.getLastX() + : parentKey.mX + parentKey.mWidth / 2; + final int pointY = parentKey.mY; + final int miniKeyboardX = pointX - miniKeyboard.getDefaultCoordX() + - container.getPaddingLeft() + + parentKeyboardView.getPaddingLeft() + mCoordinates[0]; + final int miniKeyboardY = pointY - parentKeyboard.getVerticalGap() + - (container.getMeasuredHeight() - container.getPaddingBottom()) + + parentKeyboardView.getPaddingTop() + mCoordinates[1]; + final int x = miniKeyboardX; + final int y = parentKeyboardView.isKeyPreviewEnabled() && miniKeyboard.isOneRowKeyboard() + ? keyPreviewY : miniKeyboardY; + + if (miniKeyboard.setShifted(parentKeyboard.isShiftedOrShiftLocked())) { + invalidateAllKeys(); + } + window.setContentView(container); + window.setWidth(container.getMeasuredWidth()); + window.setHeight(container.getMeasuredHeight()); + window.showAtLocation(parentKeyboardView, Gravity.NO_GRAVITY, x, y); + + mOriginX = x + container.getPaddingLeft() - mCoordinates[0]; + mOriginY = y + container.getPaddingTop() - mCoordinates[1]; + mTrackerId = tracker.mPointerId; + mDownTime = SystemClock.uptimeMillis(); + + // Inject down event on the key to mini keyboard. + final MotionEvent downEvent = translateMotionEvent(MotionEvent.ACTION_DOWN, pointX, + pointY + parentKey.mHeight / 2, mDownTime); + onTouchEvent(downEvent); + downEvent.recycle(); + } + + private MotionEvent translateMotionEvent(int action, float x, float y, long eventTime) { + return MotionEvent.obtain(mDownTime, eventTime, action, x - mOriginX, y - mOriginY, 0); + } + + @Override + public boolean onTouchEvent(MotionEvent me) { + final int index = me.getActionIndex(); + final int id = me.getPointerId(index); + if (id == mTrackerId) { + final MotionEvent translated = translateMotionEvent(me.getAction(), me.getX(index), + me.getY(index), me.getEventTime()); + super.onTouchEvent(translated); + translated.recycle(); + } + return true; + } +} |
