Merge "Put SuggestionSpan at commitText"

main
satok 2011-05-25 02:00:05 -07:00 committed by Android (Google) Code Review
commit f4c9e9d359
4 changed files with 98 additions and 46 deletions

View File

@ -22,7 +22,6 @@ import android.util.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
@ -74,23 +73,22 @@ public class CompatUtils {
return targetClass.getMethod(name, parameterTypes);
} catch (SecurityException e) {
// ignore
return null;
} catch (NoSuchMethodException e) {
// ignore
return null;
}
return null;
}
public static Field getField(Class<?> targetClass, String name) {
if (targetClass == null || TextUtils.isEmpty(name)) return null;
try {
return targetClass.getField(name);
} catch (SecurityException e) {
// ignore
return null;
} catch (NoSuchFieldException e) {
// ignore
return null;
}
return null;
}
public static Constructor<?> getConstructor(Class<?> targetClass, Class<?>[] types) {
@ -99,11 +97,20 @@ public class CompatUtils {
return targetClass.getConstructor(types);
} catch (SecurityException e) {
// ignore
return null;
} catch (NoSuchMethodException e) {
// ignore
}
return null;
}
public static Object newInstance(Constructor<?> constructor, Object[] args) {
if (constructor == null) return null;
try {
return constructor.newInstance(args);
} catch (Exception e) {
Log.e(TAG, "Exception in newInstance: " + e.getClass().getSimpleName());
}
return null;
}
public static Object invoke(
@ -111,39 +118,28 @@ public class CompatUtils {
if (method == null) return defaultValue;
try {
return method.invoke(receiver, args);
} catch (IllegalArgumentException e) {
Log.e(TAG, "Exception in invoke: IllegalArgumentException");
return defaultValue;
} catch (IllegalAccessException e) {
Log.e(TAG, "Exception in invoke: IllegalAccessException");
return defaultValue;
} catch (InvocationTargetException e) {
Log.e(TAG, "Exception in invoke: IllegalTargetException");
return defaultValue;
} catch (Exception e) {
Log.e(TAG, "Exception in invoke: " + e.getClass().getSimpleName());
}
return defaultValue;
}
public static Object getFieldValue(Object receiver, Object defaultValue, Field field) {
if (field == null) return defaultValue;
try {
return field.get(receiver);
} catch (IllegalArgumentException e) {
Log.e(TAG, "Exception in getFieldValue: IllegalArgumentException");
return defaultValue;
} catch (IllegalAccessException e) {
Log.e(TAG, "Exception in getFieldValue: IllegalAccessException");
return defaultValue;
} catch (Exception e) {
Log.e(TAG, "Exception in getFieldValue: " + e.getClass().getSimpleName());
}
return defaultValue;
}
public static void setFieldValue(Object receiver, Field field, Object value) {
if (field == null) return;
try {
field.set(receiver, value);
} catch (IllegalArgumentException e) {
Log.e(TAG, "Exception in setFieldValue: IllegalArgumentException");
} catch (IllegalAccessException e) {
Log.e(TAG, "Exception in setFieldValue: IllegalAccessException");
} catch (Exception e) {
Log.e(TAG, "Exception in setFieldValue: " + e.getClass().getSimpleName());
}
}

View File

@ -18,15 +18,12 @@ package com.android.inputmethod.compat;
import com.android.inputmethod.latin.EditingUtils.SelectedWord;
import android.util.Log;
import android.view.inputmethod.InputConnection;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class InputConnectionCompatUtils {
private static final String TAG = InputConnectionCompatUtils.class.getSimpleName();
private static final Class<?> CLASS_CorrectionInfo = CompatUtils
.getClass("android.view.inputmethod.CorrectionInfo");
private static final Class<?>[] INPUT_TYPE_CorrectionInfo = new Class<?>[] { int.class,
@ -53,18 +50,10 @@ public class InputConnectionCompatUtils {
return;
}
Object[] args = { offset, oldText, newText };
try {
Object correctionInfo = CONSTRUCTOR_CorrectionInfo.newInstance(args);
Object correctionInfo = CompatUtils.newInstance(CONSTRUCTOR_CorrectionInfo, args);
if (correctionInfo != null) {
CompatUtils.invoke(ic, null, METHOD_InputConnection_commitCorrection,
correctionInfo);
} catch (IllegalArgumentException e) {
Log.e(TAG, "Error in commitCorrection: IllegalArgumentException");
} catch (InstantiationException e) {
Log.e(TAG, "Error in commitCorrection: InstantiationException");
} catch (IllegalAccessException e) {
Log.e(TAG, "Error in commitCorrection: IllegalAccessException");
} catch (InvocationTargetException e) {
Log.e(TAG, "Error in commitCorrection: InvocationTargetException");
}
}

View File

@ -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;
}
}

View File

@ -59,6 +59,7 @@ import com.android.inputmethod.compat.InputConnectionCompatUtils;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.compat.InputMethodServiceCompatWrapper;
import com.android.inputmethod.compat.InputTypeCompatUtils;
import com.android.inputmethod.compat.SuggestionSpanUtils;
import com.android.inputmethod.deprecated.LanguageSwitcherProxy;
import com.android.inputmethod.deprecated.VoiceProxy;
import com.android.inputmethod.deprecated.recorrection.Recorrection;
@ -1509,7 +1510,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
if (mBestWord != null && mBestWord.length() > 0) {
TextEntryState.acceptedDefault(mWord.getTypedWord(), mBestWord, separatorCode);
mJustAccepted = true;
pickSuggestion(mBestWord);
commitBestWord(mBestWord);
// Add the word to the auto dictionary if it's not a known word
addToAutoAndUserBigramDictionaries(mBestWord, AutoDictionary.FREQUENCY_FOR_TYPED);
return true;
@ -1576,7 +1577,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mWord.reset();
}
mJustAccepted = true;
pickSuggestion(suggestion);
commitBestWord(suggestion);
// Add the word to the auto dictionary if it's not a known word
if (index == 0) {
addToAutoAndUserBigramDictionaries(suggestion, AutoDictionary.FREQUENCY_FOR_PICKED);
@ -1633,18 +1634,20 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
* @param suggestion the suggestion picked by the user to be committed to
* the text field
*/
private void pickSuggestion(CharSequence suggestion) {
private void commitBestWord(CharSequence bestWord) {
KeyboardSwitcher switcher = mKeyboardSwitcher;
if (!switcher.isKeyboardAvailable())
return;
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
mVoiceProxy.rememberReplacedWord(suggestion, mSettingsValues.mWordSeparators);
ic.commitText(suggestion, 1);
mVoiceProxy.rememberReplacedWord(bestWord, mSettingsValues.mWordSeparators);
SuggestedWords suggestedWords = mCandidateView.getSuggestions();
ic.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan(
this, bestWord, suggestedWords), 1);
}
mRecorrection.saveRecorrectionSuggestion(mWord, suggestion);
mRecorrection.saveRecorrectionSuggestion(mWord, bestWord);
mHasUncommittedTypedChars = false;
mCommittedLength = suggestion.length();
mCommittedLength = bestWord.length();
}
private static final WordComposer sEmptyWordComposer = new WordComposer();