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;
|
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class InputView extends LinearLayout {
|
2013-08-06 01:48:39 +00:00
|
|
|
private View mSuggestionStripView;
|
2011-09-20 07:16:34 +00:00
|
|
|
private View mKeyboardView;
|
|
|
|
private int mKeyboardTopPadding;
|
|
|
|
|
|
|
|
private boolean mIsForwardingEvent;
|
|
|
|
private final Rect mInputViewRect = new Rect();
|
|
|
|
private final Rect mEventForwardingRect = new Rect();
|
|
|
|
private final Rect mEventReceivingRect = new Rect();
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-08-06 01:59:19 +00:00
|
|
|
public void setKeyboardGeometry(final int keyboardTopPadding) {
|
2011-09-20 07:16:34 +00:00
|
|
|
mKeyboardTopPadding = keyboardTopPadding;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onFinishInflate() {
|
2013-08-06 01:48:39 +00:00
|
|
|
mSuggestionStripView = findViewById(R.id.suggestion_strip_view);
|
2011-09-20 07:16:34 +00:00
|
|
|
mKeyboardView = findViewById(R.id.keyboard_view);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-08-06 01:59:19 +00:00
|
|
|
public boolean dispatchTouchEvent(final MotionEvent me) {
|
|
|
|
if (mSuggestionStripView.getVisibility() != VISIBLE
|
|
|
|
|| mKeyboardView.getVisibility() != VISIBLE) {
|
|
|
|
return super.dispatchTouchEvent(me);
|
2011-09-20 07:16:34 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 01:59:19 +00:00
|
|
|
// The touch events that hit the top padding of keyboard should be forwarded to
|
|
|
|
// {@link SuggestionStripView}.
|
2011-09-20 07:16:34 +00:00
|
|
|
final Rect rect = mInputViewRect;
|
|
|
|
this.getGlobalVisibleRect(rect);
|
|
|
|
final int x = (int)me.getX() + rect.left;
|
|
|
|
final int y = (int)me.getY() + rect.top;
|
|
|
|
|
|
|
|
final Rect forwardingRect = mEventForwardingRect;
|
|
|
|
mKeyboardView.getGlobalVisibleRect(forwardingRect);
|
|
|
|
if (!mIsForwardingEvent && !forwardingRect.contains(x, y)) {
|
2013-08-06 01:59:19 +00:00
|
|
|
return super.dispatchTouchEvent(me);
|
2011-09-20 07:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final int forwardingLimitY = forwardingRect.top + mKeyboardTopPadding;
|
|
|
|
boolean sendToTarget = false;
|
|
|
|
|
|
|
|
switch (me.getAction()) {
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
if (y < forwardingLimitY) {
|
|
|
|
// This down event and further move and up events should be forwarded to the target.
|
|
|
|
mIsForwardingEvent = true;
|
|
|
|
sendToTarget = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
|
sendToTarget = mIsForwardingEvent;
|
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
case MotionEvent.ACTION_CANCEL:
|
|
|
|
sendToTarget = mIsForwardingEvent;
|
|
|
|
mIsForwardingEvent = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sendToTarget) {
|
2013-08-06 01:59:19 +00:00
|
|
|
return super.dispatchTouchEvent(me);
|
2011-09-20 07:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final Rect receivingRect = mEventReceivingRect;
|
2013-08-06 01:48:39 +00:00
|
|
|
mSuggestionStripView.getGlobalVisibleRect(receivingRect);
|
2011-09-20 07:16:34 +00:00
|
|
|
final int translatedX = x - receivingRect.left;
|
|
|
|
final int translatedY;
|
|
|
|
if (y < forwardingLimitY) {
|
|
|
|
// The forwarded event should have coordinates that are inside of the target.
|
|
|
|
translatedY = Math.min(y - receivingRect.top, receivingRect.height() - 1);
|
|
|
|
} else {
|
|
|
|
translatedY = y - receivingRect.top;
|
|
|
|
}
|
|
|
|
me.setLocation(translatedX, translatedY);
|
2013-08-06 01:48:39 +00:00
|
|
|
mSuggestionStripView.dispatchTouchEvent(me);
|
2011-09-20 07:16:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|