1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.latin;
18
19import android.content.Context;
20import android.graphics.Rect;
21import android.util.AttributeSet;
22import android.view.MotionEvent;
23import android.view.View;
24import android.widget.LinearLayout;
25
26public final class InputView extends LinearLayout {
27    private View mSuggestionStripView;
28    private View mKeyboardView;
29    private int mKeyboardTopPadding;
30
31    private boolean mIsForwardingEvent;
32    private final Rect mInputViewRect = new Rect();
33    private final Rect mEventForwardingRect = new Rect();
34    private final Rect mEventReceivingRect = new Rect();
35
36    public InputView(final Context context, final AttributeSet attrs) {
37        super(context, attrs, 0);
38    }
39
40    public void setKeyboardGeometry(final int keyboardTopPadding) {
41        mKeyboardTopPadding = keyboardTopPadding;
42    }
43
44    @Override
45    protected void onFinishInflate() {
46        mSuggestionStripView = findViewById(R.id.suggestion_strip_view);
47        mKeyboardView = findViewById(R.id.keyboard_view);
48    }
49
50    @Override
51    public boolean dispatchTouchEvent(final MotionEvent me) {
52        if (mSuggestionStripView.getVisibility() != VISIBLE
53                || mKeyboardView.getVisibility() != VISIBLE) {
54            return super.dispatchTouchEvent(me);
55        }
56
57        // The touch events that hit the top padding of keyboard should be forwarded to
58        // {@link SuggestionStripView}.
59        final Rect rect = mInputViewRect;
60        this.getGlobalVisibleRect(rect);
61        final int x = (int)me.getX() + rect.left;
62        final int y = (int)me.getY() + rect.top;
63
64        final Rect forwardingRect = mEventForwardingRect;
65        mKeyboardView.getGlobalVisibleRect(forwardingRect);
66        if (!mIsForwardingEvent && !forwardingRect.contains(x, y)) {
67            return super.dispatchTouchEvent(me);
68        }
69
70        final int forwardingLimitY = forwardingRect.top + mKeyboardTopPadding;
71        boolean sendToTarget = false;
72
73        switch (me.getAction()) {
74        case MotionEvent.ACTION_DOWN:
75            if (y < forwardingLimitY) {
76                // This down event and further move and up events should be forwarded to the target.
77                mIsForwardingEvent = true;
78                sendToTarget = true;
79            }
80            break;
81        case MotionEvent.ACTION_MOVE:
82            sendToTarget = mIsForwardingEvent;
83            break;
84        case MotionEvent.ACTION_UP:
85        case MotionEvent.ACTION_CANCEL:
86            sendToTarget = mIsForwardingEvent;
87            mIsForwardingEvent = false;
88            break;
89        }
90
91        if (!sendToTarget) {
92            return super.dispatchTouchEvent(me);
93        }
94
95        final Rect receivingRect = mEventReceivingRect;
96        mSuggestionStripView.getGlobalVisibleRect(receivingRect);
97        final int translatedX = x - receivingRect.left;
98        final int translatedY;
99        if (y < forwardingLimitY) {
100            // The forwarded event should have coordinates that are inside of the target.
101            translatedY = Math.min(y - receivingRect.top, receivingRect.height() - 1);
102        } else {
103            translatedY = y - receivingRect.top;
104        }
105        me.setLocation(translatedX, translatedY);
106        mSuggestionStripView.dispatchTouchEvent(me);
107        return true;
108    }
109}
110