am 602bcecf: Merge "Put SuggestionSpan as the indicater of the auto-correction"
* commit '602bcecf6bbe64080c602cd71e33f814551370f4': Put SuggestionSpan as the indicater of the auto-correctionmain
commit
2ac4000d1f
|
@ -24,18 +24,22 @@ import android.text.Spannable;
|
||||||
import android.text.SpannableString;
|
import android.text.SpannableString;
|
||||||
import android.text.Spanned;
|
import android.text.Spanned;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class SuggestionSpanUtils {
|
public class SuggestionSpanUtils {
|
||||||
|
private static final String TAG = SuggestionSpanUtils.class.getSimpleName();
|
||||||
// TODO: Use reflection to get field values
|
// TODO: Use reflection to get field values
|
||||||
public static final String ACTION_SUGGESTION_PICKED =
|
public static final String ACTION_SUGGESTION_PICKED =
|
||||||
"android.text.style.SUGGESTION_PICKED";
|
"android.text.style.SUGGESTION_PICKED";
|
||||||
public static final String SUGGESTION_SPAN_PICKED_AFTER = "after";
|
public static final String SUGGESTION_SPAN_PICKED_AFTER = "after";
|
||||||
public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before";
|
public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before";
|
||||||
public static final String SUGGESTION_SPAN_PICKED_HASHCODE = "hashcode";
|
public static final String SUGGESTION_SPAN_PICKED_HASHCODE = "hashcode";
|
||||||
|
// TODO: Use the API constant after it gets public.
|
||||||
|
public static final int FLAG_AUTO_CORRECTION = 0x0004;
|
||||||
public static final int SUGGESTION_MAX_SIZE = 5;
|
public static final int SUGGESTION_MAX_SIZE = 5;
|
||||||
public static final boolean SUGGESTION_SPAN_IS_SUPPORTED;
|
public static final boolean SUGGESTION_SPAN_IS_SUPPORTED;
|
||||||
|
|
||||||
|
@ -50,6 +54,25 @@ public class SuggestionSpanUtils {
|
||||||
CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null;
|
CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static CharSequence getTextWithAutoCorrectionIndicatorUnderline(
|
||||||
|
Context context, CharSequence text) {
|
||||||
|
if (TextUtils.isEmpty(text) || CONSTRUCTOR_SuggestionSpan == null) {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
final Spannable spannable = text instanceof Spannable
|
||||||
|
? (Spannable) text : new SpannableString(text);
|
||||||
|
final Object[] args =
|
||||||
|
{ context, null, new String[] {}, FLAG_AUTO_CORRECTION,
|
||||||
|
(Class<?>) SuggestionSpanPickedNotificationReceiver.class };
|
||||||
|
final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args);
|
||||||
|
if (ss == null) {
|
||||||
|
Log.w(TAG, "Suggestion span was not created.");
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
spannable.setSpan(ss, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||||
|
return spannable;
|
||||||
|
}
|
||||||
|
|
||||||
public static CharSequence getTextWithSuggestionSpan(Context context,
|
public static CharSequence getTextWithSuggestionSpan(Context context,
|
||||||
CharSequence pickedWord, SuggestedWords suggestedWords) {
|
CharSequence pickedWord, SuggestedWords suggestedWords) {
|
||||||
if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null
|
if (TextUtils.isEmpty(pickedWord) || CONSTRUCTOR_SuggestionSpan == null
|
||||||
|
|
|
@ -1608,6 +1608,17 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
|
||||||
mKeyboardSwitcher.onAutoCorrectionStateChanged(
|
mKeyboardSwitcher.onAutoCorrectionStateChanged(
|
||||||
words.hasWordAboveAutoCorrectionScoreThreshold());
|
words.hasWordAboveAutoCorrectionScoreThreshold());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Put a blue underline to a word in TextView which will be auto-corrected.
|
||||||
|
final InputConnection ic = getCurrentInputConnection();
|
||||||
|
if (ic != null && Utils.willAutoCorrect(words)) {
|
||||||
|
final CharSequence textWithUnderline =
|
||||||
|
SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(
|
||||||
|
this, mComposingStringBuilder);
|
||||||
|
if (!TextUtils.isEmpty(textWithUnderline)) {
|
||||||
|
ic.setComposingText(textWithUnderline, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateSuggestions() {
|
public void updateSuggestions() {
|
||||||
|
|
|
@ -260,7 +260,7 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener,
|
||||||
|
|
||||||
private CharSequence getStyledSuggestionWord(SuggestedWords suggestions, int pos) {
|
private CharSequence getStyledSuggestionWord(SuggestedWords suggestions, int pos) {
|
||||||
final CharSequence word = suggestions.getWord(pos);
|
final CharSequence word = suggestions.getWord(pos);
|
||||||
final boolean isAutoCorrect = pos == 1 && willAutoCorrect(suggestions);
|
final boolean isAutoCorrect = pos == 1 && Utils.willAutoCorrect(suggestions);
|
||||||
final boolean isTypedWordValid = pos == 0 && suggestions.mTypedWordValid;
|
final boolean isTypedWordValid = pos == 0 && suggestions.mTypedWordValid;
|
||||||
if (!isAutoCorrect && !isTypedWordValid)
|
if (!isAutoCorrect && !isTypedWordValid)
|
||||||
return word;
|
return word;
|
||||||
|
@ -278,14 +278,10 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener,
|
||||||
return spannedWord;
|
return spannedWord;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean willAutoCorrect(SuggestedWords suggestions) {
|
|
||||||
return !suggestions.mTypedWordValid && suggestions.mHasMinimalSuggestion;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getWordPosition(int index, SuggestedWords suggestions) {
|
private int getWordPosition(int index, SuggestedWords suggestions) {
|
||||||
// TODO: This works for 3 suggestions. Revisit this algorithm when there are 5 or more
|
// TODO: This works for 3 suggestions. Revisit this algorithm when there are 5 or more
|
||||||
// suggestions.
|
// suggestions.
|
||||||
final int centerPos = willAutoCorrect(suggestions) ? 1 : 0;
|
final int centerPos = Utils.willAutoCorrect(suggestions) ? 1 : 0;
|
||||||
if (index == mCenterSuggestionIndex) {
|
if (index == mCenterSuggestionIndex) {
|
||||||
return centerPos;
|
return centerPos;
|
||||||
} else if (index == centerPos) {
|
} else if (index == centerPos) {
|
||||||
|
@ -300,7 +296,7 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener,
|
||||||
final boolean isSuggested = (pos != 0);
|
final boolean isSuggested = (pos != 0);
|
||||||
|
|
||||||
final int color;
|
final int color;
|
||||||
if (index == mCenterSuggestionIndex && willAutoCorrect(suggestions)) {
|
if (index == mCenterSuggestionIndex && Utils.willAutoCorrect(suggestions)) {
|
||||||
color = mColorAutoCorrect;
|
color = mColorAutoCorrect;
|
||||||
} else if (isSuggested) {
|
} else if (isSuggested) {
|
||||||
color = mColorSuggested;
|
color = mColorSuggested;
|
||||||
|
|
|
@ -790,4 +790,8 @@ public class Utils {
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean willAutoCorrect(SuggestedWords suggestions) {
|
||||||
|
return !suggestions.mTypedWordValid && suggestions.mHasMinimalSuggestion;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue