2011-05-24 11:30:30 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2011-05-26 05:10:00 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver;
|
2011-05-24 11:30:30 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.text.Spannable;
|
|
|
|
import android.text.SpannableString;
|
|
|
|
import android.text.Spanned;
|
|
|
|
|
|
|
|
import java.lang.reflect.Constructor;
|
2011-05-26 05:10:00 +00:00
|
|
|
import java.util.Locale;
|
2011-05-24 11:30:30 +00:00
|
|
|
|
|
|
|
public class SuggestionSpanUtils {
|
2011-05-26 05:10:00 +00:00
|
|
|
public static final String ACTION_SUGGESTION_PICKED =
|
|
|
|
"android.text.style.SUGGESTION_PICKED";
|
|
|
|
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_HASHCODE = "hashcode";
|
|
|
|
|
|
|
|
private static final Class<?> CLASS_SuggestionSpan = CompatUtils
|
|
|
|
.getClass("android.text.style.SuggestionSpan");
|
2011-05-24 11:30:30 +00:00
|
|
|
private static final Class<?>[] INPUT_TYPE_SuggestionSpan = new Class<?>[] {
|
2011-05-26 05:10:00 +00:00
|
|
|
Context.class, Locale.class, String[].class, int.class, Class.class };
|
|
|
|
private static final Constructor<?> CONSTRUCTOR_SuggestionSpan = CompatUtils
|
|
|
|
.getConstructor(CLASS_SuggestionSpan, INPUT_TYPE_SuggestionSpan);
|
2011-05-25 09:30:31 +00:00
|
|
|
public static final boolean SUGGESTION_SPAN_IS_SUPPORTED;
|
|
|
|
static {
|
2011-05-26 05:10:00 +00:00
|
|
|
SUGGESTION_SPAN_IS_SUPPORTED =
|
|
|
|
CLASS_SuggestionSpan != null && CONSTRUCTOR_SuggestionSpan != null;
|
2011-05-25 09:30:31 +00:00
|
|
|
}
|
2011-05-24 11:30:30 +00:00
|
|
|
|
2011-05-26 05:10:00 +00:00
|
|
|
public static CharSequence getTextWithSuggestionSpan(Context context,
|
|
|
|
CharSequence suggestion, SuggestedWords suggestedWords) {
|
2011-05-24 11:30:30 +00:00
|
|
|
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();
|
|
|
|
}
|
2011-05-26 05:10:00 +00:00
|
|
|
final Object[] args =
|
|
|
|
{ context, null, suggestionsArray, 0,
|
|
|
|
(Class<?>) SuggestionSpanPickedNotificationReceiver.class };
|
2011-05-24 11:30:30 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|