From f23f00a13a1bda593c24bef33c691a950501c1fa Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Tue, 24 May 2011 11:15:18 +0900 Subject: Fix reflection method invokation Either of Method.invoke, Field.get and Field.set can accept null as receiver. Change-Id: I4539dcc95a794f6ee84cf4e7aabf4e8f0206728f --- java/src/com/android/inputmethod/compat/CompatUtils.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'java/src/com/android/inputmethod/compat/CompatUtils.java') diff --git a/java/src/com/android/inputmethod/compat/CompatUtils.java b/java/src/com/android/inputmethod/compat/CompatUtils.java index 0b532f7f0..10b2a106f 100644 --- a/java/src/com/android/inputmethod/compat/CompatUtils.java +++ b/java/src/com/android/inputmethod/compat/CompatUtils.java @@ -108,7 +108,7 @@ public class CompatUtils { public static Object invoke( Object receiver, Object defaultValue, Method method, Object... args) { - if (receiver == null || method == null) return defaultValue; + if (method == null) return defaultValue; try { return method.invoke(receiver, args); } catch (IllegalArgumentException e) { @@ -124,7 +124,7 @@ public class CompatUtils { } public static Object getFieldValue(Object receiver, Object defaultValue, Field field) { - if (receiver == null || field == null) return defaultValue; + if (field == null) return defaultValue; try { return field.get(receiver); } catch (IllegalArgumentException e) { @@ -137,7 +137,7 @@ public class CompatUtils { } public static void setFieldValue(Object receiver, Field field, Object value) { - if (receiver == null || field == null) return; + if (field == null) return; try { field.set(receiver, value); } catch (IllegalArgumentException e) { -- cgit v1.2.3-83-g751a From 1fef530ec7626fa16777f52b48191e61db8f46d4 Mon Sep 17 00:00:00 2001 From: satok Date: Tue, 24 May 2011 20:30:30 +0900 Subject: Put SuggestionSpan at commitText Bug: 4346045 Change-Id: Iaabdb8a148b2601bb9cbc2b08509adac164105a4 --- .../android/inputmethod/compat/CompatUtils.java | 48 ++++++++-------- .../compat/InputConnectionCompatUtils.java | 15 +---- .../inputmethod/compat/SuggestionSpanUtils.java | 64 ++++++++++++++++++++++ .../com/android/inputmethod/latin/LatinIME.java | 17 +++--- 4 files changed, 98 insertions(+), 46 deletions(-) create mode 100644 java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java (limited to 'java/src/com/android/inputmethod/compat/CompatUtils.java') diff --git a/java/src/com/android/inputmethod/compat/CompatUtils.java b/java/src/com/android/inputmethod/compat/CompatUtils.java index 10b2a106f..6e14bfa8b 100644 --- a/java/src/com/android/inputmethod/compat/CompatUtils.java +++ b/java/src/com/android/inputmethod/compat/CompatUtils.java @@ -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; } + 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()); } } diff --git a/java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java b/java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java index c926be06f..7d00b6007 100644 --- a/java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/InputConnectionCompatUtils.java @@ -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"); } } 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; + } +} diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 6cb928321..2afee1f29 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -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; @@ -1508,7 +1509,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; @@ -1575,7 +1576,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); @@ -1632,18 +1633,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(); -- cgit v1.2.3-83-g751a From f921129f9b1083ac9d7d7be8c94701bdf6435b47 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Mon, 20 Jun 2011 23:23:33 +0900 Subject: Use 2 arguments LinearLayout constructor To maintain backward compatibility back to Froyo, we can't call 3-argument constructor of LinearLayout. Change-Id: I35310704f92812d2748d32f35820a5058fa2bf31 --- java/res/layout/input_view.xml | 3 +- .../android/inputmethod/compat/CompatUtils.java | 4 +- .../compat/LinearLayoutCompatUtils.java | 55 ++++++++++++++++++++++ .../android/inputmethod/latin/CandidateView.java | 14 +++++- 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 java/src/com/android/inputmethod/compat/LinearLayoutCompatUtils.java (limited to 'java/src/com/android/inputmethod/compat/CompatUtils.java') diff --git a/java/res/layout/input_view.xml b/java/res/layout/input_view.xml index f0a8ef956..93a046619 100644 --- a/java/res/layout/input_view.xml +++ b/java/res/layout/input_view.xml @@ -44,7 +44,8 @@ android:layout_weight="1.0" android:layout_width="0dp" android:layout_height="@dimen/candidate_strip_height" - android:gravity="center_vertical" /> + android:gravity="center_vertical" + style="?attr/candidateViewStyle" /> getConstructor(Class targetClass, Class[] types) { + public static Constructor getConstructor(Class targetClass, Class ... types) { if (targetClass == null || types == null) return null; try { return targetClass.getConstructor(types); @@ -103,7 +103,7 @@ public class CompatUtils { return null; } - public static Object newInstance(Constructor constructor, Object[] args) { + public static Object newInstance(Constructor constructor, Object ... args) { if (constructor == null) return null; try { return constructor.newInstance(args); diff --git a/java/src/com/android/inputmethod/compat/LinearLayoutCompatUtils.java b/java/src/com/android/inputmethod/compat/LinearLayoutCompatUtils.java new file mode 100644 index 000000000..674cbe74b --- /dev/null +++ b/java/src/com/android/inputmethod/compat/LinearLayoutCompatUtils.java @@ -0,0 +1,55 @@ +/* + * 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 android.content.Context; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.util.Log; + +import java.lang.reflect.Field; + +public class LinearLayoutCompatUtils { + private static final String TAG = LinearLayoutCompatUtils.class.getSimpleName(); + + private static final Class CLASS_R_STYLEABLE = CompatUtils.getClass( + "com.android.internal.R$styleable"); + private static final Field STYLEABLE_VIEW = CompatUtils.getField( + CLASS_R_STYLEABLE, "View"); + private static final Field STYLEABLE_VIEW_BACKGROUND = CompatUtils.getField( + CLASS_R_STYLEABLE, "View_background"); + private static final Object VALUE_STYLEABLE_VIEW = CompatUtils.getFieldValue( + null, null, STYLEABLE_VIEW); + private static final Integer VALUE_STYLEABLE_VIEW_BACKGROUND = + (Integer)CompatUtils.getFieldValue(null, null, STYLEABLE_VIEW_BACKGROUND); + + public static Drawable getBackgroundDrawable(Context context, AttributeSet attrs, + int defStyleAttr, int defStyleRes) { + if (!(VALUE_STYLEABLE_VIEW instanceof int[]) || VALUE_STYLEABLE_VIEW_BACKGROUND == null) { + Log.w(TAG, "Can't get View background attribute using reflection"); + return null; + } + + final int[] styleableView = (int[])VALUE_STYLEABLE_VIEW; + final TypedArray a = context.obtainStyledAttributes( + attrs, styleableView, defStyleAttr, defStyleRes); + final Drawable background = a.getDrawable(VALUE_STYLEABLE_VIEW_BACKGROUND); + a.recycle(); + return background; + } +} diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java index 92d26a01c..fa27ca5ad 100644 --- a/java/src/com/android/inputmethod/latin/CandidateView.java +++ b/java/src/com/android/inputmethod/latin/CandidateView.java @@ -44,6 +44,7 @@ import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; +import com.android.inputmethod.compat.LinearLayoutCompatUtils; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import java.util.ArrayList; @@ -148,7 +149,18 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo } public CandidateView(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); + // Note: Up to version 10 (Gingerbread) of the API, LinearLayout doesn't have 3-argument + // constructor. + // TODO: Call 3-argument constructor, super(context, attrs, defStyle), when we abandon + // backward compatibility with the version 10 or earlier of the API. + super(context, attrs); + if (defStyle != R.attr.candidateViewStyle) { + throw new IllegalArgumentException( + "can't accept defStyle other than R.attr.candidayeViewStyle: defStyle=" + + defStyle); + } + setBackgroundDrawable(LinearLayoutCompatUtils.getBackgroundDrawable( + context, attrs, defStyle, R.style.CandidateViewStyle)); Resources res = context.getResources(); LayoutInflater inflater = LayoutInflater.from(context); -- cgit v1.2.3-83-g751a