diff options
Diffstat (limited to 'java/src')
31 files changed, 405 insertions, 110 deletions
diff --git a/java/src/com/android/inputmethod/compat/CompatUtils.java b/java/src/com/android/inputmethod/compat/CompatUtils.java index 6e14bfa8b..b42633cd9 100644 --- a/java/src/com/android/inputmethod/compat/CompatUtils.java +++ b/java/src/com/android/inputmethod/compat/CompatUtils.java @@ -91,7 +91,7 @@ public class CompatUtils { return null; } - public static Constructor<?> 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/FrameLayoutCompatUtils.java b/java/src/com/android/inputmethod/compat/FrameLayoutCompatUtils.java new file mode 100644 index 000000000..46499f19a --- /dev/null +++ b/java/src/com/android/inputmethod/compat/FrameLayoutCompatUtils.java @@ -0,0 +1,52 @@ +/* + * 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.view.ViewGroup; +import android.view.ViewGroup.MarginLayoutParams; +import android.widget.FrameLayout; +import android.widget.RelativeLayout; + +public class FrameLayoutCompatUtils { + private static final boolean NEEDS_FRAME_LAYOUT_HACK = ( + android.os.Build.VERSION.SDK_INT < 11 /* Honeycomb */); + + public static ViewGroup getPlacer(ViewGroup container) { + if (NEEDS_FRAME_LAYOUT_HACK) { + // Insert RelativeLayout to be able to setMargin because pre-Honeycomb FrameLayout + // could not handle setMargin properly. + final ViewGroup placer = new RelativeLayout(container.getContext()); + container.addView(placer); + return placer; + } else { + return container; + } + } + + public static MarginLayoutParams newLayoutParam(ViewGroup placer, int width, int height) { + if (placer instanceof FrameLayout) { + return new FrameLayout.LayoutParams(width, height); + } else if (placer instanceof RelativeLayout) { + return new RelativeLayout.LayoutParams(width, height); + } else if (placer == null) { + throw new NullPointerException("placer is null"); + } else { + throw new IllegalArgumentException("placer is neither FrameLayout nor RelativeLayout: " + + placer.getClass().getName()); + } + } +} 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/deprecated/languageswitcher/InputLanguageSelection.java b/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java index fe70eef96..cf6cd0f5e 100644 --- a/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java +++ b/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java @@ -16,7 +16,7 @@ package com.android.inputmethod.deprecated.languageswitcher; -import com.android.inputmethod.keyboard.KeyboardParser; +import com.android.inputmethod.keyboard.internal.KeyboardParser; import com.android.inputmethod.latin.DictionaryFactory; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.Settings; diff --git a/java/src/com/android/inputmethod/keyboard/KeyDetector.java b/java/src/com/android/inputmethod/keyboard/KeyDetector.java index 7add43a6d..818f3f925 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyDetector.java +++ b/java/src/com/android/inputmethod/keyboard/KeyDetector.java @@ -18,6 +18,8 @@ package com.android.inputmethod.keyboard; import android.util.Log; +import com.android.inputmethod.keyboard.internal.Key; + import java.util.Arrays; import java.util.List; diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java index 58629ba51..889d54bf3 100644 --- a/java/src/com/android/inputmethod/keyboard/Keyboard.java +++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java @@ -21,6 +21,10 @@ import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.util.Log; +import com.android.inputmethod.keyboard.internal.Key; +import com.android.inputmethod.keyboard.internal.KeyboardIconsSet; +import com.android.inputmethod.keyboard.internal.KeyboardParser; +import com.android.inputmethod.keyboard.internal.KeyboardShiftState; import com.android.inputmethod.latin.R; import org.xmlpull.v1.XmlPullParserException; @@ -51,7 +55,7 @@ import java.util.Map; * </pre> */ public class Keyboard { - private static final String TAG = "Keyboard"; + private static final String TAG = Keyboard.class.getSimpleName(); public static final int EDGE_LEFT = 0x01; public static final int EDGE_RIGHT = 0x02; @@ -130,6 +134,8 @@ public class Keyboard { public final KeyboardId mId; + public final KeyboardIconsSet mIconsSet = new KeyboardIconsSet(); + // Variables for pre-computing nearest keys. // TODO: Change GRID_WIDTH and GRID_HEIGHT to private. @@ -448,7 +454,7 @@ public class Keyboard { } } - protected static void setDefaultBounds(Drawable drawable) { + public static void setDefaultBounds(Drawable drawable) { if (drawable != null) drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java index 1218a5abd..157337cca 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java @@ -27,6 +27,9 @@ import android.view.View; import android.view.inputmethod.EditorInfo; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; +import com.android.inputmethod.keyboard.internal.Key; +import com.android.inputmethod.keyboard.internal.ModifierKeyState; +import com.android.inputmethod.keyboard.internal.ShiftKeyState; import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.R; diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index f73cdc083..c372281c2 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -42,11 +42,14 @@ import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.view.ViewGroup.MarginLayoutParams; -import android.widget.FrameLayout; -import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; +import com.android.inputmethod.compat.FrameLayoutCompatUtils; +import com.android.inputmethod.keyboard.internal.Key; +import com.android.inputmethod.keyboard.internal.MiniKeyboardBuilder; +import com.android.inputmethod.keyboard.internal.PointerTrackerQueue; +import com.android.inputmethod.keyboard.internal.SwipeTracker; import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.R; @@ -942,27 +945,12 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { } private void addKeyPreview(TextView keyPreview) { - ViewGroup placer = mPreviewPlacer; - if (placer == null) { - final FrameLayout screenContent = (FrameLayout) getRootView().findViewById( - android.R.id.content); - if (android.os.Build.VERSION.SDK_INT >= /* HONEYCOMB */11) { - placer = screenContent; - } else { - // Insert LinearLayout to be able to setMargin because pre-Honeycomb FrameLayout - // could not handle setMargin properly. - placer = new LinearLayout(getContext()); - screenContent.addView(placer); - } - mPreviewPlacer = placer; - } - if (placer instanceof FrameLayout) { - // Honeycomb or later. - placer.addView(keyPreview, new FrameLayout.LayoutParams(0, 0)); - } else { - // Gingerbread or ealier. - placer.addView(keyPreview, new LinearLayout.LayoutParams(0, 0)); + if (mPreviewPlacer == null) { + mPreviewPlacer = FrameLayoutCompatUtils.getPlacer( + (ViewGroup)getRootView().findViewById(android.R.id.content)); } + final ViewGroup placer = mPreviewPlacer; + placer.addView(keyPreview, FrameLayoutCompatUtils.newLayoutParam(placer, 0, 0)); } // TODO: Introduce minimum duration for displaying key previews diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java index 020fb56ca..e741625ca 100644 --- a/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java +++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java @@ -33,6 +33,8 @@ import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import com.android.inputmethod.keyboard.internal.Key; +import com.android.inputmethod.keyboard.internal.SlidingLocaleDrawable; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.SubtypeSwitcher; diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java index 901df6ab7..d25d1f098 100644 --- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java @@ -24,6 +24,7 @@ import android.util.Log; import android.view.MotionEvent; import com.android.inputmethod.deprecated.VoiceProxy; +import com.android.inputmethod.keyboard.internal.Key; import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.Utils; diff --git a/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java b/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java index 2d6766f2d..d3d3fa59f 100644 --- a/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java +++ b/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java @@ -18,6 +18,8 @@ package com.android.inputmethod.keyboard; import android.content.Context; +import com.android.inputmethod.keyboard.internal.Key; + import java.util.List; public class MiniKeyboard extends Keyboard { diff --git a/java/src/com/android/inputmethod/keyboard/MiniKeyboardKeyDetector.java b/java/src/com/android/inputmethod/keyboard/MiniKeyboardKeyDetector.java index cc5c3bbfe..9170c3bd1 100644 --- a/java/src/com/android/inputmethod/keyboard/MiniKeyboardKeyDetector.java +++ b/java/src/com/android/inputmethod/keyboard/MiniKeyboardKeyDetector.java @@ -16,6 +16,8 @@ package com.android.inputmethod.keyboard; +import com.android.inputmethod.keyboard.internal.Key; + import java.util.List; public class MiniKeyboardKeyDetector extends KeyDetector { diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java index 0a727ad42..1d70481f4 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java +++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java @@ -16,16 +16,19 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.keyboard.KeyboardView.UIHandler; -import com.android.inputmethod.latin.LatinImeLogger; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.SubtypeSwitcher; - import android.content.res.Resources; import android.os.SystemClock; import android.util.Log; import android.view.MotionEvent; +import com.android.inputmethod.keyboard.KeyboardView.UIHandler; +import com.android.inputmethod.keyboard.internal.Key; +import com.android.inputmethod.keyboard.internal.PointerTrackerKeyState; +import com.android.inputmethod.keyboard.internal.PointerTrackerQueue; +import com.android.inputmethod.latin.LatinImeLogger; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.SubtypeSwitcher; + import java.util.Arrays; import java.util.List; diff --git a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java index ff64c73cd..2085404dc 100644 --- a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java @@ -25,6 +25,7 @@ import android.view.MotionEvent; import android.view.View; import android.widget.PopupWindow; +import com.android.inputmethod.keyboard.internal.Key; import com.android.inputmethod.latin.R; /** diff --git a/java/src/com/android/inputmethod/keyboard/PopupPanel.java b/java/src/com/android/inputmethod/keyboard/PopupPanel.java index 6f2b16148..72fa7406a 100644 --- a/java/src/com/android/inputmethod/keyboard/PopupPanel.java +++ b/java/src/com/android/inputmethod/keyboard/PopupPanel.java @@ -19,6 +19,8 @@ package com.android.inputmethod.keyboard; import android.view.MotionEvent; import android.widget.PopupWindow; +import com.android.inputmethod.keyboard.internal.Key; + public interface PopupPanel { /** * Show popup panel. diff --git a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java index 33acc6907..a6a07e518 100644 --- a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java +++ b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java @@ -16,6 +16,7 @@ package com.android.inputmethod.keyboard; +import com.android.inputmethod.keyboard.internal.Key; import com.android.inputmethod.latin.Utils; import java.util.Arrays; diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/internal/Key.java index e5ee272a2..5470067dc 100644 --- a/java/src/com/android/inputmethod/keyboard/Key.java +++ b/java/src/com/android/inputmethod/keyboard/internal/Key.java @@ -14,11 +14,7 @@ * the License. */ -package com.android.inputmethod.keyboard; - -import com.android.inputmethod.keyboard.KeyStyles.KeyStyle; -import com.android.inputmethod.keyboard.KeyboardParser.ParseException; -import com.android.inputmethod.latin.R; +package com.android.inputmethod.keyboard.internal; import android.content.res.Resources; import android.content.res.TypedArray; @@ -27,6 +23,11 @@ import android.graphics.drawable.Drawable; import android.text.TextUtils; import android.util.Xml; +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.internal.KeyStyles.KeyStyle; +import com.android.inputmethod.keyboard.internal.KeyboardParser.ParseException; +import com.android.inputmethod.latin.R; + import java.util.ArrayList; /** @@ -164,7 +165,7 @@ public class Key { mLabel = PopupCharactersParser.getLabel(popupSpecification); mOutputText = PopupCharactersParser.getOutputText(popupSpecification); mCode = PopupCharactersParser.getCode(res, popupSpecification); - mIcon = PopupCharactersParser.getIcon(res, popupSpecification); + mIcon = keyboard.mIconsSet.getIcon(PopupCharactersParser.getIconId(popupSpecification)); // Horizontal gap is divided equally to both sides of the key. mX = x + mGap / 2; mY = y; @@ -262,13 +263,18 @@ public class Key { mEdgeFlags = style.getFlag(keyAttr, R.styleable.Keyboard_Key_keyEdgeFlags, 0) | row.mRowEdgeFlags; + final KeyboardIconsSet iconsSet = mKeyboard.mIconsSet; mVisualInsetsLeft = KeyboardParser.getDimensionOrFraction(keyAttr, R.styleable.Keyboard_Key_visualInsetsLeft, mKeyboard.getDisplayHeight(), 0); mVisualInsetsRight = KeyboardParser.getDimensionOrFraction(keyAttr, R.styleable.Keyboard_Key_visualInsetsRight, mKeyboard.getDisplayHeight(), 0); - mPreviewIcon = style.getDrawable(keyAttr, R.styleable.Keyboard_Key_iconPreview); + mPreviewIcon = iconsSet.getIcon(style.getInt( + keyAttr, R.styleable.Keyboard_Key_keyIconPreview, + KeyboardIconsSet.ICON_UNDEFINED)); Keyboard.setDefaultBounds(mPreviewIcon); - mIcon = style.getDrawable(keyAttr, R.styleable.Keyboard_Key_keyIcon); + mIcon = iconsSet.getIcon(style.getInt( + keyAttr, R.styleable.Keyboard_Key_keyIcon, + KeyboardIconsSet.ICON_UNDEFINED)); Keyboard.setDefaultBounds(mIcon); mHintLetter = style.getText(keyAttr, R.styleable.Keyboard_Key_keyHintLetter); @@ -287,8 +293,9 @@ public class Key { mCode = Keyboard.CODE_DUMMY; } - final Drawable shiftedIcon = style.getDrawable(keyAttr, - R.styleable.Keyboard_Key_shiftedIcon); + final Drawable shiftedIcon = iconsSet.getIcon(style.getInt( + keyAttr, R.styleable.Keyboard_Key_keyIconShifted, + KeyboardIconsSet.ICON_UNDEFINED)); if (shiftedIcon != null) mKeyboard.getShiftedIcons().put(this, shiftedIcon); } finally { diff --git a/java/src/com/android/inputmethod/keyboard/KeyStyles.java b/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java index ce5ee5495..983f0649d 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyStyles.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyStyles.java @@ -14,16 +14,15 @@ * the License. */ -package com.android.inputmethod.keyboard; - -import com.android.inputmethod.keyboard.KeyboardParser.ParseException; -import com.android.inputmethod.latin.R; +package com.android.inputmethod.keyboard.internal; import android.content.res.TypedArray; import android.content.res.XmlResourceParser; -import android.graphics.drawable.Drawable; import android.util.Log; +import com.android.inputmethod.keyboard.internal.KeyboardParser.ParseException; +import com.android.inputmethod.latin.R; + import java.util.ArrayList; import java.util.HashMap; @@ -37,7 +36,6 @@ public class KeyStyles { public interface KeyStyle { public CharSequence[] getTextArray(TypedArray a, int index); - public Drawable getDrawable(TypedArray a, int index); public CharSequence getText(TypedArray a, int index); public int getInt(TypedArray a, int index, int defaultValue); public int getFlag(TypedArray a, int index, int defaultValue); @@ -55,11 +53,6 @@ public class KeyStyles { } @Override - public Drawable getDrawable(TypedArray a, int index) { - return a.getDrawable(index); - } - - @Override public CharSequence getText(TypedArray a, int index) { return a.getText(index); } @@ -140,12 +133,6 @@ public class KeyStyles { } @Override - public Drawable getDrawable(TypedArray a, int index) { - return a.hasValue(index) - ? super.getDrawable(a, index) : (Drawable)mAttributes.get(index); - } - - @Override public CharSequence getText(TypedArray a, int index) { return a.hasValue(index) ? super.getText(a, index) : (CharSequence)mAttributes.get(index); @@ -177,25 +164,20 @@ public class KeyStyles { // TODO: Currently not all Key attributes can be declared as style. readInt(keyAttr, R.styleable.Keyboard_Key_code); readText(keyAttr, R.styleable.Keyboard_Key_keyLabel); - readFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelOption); - readTextArray(keyAttr, R.styleable.Keyboard_Key_popupCharacters); - readInt(keyAttr, R.styleable.Keyboard_Key_maxPopupKeyboardColumn); readText(keyAttr, R.styleable.Keyboard_Key_keyOutputText); - readDrawable(keyAttr, R.styleable.Keyboard_Key_keyIcon); - readDrawable(keyAttr, R.styleable.Keyboard_Key_iconPreview); readText(keyAttr, R.styleable.Keyboard_Key_keyHintLetter); - readDrawable(keyAttr, R.styleable.Keyboard_Key_shiftedIcon); + readTextArray(keyAttr, R.styleable.Keyboard_Key_popupCharacters); + readFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelOption); + readInt(keyAttr, R.styleable.Keyboard_Key_keyIcon); + readInt(keyAttr, R.styleable.Keyboard_Key_keyIconPreview); + readInt(keyAttr, R.styleable.Keyboard_Key_keyIconShifted); + readInt(keyAttr, R.styleable.Keyboard_Key_maxPopupKeyboardColumn); readBoolean(keyAttr, R.styleable.Keyboard_Key_isFunctional); readBoolean(keyAttr, R.styleable.Keyboard_Key_isSticky); readBoolean(keyAttr, R.styleable.Keyboard_Key_isRepeatable); readBoolean(keyAttr, R.styleable.Keyboard_Key_enabled); } - private void readDrawable(TypedArray a, int index) { - if (a.hasValue(index)) - mAttributes.put(index, a.getDrawable(index)); - } - private void readText(TypedArray a, int index) { if (a.hasValue(index)) mAttributes.put(index, a.getText(index)); diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java new file mode 100644 index 000000000..7be738ceb --- /dev/null +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java @@ -0,0 +1,147 @@ +/* + * 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.keyboard.internal; + +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; + +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.latin.R; + +public class KeyboardIconsSet { + public static final int ICON_UNDEFINED = 0; + + // This should be aligned with Keyboard.keyIcon enum. + private static final int ICON_SHIFT_KEY = 1; + private static final int ICON_TO_SYMBOL_KEY = 2; + private static final int ICON_TO_SYMBOL_KEY_WITH_SHORTCUT = 3; + private static final int ICON_DELETE_KEY = 4; + private static final int ICON_SETTINGS_KEY = 5; + private static final int ICON_SHORTCUT_KEY = 6; + private static final int ICON_SPACE_KEY = 7; + private static final int ICON_RETURN_KEY = 8; + private static final int ICON_SEARCH_KEY = 9; + private static final int ICON_TAB_KEY = 10; + private static final int ICON_NUM1_KEY = 11; + private static final int ICON_NUM2_KEY = 12; + private static final int ICON_NUM3_KEY = 13; + private static final int ICON_NUM4_KEY = 14; + private static final int ICON_NUM5_KEY = 15; + private static final int ICON_NUM6_KEY = 16; + private static final int ICON_NUM7_KEY = 17; + private static final int ICON_NUM8_KEY = 18; + private static final int ICON_NUM9_KEY = 19; + private static final int ICON_NUM0_KEY = 20; + private static final int ICON_NUM_STAR_KEY = 21; + private static final int ICON_NUM_POUND_KEY = 22; + private static final int ICON_NUM_ALT_KEY = 23; + // This should be aligned with Keyboard.keyIconShifted enum. + private static final int ICON_SHIFTED_SHIFT_KEY = 24; + // This should be aligned with Keyboard.keyIconPreview enum. + private static final int ICON_PREVIEW_SPACE_KEY = 25; + private static final int ICON_PREVIEW_TAB_KEY = 26; + private static final int ICON_PREVIEW_SETTINGS_KEY = 27; + private static final int ICON_PREVIEW_SHORTCUT_KEY = 28; + + private static final int ICON_LAST = 28; + + private final Drawable mIcons[] = new Drawable[ICON_LAST + 1]; + + private static final int getIconId(int attrIndex) { + switch (attrIndex) { + case R.styleable.Keyboard_iconShiftKey: + return ICON_SHIFT_KEY; + case R.styleable.Keyboard_iconToSymbolKey: + return ICON_TO_SYMBOL_KEY; + case R.styleable.Keyboard_iconToSymbolKeyWithShortcut: + return ICON_TO_SYMBOL_KEY_WITH_SHORTCUT; + case R.styleable.Keyboard_iconDeleteKey: + return ICON_DELETE_KEY; + case R.styleable.Keyboard_iconSettingsKey: + return ICON_SETTINGS_KEY; + case R.styleable.Keyboard_iconShortcutKey: + return ICON_SHORTCUT_KEY; + case R.styleable.Keyboard_iconSpaceKey: + return ICON_SPACE_KEY; + case R.styleable.Keyboard_iconReturnKey: + return ICON_RETURN_KEY; + case R.styleable.Keyboard_iconSearchKey: + return ICON_SEARCH_KEY; + case R.styleable.Keyboard_iconTabKey: + return ICON_TAB_KEY; + case R.styleable.Keyboard_iconNum1Key: + return ICON_NUM1_KEY; + case R.styleable.Keyboard_iconNum2Key: + return ICON_NUM2_KEY; + case R.styleable.Keyboard_iconNum3Key: + return ICON_NUM3_KEY; + case R.styleable.Keyboard_iconNum4Key: + return ICON_NUM4_KEY; + case R.styleable.Keyboard_iconNum5Key: + return ICON_NUM5_KEY; + case R.styleable.Keyboard_iconNum6Key: + return ICON_NUM6_KEY; + case R.styleable.Keyboard_iconNum7Key: + return ICON_NUM7_KEY; + case R.styleable.Keyboard_iconNum8Key: + return ICON_NUM8_KEY; + case R.styleable.Keyboard_iconNum9Key: + return ICON_NUM9_KEY; + case R.styleable.Keyboard_iconNum0Key: + return ICON_NUM0_KEY; + case R.styleable.Keyboard_iconNumStarKey: + return ICON_NUM_STAR_KEY; + case R.styleable.Keyboard_iconNumPoundKey: + return ICON_NUM_POUND_KEY; + case R.styleable.Keyboard_iconNumAltKey: + return ICON_NUM_ALT_KEY; + case R.styleable.Keyboard_iconShiftedShiftKey: + return ICON_SHIFTED_SHIFT_KEY; + case R.styleable.Keyboard_iconPreviewSpaceKey: + return ICON_PREVIEW_SPACE_KEY; + case R.styleable.Keyboard_iconPreviewTabKey: + return ICON_PREVIEW_TAB_KEY; + case R.styleable.Keyboard_iconPreviewSettingsKey: + return ICON_PREVIEW_SETTINGS_KEY; + case R.styleable.Keyboard_iconPreviewShortcutKey: + return ICON_PREVIEW_SHORTCUT_KEY; + default: + return ICON_UNDEFINED; + } + } + + public void loadIcons(TypedArray keyboardAttrs) { + final int count = keyboardAttrs.getIndexCount(); + for (int i = 0; i < count; i++) { + final int attrIndex = keyboardAttrs.getIndex(i); + final int iconId = getIconId(attrIndex); + if (iconId != ICON_UNDEFINED) { + final Drawable icon = keyboardAttrs.getDrawable(attrIndex); + Keyboard.setDefaultBounds(icon); + mIcons[iconId] = icon; + } + } + } + + public Drawable getIcon(int iconId) { + if (iconId == ICON_UNDEFINED) + return null; + if (iconId < 0 || iconId >= mIcons.length) + throw new IllegalArgumentException("icon id is out of range: " + iconId); + return mIcons[iconId]; + } +} diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardParser.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardParser.java index 20af12bc5..9525b0e00 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardParser.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardParser.java @@ -14,7 +14,7 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; import android.content.Context; import android.content.res.Resources; @@ -26,6 +26,8 @@ import android.util.Xml; import android.view.InflateException; import com.android.inputmethod.compat.EditorInfoCompatUtils; +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.KeyboardId; import com.android.inputmethod.latin.R; import org.xmlpull.v1.XmlPullParser; @@ -230,6 +232,8 @@ public class KeyboardParser { keyboard.setMaxPopupKeyboardColumn(keyAttr.getInt( R.styleable.Keyboard_Key_maxPopupKeyboardColumn, 5)); + + mKeyboard.mIconsSet.loadIcons(keyboardAttr); } finally { keyAttr.recycle(); keyboardAttr.recycle(); diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardShiftState.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardShiftState.java index e015b5158..0cde4e5b5 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardShiftState.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardShiftState.java @@ -14,10 +14,12 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; import android.util.Log; +import com.android.inputmethod.keyboard.KeyboardSwitcher; + public class KeyboardShiftState { private static final String TAG = "KeyboardShiftState"; private static final boolean DEBUG = KeyboardSwitcher.DEBUG_STATE; diff --git a/java/src/com/android/inputmethod/keyboard/MiniKeyboardBuilder.java b/java/src/com/android/inputmethod/keyboard/internal/MiniKeyboardBuilder.java index 6e939123d..01faae61d 100644 --- a/java/src/com/android/inputmethod/keyboard/MiniKeyboardBuilder.java +++ b/java/src/com/android/inputmethod/keyboard/internal/MiniKeyboardBuilder.java @@ -14,15 +14,18 @@ * the License. */ -package com.android.inputmethod.keyboard; - -import com.android.inputmethod.latin.R; +package com.android.inputmethod.keyboard.internal; import android.content.Context; import android.content.res.Resources; import android.graphics.Paint; import android.graphics.Rect; +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.KeyboardView; +import com.android.inputmethod.keyboard.MiniKeyboard; +import com.android.inputmethod.latin.R; + import java.util.List; public class MiniKeyboardBuilder { diff --git a/java/src/com/android/inputmethod/keyboard/ModifierKeyState.java b/java/src/com/android/inputmethod/keyboard/internal/ModifierKeyState.java index ebbc79a9e..dae73c4e4 100644 --- a/java/src/com/android/inputmethod/keyboard/ModifierKeyState.java +++ b/java/src/com/android/inputmethod/keyboard/internal/ModifierKeyState.java @@ -14,10 +14,12 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; import android.util.Log; +import com.android.inputmethod.keyboard.KeyboardSwitcher; + public class ModifierKeyState { protected static final String TAG = "ModifierKeyState"; protected static final boolean DEBUG = KeyboardSwitcher.DEBUG_STATE; diff --git a/java/src/com/android/inputmethod/keyboard/PointerTrackerKeyState.java b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerKeyState.java index eecbb26f3..ddadb1338 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTrackerKeyState.java +++ b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerKeyState.java @@ -14,12 +14,15 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; + +import com.android.inputmethod.keyboard.KeyDetector; +import com.android.inputmethod.keyboard.PointerTracker; /** * This class keeps track of a key index and a position where {@link PointerTracker} is. */ -/* package */ class PointerTrackerKeyState { +public class PointerTrackerKeyState { private final KeyDetector mKeyDetector; // The position and time at which first down event occurred. diff --git a/java/src/com/android/inputmethod/keyboard/PointerTrackerQueue.java b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java index 9e287c67d..f87cd869e 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTrackerQueue.java +++ b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java @@ -14,7 +14,9 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; + +import com.android.inputmethod.keyboard.PointerTracker; import java.util.LinkedList; diff --git a/java/src/com/android/inputmethod/keyboard/PopupCharactersParser.java b/java/src/com/android/inputmethod/keyboard/internal/PopupCharactersParser.java index ff78ee5c9..8276f5d78 100644 --- a/java/src/com/android/inputmethod/keyboard/PopupCharactersParser.java +++ b/java/src/com/android/inputmethod/keyboard/internal/PopupCharactersParser.java @@ -14,13 +14,14 @@ * the License. */ -package com.android.inputmethod.keyboard; - -import com.android.inputmethod.latin.R; +package com.android.inputmethod.keyboard.internal; import android.content.res.Resources; -import android.graphics.drawable.Drawable; import android.text.TextUtils; +import android.util.Log; + +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.latin.R; /** * String parser of popupCharacters attribute of Key. @@ -28,16 +29,19 @@ import android.text.TextUtils; * Each popup key text is one of the following: * - A single letter (Letter) * - Label optionally followed by keyOutputText or code (keyLabel|keyOutputText). - * - Icon followed by keyOutputText or code (@drawable/icon|@integer/key_code) + * - Icon followed by keyOutputText or code (@icon/icon_number|@integer/key_code) * Special character, comma ',' backslash '\', and bar '|' can be escaped by '\' * character. * Note that the character '@' and '\' are also parsed by XML parser and CSV parser as well. + * See {@link KeyboardIconsSet} about icon_number. */ public class PopupCharactersParser { + private static final String TAG = PopupCharactersParser.class.getSimpleName(); + private static final char ESCAPE = '\\'; private static final String LABEL_END = "|"; private static final String PREFIX_AT = "@"; - private static final String PREFIX_ICON = PREFIX_AT + "drawable/"; + private static final String PREFIX_ICON = PREFIX_AT + "icon/"; private static final String PREFIX_CODE = PREFIX_AT + "integer/"; private PopupCharactersParser() { @@ -150,13 +154,18 @@ public class PopupCharactersParser { return Keyboard.CODE_DUMMY; } - public static Drawable getIcon(Resources res, String popupSpec) { + public static int getIconId(String popupSpec) { if (hasIcon(popupSpec)) { int end = popupSpec.indexOf(LABEL_END, PREFIX_ICON.length() + 1); - int resId = getResourceId(res, popupSpec.substring(PREFIX_AT.length(), end)); - return res.getDrawable(resId); + final String iconId = popupSpec.substring(PREFIX_ICON.length(), end); + try { + return Integer.valueOf(iconId); + } catch (NumberFormatException e) { + Log.w(TAG, "illegal icon id specified: " + iconId); + return KeyboardIconsSet.ICON_UNDEFINED; + } } - return null; + return KeyboardIconsSet.ICON_UNDEFINED; } private static int getResourceId(Resources res, String name) { diff --git a/java/src/com/android/inputmethod/keyboard/Row.java b/java/src/com/android/inputmethod/keyboard/internal/Row.java index 40d7e1472..06aadcc05 100644 --- a/java/src/com/android/inputmethod/keyboard/Row.java +++ b/java/src/com/android/inputmethod/keyboard/internal/Row.java @@ -14,15 +14,16 @@ * the License. */ -package com.android.inputmethod.keyboard; - -import com.android.inputmethod.latin.R; +package com.android.inputmethod.keyboard.internal; import android.content.res.Resources; import android.content.res.TypedArray; import android.content.res.XmlResourceParser; import android.util.Xml; +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.latin.R; + /** * Container for keys in the keyboard. All keys in a row are at the same Y-coordinate. * Some of the key size defaults can be overridden per row from what the {@link Keyboard} diff --git a/java/src/com/android/inputmethod/keyboard/ShiftKeyState.java b/java/src/com/android/inputmethod/keyboard/internal/ShiftKeyState.java index ba15624f0..6617b917f 100644 --- a/java/src/com/android/inputmethod/keyboard/ShiftKeyState.java +++ b/java/src/com/android/inputmethod/keyboard/internal/ShiftKeyState.java @@ -14,7 +14,7 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; import android.util.Log; diff --git a/java/src/com/android/inputmethod/keyboard/SlidingLocaleDrawable.java b/java/src/com/android/inputmethod/keyboard/internal/SlidingLocaleDrawable.java index dd271de64..df4b575f1 100644 --- a/java/src/com/android/inputmethod/keyboard/SlidingLocaleDrawable.java +++ b/java/src/com/android/inputmethod/keyboard/internal/SlidingLocaleDrawable.java @@ -14,7 +14,7 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; import android.content.Context; import android.content.res.TypedArray; @@ -28,6 +28,8 @@ import android.graphics.drawable.Drawable; import android.text.TextPaint; import android.view.ViewConfiguration; +import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.LatinKeyboard; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.SubtypeSwitcher; @@ -78,7 +80,7 @@ public class SlidingLocaleDrawable extends Drawable { mThreshold = ViewConfiguration.get(context).getScaledTouchSlop(); } - void setDiff(int diff) { + public void setDiff(int diff) { if (diff == Integer.MAX_VALUE) { mHitThreshold = false; mCurrentLanguage = null; diff --git a/java/src/com/android/inputmethod/keyboard/SwipeTracker.java b/java/src/com/android/inputmethod/keyboard/internal/SwipeTracker.java index 975b13b50..8d192c2f0 100644 --- a/java/src/com/android/inputmethod/keyboard/SwipeTracker.java +++ b/java/src/com/android/inputmethod/keyboard/internal/SwipeTracker.java @@ -14,7 +14,7 @@ * the License. */ -package com.android.inputmethod.keyboard; +package com.android.inputmethod.keyboard.internal; import android.view.MotionEvent; diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java index 92d26a01c..09b356d65 100644 --- a/java/src/com/android/inputmethod/latin/CandidateView.java +++ b/java/src/com/android/inputmethod/latin/CandidateView.java @@ -44,6 +44,8 @@ import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; +import com.android.inputmethod.compat.FrameLayoutCompatUtils; +import com.android.inputmethod.compat.LinearLayoutCompatUtils; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import java.util.ArrayList; @@ -148,7 +150,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); @@ -232,7 +245,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo public void setListener(Listener listener, View inputView) { mListener = listener; mKeyboardView = inputView.findViewById(R.id.keyboard_view); - mCandidatesPane = (ViewGroup)inputView.findViewById(R.id.candidates_pane); + mCandidatesPane = FrameLayoutCompatUtils.getPlacer( + (ViewGroup)inputView.findViewById(R.id.candidates_pane)); mCandidatesPane.setOnClickListener(this); mCandidatesPaneContainer = (ViewGroup)inputView.findViewById( R.id.candidates_pane_container); @@ -334,12 +348,10 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo } if (x != 0) { final View divider = mDividers.get(i - NUM_CANDIDATES_IN_STRIP); - mCandidatesPane.addView(divider); - placeCandidateAt(divider, x, y); + addCandidateAt(divider, x, y); x += dividerWidth; } - mCandidatesPane.addView(tv); - placeCandidateAt(tv, x, y); + addCandidateAt(tv, x, y); x += width; } @@ -360,14 +372,13 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo } } - private void placeCandidateAt(View v, int x, int y) { - ViewGroup.LayoutParams lp = v.getLayoutParams(); - if (lp instanceof ViewGroup.MarginLayoutParams) { - ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)lp; - mlp.width = v.getMeasuredWidth(); - mlp.height = v.getMeasuredHeight(); - mlp.setMargins(x, y + (mCandidateStripHeight - mlp.height) / 2, 0, 0); - } + private void addCandidateAt(View v, int x, int y) { + final int width = v.getMeasuredWidth(); + final int height = v.getMeasuredHeight(); + final MarginLayoutParams marginLayoutParams = FrameLayoutCompatUtils.newLayoutParam( + mCandidatesPane, width, height); + marginLayoutParams.setMargins(x, y + (mCandidateStripHeight - height) / 2, 0, 0); + mCandidatesPane.addView(v, marginLayoutParams); } private void centeringCandidates(int from, int to, int width, int paneWidth) { |