Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1275)

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/input/CursorAnchorInfoController.java

Issue 1589953005: Support InputMethodManager#updateCursorAnchorInfo for Android 5.0 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comment remove. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.content.browser.input;
6
7 import android.annotation.TargetApi;
8 import android.graphics.Matrix;
9 import android.os.Build;
10 import android.view.View;
11 import android.view.inputmethod.CursorAnchorInfo;
12 import android.view.inputmethod.InputConnection;
13
14 import org.chromium.base.VisibleForTesting;
15 import org.chromium.content.browser.RenderCoordinates;
16
17 import java.util.Arrays;
18
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21
22 /*
23 * A state machine interface which receives Chromium internal events to determin es when to call
24 * {@link InputMethodManager#updateCursorAnchorInfo(View, CursorAnchorInfo)}. Th is interface is
25 * also used in unit tests to mock out {@link CursorAnchorInfo}, which is availa ble only in
26 * Android 5.0 (Lollipop) and later.
27 */
28 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
29 final class CursorAnchorInfoController {
30 /**
31 * An interface to mock out {@link View#getLocationOnScreen(int[])} for test ing.
32 */
33 public interface ViewDelegate {
34 void getLocationOnScreen(View view, int[] location);
35 }
36
37 /**
38 * An interface to mock out composing text retrieval from ImeAdapter.
39 */
40 public interface ComposingTextDelegate {
41 public CharSequence getText();
42 public int getSelectionStart();
43 public int getSelectionEnd();
44 public int getComposingTextStart();
45 public int getComposingTextEnd();
46 }
47
48 // Current focus and monitoring states.
49 private boolean mIsEditable;
50 private boolean mHasPendingImmediateRequest;
51 private boolean mMonitorModeEnabled;
52
53 // Parmeter for CursorAnchorInfo, updated by setCompositionCharacterBounds.
54 @Nullable
55 private float[] mCompositionCharacterBounds;
56 // Paremeters for CursorAnchorInfo, updated by onUpdateFrameInfo.
57 private boolean mHasCoordinateInfo;
58 private float mScale;
59 private float mTranslationX;
60 private float mTranslationY;
61 private boolean mHasInsertionMarker;
62 private boolean mIsInsertionMarkerVisible;
63 private float mInsertionMarkerHorizontal;
64 private float mInsertionMarkerTop;
65 private float mInsertionMarkerBottom;
66
67 @Nullable
68 private CursorAnchorInfo mLastCursorAnchorInfo;
69
70 @Nonnull
71 private final Matrix mMatrix = new Matrix();
72 @Nonnull
73 private final int[] mViewOrigin = new int[2];
74 @Nonnull
75 private final CursorAnchorInfo.Builder mCursorAnchorInfoBuilder =
76 new CursorAnchorInfo.Builder();
77
78 @Nullable
79 private InputMethodManagerWrapper mInputMethodManagerWrapper;
80 @Nullable
81 private final ComposingTextDelegate mComposingTextDelegate;
82 @Nonnull
83 private final ViewDelegate mViewDelegate;
84
85 /**
86 * @return {@code true} if {@link CursorAnchorInfo} is supported on this dev ice.
87 */
88 public static boolean isSupported() {
89 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
90 return false;
91 }
92 return true;
93 }
94
95 private CursorAnchorInfoController(InputMethodManagerWrapper inputMethodMana gerWrapper,
96 ComposingTextDelegate composingTextDelegate, ViewDelegate viewDelega te) {
97 mInputMethodManagerWrapper = inputMethodManagerWrapper;
98 mComposingTextDelegate = composingTextDelegate;
99 mViewDelegate = viewDelegate;
100 }
101
102 public static CursorAnchorInfoController create(
103 InputMethodManagerWrapper inputMethodManagerWrapper,
104 ComposingTextDelegate composingTextDelegate) {
105 return isSupported() ? new CursorAnchorInfoController(inputMethodManager Wrapper,
106 composingTextDelegate, new ViewDelegate() {
107 @Override
108 public void getLocationOnScreen(View view, int[] location) {
109 view.getLocationOnScreen(location);
110 }
111 }) : null;
112 }
113
114 @VisibleForTesting
115 public void setInputMethodManagerWrapper(InputMethodManagerWrapper inputMeth odManagerWrapper) {
116 mInputMethodManagerWrapper = inputMethodManagerWrapper;
117 }
118
119 @VisibleForTesting
120 public static CursorAnchorInfoController createForTest(
121 InputMethodManagerWrapper inputMethodManagerWrapper,
122 ComposingTextDelegate composingTextDelegate,
123 ViewDelegate viewDelegate) {
124 return new CursorAnchorInfoController(inputMethodManagerWrapper, composi ngTextDelegate,
125 viewDelegate);
126 }
127
128 /**
129 * Called by ImeAdapter when a IME related web content state is changed.
130 */
131 public void invalidateLastCursorAnchorInfo() {
132 if (!mIsEditable) return;
133
134 mLastCursorAnchorInfo = null;
135 }
136
137 /**
138 * Sets positional information of composing text as an array of character bo unds.
139 * @param compositionCharacterBounds Array of character bounds in local coor dinates.
140 */
141 public void setCompositionCharacterBounds(float[] compositionCharacterBounds ) {
142 if (!mIsEditable) return;
143
144 if (!Arrays.equals(compositionCharacterBounds, mCompositionCharacterBoun ds)) {
145 mLastCursorAnchorInfo = null;
146 mCompositionCharacterBounds = compositionCharacterBounds;
147 }
148 }
149
150 /**
151 * Sets coordinates system parameters and selection marker information.
152 * @param hasInsertionMarker {@code true} if the insertion marker exists.
153 * @param isInsertionMarkerVisible {@code true} if the insertion insertion m arker is visible.
154 * @param insertionMarkerHorizontal X coordinate of the top of the first sel ection marker.
155 * @param insertionMarkerTop Y coordinate of the top of the first selection marker.
156 * @param insertionMarkerBottom Y coordinate of the bottom of the first sele ction marker.
157 * @param view The attached view.
158 */
159 public void onUpdateFrameInfo(@Nonnull RenderCoordinates renderCoordinates,
160 boolean hasInsertionMarker, boolean isInsertionMarkerVisible,
161 float insertionMarkerHorizontal, float insertionMarkerTop,
162 float insertionMarkerBottom, @Nonnull View view) {
163 if (!mIsEditable) return;
164
165 // Reuse {@param #mViewOrigin} to avoid object creation, as this method is supposed to be
166 // called at relatively high rate.
167 mViewDelegate.getLocationOnScreen(view, mViewOrigin);
aelias_OOO_until_Jul13 2016/02/26 08:25:33 You've tested that this gets the right position fo
kinaba 2016/03/01 08:46:46 At least in WebView shell (android_webview_apk), y
168
169 // Character bounds and insertion marker locations come in device indepe ndent pixels
170 // relative from the top-left corner of the web view content area. (In o ther words, the
171 // effects of various kinds of zooming and scrolling are already taken i nto account.)
172 //
173 // We need to prepare parameters that convert such values to physical pi xels, in the
174 // screen coordinate. Hence the following values are derived.
175 float scale = renderCoordinates.getDeviceScaleFactor();
176 float translationX = mViewOrigin[0];
177 float translationY = mViewOrigin[1] + renderCoordinates.getContentOffset YPix();
178
179 if (!mHasCoordinateInfo
180 || Math.abs(scale - mScale) > 1e-5
181 || Math.abs(translationX - mTranslationX) > 1e-5
182 || Math.abs(translationY - mTranslationY) > 1e-5
183 || hasInsertionMarker != mHasInsertionMarker
184 || isInsertionMarkerVisible != mIsInsertionMarkerVisible
185 || Math.abs(insertionMarkerHorizontal - mInsertionMarkerHorizont al) > 1e-5
186 || Math.abs(insertionMarkerTop - mInsertionMarkerTop) > 1e-5
187 || Math.abs(insertionMarkerBottom - mInsertionMarkerBottom) > 1e -5) {
188 mLastCursorAnchorInfo = null;
189 mHasCoordinateInfo = true;
190 mScale = scale;
191 mTranslationX = translationX;
192 mTranslationY = translationY;
193 mHasInsertionMarker = hasInsertionMarker;
194 mIsInsertionMarkerVisible = isInsertionMarkerVisible;
195 mInsertionMarkerHorizontal = insertionMarkerHorizontal;
196 mInsertionMarkerTop = insertionMarkerTop;
197 mInsertionMarkerBottom = insertionMarkerBottom;
198 }
199
200 // Notify to IME if there is a pending request, or if it is in monitor m ode and we have
201 // some change in the state.
202 if (mHasPendingImmediateRequest
203 || (mMonitorModeEnabled && mLastCursorAnchorInfo == null)) {
204 updateCursorAnchorInfo(view);
205 }
206 }
207
208 /**
209 * Resets the current state on update monitoring mode to the default (= do n othing.)
210 */
211 public void resetMonitoringState() {
212 mMonitorModeEnabled = false;
213 mHasPendingImmediateRequest = false;
214 }
215
216 public void focusedNodeChanged(boolean isEditable) {
217 mIsEditable = isEditable;
218 mCompositionCharacterBounds = null;
219 mHasCoordinateInfo = false;
220 mLastCursorAnchorInfo = null;
221 }
222
223 public boolean onRequestCursorUpdates(int cursorUpdateMode, View view) {
224 if (!mIsEditable) return false;
225
226 mMonitorModeEnabled = (cursorUpdateMode & InputConnection.CURSOR_UPDATE_ MONITOR) != 0;
227 if ((cursorUpdateMode & InputConnection.CURSOR_UPDATE_IMMEDIATE) != 0) {
228 mHasPendingImmediateRequest = true;
229 updateCursorAnchorInfo(view);
230 }
231 return true;
232 }
233
234 /**
235 * Computes the CursorAnchorInfo instance and notify to InputMethodManager i f needed.
236 */
237 private void updateCursorAnchorInfo(View view) {
238 if (!mHasCoordinateInfo) return;
239
240 if (mLastCursorAnchorInfo == null) {
241 // Reuse the builder object.
242 mCursorAnchorInfoBuilder.reset();
243
244 CharSequence text = mComposingTextDelegate.getText();
245 int selectionStart = mComposingTextDelegate.getSelectionStart();
246 int selectionEnd = mComposingTextDelegate.getSelectionEnd();
247 int composingTextStart = mComposingTextDelegate.getComposingTextStar t();
248 int composingTextEnd = mComposingTextDelegate.getComposingTextEnd();
249 if (text != null && 0 <= composingTextStart && composingTextEnd <= t ext.length()) {
250 mCursorAnchorInfoBuilder.setComposingText(composingTextStart,
251 text.subSequence(composingTextStart, composingTextEnd));
252 float[] compositionCharacterBounds = mCompositionCharacterBounds ;
253 if (compositionCharacterBounds != null) {
254 int numCharacter = compositionCharacterBounds.length / 4;
255 for (int i = 0; i < numCharacter; ++i) {
256 float left = compositionCharacterBounds[i * 4];
257 float top = compositionCharacterBounds[i * 4 + 1];
258 float right = compositionCharacterBounds[i * 4 + 2];
259 float bottom = compositionCharacterBounds[i * 4 + 3];
260 int charIndex = composingTextStart + i;
261 mCursorAnchorInfoBuilder.addCharacterBounds(charIndex, l eft, top, right,
262 bottom, CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION );
263 }
264 }
265 }
266 mCursorAnchorInfoBuilder.setSelectionRange(selectionStart, selection End);
267 mMatrix.setScale(mScale, mScale);
268 mMatrix.postTranslate(mTranslationX, mTranslationY);
269 mCursorAnchorInfoBuilder.setMatrix(mMatrix);
270 if (mHasInsertionMarker) {
271 mCursorAnchorInfoBuilder.setInsertionMarkerLocation(
272 mInsertionMarkerHorizontal,
273 mInsertionMarkerTop,
274 mInsertionMarkerBottom,
275 mInsertionMarkerBottom,
276 mIsInsertionMarkerVisible ? CursorAnchorInfo.FLAG_HAS_VI SIBLE_REGION :
277 CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION);
278 }
279 mLastCursorAnchorInfo = mCursorAnchorInfoBuilder.build();
280 }
281
282 if (mInputMethodManagerWrapper != null) {
283 mInputMethodManagerWrapper.updateCursorAnchorInfo(view, mLastCursorA nchorInfo);
284 }
285 mHasPendingImmediateRequest = false;
286 }
287 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698