Index: chrome/android/java/src/org/chromium/chrome/browser/contextualsearch/ContextualSearchContext.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/contextualsearch/ContextualSearchContext.java b/chrome/android/java/src/org/chromium/chrome/browser/contextualsearch/ContextualSearchContext.java |
index 1ad2a6528e5087d7f6ce242986cf5256414961c9..d184d43d2d850a36f08f92f7e43d8b88512e624a 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/contextualsearch/ContextualSearchContext.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/contextualsearch/ContextualSearchContext.java |
@@ -4,6 +4,8 @@ |
package org.chromium.chrome.browser.contextualsearch; |
+import android.text.TextUtils; |
+ |
import org.chromium.base.annotations.CalledByNative; |
/** |
@@ -11,33 +13,48 @@ import org.chromium.base.annotations.CalledByNative; |
* Includes the selection, selection offsets, surrounding page content, etc. |
*/ |
public class ContextualSearchContext { |
- // The initial selection that established this context, or null. |
- private final String mSelection; |
+ private static final int INVALID_SELECTION_OFFSET = -1; |
// Pointer to the native instance of this class. |
private long mNativePointer; |
+ // The initial word selected by a Tap, or null. |
+ private String mSelectedWord; |
+ |
+ // The text that follows the selection (shown in grey in the bar). |
+ private String mTextFollowingSelection; |
+ |
+ // A shortened version of the actual text content surrounding the selection, or null if not yet |
+ // established. |
+ private String mSurroundingText; |
+ |
+ // The original encoding of the base page. |
+ private String mEncoding; |
+ |
+ // The start and end offsets of the selection within the text content. |
+ private int mSelectionStartOffset = INVALID_SELECTION_OFFSET; |
+ private int mSelectionEndOffset = INVALID_SELECTION_OFFSET; |
+ |
+ private long mSelectionStartTimeNanoseconds; |
+ |
/** |
* Constructs a context that cannot resolve a search term and has a small amount of |
* page content. |
*/ |
ContextualSearchContext() { |
mNativePointer = nativeInit(); |
- mSelection = null; |
} |
/** |
* Constructs a context that can resolve a search term and has a large amount of |
* page content. |
- * @param selection The current selection. |
* @param homeCountry The country where the user usually resides, or an empty string if not |
* known. |
* @param maySendBasePageUrl Whether policy allows sending the base-page URL to the server. |
*/ |
- ContextualSearchContext(String selection, String homeCountry, boolean maySendBasePageUrl) { |
+ ContextualSearchContext(String homeCountry, boolean maySendBasePageUrl) { |
mNativePointer = nativeInit(); |
- mSelection = selection; |
- nativeSetResolveProperties(getNativePointer(), selection, homeCountry, maySendBasePageUrl); |
+ nativeSetResolveProperties(getNativePointer(), homeCountry, maySendBasePageUrl); |
} |
/** |
@@ -48,15 +65,120 @@ public class ContextualSearchContext { |
assert mNativePointer != 0; |
nativeDestroy(mNativePointer); |
mNativePointer = 0; |
+ |
+ // Also zero out private data that may be sizable. |
+ mTextFollowingSelection = null; |
+ mSurroundingText = null; |
+ } |
+ |
+ /** |
+ * @return the initial word selected by a Tap. |
+ */ |
+ String getSelectedWord() { |
+ return mSelectedWord; |
+ } |
+ |
+ long getSelectionStartTimeNanoseconds() { |
+ return mSelectionStartTimeNanoseconds; |
+ } |
+ |
+ String getEncoding() { |
+ return mEncoding; |
+ } |
+ |
+ int getSelectionStartOffset() { |
+ return mSelectionStartOffset; |
+ } |
+ |
+ int getSelectionEndOffset() { |
+ return mSelectionEndOffset; |
+ } |
+ |
+ String getSurroundingText() { |
+ return mSurroundingText; |
} |
/** |
- * @return the original selection. |
+ * @return the text content that follows the selection (one side of the surrounding text) |
*/ |
- String getSelection() { |
- return mSelection; |
+ String getTextContentFollowingSelection() { |
+ return mTextFollowingSelection; |
+ } |
+ |
+ // DOCUMENT!!!!!!!!!!!!!! |
+ void setTextContentFollowingSelection(String textFollowingSelection) { |
+ mTextFollowingSelection = textFollowingSelection; |
} |
+ void setSelectionAdjust(int startAdjust, int endAdjust) { |
+ makeSelectionAdjustments(startAdjust, endAdjust); |
+ } |
+ |
+ void setSurroundingText( |
+ String encoding, String surroundingText, int startOffset, int endOffset) { |
+ mEncoding = encoding; |
+ mSurroundingText = surroundingText; |
+ mSelectionStartOffset = startOffset; |
+ mSelectionEndOffset = endOffset; |
+ } |
+ |
+ // Update the context based on the new selection. |
+ // |
+ // |
+ /** |
+ * Update the context based on the given selection. |
+ * TODO(donnd): This method of finding the adjustment to the selection is unreliable! |
+ * TODO(donnd): Replace by getting the selection adjustment directly from a |
+ * SelectWordAroundCaretAck, since it knows how the selection was adjusted. |
+ * @param selection The new selection. |
+ * @param selectionStartTimeNanoseconds The time in nanoseconds when the selection was started. |
+ * @return Whether the adjustment could be determined or not. |
+ */ |
+ boolean updateContextFromSelection(String selection, long selectionStartTimeNanoseconds) { |
+ // TODO(donnd): consider the case where the user extends the selection, and how to notify |
+ // icing correctly! |
+ mSelectedWord = selection; |
+ mSelectionStartTimeNanoseconds = selectionStartTimeNanoseconds; |
+ if (mSelectionStartOffset != INVALID_SELECTION_OFFSET |
+ && mSelectionEndOffset != INVALID_SELECTION_OFFSET |
+ && !TextUtils.isEmpty(mSurroundingText) && !TextUtils.isEmpty(selection)) { |
+ int selectionLength = selection.length(); |
+ for (int i = 0; i <= selectionLength; i++) { |
+ int possibleStart = mSelectionStartOffset - i; |
+ int possibleEnd = possibleStart + selectionLength; |
+ if (possibleStart < 0 || possibleEnd > mSurroundingText.length()) return false; |
+ |
+ if (selection.equals(mSurroundingText.substring(possibleStart, possibleEnd))) { |
+ mSelectionStartOffset = possibleStart; |
+ mSelectionEndOffset = possibleEnd; |
+ makeSelectionAdjustments(-i, selectionLength - i); |
+ return true; |
+ } |
+ } |
+ } |
+ System.out.println("ctxs update context failed!!!!!!!"); |
+ return false; |
+ } |
+ |
+ /** |
+ * Makes adjustments to the selection offsets. |
+ * @param startAdjust |
+ * @param endAdjust |
+ */ |
+ private void makeSelectionAdjustments(int startAdjust, int endAdjust) { |
+ nativeAdjustSelection(getNativePointer(), startAdjust, endAdjust); |
+ if (mTextFollowingSelection != null && endAdjust > 0 |
+ && endAdjust <= mTextFollowingSelection.length()) { |
+ mTextFollowingSelection = mTextFollowingSelection.substring(endAdjust); |
+ } |
+ // Needed?????????????? |
+ mSelectionStartOffset += startAdjust; |
+ mSelectionEndOffset += endAdjust; |
+ } |
+ |
+ // DLD Add a stub for analyze context? |
+ // DLD Add a test for this class, or for a separate Analyze Context class? |
+ |
// ============================================================================================ |
// Native callback support. |
// ============================================================================================ |
@@ -72,6 +194,8 @@ public class ContextualSearchContext { |
// ============================================================================================ |
private native long nativeInit(); |
private native void nativeDestroy(long nativeContextualSearchContext); |
- private native void nativeSetResolveProperties(long nativeContextualSearchContext, |
- String selection, String homeCountry, boolean maySendBasePageUrl); |
+ private native void nativeSetResolveProperties( |
+ long nativeContextualSearchContext, String homeCountry, boolean maySendBasePageUrl); |
+ private native void nativeAdjustSelection( |
+ long nativeContextualSearchContext, int startAdjust, int endAdjust); |
} |