Skip to content
Snippets Groups Projects
Commit f4c9e9d3 authored by satok's avatar satok Committed by Android (Google) Code Review
Browse files

Merge "Put SuggestionSpan at commitText"

parents 3e17686c 1fef530e
No related branches found
No related tags found
No related merge requests found
...@@ -22,7 +22,6 @@ import android.util.Log; ...@@ -22,7 +22,6 @@ import android.util.Log;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -74,23 +73,22 @@ public class CompatUtils { ...@@ -74,23 +73,22 @@ public class CompatUtils {
return targetClass.getMethod(name, parameterTypes); return targetClass.getMethod(name, parameterTypes);
} catch (SecurityException e) { } catch (SecurityException e) {
// ignore // ignore
return null;
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
// ignore // ignore
return null;
} }
return null;
} }
public static Field getField(Class<?> targetClass, String name) { public static Field getField(Class<?> targetClass, String name) {
if (targetClass == null || TextUtils.isEmpty(name)) return null;
try { try {
return targetClass.getField(name); return targetClass.getField(name);
} catch (SecurityException e) { } catch (SecurityException e) {
// ignore // ignore
return null;
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {
// ignore // ignore
return null;
} }
return null;
} }
public static Constructor<?> getConstructor(Class<?> targetClass, Class<?>[] types) { public static Constructor<?> getConstructor(Class<?> targetClass, Class<?>[] types) {
...@@ -99,11 +97,20 @@ public class CompatUtils { ...@@ -99,11 +97,20 @@ public class CompatUtils {
return targetClass.getConstructor(types); return targetClass.getConstructor(types);
} catch (SecurityException e) { } catch (SecurityException e) {
// ignore // ignore
return null;
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
// ignore // ignore
return null;
} }
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( public static Object invoke(
...@@ -111,39 +118,28 @@ public class CompatUtils { ...@@ -111,39 +118,28 @@ public class CompatUtils {
if (method == null) return defaultValue; if (method == null) return defaultValue;
try { try {
return method.invoke(receiver, args); return method.invoke(receiver, args);
} catch (IllegalArgumentException e) { } catch (Exception e) {
Log.e(TAG, "Exception in invoke: IllegalArgumentException"); Log.e(TAG, "Exception in invoke: " + e.getClass().getSimpleName());
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;
} }
return defaultValue;
} }
public static Object getFieldValue(Object receiver, Object defaultValue, Field field) { public static Object getFieldValue(Object receiver, Object defaultValue, Field field) {
if (field == null) return defaultValue; if (field == null) return defaultValue;
try { try {
return field.get(receiver); return field.get(receiver);
} catch (IllegalArgumentException e) { } catch (Exception e) {
Log.e(TAG, "Exception in getFieldValue: IllegalArgumentException"); Log.e(TAG, "Exception in getFieldValue: " + e.getClass().getSimpleName());
return defaultValue;
} catch (IllegalAccessException e) {
Log.e(TAG, "Exception in getFieldValue: IllegalAccessException");
return defaultValue;
} }
return defaultValue;
} }
public static void setFieldValue(Object receiver, Field field, Object value) { public static void setFieldValue(Object receiver, Field field, Object value) {
if (field == null) return; if (field == null) return;
try { try {
field.set(receiver, value); field.set(receiver, value);
} catch (IllegalArgumentException e) { } catch (Exception e) {
Log.e(TAG, "Exception in setFieldValue: IllegalArgumentException"); Log.e(TAG, "Exception in setFieldValue: " + e.getClass().getSimpleName());
} catch (IllegalAccessException e) {
Log.e(TAG, "Exception in setFieldValue: IllegalAccessException");
} }
} }
......
...@@ -18,15 +18,12 @@ package com.android.inputmethod.compat; ...@@ -18,15 +18,12 @@ package com.android.inputmethod.compat;
import com.android.inputmethod.latin.EditingUtils.SelectedWord; import com.android.inputmethod.latin.EditingUtils.SelectedWord;
import android.util.Log;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class InputConnectionCompatUtils { public class InputConnectionCompatUtils {
private static final String TAG = InputConnectionCompatUtils.class.getSimpleName();
private static final Class<?> CLASS_CorrectionInfo = CompatUtils private static final Class<?> CLASS_CorrectionInfo = CompatUtils
.getClass("android.view.inputmethod.CorrectionInfo"); .getClass("android.view.inputmethod.CorrectionInfo");
private static final Class<?>[] INPUT_TYPE_CorrectionInfo = new Class<?>[] { int.class, private static final Class<?>[] INPUT_TYPE_CorrectionInfo = new Class<?>[] { int.class,
...@@ -53,18 +50,10 @@ public class InputConnectionCompatUtils { ...@@ -53,18 +50,10 @@ public class InputConnectionCompatUtils {
return; return;
} }
Object[] args = { offset, oldText, newText }; Object[] args = { offset, oldText, newText };
try { Object correctionInfo = CompatUtils.newInstance(CONSTRUCTOR_CorrectionInfo, args);
Object correctionInfo = CONSTRUCTOR_CorrectionInfo.newInstance(args); if (correctionInfo != null) {
CompatUtils.invoke(ic, null, METHOD_InputConnection_commitCorrection, CompatUtils.invoke(ic, null, METHOD_InputConnection_commitCorrection,
correctionInfo); 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");
} }
} }
......
/*
* 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;
}
}
...@@ -59,6 +59,7 @@ import com.android.inputmethod.compat.InputConnectionCompatUtils; ...@@ -59,6 +59,7 @@ import com.android.inputmethod.compat.InputConnectionCompatUtils;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.compat.InputMethodServiceCompatWrapper; import com.android.inputmethod.compat.InputMethodServiceCompatWrapper;
import com.android.inputmethod.compat.InputTypeCompatUtils; import com.android.inputmethod.compat.InputTypeCompatUtils;
import com.android.inputmethod.compat.SuggestionSpanUtils;
import com.android.inputmethod.deprecated.LanguageSwitcherProxy; import com.android.inputmethod.deprecated.LanguageSwitcherProxy;
import com.android.inputmethod.deprecated.VoiceProxy; import com.android.inputmethod.deprecated.VoiceProxy;
import com.android.inputmethod.deprecated.recorrection.Recorrection; import com.android.inputmethod.deprecated.recorrection.Recorrection;
...@@ -1509,7 +1510,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar ...@@ -1509,7 +1510,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
if (mBestWord != null && mBestWord.length() > 0) { if (mBestWord != null && mBestWord.length() > 0) {
TextEntryState.acceptedDefault(mWord.getTypedWord(), mBestWord, separatorCode); TextEntryState.acceptedDefault(mWord.getTypedWord(), mBestWord, separatorCode);
mJustAccepted = true; mJustAccepted = true;
pickSuggestion(mBestWord); commitBestWord(mBestWord);
// Add the word to the auto dictionary if it's not a known word // Add the word to the auto dictionary if it's not a known word
addToAutoAndUserBigramDictionaries(mBestWord, AutoDictionary.FREQUENCY_FOR_TYPED); addToAutoAndUserBigramDictionaries(mBestWord, AutoDictionary.FREQUENCY_FOR_TYPED);
return true; return true;
...@@ -1576,7 +1577,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar ...@@ -1576,7 +1577,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mWord.reset(); mWord.reset();
} }
mJustAccepted = true; mJustAccepted = true;
pickSuggestion(suggestion); commitBestWord(suggestion);
// Add the word to the auto dictionary if it's not a known word // Add the word to the auto dictionary if it's not a known word
if (index == 0) { if (index == 0) {
addToAutoAndUserBigramDictionaries(suggestion, AutoDictionary.FREQUENCY_FOR_PICKED); addToAutoAndUserBigramDictionaries(suggestion, AutoDictionary.FREQUENCY_FOR_PICKED);
...@@ -1633,18 +1634,20 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar ...@@ -1633,18 +1634,20 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
* @param suggestion the suggestion picked by the user to be committed to * @param suggestion the suggestion picked by the user to be committed to
* the text field * the text field
*/ */
private void pickSuggestion(CharSequence suggestion) { private void commitBestWord(CharSequence bestWord) {
KeyboardSwitcher switcher = mKeyboardSwitcher; KeyboardSwitcher switcher = mKeyboardSwitcher;
if (!switcher.isKeyboardAvailable()) if (!switcher.isKeyboardAvailable())
return; return;
InputConnection ic = getCurrentInputConnection(); InputConnection ic = getCurrentInputConnection();
if (ic != null) { if (ic != null) {
mVoiceProxy.rememberReplacedWord(suggestion, mSettingsValues.mWordSeparators); mVoiceProxy.rememberReplacedWord(bestWord, mSettingsValues.mWordSeparators);
ic.commitText(suggestion, 1); SuggestedWords suggestedWords = mCandidateView.getSuggestions();
ic.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan(
this, bestWord, suggestedWords), 1);
} }
mRecorrection.saveRecorrectionSuggestion(mWord, suggestion); mRecorrection.saveRecorrectionSuggestion(mWord, bestWord);
mHasUncommittedTypedChars = false; mHasUncommittedTypedChars = false;
mCommittedLength = suggestion.length(); mCommittedLength = bestWord.length();
} }
private static final WordComposer sEmptyWordComposer = new WordComposer(); private static final WordComposer sEmptyWordComposer = new WordComposer();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment