diff options
author | 2011-05-24 20:30:30 +0900 | |
---|---|---|
committer | 2011-05-25 17:48:31 +0900 | |
commit | 1fef530ec7626fa16777f52b48191e61db8f46d4 (patch) | |
tree | fb4f436bebe0f723bf41bb44fc9a34e1a9ee513f /java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java | |
parent | bde078fe0dcefdd77c7e77080dedb4bece081e73 (diff) | |
download | latinime-1fef530ec7626fa16777f52b48191e61db8f46d4.tar.gz latinime-1fef530ec7626fa16777f52b48191e61db8f46d4.tar.xz latinime-1fef530ec7626fa16777f52b48191e61db8f46d4.zip |
Put SuggestionSpan at commitText
Bug: 4346045
Change-Id: Iaabdb8a148b2601bb9cbc2b08509adac164105a4
Diffstat (limited to 'java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java')
-rw-r--r-- | java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java new file mode 100644 index 000000000..e8be88759 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.compat; + +import com.android.inputmethod.latin.SuggestedWords; + +import android.content.Context; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.Spanned; + +import java.lang.reflect.Constructor; + +public class SuggestionSpanUtils { + private static final Class<?> CLASS_SuggestionSpan = + CompatUtils.getClass("android.text.style.SuggestionSpan"); + private static final Class<?>[] INPUT_TYPE_SuggestionSpan = new Class<?>[] { + Context.class, String[].class, int.class + }; + private static final Constructor<?> CONSTRUCTOR_SuggestionSpan = + CompatUtils.getConstructor(CLASS_SuggestionSpan, INPUT_TYPE_SuggestionSpan); + + public static CharSequence getTextWithSuggestionSpan( + Context context, CharSequence suggestion, SuggestedWords suggestedWords) { + if (CONSTRUCTOR_SuggestionSpan == null || suggestedWords == null + || suggestedWords.size() == 0) { + return suggestion; + } + + final Spannable spannable; + if (suggestion instanceof Spannable) { + spannable = (Spannable) suggestion; + } else { + spannable = new SpannableString(suggestion); + } + // TODO: Use SUGGESTIONS_MAX_SIZE instead of 5. + final int N = Math.min(5, suggestedWords.size()); + final String[] suggestionsArray = new String[N]; + for (int i = 0; i < N; ++i) { + suggestionsArray[i] = suggestedWords.getWord(i).toString(); + } + final Object[] args = {context, suggestionsArray, 0}; + final Object ss = CompatUtils.newInstance(CONSTRUCTOR_SuggestionSpan, args); + if (ss == null) { + return suggestion; + } + spannable.setSpan(ss, 0, suggestion.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + return spannable; + } +} |