OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.contextualsearch; | 5 package org.chromium.chrome.browser.contextualsearch; |
6 | 6 |
7 import android.os.Handler; | 7 import android.os.Handler; |
8 | 8 |
9 import org.chromium.base.VisibleForTesting; | 9 import org.chromium.base.VisibleForTesting; |
10 import org.chromium.chrome.browser.ChromeActivity; | 10 import org.chromium.chrome.browser.ChromeActivity; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 private final Handler mRunnableHandler; | 47 private final Handler mRunnableHandler; |
48 private final float mPxToDp; | 48 private final float mPxToDp; |
49 private final Pattern mContainsWordPattern; | 49 private final Pattern mContainsWordPattern; |
50 | 50 |
51 private String mSelectedText; | 51 private String mSelectedText; |
52 private SelectionType mSelectionType; | 52 private SelectionType mSelectionType; |
53 private boolean mWasTapGestureDetected; | 53 private boolean mWasTapGestureDetected; |
54 private boolean mIsSelectionBeingModified; | 54 private boolean mIsSelectionBeingModified; |
55 private boolean mWasLastTapValid; | 55 private boolean mWasLastTapValid; |
56 private boolean mIsWaitingForInvalidTapDetection; | 56 private boolean mIsWaitingForInvalidTapDetection; |
57 private boolean mIsSelectionBoundsDissolved; | |
57 private boolean mShouldHandleSelectionModification; | 58 private boolean mShouldHandleSelectionModification; |
58 private boolean mDidExpandSelection; | 59 private boolean mDidExpandSelection; |
59 | 60 |
60 private float mX; | 61 private float mX; |
61 private float mY; | 62 private float mY; |
62 | 63 |
63 private class ContextualSearchGestureStateListener extends GestureStateListe ner { | 64 private class ContextualSearchGestureStateListener extends GestureStateListe ner { |
64 @Override | 65 @Override |
65 public void onScrollStarted(int scrollOffsetY, int scrollExtentY) { | 66 public void onScrollStarted(int scrollOffsetY, int scrollExtentY) { |
66 mHandler.handleScroll(); | 67 mHandler.handleScroll(); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 } | 171 } |
171 if (mIsSelectionBeingModified) { | 172 if (mIsSelectionBeingModified) { |
172 mSelectedText = selection; | 173 mSelectedText = selection; |
173 mHandler.handleSelectionModification(selection, mX, mY); | 174 mHandler.handleSelectionModification(selection, mX, mY); |
174 } else if (mWasTapGestureDetected) { | 175 } else if (mWasTapGestureDetected) { |
175 mSelectedText = selection; | 176 mSelectedText = selection; |
176 mSelectionType = SelectionType.TAP; | 177 mSelectionType = SelectionType.TAP; |
177 handleSelection(selection, mSelectionType); | 178 handleSelection(selection, mSelectionType); |
178 mWasTapGestureDetected = false; | 179 mWasTapGestureDetected = false; |
179 } | 180 } |
181 mIsSelectionBoundsDissolved = false; | |
180 } | 182 } |
181 | 183 |
182 /** | 184 /** |
183 * Handles a notification that a selection event took place. | 185 * Handles a notification that a selection event took place. |
184 * @param eventType The type of event that took place. | 186 * @param eventType The type of event that took place. |
185 * @param posXPix The x coordinate of the selection start handle. | 187 * @param posXPix The x coordinate of the selection start handle. |
186 * @param posYPix The y coordinate of the selection start handle. | 188 * @param posYPix The y coordinate of the selection start handle. |
187 */ | 189 */ |
188 void handleSelectionEvent(int eventType, float posXPix, float posYPix) { | 190 void handleSelectionEvent(int eventType, float posXPix, float posYPix) { |
189 boolean shouldHandleSelection = false; | 191 boolean shouldHandleSelection = false; |
190 switch (eventType) { | 192 switch (eventType) { |
191 case SelectionEventType.SELECTION_SHOWN: | 193 case SelectionEventType.SELECTION_SHOWN: |
192 mWasTapGestureDetected = false; | 194 mWasTapGestureDetected = false; |
193 mSelectionType = SelectionType.LONG_PRESS; | 195 mSelectionType = SelectionType.LONG_PRESS; |
194 shouldHandleSelection = true; | 196 shouldHandleSelection = true; |
195 break; | 197 break; |
196 case SelectionEventType.SELECTION_CLEARED: | 198 case SelectionEventType.SELECTION_CLEARED: |
197 mHandler.handleSelectionDismissal(); | 199 mHandler.handleSelectionDismissal(); |
198 resetAllStates(); | 200 resetAllStates(); |
199 break; | 201 break; |
200 case SelectionEventType.SELECTION_DRAG_STARTED: | 202 case SelectionEventType.SELECTION_DRAG_STARTED: |
201 mIsSelectionBeingModified = true; | 203 mIsSelectionBeingModified = true; |
202 break; | 204 break; |
203 case SelectionEventType.SELECTION_DRAG_STOPPED: | 205 case SelectionEventType.SELECTION_DRAG_STOPPED: |
204 mIsSelectionBeingModified = false; | 206 mIsSelectionBeingModified = false; |
205 shouldHandleSelection = mShouldHandleSelectionModification; | 207 shouldHandleSelection = mShouldHandleSelectionModification; |
206 break; | 208 break; |
209 case SelectionEventType.BOUNDS_DISSOLVED: | |
210 mIsSelectionBoundsDissolved = true; | |
jdduke (slow)
2015/07/15 23:08:51
Shouldn't you reset this when there's a new select
| |
211 break; | |
207 default: | 212 default: |
208 } | 213 } |
209 | 214 |
210 if (shouldHandleSelection) { | 215 if (shouldHandleSelection) { |
211 ContentViewCore baseContentView = getBaseContentView(); | 216 ContentViewCore baseContentView = getBaseContentView(); |
212 if (baseContentView != null) { | 217 if (baseContentView != null) { |
213 String selection = baseContentView.getSelectedText(); | 218 String selection = baseContentView.getSelectedText(); |
214 if (selection != null) { | 219 if (selection != null) { |
215 mX = posXPix; | 220 mX = posXPix; |
216 mY = posYPix; | 221 mY = posYPix; |
217 mSelectedText = selection; | 222 mSelectedText = selection; |
218 handleSelection(selection, SelectionType.LONG_PRESS); | 223 handleSelection(selection, SelectionType.LONG_PRESS); |
219 } | 224 } |
220 } | 225 } |
221 } | 226 } |
222 } | 227 } |
223 | 228 |
224 /** | 229 /** |
225 * Re-enables selection modification handling and invokes | 230 * Re-enables selection modification handling and invokes |
226 * ContextualSearchSelectionHandler.handleSelection(). | 231 * ContextualSearchSelectionHandler.handleSelection(). |
227 * @param selection The text that was selected. | 232 * @param selection The text that was selected. |
228 * @param type The type of selection made by the user. | 233 * @param type The type of selection made by the user. |
229 */ | 234 */ |
230 private void handleSelection(String selection, SelectionType type) { | 235 private void handleSelection(String selection, SelectionType type) { |
231 mShouldHandleSelectionModification = true; | 236 mShouldHandleSelectionModification = true; |
232 mHandler.handleSelection(selection, isValidSelection(selection), type, m X, mY); | 237 mHandler.handleSelection(selection, isValidSelection(selection), type, m X, mY); |
233 } | 238 } |
234 | 239 |
235 | |
236 /** | 240 /** |
237 * Resets all internal state of this class, including the tap state. | 241 * Resets all internal state of this class, including the tap state. |
238 */ | 242 */ |
239 private void resetAllStates() { | 243 private void resetAllStates() { |
240 resetSelectionStates(); | 244 resetSelectionStates(); |
241 mWasLastTapValid = false; | 245 mWasLastTapValid = false; |
242 } | 246 } |
243 | 247 |
244 /** | 248 /** |
245 * Resets all of the internal state of this class that handles the selection . | 249 * Resets all of the internal state of this class that handles the selection . |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
356 } | 360 } |
357 | 361 |
358 /** | 362 /** |
359 * @return whether a tap gesture has been detected, for testing. | 363 * @return whether a tap gesture has been detected, for testing. |
360 */ | 364 */ |
361 @VisibleForTesting | 365 @VisibleForTesting |
362 boolean wasAnyTapGestureDetected() { | 366 boolean wasAnyTapGestureDetected() { |
363 return mIsWaitingForInvalidTapDetection; | 367 return mIsWaitingForInvalidTapDetection; |
364 } | 368 } |
365 | 369 |
370 /** | |
371 * @return whether an insertion point is currently shown, for testing. | |
372 */ | |
373 @VisibleForTesting | |
374 boolean isSelectionBoundsDissolved() { | |
375 return mIsSelectionBoundsDissolved; | |
376 } | |
377 | |
366 /** Determines if the given selection is valid or not. | 378 /** Determines if the given selection is valid or not. |
367 * @param selection The selection portion of the context. | 379 * @param selection The selection portion of the context. |
368 * @return whether the given selection is considered a valid target for a se arch. | 380 * @return whether the given selection is considered a valid target for a se arch. |
369 */ | 381 */ |
370 private boolean isValidSelection(String selection) { | 382 private boolean isValidSelection(String selection) { |
371 return isValidSelection(selection, getBaseContentView()); | 383 return isValidSelection(selection, getBaseContentView()); |
372 } | 384 } |
373 | 385 |
374 @VisibleForTesting | 386 @VisibleForTesting |
375 boolean isValidSelection(String selection, ContentViewCore baseContentView) { | 387 boolean isValidSelection(String selection, ContentViewCore baseContentView) { |
376 if (selection.length() > MAX_SELECTION_LENGTH || !doesContainAWord(selec tion)) { | 388 if (selection.length() > MAX_SELECTION_LENGTH || !doesContainAWord(selec tion)) { |
377 return false; | 389 return false; |
378 } | 390 } |
379 return baseContentView != null && !baseContentView.isFocusedNodeEditable (); | 391 return baseContentView != null && !baseContentView.isFocusedNodeEditable (); |
380 } | 392 } |
381 | 393 |
382 /** | 394 /** |
383 * Determines if the given selection contains a word or not. | 395 * Determines if the given selection contains a word or not. |
384 * @param selection The the selection to check for a word. | 396 * @param selection The the selection to check for a word. |
385 * @return Whether the selection contains a word anywhere within it or not. | 397 * @return Whether the selection contains a word anywhere within it or not. |
386 */ | 398 */ |
387 @VisibleForTesting | 399 @VisibleForTesting |
388 public boolean doesContainAWord(String selection) { | 400 public boolean doesContainAWord(String selection) { |
389 return mContainsWordPattern.matcher(selection).find(); | 401 return mContainsWordPattern.matcher(selection).find(); |
390 } | 402 } |
391 } | 403 } |
OLD | NEW |