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 mIsSelectionDissolved; | |
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 mIsSelectionDissolved = 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_HANDLES_SHOWN: |
192 mWasTapGestureDetected = false; | 194 mWasTapGestureDetected = false; |
193 mSelectionType = SelectionType.LONG_PRESS; | 195 mSelectionType = SelectionType.LONG_PRESS; |
194 shouldHandleSelection = true; | 196 shouldHandleSelection = true; |
197 mIsSelectionDissolved = false; | |
jdduke (slow)
2015/07/16 22:34:40
Please use the inverse of this, SELECTION_ESTABLIS
Donn Denman
2015/07/16 22:53:09
Done.
| |
195 break; | 198 break; |
196 case SelectionEventType.SELECTION_CLEARED: | 199 case SelectionEventType.SELECTION_HANDLES_CLEARED: |
197 mHandler.handleSelectionDismissal(); | 200 mHandler.handleSelectionDismissal(); |
198 resetAllStates(); | 201 resetAllStates(); |
199 break; | 202 break; |
200 case SelectionEventType.SELECTION_DRAG_STARTED: | 203 case SelectionEventType.SELECTION_HANDLE_DRAG_STARTED: |
201 mIsSelectionBeingModified = true; | 204 mIsSelectionBeingModified = true; |
202 break; | 205 break; |
203 case SelectionEventType.SELECTION_DRAG_STOPPED: | 206 case SelectionEventType.SELECTION_HANDLE_DRAG_STOPPED: |
204 mIsSelectionBeingModified = false; | 207 mIsSelectionBeingModified = false; |
205 shouldHandleSelection = mShouldHandleSelectionModification; | 208 shouldHandleSelection = mShouldHandleSelectionModification; |
206 break; | 209 break; |
210 case SelectionEventType.SELECTION_DISSOLVED: | |
211 mIsSelectionDissolved = true; | |
212 break; | |
jdduke (slow)
2015/07/16 22:34:40
It's a bit of a shame to keep this bit solely for
Donn Denman
2015/07/16 22:53:09
On 2015/07/16 22:34:40, jdduke wrote:
Don't have a
| |
207 default: | 213 default: |
208 } | 214 } |
209 | 215 |
210 if (shouldHandleSelection) { | 216 if (shouldHandleSelection) { |
211 ContentViewCore baseContentView = getBaseContentView(); | 217 ContentViewCore baseContentView = getBaseContentView(); |
212 if (baseContentView != null) { | 218 if (baseContentView != null) { |
213 String selection = baseContentView.getSelectedText(); | 219 String selection = baseContentView.getSelectedText(); |
214 if (selection != null) { | 220 if (selection != null) { |
215 mX = posXPix; | 221 mX = posXPix; |
216 mY = posYPix; | 222 mY = posYPix; |
217 mSelectedText = selection; | 223 mSelectedText = selection; |
218 handleSelection(selection, SelectionType.LONG_PRESS); | 224 handleSelection(selection, SelectionType.LONG_PRESS); |
219 } | 225 } |
220 } | 226 } |
221 } | 227 } |
222 } | 228 } |
223 | 229 |
224 /** | 230 /** |
225 * Re-enables selection modification handling and invokes | 231 * Re-enables selection modification handling and invokes |
226 * ContextualSearchSelectionHandler.handleSelection(). | 232 * ContextualSearchSelectionHandler.handleSelection(). |
227 * @param selection The text that was selected. | 233 * @param selection The text that was selected. |
228 * @param type The type of selection made by the user. | 234 * @param type The type of selection made by the user. |
229 */ | 235 */ |
230 private void handleSelection(String selection, SelectionType type) { | 236 private void handleSelection(String selection, SelectionType type) { |
231 mShouldHandleSelectionModification = true; | 237 mShouldHandleSelectionModification = true; |
232 mHandler.handleSelection(selection, isValidSelection(selection), type, m X, mY); | 238 mHandler.handleSelection(selection, isValidSelection(selection), type, m X, mY); |
233 } | 239 } |
234 | 240 |
235 | |
236 /** | 241 /** |
237 * Resets all internal state of this class, including the tap state. | 242 * Resets all internal state of this class, including the tap state. |
238 */ | 243 */ |
239 private void resetAllStates() { | 244 private void resetAllStates() { |
240 resetSelectionStates(); | 245 resetSelectionStates(); |
241 mWasLastTapValid = false; | 246 mWasLastTapValid = false; |
242 } | 247 } |
243 | 248 |
244 /** | 249 /** |
245 * Resets all of the internal state of this class that handles the selection . | 250 * 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 } | 361 } |
357 | 362 |
358 /** | 363 /** |
359 * @return whether a tap gesture has been detected, for testing. | 364 * @return whether a tap gesture has been detected, for testing. |
360 */ | 365 */ |
361 @VisibleForTesting | 366 @VisibleForTesting |
362 boolean wasAnyTapGestureDetected() { | 367 boolean wasAnyTapGestureDetected() { |
363 return mIsWaitingForInvalidTapDetection; | 368 return mIsWaitingForInvalidTapDetection; |
364 } | 369 } |
365 | 370 |
371 /** | |
372 * @return whether the selection has been dissolved, for testing. | |
373 */ | |
374 @VisibleForTesting | |
375 boolean isSelectionDissolved() { | |
376 return mIsSelectionDissolved; | |
377 } | |
378 | |
366 /** Determines if the given selection is valid or not. | 379 /** Determines if the given selection is valid or not. |
367 * @param selection The selection portion of the context. | 380 * @param selection The selection portion of the context. |
368 * @return whether the given selection is considered a valid target for a se arch. | 381 * @return whether the given selection is considered a valid target for a se arch. |
369 */ | 382 */ |
370 private boolean isValidSelection(String selection) { | 383 private boolean isValidSelection(String selection) { |
371 return isValidSelection(selection, getBaseContentView()); | 384 return isValidSelection(selection, getBaseContentView()); |
372 } | 385 } |
373 | 386 |
374 @VisibleForTesting | 387 @VisibleForTesting |
375 boolean isValidSelection(String selection, ContentViewCore baseContentView) { | 388 boolean isValidSelection(String selection, ContentViewCore baseContentView) { |
376 if (selection.length() > MAX_SELECTION_LENGTH || !doesContainAWord(selec tion)) { | 389 if (selection.length() > MAX_SELECTION_LENGTH || !doesContainAWord(selec tion)) { |
377 return false; | 390 return false; |
378 } | 391 } |
379 return baseContentView != null && !baseContentView.isFocusedNodeEditable (); | 392 return baseContentView != null && !baseContentView.isFocusedNodeEditable (); |
380 } | 393 } |
381 | 394 |
382 /** | 395 /** |
383 * Determines if the given selection contains a word or not. | 396 * Determines if the given selection contains a word or not. |
384 * @param selection The the selection to check for a word. | 397 * @param selection The the selection to check for a word. |
385 * @return Whether the selection contains a word anywhere within it or not. | 398 * @return Whether the selection contains a word anywhere within it or not. |
386 */ | 399 */ |
387 @VisibleForTesting | 400 @VisibleForTesting |
388 public boolean doesContainAWord(String selection) { | 401 public boolean doesContainAWord(String selection) { |
389 return mContainsWordPattern.matcher(selection).find(); | 402 return mContainsWordPattern.matcher(selection).find(); |
390 } | 403 } |
391 } | 404 } |
OLD | NEW |