diff options
Diffstat (limited to 'java/src/com/android')
19 files changed, 366 insertions, 262 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/KeyDetector.java b/java/src/com/android/inputmethod/keyboard/KeyDetector.java index 97d88af4a..c0e6aa8d7 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyDetector.java +++ b/java/src/com/android/inputmethod/keyboard/KeyDetector.java @@ -59,6 +59,9 @@ public class KeyDetector { } public Keyboard getKeyboard() { + if (mKeyboard == null) { + throw new IllegalStateException("keyboard isn't set"); + } return mKeyboard; } diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java index 3abe890cb..919850095 100644 --- a/java/src/com/android/inputmethod/keyboard/Keyboard.java +++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java @@ -219,6 +219,11 @@ public class Keyboard { return code >= CODE_SPACE; } + @Override + public String toString() { + return mId.toString(); + } + public static class Params { public KeyboardId mId; public int mThemeId; diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index 0e6de7032..fcf97b99c 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -108,14 +108,9 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { private int mDelayAfterPreview; private final PreviewPlacerView mPreviewPlacerView; - /** True if {@link KeyboardView} should handle gesture events. */ - protected boolean mShouldHandleGesture; - // Drawing /** True if the entire keyboard needs to be dimmed. */ private boolean mNeedsToDimEntireKeyboard; - /** Whether the keyboard bitmap buffer needs to be redrawn before it's blitted. **/ - private boolean mBufferNeedsUpdate; /** True if all keys should be drawn */ private boolean mInvalidateAllKeys; /** The keys that should be drawn */ @@ -438,9 +433,8 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { return mShowKeyPreviewPopup; } - public void setGestureHandlingMode(boolean shouldHandleGesture, - boolean drawsGesturePreviewTrail, boolean drawsGestureFloatingPreviewText) { - mShouldHandleGesture = shouldHandleGesture; + public void setGesturePreviewMode(boolean drawsGesturePreviewTrail, + boolean drawsGestureFloatingPreviewText) { mPreviewPlacerView.setGesturePreviewMode( drawsGesturePreviewTrail, drawsGestureFloatingPreviewText); } @@ -463,8 +457,9 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { onDrawKeyboard(canvas); return; } - if (mBufferNeedsUpdate || mOffscreenBuffer == null) { - mBufferNeedsUpdate = false; + + final boolean bufferNeedsUpdates = mInvalidateAllKeys || !mInvalidatedKeys.isEmpty(); + if (bufferNeedsUpdates || mOffscreenBuffer == null) { if (maybeAllocateOffscreenBuffer()) { mInvalidateAllKeys = true; // TODO: Stop using the offscreen canvas even when in software rendering @@ -528,13 +523,12 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { } if (!isHardwareAccelerated) { canvas.clipRegion(mClipRegion, Region.Op.REPLACE); - } - - // Draw keyboard background. - canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR); - final Drawable background = getBackground(); - if (background != null) { - background.draw(canvas); + // Draw keyboard background. + canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR); + final Drawable background = getBackground(); + if (background != null) { + background.draw(canvas); + } } // TODO: Confirm if it's really required to draw all keys when hardware acceleration is on. @@ -1052,7 +1046,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { public void invalidateAllKeys() { mInvalidatedKeys.clear(); mInvalidateAllKeys = true; - mBufferNeedsUpdate = true; invalidate(); } @@ -1070,9 +1063,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { mInvalidatedKeys.add(key); final int x = key.mX + getPaddingLeft(); final int y = key.mY + getPaddingTop(); - mWorkingRect.set(x, y, x + key.mWidth, y + key.mHeight); - mBufferNeedsUpdate = true; - invalidate(mWorkingRect); + invalidate(x, y, x + key.mWidth, y + key.mHeight); } public void closing() { diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java index fe9cb9415..9590290ea 100644 --- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java @@ -482,7 +482,7 @@ public class MainKeyboardView extends KeyboardView implements PointerTracker.Key super.setKeyboard(keyboard); mKeyDetector.setKeyboard( keyboard, -getPaddingLeft(), -getPaddingTop() + mVerticalCorrection); - PointerTracker.setKeyDetector(mKeyDetector, mShouldHandleGesture); + PointerTracker.setKeyDetector(mKeyDetector); mTouchScreenRegulator.setKeyboard(keyboard); mMoreKeysPanelCache.clear(); @@ -500,12 +500,13 @@ public class MainKeyboardView extends KeyboardView implements PointerTracker.Key AccessibleKeyboardViewProxy.getInstance().setKeyboard(keyboard); } - @Override - public void setGestureHandlingMode(final boolean shouldHandleGesture, - boolean drawsGesturePreviewTrail, boolean drawsGestureFloatingPreviewText) { - super.setGestureHandlingMode(shouldHandleGesture, drawsGesturePreviewTrail, - drawsGestureFloatingPreviewText); - PointerTracker.setKeyDetector(mKeyDetector, shouldHandleGesture); + // Note that this method is called from a non-UI thread. + public void setMainDictionaryAvailability(boolean mainDictionaryAvailable) { + PointerTracker.setMainDictionaryAvailability(mainDictionaryAvailable); + } + + public void setGestureHandlingEnabledByUser(boolean gestureHandlingEnabledByUser) { + PointerTracker.setGestureHandlingEnabledByUser(gestureHandlingEnabledByUser); } /** @@ -834,20 +835,6 @@ public class MainKeyboardView extends KeyboardView implements PointerTracker.Key return false; } - @Override - public void draw(Canvas c) { - Utils.GCUtils.getInstance().reset(); - boolean tryGC = true; - for (int i = 0; i < Utils.GCUtils.GC_TRY_LOOP_MAX && tryGC; ++i) { - try { - super.draw(c); - tryGC = false; - } catch (OutOfMemoryError e) { - tryGC = Utils.GCUtils.getInstance().tryGCOrWait(TAG, e); - } - } - } - /** * Receives hover events from the input framework. * diff --git a/java/src/com/android/inputmethod/keyboard/MoreKeysDetector.java b/java/src/com/android/inputmethod/keyboard/MoreKeysDetector.java index a183546dd..cd4e3001e 100644 --- a/java/src/com/android/inputmethod/keyboard/MoreKeysDetector.java +++ b/java/src/com/android/inputmethod/keyboard/MoreKeysDetector.java @@ -39,11 +39,7 @@ public class MoreKeysDetector extends KeyDetector { Key nearestKey = null; int nearestDist = (y < 0) ? mSlideAllowanceSquareTop : mSlideAllowanceSquare; - final Keyboard keyboard = getKeyboard(); - if (keyboard == null) { - throw new NullPointerException("Keyboard isn't set"); - } - for (final Key key : keyboard.mKeys) { + for (final Key key : getKeyboard().mKeys) { final int dist = key.squaredDistanceToEdge(touchX, touchY); if (dist < nearestDist) { nearestKey = key; diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java index 184011ffe..7d565a64f 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java +++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java @@ -34,7 +34,7 @@ import com.android.inputmethod.research.ResearchLogger; import java.util.ArrayList; -public class PointerTracker implements PointerTrackerQueue.ElementActions { +public class PointerTracker implements PointerTrackerQueue.Element { private static final String TAG = PointerTracker.class.getSimpleName(); private static final boolean DEBUG_EVENT = false; private static final boolean DEBUG_MOVE_EVENT = false; @@ -43,6 +43,9 @@ public class PointerTracker implements PointerTrackerQueue.ElementActions { /** True if {@link PointerTracker}s should handle gesture events. */ private static boolean sShouldHandleGesture = false; + private static boolean sMainDictionaryAvailable = false; + private static boolean sGestureHandlingEnabledByInputField = false; + private static boolean sGestureHandlingEnabledByUser = false; private static final int MIN_GESTURE_RECOGNITION_TIME = 100; // msec @@ -126,10 +129,6 @@ public class PointerTracker implements PointerTrackerQueue.ElementActions { private static final InputPointers sAggregratedPointers = new InputPointers( GestureStroke.DEFAULT_CAPACITY); private static PointerTrackerQueue sPointerTrackerQueue; - // HACK: Change gesture detection criteria depending on this variable. - // TODO: Find more comprehensive ways to detect a gesture start. - // True when the previous user input was a gesture input, not a typing input. - private static boolean sWasInGesture; public final int mPointerId; @@ -198,7 +197,6 @@ public class PointerTracker implements PointerTrackerQueue.ElementActions { sNeedsPhantomSuddenMoveEventHack = needsPhantomSuddenMoveEventHack; setParameters(MainKeyboardView.PointerTrackerParams.DEFAULT); - updateGestureHandlingMode(null, false /* shouldHandleGesture */); } public static void setParameters(MainKeyboardView.PointerTrackerParams params) { @@ -207,14 +205,22 @@ public class PointerTracker implements PointerTrackerQueue.ElementActions { params.mTouchNoiseThresholdDistance * params.mTouchNoiseThresholdDistance); } - private static void updateGestureHandlingMode(Keyboard keyboard, boolean shouldHandleGesture) { - if (!shouldHandleGesture - || AccessibilityUtils.getInstance().isTouchExplorationEnabled() - || (keyboard != null && keyboard.mId.passwordInput())) { - sShouldHandleGesture = false; - } else { - sShouldHandleGesture = true; - } + private static void updateGestureHandlingMode() { + sShouldHandleGesture = sMainDictionaryAvailable + && sGestureHandlingEnabledByInputField + && sGestureHandlingEnabledByUser + && !AccessibilityUtils.getInstance().isTouchExplorationEnabled(); + } + + // Note that this method is called from a non-UI thread. + public static void setMainDictionaryAvailability(boolean mainDictionaryAvailable) { + sMainDictionaryAvailable = mainDictionaryAvailable; + updateGestureHandlingMode(); + } + + public static void setGestureHandlingEnabledByUser(boolean gestureHandlingEnabledByUser) { + sGestureHandlingEnabledByUser = gestureHandlingEnabledByUser; + updateGestureHandlingMode(); } public static PointerTracker getPointerTracker(final int id, KeyEventHandler handler) { @@ -241,7 +247,7 @@ public class PointerTracker implements PointerTrackerQueue.ElementActions { } } - public static void setKeyDetector(KeyDetector keyDetector, boolean shouldHandleGesture) { + public static void setKeyDetector(KeyDetector keyDetector) { final int trackersSize = sTrackers.size(); for (int i = 0; i < trackersSize; ++i) { final PointerTracker tracker = sTrackers.get(i); @@ -250,7 +256,8 @@ public class PointerTracker implements PointerTrackerQueue.ElementActions { tracker.mKeyboardLayoutHasBeenChanged = true; } final Keyboard keyboard = keyDetector.getKeyboard(); - updateGestureHandlingMode(keyboard, shouldHandleGesture); + sGestureHandlingEnabledByInputField = !keyboard.mId.passwordInput(); + updateGestureHandlingMode(); } public static void dismissAllKeyPreviews() { @@ -401,8 +408,7 @@ public class PointerTracker implements PointerTrackerQueue.ElementActions { mKeyDetector = keyDetector; mKeyboard = keyDetector.getKeyboard(); mIsAlphabetKeyboard = mKeyboard.mId.isAlphabetKeyboard(); - mGestureStroke.setGestureSampleLength( - mKeyboard.mMostCommonKeyWidth, mKeyboard.mMostCommonKeyHeight); + mGestureStroke.setGestureSampleLength(mKeyboard.mMostCommonKeyWidth); final Key newKey = mKeyDetector.detectHitKey(mKeyX, mKeyY); if (newKey != mCurrentKey) { if (mDrawingProxy != null) { @@ -514,7 +520,7 @@ public class PointerTracker implements PointerTrackerQueue.ElementActions { public void drawGestureTrail(Canvas canvas, Paint paint) { if (mInGesture) { - mGestureStroke.drawGestureTrail(canvas, paint, mLastX, mLastY); + mGestureStroke.drawGestureTrail(canvas, paint); } } @@ -574,7 +580,6 @@ public class PointerTracker implements PointerTrackerQueue.ElementActions { mListener.onEndBatchInput(batchPoints); clearBatchInputRecognitionStateOfThisPointerTracker(); clearBatchInputPointsOfAllPointerTrackers(); - sWasInGesture = true; } private void abortBatchInput() { @@ -707,7 +712,7 @@ public class PointerTracker implements PointerTrackerQueue.ElementActions { if (sShouldHandleGesture && mIsPossibleGesture) { final GestureStroke stroke = mGestureStroke; stroke.addPoint(x, y, gestureTime, isHistorical); - if (!mInGesture && stroke.isStartOfAGesture(gestureTime, sWasInGesture)) { + if (!mInGesture && stroke.isStartOfAGesture()) { startBatchInput(); } } @@ -990,7 +995,6 @@ public class PointerTracker implements PointerTrackerQueue.ElementActions { int code = key.mCode; callListenerOnCodeInput(key, code, x, y); callListenerOnRelease(key, code, false); - sWasInGesture = false; } private void printTouchEvent(String title, int x, int y, long eventTime) { diff --git a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java index ae123e29a..ac0a56ba3 100644 --- a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java +++ b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java @@ -18,7 +18,6 @@ package com.android.inputmethod.keyboard; import android.graphics.Rect; import android.text.TextUtils; -import android.util.FloatMath; import com.android.inputmethod.keyboard.Keyboard.Params.TouchPositionCorrection; import com.android.inputmethod.latin.JniUtils; @@ -155,7 +154,9 @@ public class ProximityInfo { final float radius = touchPositionCorrection.mRadii[row]; sweetSpotCenterXs[i] = hitBox.exactCenterX() + x * hitBoxWidth; sweetSpotCenterYs[i] = hitBox.exactCenterY() + y * hitBoxHeight; - sweetSpotRadii[i] = radius * FloatMath.sqrt( + // Note that, in recent versions of Android, FloatMath is actually slower than + // java.lang.Math due to the way the JIT optimizes java.lang.Math. + sweetSpotRadii[i] = radius * (float)Math.sqrt( hitBoxWidth * hitBoxWidth + hitBoxHeight * hitBoxHeight); } } diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java b/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java index 28d6c1d07..79e977a40 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java +++ b/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java @@ -16,7 +16,6 @@ package com.android.inputmethod.keyboard.internal; import android.graphics.Canvas; import android.graphics.Paint; -import android.util.FloatMath; import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.InputPointers; @@ -38,19 +37,16 @@ public class GestureStroke { private int mLastPointY; private int mMinGestureLength; - private int mMinGestureLengthWhileInGesture; private int mMinGestureSampleLength; // TODO: Move some of these to resource. - private static final float MIN_GESTURE_LENGTH_RATIO_TO_KEY_WIDTH = 1.0f; - private static final float MIN_GESTURE_LENGTH_RATIO_TO_KEY_WIDTH_WHILE_IN_GESTURE = 0.5f; - private static final int MIN_GESTURE_DURATION = 150; // msec - private static final int MIN_GESTURE_DURATION_WHILE_IN_GESTURE = 75; // msec - private static final float MIN_GESTURE_SAMPLING_RATIO_TO_KEY_HEIGHT = 1.0f / 6.0f; + private static final float MIN_GESTURE_LENGTH_RATIO_TO_KEY_WIDTH = 0.75f; + private static final int MIN_GESTURE_DURATION = 100; // msec + private static final float MIN_GESTURE_SAMPLING_RATIO_TO_KEY_WIDTH = 1.0f / 6.0f; private static final float GESTURE_RECOG_SPEED_THRESHOLD = 0.4f; // dip/msec private static final float GESTURE_RECOG_CURVATURE_THRESHOLD = (float)(Math.PI / 4.0f); - private static final float DOUBLE_PI = (float)(2 * Math.PI); + private static final float DOUBLE_PI = (float)(2.0f * Math.PI); // Fade based on number of gesture samples, see MIN_GESTURE_SAMPLING_RATIO_TO_KEY_HEIGHT private static final int DRAWING_GESTURE_FADE_START = 10; @@ -61,21 +57,15 @@ public class GestureStroke { reset(); } - public void setGestureSampleLength(final int keyWidth, final int keyHeight) { + public void setGestureSampleLength(final int keyWidth) { // TODO: Find an appropriate base metric for these length. Maybe diagonal length of the key? mMinGestureLength = (int)(keyWidth * MIN_GESTURE_LENGTH_RATIO_TO_KEY_WIDTH); - mMinGestureLengthWhileInGesture = (int)( - keyWidth * MIN_GESTURE_LENGTH_RATIO_TO_KEY_WIDTH_WHILE_IN_GESTURE); - mMinGestureSampleLength = (int)(keyHeight * MIN_GESTURE_SAMPLING_RATIO_TO_KEY_HEIGHT); + mMinGestureSampleLength = (int)(keyWidth * MIN_GESTURE_SAMPLING_RATIO_TO_KEY_WIDTH); } - public boolean isStartOfAGesture(final int downDuration, final boolean wasInGesture) { - // The tolerance of the time duration and the stroke length to detect the start of a - // gesture stroke should be eased when the previous input was a gesture input. - if (wasInGesture) { - return downDuration > MIN_GESTURE_DURATION_WHILE_IN_GESTURE - && mLength > mMinGestureLengthWhileInGesture; - } + public boolean isStartOfAGesture() { + final int size = mEventTimes.getLength(); + final int downDuration = (size > 0) ? mEventTimes.get(size - 1) : 0; return downDuration > MIN_GESTURE_DURATION && mLength > mMinGestureLength; } @@ -154,19 +144,21 @@ public class GestureStroke { mLastIncrementalBatchSize = size; } - private static float getDistance(final int p1x, final int p1y, - final int p2x, final int p2y) { - final float dx = p1x - p2x; - final float dy = p1y - p2y; - // TODO: Optimize out this {@link FloatMath#sqrt(float)} call. - return FloatMath.sqrt(dx * dx + dy * dy); + private static float getDistance(final int x1, final int y1, final int x2, final int y2) { + final float dx = x1 - x2; + final float dy = y1 - y2; + // Note that, in recent versions of Android, FloatMath is actually slower than + // java.lang.Math due to the way the JIT optimizes java.lang.Math. + return (float)Math.sqrt(dx * dx + dy * dy); } - private static float getAngle(final int p1x, final int p1y, final int p2x, final int p2y) { - final int dx = p1x - p2x; - final int dy = p1y - p2y; + private static float getAngle(final int x1, final int y1, final int x2, final int y2) { + final int dx = x1 - x2; + final int dy = y1 - y2; if (dx == 0 && dy == 0) return 0; - return (float)Math.atan2(dy, dx); + // Would it be faster to call atan2f() directly via JNI? Not sure about what the JIT + // does with Math.atan2(). + return (float)Math.atan2((double)dy, (double)dx); } private static float getAngleDiff(final float a1, final float a2) { @@ -177,12 +169,12 @@ public class GestureStroke { return diff; } - public void drawGestureTrail(Canvas canvas, Paint paint, int lastX, int lastY) { + public void drawGestureTrail(final Canvas canvas, final Paint paint) { // TODO: These paint parameter interpolation should be tunable, possibly introduce an object // that implements an interface such as Paint getPaint(int step, int strokePoints) final int size = mXCoordinates.getLength(); - int[] xCoords = mXCoordinates.getPrimitiveArray(); - int[] yCoords = mYCoordinates.getPrimitiveArray(); + final int[] xCoords = mXCoordinates.getPrimitiveArray(); + final int[] yCoords = mYCoordinates.getPrimitiveArray(); int alpha = Constants.Color.ALPHA_OPAQUE; for (int i = size - 1; i > 0 && alpha > 0; i--) { paint.setAlpha(alpha); @@ -190,9 +182,6 @@ public class GestureStroke { alpha -= DRAWING_GESTURE_FADE_RATE; } canvas.drawLine(xCoords[i - 1], yCoords[i - 1], xCoords[i], yCoords[i], paint); - if (i == size - 1) { - canvas.drawLine(lastX, lastY, xCoords[i], yCoords[i], paint); - } } } } diff --git a/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java index bd1648014..1c7ceaf92 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java +++ b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java @@ -18,85 +18,146 @@ package com.android.inputmethod.keyboard.internal; import android.util.Log; -import java.util.Iterator; -import java.util.LinkedList; +import java.util.ArrayList; public class PointerTrackerQueue { private static final String TAG = PointerTrackerQueue.class.getSimpleName(); private static final boolean DEBUG = false; - public interface ElementActions { + public interface Element { public boolean isModifier(); public boolean isInSlidingKeyInput(); public void onPhantomUpEvent(long eventTime); } - // TODO: Use ring buffer instead of {@link LinkedList}. - private final LinkedList<ElementActions> mQueue = new LinkedList<ElementActions>(); + private static final int INITIAL_CAPACITY = 10; + private final ArrayList<Element> mExpandableArrayOfActivePointers = + new ArrayList<Element>(INITIAL_CAPACITY); + private int mArraySize = 0; - public int size() { - return mQueue.size(); + public synchronized int size() { + return mArraySize; } - public synchronized void add(ElementActions tracker) { - mQueue.add(tracker); + public synchronized void add(final Element pointer) { + final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; + final int arraySize = mArraySize; + if (arraySize < expandableArray.size()) { + expandableArray.set(arraySize, pointer); + } else { + expandableArray.add(pointer); + } + mArraySize = arraySize + 1; } - public synchronized void remove(ElementActions tracker) { - mQueue.remove(tracker); + public synchronized void remove(final Element pointer) { + final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; + final int arraySize = mArraySize; + int newSize = 0; + for (int index = 0; index < arraySize; index++) { + final Element element = expandableArray.get(index); + if (element == pointer) { + if (newSize != index) { + Log.w(TAG, "Found duplicated element in remove: " + pointer); + } + continue; // Remove this element from the expandableArray. + } + if (newSize != index) { + // Shift this element toward the beginning of the expandableArray. + expandableArray.set(newSize, element); + } + newSize++; + } + mArraySize = newSize; } - public synchronized void releaseAllPointersOlderThan(ElementActions tracker, - long eventTime) { + public synchronized void releaseAllPointersOlderThan(final Element pointer, + final long eventTime) { if (DEBUG) { - Log.d(TAG, "releaseAllPoniterOlderThan: " + tracker + " " + this); + Log.d(TAG, "releaseAllPoniterOlderThan: " + pointer + " " + this); } - if (!mQueue.contains(tracker)) { - return; + final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; + final int arraySize = mArraySize; + int newSize, index; + for (newSize = index = 0; index < arraySize; index++) { + final Element element = expandableArray.get(index); + if (element == pointer) { + break; // Stop releasing elements. + } + if (!element.isModifier()) { + element.onPhantomUpEvent(eventTime); + continue; // Remove this element from the expandableArray. + } + if (newSize != index) { + // Shift this element toward the beginning of the expandableArray. + expandableArray.set(newSize, element); + } + newSize++; } - final Iterator<ElementActions> it = mQueue.iterator(); - while (it.hasNext()) { - final ElementActions t = it.next(); - if (t == tracker) { - break; + // Shift rest of the expandableArray. + int count = 0; + for (; index < arraySize; index++) { + final Element element = expandableArray.get(index); + if (element == pointer) { + if (count > 0) { + Log.w(TAG, "Found duplicated element in releaseAllPointersOlderThan: " + + pointer); + } + count++; } - if (!t.isModifier()) { - t.onPhantomUpEvent(eventTime); - it.remove(); + if (newSize != index) { + expandableArray.set(newSize, expandableArray.get(index)); + newSize++; } } + mArraySize = newSize; } - public void releaseAllPointers(long eventTime) { + public void releaseAllPointers(final long eventTime) { releaseAllPointersExcept(null, eventTime); } - public synchronized void releaseAllPointersExcept(ElementActions tracker, long eventTime) { + public synchronized void releaseAllPointersExcept(final Element pointer, + final long eventTime) { if (DEBUG) { - if (tracker == null) { + if (pointer == null) { Log.d(TAG, "releaseAllPoniters: " + this); } else { - Log.d(TAG, "releaseAllPoniterExcept: " + tracker + " " + this); + Log.d(TAG, "releaseAllPoniterExcept: " + pointer + " " + this); } } - final Iterator<ElementActions> it = mQueue.iterator(); - while (it.hasNext()) { - final ElementActions t = it.next(); - if (t != tracker) { - t.onPhantomUpEvent(eventTime); - it.remove(); + final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; + final int arraySize = mArraySize; + int newSize = 0, count = 0; + for (int index = 0; index < arraySize; index++) { + final Element element = expandableArray.get(index); + if (element == pointer) { + if (count > 0) { + Log.w(TAG, "Found duplicated element in releaseAllPointersExcept: " + pointer); + } + count++; + } else { + element.onPhantomUpEvent(eventTime); + continue; // Remove this element from the expandableArray. + } + if (newSize != index) { + // Shift this element toward the beginning of the expandableArray. + expandableArray.set(newSize, element); } + newSize++; } + mArraySize = newSize; } - public synchronized boolean hasModifierKeyOlderThan(ElementActions tracker) { - final Iterator<ElementActions> it = mQueue.iterator(); - while (it.hasNext()) { - final ElementActions t = it.next(); - if (t == tracker) { - break; + public synchronized boolean hasModifierKeyOlderThan(final Element pointer) { + final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; + final int arraySize = mArraySize; + for (int index = 0; index < arraySize; index++) { + final Element element = expandableArray.get(index); + if (element == pointer) { + return false; // Stop searching modifier key. } - if (t.isModifier()) { + if (element.isModifier()) { return true; } } @@ -104,8 +165,11 @@ public class PointerTrackerQueue { } public synchronized boolean isAnyInSlidingKeyInput() { - for (final ElementActions tracker : mQueue) { - if (tracker.isInSlidingKeyInput()) { + final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; + final int arraySize = mArraySize; + for (int index = 0; index < arraySize; index++) { + final Element element = expandableArray.get(index); + if (element.isInSlidingKeyInput()) { return true; } } @@ -113,12 +177,15 @@ public class PointerTrackerQueue { } @Override - public String toString() { + public synchronized String toString() { final StringBuilder sb = new StringBuilder(); - for (final ElementActions tracker : mQueue) { + final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; + final int arraySize = mArraySize; + for (int index = 0; index < arraySize; index++) { + final Element element = expandableArray.get(index); if (sb.length() > 0) sb.append(" "); - sb.append(tracker.toString()); + sb.append(element.toString()); } return "[" + sb.toString() + "]"; } diff --git a/java/src/com/android/inputmethod/latin/AutoCorrection.java b/java/src/com/android/inputmethod/latin/AutoCorrection.java index a66337404..048166807 100644 --- a/java/src/com/android/inputmethod/latin/AutoCorrection.java +++ b/java/src/com/android/inputmethod/latin/AutoCorrection.java @@ -75,17 +75,10 @@ public class AutoCorrection { return maxFreq; } - // Returns true if this is a whitelist entry, or it isn't in any dictionary. - public static boolean isWhitelistedOrNotAWord( + // Returns true if this isn't in any dictionary. + public static boolean isNotAWord( final ConcurrentHashMap<String, Dictionary> dictionaries, final CharSequence word, final boolean ignoreCase) { - final WhitelistDictionary whitelistDictionary = - (WhitelistDictionary)dictionaries.get(Dictionary.TYPE_WHITELIST); - // If "word" is in the whitelist dictionary, it should not be auto corrected. - if (whitelistDictionary != null - && whitelistDictionary.shouldForciblyAutoCorrectFrom(word)) { - return true; - } return !isValidWord(dictionaries, word, ignoreCase); } diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 534cffb2d..f0f5cd320 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -51,13 +51,15 @@ public class BinaryDictionary extends Dictionary { private static final int TYPED_LETTER_MULTIPLIER = 2; private long mNativeDict; - private final int[] mInputCodes = new int[MAX_WORD_LENGTH]; + private final int[] mInputCodePoints = new int[MAX_WORD_LENGTH]; + // TODO: The below should be int[] mOutputCodePoints private final char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_RESULTS]; private final int[] mSpaceIndices = new int[MAX_SPACES]; private final int[] mOutputScores = new int[MAX_RESULTS]; private final int[] mOutputTypes = new int[MAX_RESULTS]; private final boolean mUseFullEditDistance; + private final DicTraverseSession mDicTraverseSession; /** * Constructor for the binary dictionary. This is supposed to be called from the @@ -76,6 +78,8 @@ public class BinaryDictionary extends Dictionary { super(dictType); mUseFullEditDistance = useFullEditDistance; loadDictionary(filename, offset, length); + mDicTraverseSession = new DicTraverseSession(locale); + mDicTraverseSession.initSession(mNativeDict); } static { @@ -86,18 +90,17 @@ public class BinaryDictionary extends Dictionary { int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength, int maxWords, int maxPredictions); private native void closeNative(long dict); - private native int getFrequencyNative(long dict, int[] word, int wordLength); + private native int getFrequencyNative(long dict, int[] word); private native boolean isValidBigramNative(long dict, int[] word1, int[] word2); - private native int getSuggestionsNative(long dict, long proximityInfo, int[] xCoordinates, - int[] yCoordinates, int[] times, int[] pointerIds, int[] inputCodes, int codesSize, - int commitPoint, boolean isGesture, + private native int getSuggestionsNative(long dict, long proximityInfo, long traverseSession, + int[] xCoordinates, int[] yCoordinates, int[] times, int[] pointerIds, + int[] inputCodePoints, int codesSize, int commitPoint, boolean isGesture, int[] prevWordCodePointArray, boolean useFullEditDistance, char[] outputChars, int[] outputScores, int[] outputIndices, int[] outputTypes); - private static native float calcNormalizedScoreNative( - char[] before, int beforeLength, char[] after, int afterLength, int score); - private static native int editDistanceNative( - char[] before, int beforeLength, char[] after, int afterLength); + private static native float calcNormalizedScoreNative(char[] before, char[] after, int score); + private static native int editDistanceNative(char[] before, char[] after); + // TODO: Move native dict into session private final void loadDictionary(String path, long startOffset, long length) { mNativeDict = openNative(path, startOffset, length, TYPED_LETTER_MULTIPLIER, FULL_WORD_SCORE_MULTIPLIER, MAX_WORD_LENGTH, MAX_WORDS, MAX_PREDICTIONS); @@ -107,9 +110,7 @@ public class BinaryDictionary extends Dictionary { public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, final CharSequence prevWord, final ProximityInfo proximityInfo) { if (!isValidDictionary()) return null; - Arrays.fill(mInputCodes, WordComposer.NOT_A_CODE); - Arrays.fill(mOutputChars, (char) 0); - Arrays.fill(mOutputScores, 0); + Arrays.fill(mInputCodePoints, WordComposer.NOT_A_CODE); // TODO: toLowerCase in the native code final int[] prevWordCodePointArray = (null == prevWord) ? null : StringUtils.toCodePointArray(prevWord.toString()); @@ -119,7 +120,7 @@ public class BinaryDictionary extends Dictionary { if (composerSize <= 1 || !isGesture) { if (composerSize > MAX_WORD_LENGTH - 1) return null; for (int i = 0; i < composerSize; i++) { - mInputCodes[i] = composer.getCodeAt(i); + mInputCodePoints[i] = composer.getCodeAt(i); } } @@ -127,9 +128,9 @@ public class BinaryDictionary extends Dictionary { final int codesSize = isGesture ? ips.getPointerSize() : composerSize; // proximityInfo and/or prevWordForBigrams may not be null. final int tmpCount = getSuggestionsNative(mNativeDict, - proximityInfo.getNativeProximityInfo(), ips.getXCoordinates(), - ips.getYCoordinates(), ips.getTimes(), ips.getPointerIds(), - mInputCodes, codesSize, 0 /* commitPoint */, isGesture, prevWordCodePointArray, + proximityInfo.getNativeProximityInfo(), mDicTraverseSession.getSession(), + ips.getXCoordinates(), ips.getYCoordinates(), ips.getTimes(), ips.getPointerIds(), + mInputCodePoints, codesSize, 0 /* commitPoint */, isGesture, prevWordCodePointArray, mUseFullEditDistance, mOutputChars, mOutputScores, mSpaceIndices, mOutputTypes); final int count = Math.min(tmpCount, MAX_PREDICTIONS); @@ -142,9 +143,10 @@ public class BinaryDictionary extends Dictionary { ++len; } if (len > 0) { + final int score = SuggestedWordInfo.KIND_WHITELIST == mOutputTypes[j] + ? SuggestedWordInfo.MAX_SCORE : mOutputScores[j]; suggestions.add(new SuggestedWordInfo( - new String(mOutputChars, start, len), - mOutputScores[j], SuggestedWordInfo.KIND_CORRECTION, mDictType)); + new String(mOutputChars, start, len), score, mOutputTypes[j], mDictType)); } } return suggestions; @@ -155,13 +157,11 @@ public class BinaryDictionary extends Dictionary { } public static float calcNormalizedScore(String before, String after, int score) { - return calcNormalizedScoreNative(before.toCharArray(), before.length(), - after.toCharArray(), after.length(), score); + return calcNormalizedScoreNative(before.toCharArray(), after.toCharArray(), score); } public static int editDistance(String before, String after) { - return editDistanceNative( - before.toCharArray(), before.length(), after.toCharArray(), after.length()); + return editDistanceNative(before.toCharArray(), after.toCharArray()); } @Override @@ -172,8 +172,8 @@ public class BinaryDictionary extends Dictionary { @Override public int getFrequency(CharSequence word) { if (word == null) return -1; - int[] chars = StringUtils.toCodePointArray(word.toString()); - return getFrequencyNative(mNativeDict, chars, chars.length); + int[] codePoints = StringUtils.toCodePointArray(word.toString()); + return getFrequencyNative(mNativeDict, codePoints); } // TODO: Add a batch process version (isValidBigramMultiple?) to avoid excessive numbers of jni @@ -187,6 +187,7 @@ public class BinaryDictionary extends Dictionary { @Override public synchronized void close() { + mDicTraverseSession.close(); closeInternal(); } diff --git a/java/src/com/android/inputmethod/latin/DicTraverseSession.java b/java/src/com/android/inputmethod/latin/DicTraverseSession.java new file mode 100644 index 000000000..c76815363 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/DicTraverseSession.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2012, 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.latin; + +import java.util.Locale; + +public class DicTraverseSession { + static { + JniUtils.loadNativeLibrary(); + } + private native long setDicTraverseSessionNative(String locale); + private native void initDicTraverseSessionNative(long nativeDicTraverseSession, + long dictionary, int[] previousWord, int previousWordLength); + private native void releaseDicTraverseSessionNative(long nativeDicTraverseSession); + + private long mNativeDicTraverseSession; + + public DicTraverseSession(Locale locale) { + mNativeDicTraverseSession = createNativeDicTraverseSession( + locale != null ? locale.toString() : ""); + } + + public long getSession() { + return mNativeDicTraverseSession; + } + + public void initSession(long dictionary) { + initSession(dictionary, null, 0); + } + + public void initSession(long dictionary, int[] previousWord, int previousWordLength) { + initDicTraverseSessionNative( + mNativeDicTraverseSession, dictionary, previousWord, previousWordLength); + } + + private final long createNativeDicTraverseSession(String locale) { + return setDicTraverseSessionNative(locale); + } + + private void closeInternal() { + if (mNativeDicTraverseSession != 0) { + releaseDicTraverseSessionNative(mNativeDicTraverseSession); + mNativeDicTraverseSession = 0; + } + } + + public void close() { + closeInternal(); + } + + @Override + protected void finalize() throws Throwable { + try { + closeInternal(); + } finally { + super.finalize(); + } + } +} diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 25cddb22d..4a7d0403b 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -433,10 +433,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen resetContactsDictionary(null == mSuggest ? null : mSuggest.getContactsDictionary()); } + // Note that this method is called from a non-UI thread. @Override public void onUpdateMainDictionaryAvailability(boolean isMainDictionaryAvailable) { mIsMainDictionaryAvailable = isMainDictionaryAvailable; - updateKeyboardViewGestureHandlingModeByMainDictionaryAvailability(); + final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView(); + if (mainKeyboardView != null) { + mainKeyboardView.setMainDictionaryAvailability(isMainDictionaryAvailable); + } } private void initSuggest() { @@ -517,7 +521,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen /* package private */ void resetSuggestMainDict() { final Locale subtypeLocale = mSubtypeSwitcher.getCurrentSubtypeLocale(); - mSuggest.resetMainDict(this, subtypeLocale); + mSuggest.resetMainDict(this, subtypeLocale, this /* SuggestInitializationListener */); mIsMainDictionaryAvailable = DictionaryFactory.isDictionaryAvailable(this, subtypeLocale); } @@ -701,7 +705,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } switcher.loadKeyboard(editorInfo, mCurrentSettings); - updateKeyboardViewGestureHandlingModeByMainDictionaryAvailability(); } setSuggestionStripShownInternal( isSuggestionsStripVisible(), /* needsInputViewShown */ false); @@ -719,8 +722,12 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mHandler.cancelUpdateSuggestionStrip(); mHandler.cancelDoubleSpacesTimer(); + mainKeyboardView.setMainDictionaryAvailability(mIsMainDictionaryAvailable); mainKeyboardView.setKeyPreviewPopupEnabled(mCurrentSettings.mKeyPreviewPopupOn, mCurrentSettings.mKeyPreviewPopupDismissDelay); + mainKeyboardView.setGestureHandlingEnabledByUser(mCurrentSettings.mGestureInputEnabled); + mainKeyboardView.setGesturePreviewMode(mCurrentSettings.mGesturePreviewTrailEnabled, + mCurrentSettings.mGestureFloatingPreviewTextEnabled); if (TRACE) Debug.startMethodTracing("/data/trace/latinime"); } @@ -2103,7 +2110,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (mKeyboardSwitcher.getMainKeyboardView() != null) { // Reload keyboard because the current language has been changed. mKeyboardSwitcher.loadKeyboard(getCurrentInputEditorInfo(), mCurrentSettings); - updateKeyboardViewGestureHandlingModeByMainDictionaryAvailability(); } // Since we just changed languages, we should re-evaluate suggestions with whatever word // we are currently composing. If we are not composing anything, we may want to display @@ -2111,17 +2117,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mHandler.postUpdateSuggestionStrip(); } - private void updateKeyboardViewGestureHandlingModeByMainDictionaryAvailability() { - final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView(); - if (mainKeyboardView != null) { - final boolean shouldHandleGesture = mCurrentSettings.mGestureInputEnabled - && mIsMainDictionaryAvailable; - mainKeyboardView.setGestureHandlingMode(shouldHandleGesture, - mCurrentSettings.mGesturePreviewTrailEnabled, - mCurrentSettings.mGestureFloatingPreviewTextEnabled); - } - } - // TODO: Remove this method from {@link LatinIME} and move {@link FeedbackManager} to // {@link KeyboardSwitcher}. Called from KeyboardSwitcher public void hapticAndAudioFeedback(final int primaryCode) { diff --git a/java/src/com/android/inputmethod/latin/NativeUtils.java b/java/src/com/android/inputmethod/latin/NativeUtils.java deleted file mode 100644 index 9cc2bc02e..000000000 --- a/java/src/com/android/inputmethod/latin/NativeUtils.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) 2012 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.latin; - -public class NativeUtils { - static { - JniUtils.loadNativeLibrary(); - } - - private NativeUtils() { - // This utility class is not publicly instantiable. - } - - /** - * This method just calls up libm's powf() directly. - */ - public static native float powf(float x, float y); -} diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 5e2a04124..8a2341d5e 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -60,13 +60,11 @@ public class Suggest { // Locale used for upper- and title-casing words private final Locale mLocale; - private final SuggestInitializationListener mListener; public Suggest(final Context context, final Locale locale, final SuggestInitializationListener listener) { - initAsynchronously(context, locale); + initAsynchronously(context, locale, listener); mLocale = locale; - mListener = listener; } /* package for test */ Suggest(final Context context, final File dictionary, @@ -74,7 +72,6 @@ public class Suggest { final Dictionary mainDict = DictionaryFactory.createDictionaryForTest(context, dictionary, startOffset, length /* useFullEditDistance */, false, locale); mLocale = locale; - mListener = null; mMainDictionary = mainDict; addOrReplaceDictionary(mDictionaries, Dictionary.TYPE_MAIN, mainDict); initWhitelistAndAutocorrectAndPool(context, locale); @@ -85,8 +82,9 @@ public class Suggest { addOrReplaceDictionary(mDictionaries, Dictionary.TYPE_WHITELIST, mWhiteListDictionary); } - private void initAsynchronously(final Context context, final Locale locale) { - resetMainDict(context, locale); + private void initAsynchronously(final Context context, final Locale locale, + final SuggestInitializationListener listener) { + resetMainDict(context, locale, listener); // TODO: read the whitelist and init the pool asynchronously too. // initPool should be done asynchronously now that the pool is thread-safe. @@ -104,10 +102,11 @@ public class Suggest { } } - public void resetMainDict(final Context context, final Locale locale) { + public void resetMainDict(final Context context, final Locale locale, + final SuggestInitializationListener listener) { mMainDictionary = null; - if (mListener != null) { - mListener.onUpdateMainDictionaryAvailability(hasMainDictionary()); + if (listener != null) { + listener.onUpdateMainDictionaryAvailability(hasMainDictionary()); } new Thread("InitializeBinaryDictionary") { @Override @@ -116,8 +115,8 @@ public class Suggest { DictionaryFactory.createMainDictionaryFromManager(context, locale); addOrReplaceDictionary(mDictionaries, Dictionary.TYPE_MAIN, newMainDict); mMainDictionary = newMainDict; - if (mListener != null) { - mListener.onUpdateMainDictionaryAvailability(hasMainDictionary()); + if (listener != null) { + listener.onUpdateMainDictionaryAvailability(hasMainDictionary()); } } }.start(); @@ -209,23 +208,34 @@ public class Suggest { wordComposerForLookup, prevWordForBigram, proximityInfo)); } - // TODO: Change this scheme - a boolean is not enough. A whitelisted word may be "valid" - // but still autocorrected from - in the case the whitelist only capitalizes the word. - // The whitelist should be case-insensitive, so it's not possible to be consistent with - // a boolean flag. Right now this is handled with a slight hack in - // WhitelistDictionary#shouldForciblyAutoCorrectFrom. - final boolean allowsToBeAutoCorrected = AutoCorrection.isWhitelistedOrNotAWord( - mDictionaries, consideredWord, wordComposer.isFirstCharCapitalized()); - - final CharSequence whitelistedWord = + final CharSequence whitelistedWordFromWhitelistDictionary = mWhiteListDictionary.getWhitelistedWord(consideredWord); - if (whitelistedWord != null) { + if (whitelistedWordFromWhitelistDictionary != null) { // MAX_SCORE ensures this will be considered strong enough to be auto-corrected - suggestionsSet.add(new SuggestedWordInfo(whitelistedWord, + suggestionsSet.add(new SuggestedWordInfo(whitelistedWordFromWhitelistDictionary, SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_WHITELIST, Dictionary.TYPE_WHITELIST)); } + final CharSequence whitelistedWord; + if (suggestionsSet.isEmpty()) { + whitelistedWord = null; + } else if (SuggestedWordInfo.KIND_WHITELIST != suggestionsSet.first().mKind) { + whitelistedWord = null; + } else { + whitelistedWord = suggestionsSet.first().mWord; + } + + // TODO: Change this scheme - a boolean is not enough. A whitelisted word may be "valid" + // but still autocorrected from - in the case the whitelist only capitalizes the word. + // The whitelist should be case-insensitive, so it's not possible to be consistent with + // a boolean flag. Right now this is handled with a slight hack in + // WhitelistDictionary#shouldForciblyAutoCorrectFrom. + final boolean allowsToBeAutoCorrected = (null != whitelistedWord + && !whitelistedWord.equals(consideredWord)) + || AutoCorrection.isNotAWord(mDictionaries, consideredWord, + wordComposer.isFirstCharCapitalized()); + final boolean hasAutoCorrection; // TODO: using isCorrectionEnabled here is not very good. It's probably useless, because // any attempt to do auto-correction is already shielded with a test for this flag; at the diff --git a/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java b/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java index 1de95d7b8..5a2fdf48e 100644 --- a/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java +++ b/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java @@ -212,7 +212,7 @@ public class UserHistoryForgettingCurveUtils { for (int j = 0; j < ELAPSED_TIME_MAX; ++j) { final float elapsedHours = j * ELAPSED_TIME_INTERVAL_HOURS; final float freq = initialFreq - * NativeUtils.powf(initialFreq, elapsedHours / HALF_LIFE_HOURS); + * (float)Math.pow(initialFreq, elapsedHours / HALF_LIFE_HOURS); final int intFreq = Math.min(FC_FREQ_MAX, Math.max(0, (int)freq)); SCORE_TABLE[i][j] = intFreq; } diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java index 0171dc06d..06f5db749 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java @@ -193,7 +193,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session { if (shouldFilterOut(inText, mScript)) { DictAndProximity dictInfo = null; try { - dictInfo = mDictionaryPool.takeOrGetNull(); + dictInfo = mDictionaryPool.pollWithDefaultTimeout(); if (null == dictInfo) { return AndroidSpellCheckerService.getNotInDictEmptySuggestions(); } @@ -236,7 +236,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session { boolean isInDict = true; DictAndProximity dictInfo = null; try { - dictInfo = mDictionaryPool.takeOrGetNull(); + dictInfo = mDictionaryPool.pollWithDefaultTimeout(); if (null == dictInfo) { return AndroidSpellCheckerService.getNotInDictEmptySuggestions(); } diff --git a/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java b/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java index 8fc632ee7..83f82faeb 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java @@ -16,14 +16,24 @@ package com.android.inputmethod.latin.spellcheck; +import android.util.Log; + import java.util.Locale; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; /** * A blocking queue that creates dictionaries up to a certain limit as necessary. + * As a deadlock-detecting device, if waiting for more than TIMEOUT = 3 seconds, we + * will clear the queue and generate its contents again. This is transparent for + * the client code, but may help with sloppy clients. */ @SuppressWarnings("serial") public class DictionaryPool extends LinkedBlockingQueue<DictAndProximity> { + private final static String TAG = DictionaryPool.class.getSimpleName(); + // How many seconds we wait for a dictionary to become available. Past this delay, we give up in + // fear some bug caused a deadlock, and reset the whole pool. + private final static int TIMEOUT = 3; private final AndroidSpellCheckerService mService; private final int mMaxSize; private final Locale mLocale; @@ -41,13 +51,23 @@ public class DictionaryPool extends LinkedBlockingQueue<DictAndProximity> { } @Override - public DictAndProximity take() throws InterruptedException { + public DictAndProximity poll(final long timeout, final TimeUnit unit) + throws InterruptedException { final DictAndProximity dict = poll(); if (null != dict) return dict; synchronized(this) { if (mSize >= mMaxSize) { - // Our pool is already full. Wait until some dictionary is ready. - return super.take(); + // Our pool is already full. Wait until some dictionary is ready, or TIMEOUT + // expires to avoid a deadlock. + final DictAndProximity result = super.poll(timeout, unit); + if (null == result) { + Log.e(TAG, "Deadlock detected ! Resetting dictionary pool"); + clear(); + mSize = 1; + return mService.createDictAndProximity(mLocale); + } else { + return result; + } } else { ++mSize; return mService.createDictAndProximity(mLocale); @@ -56,9 +76,9 @@ public class DictionaryPool extends LinkedBlockingQueue<DictAndProximity> { } // Convenience method - public DictAndProximity takeOrGetNull() { + public DictAndProximity pollWithDefaultTimeout() { try { - return take(); + return poll(TIMEOUT, TimeUnit.SECONDS); } catch (InterruptedException e) { return null; } diff --git a/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerProximityInfo.java b/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerProximityInfo.java index 0103e8423..bd92d883b 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerProximityInfo.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerProximityInfo.java @@ -111,6 +111,7 @@ public class SpellCheckerProximityInfo { NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, + NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, }; static { buildProximityIndices(PROXIMITY, INDICES); |