OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.text.TextUtils; | 7 import android.text.TextUtils; |
8 | 8 |
9 import org.chromium.base.Log; | |
10 import org.chromium.base.annotations.CalledByNative; | 9 import org.chromium.base.annotations.CalledByNative; |
11 | 10 |
12 import javax.annotation.Nullable; | 11 import javax.annotation.Nullable; |
13 | 12 |
14 /** | 13 /** |
15 * Provides a context in which to search, and links to the native ContextualSear
chContext. | 14 * Provides a context in which to search, and links to the native ContextualSear
chContext. |
16 * Includes the selection, selection offsets, surrounding page content, etc. | 15 * Includes the selection, selection offsets, surrounding page content, etc. |
17 * Requires an override of #onSelectionChanged to call when a non-empty selectio
n is established | 16 * Requires an override of #onSelectionChanged to call when a non-empty selectio
n is established |
18 * or changed. | 17 * or changed. |
19 */ | 18 */ |
20 public abstract class ContextualSearchContext { | 19 public abstract class ContextualSearchContext { |
21 static final int INVALID_SELECTION_OFFSET = -1; | 20 static final int INVALID_SELECTION_OFFSET = -1; |
22 private static final String TAG = "ContextualSearch"; | |
23 | 21 |
24 // Pointer to the native instance of this class. | 22 // Pointer to the native instance of this class. |
25 private long mNativePointer; | 23 private long mNativePointer; |
26 | 24 |
27 // Whether this context has had the required properties set so it can Resolv
e a Search Term. | 25 // Whether this context has had the required properties set so it can Resolv
e a Search Term. |
28 private boolean mHasSetResolveProperties; | 26 private boolean mHasSetResolveProperties; |
29 | 27 |
30 // A shortened version of the actual text content surrounding the selection,
or null if not yet | 28 // A shortened version of the actual text content surrounding the selection,
or null if not yet |
31 // established. | 29 // established. |
32 private String mSurroundingText; | 30 private String mSurroundingText; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 } | 149 } |
152 | 150 |
153 /** | 151 /** |
154 * Notifies of an adjustment that has been applied to the start and end of t
he selection. | 152 * Notifies of an adjustment that has been applied to the start and end of t
he selection. |
155 * @param startAdjust A signed value indicating the direction of the adjustm
ent to the start of | 153 * @param startAdjust A signed value indicating the direction of the adjustm
ent to the start of |
156 * the selection (typically a negative value when the selection expan
ds). | 154 * the selection (typically a negative value when the selection expan
ds). |
157 * @param endAdjust A signed value indicating the direction of the adjustmen
t to the end of | 155 * @param endAdjust A signed value indicating the direction of the adjustmen
t to the end of |
158 * the selection (typically a positive value when the selection expan
ds). | 156 * the selection (typically a positive value when the selection expan
ds). |
159 */ | 157 */ |
160 void onSelectionAdjusted(int startAdjust, int endAdjust) { | 158 void onSelectionAdjusted(int startAdjust, int endAdjust) { |
161 makeSelectionAdjustments(startAdjust, endAdjust); | |
162 } | |
163 | |
164 /** | |
165 * Update the context based on the given selection. | |
166 * TODO(donnd): This method of finding the adjustment to the selection is un
reliable! | |
167 * TODO(donnd): Replace by getting the selection adjustment directly from | |
168 * {@link #onSelectionAdjusted} which is called by SelectWordAr
oundCaretAck | |
169 * (since it knows how the selection was actually adjusted). | |
170 * This method can be removed once SelectWordAroundCaretAck is
in place. | |
171 * See crbug.com/435778 for details. | |
172 * @param selection The new selection. | |
173 */ | |
174 void updateContextFromSelection(String selection) { | |
175 mInitialSelectedWord = selection; | |
176 if (mSelectionStartOffset == INVALID_SELECTION_OFFSET | |
177 || mSelectionEndOffset == INVALID_SELECTION_OFFSET | |
178 || TextUtils.isEmpty(mSurroundingText) || TextUtils.isEmpty(sele
ction)) { | |
179 return; | |
180 } | |
181 | |
182 int selectionLength = selection.length(); | |
183 for (int i = 0; i <= selectionLength; i++) { | |
184 int possibleStart = mSelectionStartOffset - i; | |
185 int possibleEnd = possibleStart + selectionLength; | |
186 if (possibleStart >= 0 && possibleEnd <= mSurroundingText.length() | |
187 && selection.equals(mSurroundingText.substring(possibleStart
, possibleEnd))) { | |
188 makeSelectionAdjustments(-i, selectionLength - i); | |
189 return; | |
190 } | |
191 } | |
192 | |
193 Log.w(TAG, "Warning, unable to update context from the selection!"); | |
194 } | |
195 | |
196 /** | |
197 * Makes adjustments to the selection offsets. | |
198 * @param startAdjust A signed value indicating the direction of the adjustm
ent to the start of | |
199 * the selection (typically a negative value when the selection expan
ds). | |
200 * @param endAdjust A signed value indicating the direction of the adjustmen
t to the end of | |
201 * the selection (typically a positive value when the selection expan
ds). | |
202 */ | |
203 private void makeSelectionAdjustments(int startAdjust, int endAdjust) { | |
204 nativeAdjustSelection(getNativePointer(), startAdjust, endAdjust); | 159 nativeAdjustSelection(getNativePointer(), startAdjust, endAdjust); |
205 // Fully track the selection as it changes. | 160 // Fully track the selection as it changes. |
206 mSelectionStartOffset += startAdjust; | 161 mSelectionStartOffset += startAdjust; |
207 mSelectionEndOffset += endAdjust; | 162 mSelectionEndOffset += endAdjust; |
| 163 if (TextUtils.isEmpty(mInitialSelectedWord) && !TextUtils.isEmpty(mSurro
undingText)) { |
| 164 mInitialSelectedWord = |
| 165 mSurroundingText.substring(mSelectionStartOffset, mSelection
EndOffset); |
| 166 } |
208 // Notify of changes. | 167 // Notify of changes. |
209 onSelectionChanged(); | 168 onSelectionChanged(); |
210 } | 169 } |
211 | 170 |
212 /** | 171 /** |
213 * Notifies this instance that the selection has been changed. | 172 * Notifies this instance that the selection has been changed. |
214 */ | 173 */ |
215 abstract void onSelectionChanged(); | 174 abstract void onSelectionChanged(); |
216 | 175 |
217 // TODO(donnd): Add a test for this class! | 176 // TODO(donnd): Add a test for this class! |
(...skipping 11 matching lines...) Expand all Loading... |
229 // =========================================================================
=================== | 188 // =========================================================================
=================== |
230 // Native methods. | 189 // Native methods. |
231 // =========================================================================
=================== | 190 // =========================================================================
=================== |
232 private native long nativeInit(); | 191 private native long nativeInit(); |
233 private native void nativeDestroy(long nativeContextualSearchContext); | 192 private native void nativeDestroy(long nativeContextualSearchContext); |
234 private native void nativeSetResolveProperties( | 193 private native void nativeSetResolveProperties( |
235 long nativeContextualSearchContext, String homeCountry, boolean mayS
endBasePageUrl); | 194 long nativeContextualSearchContext, String homeCountry, boolean mayS
endBasePageUrl); |
236 private native void nativeAdjustSelection( | 195 private native void nativeAdjustSelection( |
237 long nativeContextualSearchContext, int startAdjust, int endAdjust); | 196 long nativeContextualSearchContext, int startAdjust, int endAdjust); |
238 } | 197 } |
OLD | NEW |