Avoid creating empty String[].

Minor cleanup on the way to fixing spelling for downloaded language.

Bug 19710676.

Change-Id: I7be25fa82248f0317d894d44802bede9dbc4fe2a
This commit is contained in:
Dan Zivkovic 2015-03-12 10:49:29 -07:00
parent 7e1dfaae3d
commit 26fb83c481
2 changed files with 12 additions and 11 deletions

View file

@ -92,7 +92,8 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
} }
} }
@Override public void onCreate() { @Override
public void onCreate() {
super.onCreate(); super.onCreate();
mRecommendedThreshold = mRecommendedThreshold =
Float.parseFloat(getString(R.string.spellchecker_recommended_threshold_value)); Float.parseFloat(getString(R.string.spellchecker_recommended_threshold_value));
@ -110,7 +111,8 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
} }
} }
@Override public void onDestroy() { @Override
public void onDestroy() {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "Closing and dereferencing mUserDictionaryLookup in onDestroy"); Log.d(TAG, "Closing and dereferencing mUserDictionaryLookup in onDestroy");
} }

View file

@ -41,6 +41,7 @@ import com.android.inputmethod.latin.utils.StatsUtils;
import com.android.inputmethod.latin.utils.SuggestionResults; import com.android.inputmethod.latin.utils.SuggestionResults;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Locale; import java.util.Locale;
public abstract class AndroidWordLevelSpellCheckerSession extends Session { public abstract class AndroidWordLevelSpellCheckerSession extends Session {
@ -259,7 +260,6 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
final String text = inText.replaceAll( final String text = inText.replaceAll(
AndroidSpellCheckerService.APOSTROPHE, AndroidSpellCheckerService.SINGLE_QUOTE); AndroidSpellCheckerService.APOSTROPHE, AndroidSpellCheckerService.SINGLE_QUOTE);
final int capitalizeType = StringUtils.getCapitalizationType(text); final int capitalizeType = StringUtils.getCapitalizationType(text);
boolean isInDict = true;
if (!mService.hasMainDictionaryForLocale(mLocale)) { if (!mService.hasMainDictionaryForLocale(mLocale)) {
return AndroidSpellCheckerService.getNotInDictEmptySuggestions( return AndroidSpellCheckerService.getNotInDictEmptySuggestions(
false /* reportAsTypo */); false /* reportAsTypo */);
@ -281,7 +281,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
mLocale, composer.getComposedDataSnapshot(), ngramContext, keyboard); mLocale, composer.getComposedDataSnapshot(), ngramContext, keyboard);
final Result result = getResult(capitalizeType, mLocale, suggestionsLimit, final Result result = getResult(capitalizeType, mLocale, suggestionsLimit,
mService.getRecommendedThreshold(), text, suggestionResults); mService.getRecommendedThreshold(), text, suggestionResults);
isInDict = isInDictForAnyCapitalization(text, capitalizeType); final boolean isInDict = isInDictForAnyCapitalization(text, capitalizeType);
if (DBG) { if (DBG) {
Log.i(TAG, "Spell checking results for " + text + " with suggestion limit " Log.i(TAG, "Spell checking results for " + text + " with suggestion limit "
+ suggestionsLimit); + suggestionsLimit);
@ -329,8 +329,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
private static final class Result { private static final class Result {
public final String[] mSuggestions; public final String[] mSuggestions;
public final boolean mHasRecommendedSuggestions; public final boolean mHasRecommendedSuggestions;
public Result(final String[] gatheredSuggestions, public Result(final String[] gatheredSuggestions, final boolean hasRecommendedSuggestions) {
final boolean hasRecommendedSuggestions) {
mSuggestions = gatheredSuggestions; mSuggestions = gatheredSuggestions;
mHasRecommendedSuggestions = hasRecommendedSuggestions; mHasRecommendedSuggestions = hasRecommendedSuggestions;
} }
@ -364,14 +363,15 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
StringUtils.removeDupes(suggestions); StringUtils.removeDupes(suggestions);
// This returns a String[], while toArray() returns an Object[] which cannot be cast // This returns a String[], while toArray() returns an Object[] which cannot be cast
// into a String[]. // into a String[].
final List<String> gatheredSuggestionsList =
suggestions.subList(0, Math.min(suggestions.size(), suggestionsLimit));
final String[] gatheredSuggestions = final String[] gatheredSuggestions =
suggestions.subList(0, Math.min(suggestions.size(), suggestionsLimit)) gatheredSuggestionsList.toArray(new String[gatheredSuggestionsList.size()]);
.toArray(EMPTY_STRING_ARRAY);
final int bestScore = suggestionResults.first().mScore; final int bestScore = suggestionResults.first().mScore;
final String bestSuggestion = suggestions.get(0); final String bestSuggestion = suggestions.get(0);
final float normalizedScore = BinaryDictionaryUtils.calcNormalizedScore( final float normalizedScore = BinaryDictionaryUtils.calcNormalizedScore(
originalText, bestSuggestion.toString(), bestScore); originalText, bestSuggestion, bestScore);
final boolean hasRecommendedSuggestions = (normalizedScore > recommendedThreshold); final boolean hasRecommendedSuggestions = (normalizedScore > recommendedThreshold);
if (DBG) { if (DBG) {
Log.i(TAG, "Best suggestion : " + bestSuggestion + ", score " + bestScore); Log.i(TAG, "Best suggestion : " + bestSuggestion + ", score " + bestScore);
@ -390,8 +390,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
* That's what the following method does. * That's what the following method does.
*/ */
@Override @Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
final int suggestionsLimit) {
long ident = Binder.clearCallingIdentity(); long ident = Binder.clearCallingIdentity();
try { try {
return onGetSuggestionsInternal(textInfo, suggestionsLimit); return onGetSuggestionsInternal(textInfo, suggestionsLimit);