2011-09-20 07:16:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2011-09-20 07:16:34 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-09-20 07:16:34 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2011-09-20 07:16:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
2013-12-16 08:05:07 +00:00
|
|
|
import com.android.inputmethod.keyboard.MainKeyboardView;
|
2013-12-16 09:02:58 +00:00
|
|
|
import com.android.inputmethod.latin.suggestions.MoreSuggestionsView;
|
2013-12-16 08:05:07 +00:00
|
|
|
import com.android.inputmethod.latin.suggestions.SuggestionStripView;
|
2011-09-20 07:16:34 +00:00
|
|
|
|
2013-12-16 08:05:07 +00:00
|
|
|
public final class InputView extends LinearLayout {
|
2011-09-20 07:16:34 +00:00
|
|
|
private final Rect mInputViewRect = new Rect();
|
2013-12-16 08:05:07 +00:00
|
|
|
private KeyboardTopPaddingForwarder mKeyboardTopPaddingForwarder;
|
2013-12-16 09:02:58 +00:00
|
|
|
private MoreSuggestionsViewCanceler mMoreSuggestionsViewCanceler;
|
2014-02-17 08:36:34 +00:00
|
|
|
private MotionEventForwarder<?, ?> mActiveForwarder;
|
2011-09-20 07:16:34 +00:00
|
|
|
|
2013-08-06 01:59:19 +00:00
|
|
|
public InputView(final Context context, final AttributeSet attrs) {
|
2011-09-20 07:16:34 +00:00
|
|
|
super(context, attrs, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onFinishInflate() {
|
2013-12-16 08:05:07 +00:00
|
|
|
final SuggestionStripView suggestionStripView =
|
|
|
|
(SuggestionStripView)findViewById(R.id.suggestion_strip_view);
|
|
|
|
final MainKeyboardView mainKeyboardView =
|
|
|
|
(MainKeyboardView)findViewById(R.id.keyboard_view);
|
|
|
|
mKeyboardTopPaddingForwarder = new KeyboardTopPaddingForwarder(
|
|
|
|
mainKeyboardView, suggestionStripView);
|
2013-12-16 09:02:58 +00:00
|
|
|
mMoreSuggestionsViewCanceler = new MoreSuggestionsViewCanceler(
|
|
|
|
mainKeyboardView, suggestionStripView);
|
2013-12-16 08:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setKeyboardTopPadding(final int keyboardTopPadding) {
|
|
|
|
mKeyboardTopPaddingForwarder.setKeyboardTopPadding(keyboardTopPadding);
|
2011-09-20 07:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-17 08:36:34 +00:00
|
|
|
public boolean onInterceptTouchEvent(final MotionEvent me) {
|
2011-09-20 07:16:34 +00:00
|
|
|
final Rect rect = mInputViewRect;
|
2013-12-16 08:05:07 +00:00
|
|
|
getGlobalVisibleRect(rect);
|
2014-02-17 08:36:34 +00:00
|
|
|
final int index = me.getActionIndex();
|
|
|
|
final int x = (int)me.getX(index) + rect.left;
|
|
|
|
final int y = (int)me.getY(index) + rect.top;
|
|
|
|
|
|
|
|
// The touch events that hit the top padding of keyboard should be forwarded to
|
|
|
|
// {@link SuggestionStripView}.
|
|
|
|
if (mKeyboardTopPaddingForwarder.onInterceptTouchEvent(x, y, me)) {
|
|
|
|
mActiveForwarder = mKeyboardTopPaddingForwarder;
|
2013-12-16 08:05:07 +00:00
|
|
|
return true;
|
2011-09-20 07:16:34 +00:00
|
|
|
}
|
2014-02-17 08:36:34 +00:00
|
|
|
|
2013-12-16 09:02:58 +00:00
|
|
|
// To cancel {@link MoreSuggestionsView}, we should intercept a touch event to
|
|
|
|
// {@link MainKeyboardView} and dismiss the {@link MoreSuggestionsView}.
|
2014-02-17 08:36:34 +00:00
|
|
|
if (mMoreSuggestionsViewCanceler.onInterceptTouchEvent(x, y, me)) {
|
|
|
|
mActiveForwarder = mMoreSuggestionsViewCanceler;
|
2013-12-16 09:02:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-17 08:36:34 +00:00
|
|
|
|
|
|
|
mActiveForwarder = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onTouchEvent(final MotionEvent me) {
|
|
|
|
if (mActiveForwarder == null) {
|
|
|
|
return super.onTouchEvent(me);
|
|
|
|
}
|
|
|
|
|
|
|
|
final Rect rect = mInputViewRect;
|
|
|
|
getGlobalVisibleRect(rect);
|
|
|
|
final int index = me.getActionIndex();
|
|
|
|
final int x = (int)me.getX(index) + rect.left;
|
|
|
|
final int y = (int)me.getY(index) + rect.top;
|
|
|
|
return mActiveForwarder.onTouchEvent(x, y, me);
|
2013-12-16 08:05:07 +00:00
|
|
|
}
|
2011-09-20 07:16:34 +00:00
|
|
|
|
2013-12-16 08:05:07 +00:00
|
|
|
/**
|
2014-02-17 08:36:34 +00:00
|
|
|
* This class forwards series of {@link MotionEvent}s from <code>SenderView</code> to
|
|
|
|
* <code>ReceiverView</code>.
|
2013-12-16 08:05:07 +00:00
|
|
|
*
|
2014-02-17 08:36:34 +00:00
|
|
|
* @param <SenderView> a {@link View} that may send a {@link MotionEvent} to <ReceiverView>.
|
|
|
|
* @param <ReceiverView> a {@link View} that receives forwarded {@link MotionEvent} from
|
|
|
|
* <SenderView>.
|
2013-12-16 08:05:07 +00:00
|
|
|
*/
|
2014-02-17 08:36:34 +00:00
|
|
|
private static abstract class
|
|
|
|
MotionEventForwarder<SenderView extends View, ReceiverView extends View> {
|
|
|
|
protected final SenderView mSenderView;
|
|
|
|
protected final ReceiverView mReceiverView;
|
2013-12-16 08:05:07 +00:00
|
|
|
|
|
|
|
protected final Rect mEventSendingRect = new Rect();
|
|
|
|
protected final Rect mEventReceivingRect = new Rect();
|
|
|
|
|
2014-02-17 08:36:34 +00:00
|
|
|
public MotionEventForwarder(final SenderView senderView, final ReceiverView receiverView) {
|
2013-12-16 08:05:07 +00:00
|
|
|
mSenderView = senderView;
|
|
|
|
mReceiverView = receiverView;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if a touch event of global coordinate x, y needs to be forwarded.
|
|
|
|
protected abstract boolean needsToForward(final int x, final int y);
|
|
|
|
|
2014-02-17 08:36:34 +00:00
|
|
|
// Translate global x-coordinate to <code>ReceiverView</code> local coordinate.
|
2013-12-16 08:05:07 +00:00
|
|
|
protected int translateX(final int x) {
|
|
|
|
return x - mEventReceivingRect.left;
|
|
|
|
}
|
|
|
|
|
2014-02-17 08:36:34 +00:00
|
|
|
// Translate global y-coordinate to <code>ReceiverView</code> local coordinate.
|
2013-12-16 08:05:07 +00:00
|
|
|
protected int translateY(final int y) {
|
|
|
|
return y - mEventReceivingRect.top;
|
|
|
|
}
|
2011-09-20 07:16:34 +00:00
|
|
|
|
2013-12-16 09:02:58 +00:00
|
|
|
// Callback when a {@link MotionEvent} is forwarded.
|
|
|
|
protected void onForwardingEvent(final MotionEvent me) {}
|
|
|
|
|
2014-02-17 08:36:34 +00:00
|
|
|
// Returns true if a {@link MotionEvent} is needed to be forwarded to
|
|
|
|
// <code>ReceiverView</code>. Otherwise returns false.
|
|
|
|
public boolean onInterceptTouchEvent(final int x, final int y, final MotionEvent me) {
|
|
|
|
// Forwards a {link MotionEvent} only if both <code>SenderView</code> and
|
|
|
|
// <code>ReceiverView</code> are visible.
|
2013-12-16 08:05:07 +00:00
|
|
|
if (mSenderView.getVisibility() != View.VISIBLE ||
|
|
|
|
mReceiverView.getVisibility() != View.VISIBLE) {
|
|
|
|
return false;
|
2011-09-20 07:16:34 +00:00
|
|
|
}
|
2014-02-17 08:36:34 +00:00
|
|
|
mSenderView.getGlobalVisibleRect(mEventSendingRect);
|
|
|
|
if (!mEventSendingRect.contains(x, y)) {
|
2013-12-16 08:05:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-17 08:36:34 +00:00
|
|
|
if (me.getActionMasked() == MotionEvent.ACTION_DOWN) {
|
|
|
|
// If the down event happens in the forwarding area, successive
|
|
|
|
// {@link MotionEvent}s should be forwarded to <code>ReceiverView</code>.
|
2013-12-16 08:05:07 +00:00
|
|
|
if (needsToForward(x, y)) {
|
2014-02-17 08:36:34 +00:00
|
|
|
return true;
|
2013-12-16 08:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 08:36:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-12-16 08:05:07 +00:00
|
|
|
|
2014-02-17 08:36:34 +00:00
|
|
|
// Returns true if a {@link MotionEvent} is forwarded to <code>ReceiverView</code>.
|
|
|
|
// Otherwise returns false.
|
|
|
|
public boolean onTouchEvent(final int x, final int y, final MotionEvent me) {
|
|
|
|
mReceiverView.getGlobalVisibleRect(mEventReceivingRect);
|
|
|
|
// Translate global coordinates to <code>ReceiverView</code> local coordinates.
|
2013-12-16 08:05:07 +00:00
|
|
|
me.setLocation(translateX(x), translateY(y));
|
|
|
|
mReceiverView.dispatchTouchEvent(me);
|
2013-12-16 09:02:58 +00:00
|
|
|
onForwardingEvent(me);
|
2013-12-16 08:05:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class forwards {@link MotionEvent}s happened in the top padding of
|
|
|
|
* {@link MainKeyboardView} to {@link SuggestionStripView}.
|
|
|
|
*/
|
|
|
|
private static class KeyboardTopPaddingForwarder
|
|
|
|
extends MotionEventForwarder<MainKeyboardView, SuggestionStripView> {
|
|
|
|
private int mKeyboardTopPadding;
|
|
|
|
|
|
|
|
public KeyboardTopPaddingForwarder(final MainKeyboardView mainKeyboardView,
|
|
|
|
final SuggestionStripView suggestionStripView) {
|
|
|
|
super(mainKeyboardView, suggestionStripView);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setKeyboardTopPadding(final int keyboardTopPadding) {
|
|
|
|
mKeyboardTopPadding = keyboardTopPadding;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isInKeyboardTopPadding(final int y) {
|
|
|
|
return y < mEventSendingRect.top + mKeyboardTopPadding;
|
2011-09-20 07:16:34 +00:00
|
|
|
}
|
|
|
|
|
2013-12-16 08:05:07 +00:00
|
|
|
@Override
|
|
|
|
protected boolean needsToForward(final int x, final int y) {
|
|
|
|
return isInKeyboardTopPadding(y);
|
2011-09-20 07:16:34 +00:00
|
|
|
}
|
|
|
|
|
2013-12-16 08:05:07 +00:00
|
|
|
@Override
|
|
|
|
protected int translateY(final int y) {
|
|
|
|
final int translatedY = super.translateY(y);
|
|
|
|
if (isInKeyboardTopPadding(y)) {
|
2014-02-17 08:36:34 +00:00
|
|
|
// The forwarded event should have coordinates that are inside of the target.
|
2013-12-16 08:05:07 +00:00
|
|
|
return Math.min(translatedY, mEventReceivingRect.height() - 1);
|
|
|
|
}
|
|
|
|
return translatedY;
|
2011-09-20 07:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-16 09:02:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class forwards {@link MotionEvent}s happened in the {@link MainKeyboardView} to
|
|
|
|
* {@link SuggestionStripView} when the {@link MoreSuggestionsView} is showing.
|
2014-02-17 08:36:34 +00:00
|
|
|
* {@link SuggestionStripView} dismisses {@link MoreSuggestionsView} when it receives any event
|
|
|
|
* outside of it.
|
2013-12-16 09:02:58 +00:00
|
|
|
*/
|
|
|
|
private static class MoreSuggestionsViewCanceler
|
|
|
|
extends MotionEventForwarder<MainKeyboardView, SuggestionStripView> {
|
|
|
|
public MoreSuggestionsViewCanceler(final MainKeyboardView mainKeyboardView,
|
|
|
|
final SuggestionStripView suggestionStripView) {
|
|
|
|
super(mainKeyboardView, suggestionStripView);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean needsToForward(final int x, final int y) {
|
|
|
|
return mReceiverView.isShowingMoreSuggestionPanel() && mEventSendingRect.contains(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onForwardingEvent(final MotionEvent me) {
|
|
|
|
if (me.getActionMasked() == MotionEvent.ACTION_DOWN) {
|
|
|
|
mReceiverView.dismissMoreSuggestionsPanel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-20 07:16:34 +00:00
|
|
|
}
|