1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/*
* 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 org.kelar.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.FrameLayout;
import org.kelar.inputmethod.accessibility.AccessibilityUtils;
import org.kelar.inputmethod.keyboard.MainKeyboardView;
import org.kelar.inputmethod.latin.suggestions.MoreSuggestionsView;
import org.kelar.inputmethod.latin.suggestions.SuggestionStripView;
public final class InputView extends FrameLayout {
private final Rect mInputViewRect = new Rect();
private MainKeyboardView mMainKeyboardView;
private KeyboardTopPaddingForwarder mKeyboardTopPaddingForwarder;
private MoreSuggestionsViewCanceler mMoreSuggestionsViewCanceler;
private MotionEventForwarder<?, ?> mActiveForwarder;
public InputView(final Context context, final AttributeSet attrs) {
super(context, attrs, 0);
}
@Override
protected void onFinishInflate() {
final SuggestionStripView suggestionStripView =
(SuggestionStripView)findViewById(R.id.suggestion_strip_view);
mMainKeyboardView = (MainKeyboardView)findViewById(R.id.keyboard_view);
mKeyboardTopPaddingForwarder = new KeyboardTopPaddingForwarder(
mMainKeyboardView, suggestionStripView);
mMoreSuggestionsViewCanceler = new MoreSuggestionsViewCanceler(
mMainKeyboardView, suggestionStripView);
}
public void setKeyboardTopPadding(final int keyboardTopPadding) {
mKeyboardTopPaddingForwarder.setKeyboardTopPadding(keyboardTopPadding);
}
@Override
protected boolean dispatchHoverEvent(final MotionEvent event) {
if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()
&& mMainKeyboardView.isShowingMoreKeysPanel()) {
// With accessibility mode on, discard hover events while a more keys keyboard is shown.
// The {@link MoreKeysKeyboard} receives hover events directly from the platform.
return true;
}
return super.dispatchHoverEvent(event);
}
@Override
public boolean onInterceptTouchEvent(final MotionEvent 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;
// The touch events that hit the top padding of keyboard should be forwarded to
// {@link SuggestionStripView}.
if (mKeyboardTopPaddingForwarder.onInterceptTouchEvent(x, y, me)) {
mActiveForwarder = mKeyboardTopPaddingForwarder;
return true;
}
// To cancel {@link MoreSuggestionsView}, we should intercept a touch event to
// {@link MainKeyboardView} and dismiss the {@link MoreSuggestionsView}.
if (mMoreSuggestionsViewCanceler.onInterceptTouchEvent(x, y, me)) {
mActiveForwarder = mMoreSuggestionsViewCanceler;
return true;
}
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);
}
/**
* This class forwards series of {@link MotionEvent}s from <code>SenderView</code> to
* <code>ReceiverView</code>.
*
* @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>.
*/
private static abstract class
MotionEventForwarder<SenderView extends View, ReceiverView extends View> {
protected final SenderView mSenderView;
protected final ReceiverView mReceiverView;
protected final Rect mEventSendingRect = new Rect();
protected final Rect mEventReceivingRect = new Rect();
public MotionEventForwarder(final SenderView senderView, final ReceiverView receiverView) {
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);
// Translate global x-coordinate to <code>ReceiverView</code> local coordinate.
protected int translateX(final int x) {
return x - mEventReceivingRect.left;
}
// Translate global y-coordinate to <code>ReceiverView</code> local coordinate.
protected int translateY(final int y) {
return y - mEventReceivingRect.top;
}
/**
* Callback when a {@link MotionEvent} is forwarded.
* @param me the motion event to be forwarded.
*/
protected void onForwardingEvent(final MotionEvent me) {}
// 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.
if (mSenderView.getVisibility() != View.VISIBLE ||
mReceiverView.getVisibility() != View.VISIBLE) {
return false;
}
mSenderView.getGlobalVisibleRect(mEventSendingRect);
if (!mEventSendingRect.contains(x, y)) {
return false;
}
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>.
if (needsToForward(x, y)) {
return true;
}
}
return false;
}
// 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.
me.setLocation(translateX(x), translateY(y));
mReceiverView.dispatchTouchEvent(me);
onForwardingEvent(me);
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;
}
@Override
protected boolean needsToForward(final int x, final int y) {
// Forwarding an event only when {@link MainKeyboardView} is visible.
// Because the visibility of {@link MainKeyboardView} is controlled by its parent
// view in {@link KeyboardSwitcher#setMainKeyboardFrame()}, we should check the
// visibility of the parent view.
final View mainKeyboardFrame = (View)mSenderView.getParent();
return mainKeyboardFrame.getVisibility() == View.VISIBLE && isInKeyboardTopPadding(y);
}
@Override
protected int translateY(final int y) {
final int translatedY = super.translateY(y);
if (isInKeyboardTopPadding(y)) {
// The forwarded event should have coordinates that are inside of the target.
return Math.min(translatedY, mEventReceivingRect.height() - 1);
}
return translatedY;
}
}
/**
* This class forwards {@link MotionEvent}s happened in the {@link MainKeyboardView} to
* {@link SuggestionStripView} when the {@link MoreSuggestionsView} is showing.
* {@link SuggestionStripView} dismisses {@link MoreSuggestionsView} when it receives any event
* outside of it.
*/
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();
}
}
}
}
|