diff options
author | Luca Zanolin <zano@google.com> | 2011-08-01 09:55:17 +0100 |
---|---|---|
committer | Luca Zanolin <zano@google.com> | 2011-08-01 09:57:25 +0100 |
commit | e3f89c08f24c88d8758bc8ad2f67ce00790b2120 (patch) | |
tree | 0022ee4bb3a9dbc24e6cb292b1f461a4348b5f95 | |
parent | e8f1cbae4400e8b046b405f205dffe0417826fb3 (diff) | |
download | frameworks_base-e3f89c08f24c88d8758bc8ad2f67ce00790b2120.zip frameworks_base-e3f89c08f24c88d8758bc8ad2f67ce00790b2120.tar.gz frameworks_base-e3f89c08f24c88d8758bc8ad2f67ce00790b2120.tar.bz2 |
Sort the suggestions based based on the length of the text they are attached too.
When the suggestions are displayed, the shortest one will be at the top of the list, as they are the most relevant one.
Bug: 5006130
Change-Id: Id3ac3accce5198a6a58a0c3028ee5f77957ceac6
-rw-r--r-- | core/java/android/widget/TextView.java | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 6dcae6d..ab66676 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -143,6 +143,9 @@ import java.io.IOException; import java.lang.ref.WeakReference; import java.text.BreakIterator; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; /** * Displays text to the user and optionally allows them to edit it. A TextView @@ -8812,12 +8815,42 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener int suggestionIndex; // the index of the suggestion inside suggestionSpan } + /** + * Returns the suggestion spans that cover the current cursor position. The suggestion + * spans are sorted according to the length of text that they are attached to. + */ + private SuggestionSpan[] getSuggestionSpans() { + int pos = TextView.this.getSelectionStart(); + Spannable spannable = (Spannable) TextView.this.mText; + SuggestionSpan[] suggestionSpans = spannable.getSpans(pos, pos, SuggestionSpan.class); + + // Cache the span length for performance reason. + final HashMap<SuggestionSpan, Integer> spanLengthMap = + new HashMap<SuggestionSpan, Integer>(); + + for (SuggestionSpan suggestionSpan : suggestionSpans) { + int start = spannable.getSpanStart(suggestionSpan); + int end = spannable.getSpanEnd(suggestionSpan); + spanLengthMap.put(suggestionSpan, end - start); + } + + // The suggestions are sorted according to the lenght of the text that they cover + // (shorter first) + Arrays.sort(suggestionSpans, new Comparator<SuggestionSpan>() { + public int compare(SuggestionSpan span1, SuggestionSpan span2) { + return spanLengthMap.get(span1) - spanLengthMap.get(span2); + } + }); + + return suggestionSpans; + } + public void show() { if (!(mText instanceof Editable)) return; - final int pos = TextView.this.getSelectionStart(); - Spannable spannable = (Spannable)TextView.this.mText; - SuggestionSpan[] suggestionSpans = spannable.getSpans(pos, pos, SuggestionSpan.class); + Spannable spannable = (Spannable) TextView.this.mText; + SuggestionSpan[] suggestionSpans = getSuggestionSpans(); + final int nbSpans = suggestionSpans.length; int totalNbSuggestions = 0; |