diff options
Diffstat (limited to 'java/src')
8 files changed, 413 insertions, 99 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureFloatingPreviewText.java b/java/src/com/android/inputmethod/keyboard/internal/GestureFloatingPreviewText.java index 28655a930..ab810a580 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/GestureFloatingPreviewText.java +++ b/java/src/com/android/inputmethod/keyboard/internal/GestureFloatingPreviewText.java @@ -28,7 +28,6 @@ import android.view.View; import com.android.inputmethod.keyboard.PointerTracker; import com.android.inputmethod.latin.CoordinateUtils; import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.ResizableIntArray; import com.android.inputmethod.latin.SuggestedWords; /** @@ -44,16 +43,17 @@ import com.android.inputmethod.latin.SuggestedWords; * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewRoundRadius */ public class GestureFloatingPreviewText extends AbstractDrawingPreview { - private static final class GesturePreviewTextParams { - public final int mGesturePreviewTextSize; - public final int mGesturePreviewTextColor; + protected static final class GesturePreviewTextParams { public final int mGesturePreviewTextOffset; public final int mGesturePreviewTextHeight; - public final int mGesturePreviewColor; public final float mGesturePreviewHorizontalPadding; public final float mGesturePreviewVerticalPadding; public final float mGesturePreviewRoundRadius; - public final Paint mTextPaint; + + private final int mGesturePreviewTextSize; + private final int mGesturePreviewTextColor; + private final int mGesturePreviewColor; + private final Paint mPaint = new Paint(); private static final char[] TEXT_HEIGHT_REFERENCE_CHAR = { 'M' }; @@ -73,37 +73,36 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview { mGesturePreviewRoundRadius = mainKeyboardViewAttr.getDimension( R.styleable.MainKeyboardView_gestureFloatingPreviewRoundRadius, 0.0f); - final Paint textPaint = new Paint(); - textPaint.setAntiAlias(true); - textPaint.setTextAlign(Align.CENTER); - textPaint.setTextSize(mGesturePreviewTextSize); - mTextPaint = textPaint; + final Paint textPaint = getTextPaint(); final Rect textRect = new Rect(); textPaint.getTextBounds(TEXT_HEIGHT_REFERENCE_CHAR, 0, 1, textRect); mGesturePreviewTextHeight = textRect.height(); } - } - protected final GesturePreviewTextParams mParams; - protected int mPreviewWordNum; - protected final RectF mGesturePreviewRectangle = new RectF(); - protected int mHighlightedWordIndex; + public Paint getTextPaint() { + mPaint.setAntiAlias(true); + mPaint.setTextAlign(Align.CENTER); + mPaint.setTextSize(mGesturePreviewTextSize); + mPaint.setColor(mGesturePreviewTextColor); + return mPaint; + } - private static final int PREVIEW_TEXT_ARRAY_CAPACITY = 10; - // These variables store the positions of preview words. In multi-preview mode, the gesture - // floating preview at most shows PREVIEW_TEXT_ARRAY_CAPACITY words. - protected final ResizableIntArray mPreviewTextXArray = new ResizableIntArray( - PREVIEW_TEXT_ARRAY_CAPACITY); - protected final ResizableIntArray mPreviewTextYArray = new ResizableIntArray( - PREVIEW_TEXT_ARRAY_CAPACITY); + public Paint getBackgroundPaint() { + mPaint.setColor(mGesturePreviewColor); + return mPaint; + } + } - protected SuggestedWords mSuggestedWords = SuggestedWords.EMPTY; - public final int[] mLastPointerCoords = CoordinateUtils.newInstance(); + private final GesturePreviewTextParams mParams; + private final RectF mGesturePreviewRectangle = new RectF(); + private int mPreviewTextX; + private int mPreviewTextY; + private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY; + private final int[] mLastPointerCoords = CoordinateUtils.newInstance(); public GestureFloatingPreviewText(final View drawingView, final TypedArray typedArray) { super(drawingView); mParams = new GesturePreviewTextParams(typedArray); - mHighlightedWordIndex = 0; } public void setSuggetedWords(final SuggestedWords suggestedWords) { @@ -114,13 +113,6 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview { updatePreviewPosition(); } - protected void drawText(final Canvas canvas, final String text, final float textX, - final float textY, final int color) { - final Paint paint = mParams.mTextPaint; - paint.setColor(color); - canvas.drawText(text, textX, textY, paint); - } - @Override public void setPreviewPosition(final PointerTracker tracker) { final boolean needsToUpdateLastPointer = @@ -142,14 +134,11 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview { || TextUtils.isEmpty(mSuggestedWords.getWord(0))) { return; } - final Paint paint = mParams.mTextPaint; - paint.setColor(mParams.mGesturePreviewColor); final float round = mParams.mGesturePreviewRoundRadius; - canvas.drawRoundRect(mGesturePreviewRectangle, round, round, paint); + canvas.drawRoundRect( + mGesturePreviewRectangle, round, round, mParams.getBackgroundPaint()); final String text = mSuggestedWords.getWord(0); - final int textX = mPreviewTextXArray.get(0); - final int textY = mPreviewTextYArray.get(0); - drawText(canvas, text, textX, textY, mParams.mGesturePreviewTextColor); + canvas.drawText(text, mPreviewTextX, mPreviewTextY, mParams.getTextPaint()); } /** @@ -162,11 +151,10 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview { } final String text = mSuggestedWords.getWord(0); - final Paint paint = mParams.mTextPaint; final RectF rectangle = mGesturePreviewRectangle; final int textHeight = mParams.mGesturePreviewTextHeight; - final float textWidth = paint.measureText(text); + final float textWidth = mParams.getTextPaint().measureText(text); final float hPad = mParams.mGesturePreviewHorizontalPadding; final float vPad = mParams.mGesturePreviewVerticalPadding; final float rectWidth = textWidth + hPad * 2.0f; @@ -180,10 +168,8 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview { - mParams.mGesturePreviewTextOffset - rectHeight; rectangle.set(rectX, rectY, rectX + rectWidth, rectY + rectHeight); - final int textX = (int)(rectX + hPad + textWidth / 2.0f); - final int textY = (int)(rectY + vPad) + textHeight; - mPreviewTextXArray.add(0, textX); - mPreviewTextYArray.add(0, textY); + mPreviewTextX = (int)(rectX + hPad + textWidth / 2.0f); + mPreviewTextY = (int)(rectY + vPad) + textHeight; // TODO: Should narrow the invalidate region. getDrawingView().invalidate(); } diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java index 443ffa2e9..ef440c5df 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java @@ -56,11 +56,11 @@ public final class BinaryDictionaryFileDumper { private static final String DICTIONARY_PROJECTION[] = { "id" }; - public static final String QUERY_PARAMETER_MAY_PROMPT_USER = "mayPrompt"; - public static final String QUERY_PARAMETER_TRUE = "true"; - public static final String QUERY_PARAMETER_DELETE_RESULT = "result"; - public static final String QUERY_PARAMETER_SUCCESS = "success"; - public static final String QUERY_PARAMETER_FAILURE = "failure"; + private static final String QUERY_PARAMETER_MAY_PROMPT_USER = "mayPrompt"; + private static final String QUERY_PARAMETER_TRUE = "true"; + private static final String QUERY_PARAMETER_DELETE_RESULT = "result"; + private static final String QUERY_PARAMETER_SUCCESS = "success"; + private static final String QUERY_PARAMETER_FAILURE = "failure"; // Prevents this class to be accidentally instantiated. private BinaryDictionaryFileDumper() { diff --git a/java/src/com/android/inputmethod/research/LogStatement.java b/java/src/com/android/inputmethod/research/LogStatement.java index 090c58e27..1d83e1a86 100644 --- a/java/src/com/android/inputmethod/research/LogStatement.java +++ b/java/src/com/android/inputmethod/research/LogStatement.java @@ -29,13 +29,12 @@ class LogStatement { "PointerTrackerCallListenerOnCodeInput"; public static final String KEY_CODE = "code"; public static final String VALUE_RESEARCH = "research"; - public static final String TYPE_LATIN_KEYBOARD_VIEW_ON_LONG_PRESS = - "LatinKeyboardViewOnLongPress"; + public static final String TYPE_MAIN_KEYBOARD_VIEW_ON_LONG_PRESS = + "MainKeyboardViewOnLongPress"; public static final String ACTION = "action"; public static final String VALUE_DOWN = "DOWN"; - public static final String TYPE_LATIN_KEYBOARD_VIEW_PROCESS_MOTION_EVENTS = - "LatinKeyboardViewProcessMotionEvents"; - public static final String KEY_LOGGING_RELATED = "loggingRelated"; + public static final String TYPE_MOTION_EVENT = "MotionEvent"; + public static final String KEY_IS_LOGGING_RELATED = "isLoggingRelated"; // Name specifying the LogStatement type. private final String mType; diff --git a/java/src/com/android/inputmethod/research/LogUnit.java b/java/src/com/android/inputmethod/research/LogUnit.java index 608fab3f1..2e732fc6c 100644 --- a/java/src/com/android/inputmethod/research/LogUnit.java +++ b/java/src/com/android/inputmethod/research/LogUnit.java @@ -453,13 +453,12 @@ import java.util.List; // Look for the long press that started the invocation of the research key code input. final int indexOfLastLongPressBeforeResearchKey = - findLastIndexBefore(LogStatement.TYPE_LATIN_KEYBOARD_VIEW_ON_LONG_PRESS, + findLastIndexBefore(LogStatement.TYPE_MAIN_KEYBOARD_VIEW_ON_LONG_PRESS, indexOfLastResearchKey); // Look for DOWN event preceding the long press final int indexOfLastDownEventBeforeLongPress = - findLastIndexContainingKeyValueBefore( - LogStatement.TYPE_LATIN_KEYBOARD_VIEW_PROCESS_MOTION_EVENTS, + findLastIndexContainingKeyValueBefore(LogStatement.TYPE_MOTION_EVENT, LogStatement.ACTION, LogStatement.VALUE_DOWN, indexOfLastLongPressBeforeResearchKey); @@ -471,8 +470,8 @@ import java.util.List; final LogStatement logStatement = mLogStatementList.get(index); final String type = logStatement.getType(); final Object[] values = mValuesList.get(index); - if (type.equals(LogStatement.TYPE_LATIN_KEYBOARD_VIEW_PROCESS_MOTION_EVENTS)) { - logStatement.setValue(LogStatement.KEY_LOGGING_RELATED, values, true); + if (type.equals(LogStatement.TYPE_MOTION_EVENT)) { + logStatement.setValue(LogStatement.KEY_IS_LOGGING_RELATED, values, true); } } return true; diff --git a/java/src/com/android/inputmethod/research/MotionEventReader.java b/java/src/com/android/inputmethod/research/MotionEventReader.java index 36e75be1c..e59adfa19 100644 --- a/java/src/com/android/inputmethod/research/MotionEventReader.java +++ b/java/src/com/android/inputmethod/research/MotionEventReader.java @@ -19,6 +19,8 @@ package com.android.inputmethod.research; import android.util.JsonReader; import android.util.Log; import android.view.MotionEvent; +import android.view.MotionEvent.PointerCoords; +import android.view.MotionEvent.PointerProperties; import com.android.inputmethod.latin.define.ProductionFlag; @@ -33,6 +35,14 @@ import java.util.ArrayList; public class MotionEventReader { private static final String TAG = MotionEventReader.class.getSimpleName(); private static final boolean DEBUG = false && ProductionFlag.IS_EXPERIMENTAL_DEBUG; + // Assumes that MotionEvent.ACTION_MASK does not have all bits set.` + private static final int UNINITIALIZED_ACTION = ~MotionEvent.ACTION_MASK; + // No legitimate int is negative + private static final int UNINITIALIZED_INT = -1; + // No legitimate long is negative + private static final long UNINITIALIZED_LONG = -1L; + // No legitimate float is negative + private static final float UNINITIALIZED_FLOAT = -1.0f; public ReplayData readMotionEventData(final File file) { final ReplayData replayData = new ReplayData(); @@ -55,19 +65,82 @@ public class MotionEventReader { static class ReplayData { final ArrayList<Integer> mActions = new ArrayList<Integer>(); - final ArrayList<Integer> mXCoords = new ArrayList<Integer>(); - final ArrayList<Integer> mYCoords = new ArrayList<Integer>(); + final ArrayList<PointerProperties[]> mPointerPropertiesArrays + = new ArrayList<PointerProperties[]>(); + final ArrayList<PointerCoords[]> mPointerCoordsArrays = new ArrayList<PointerCoords[]>(); final ArrayList<Long> mTimes = new ArrayList<Long>(); } - private void readLogStatement(final JsonReader jsonReader, final ReplayData replayData) - throws IOException { + /** + * Read motion data from a logStatement and store it in {@code replayData}. + * + * Two kinds of logStatements can be read. In the first variant, the MotionEvent data is + * represented as attributes at the top level like so: + * + * <pre> + * { + * "_ct": 1359590400000, + * "_ut": 4381933, + * "_ty": "MotionEvent", + * "action": "UP", + * "isLoggingRelated": false, + * "x": 100, + * "y": 200 + * } + * </pre> + * + * In the second variant, there is a separate attribute for the MotionEvent that includes + * historical data if present: + * + * <pre> + * { + * "_ct": 135959040000, + * "_ut": 4382702, + * "_ty": "MotionEvent", + * "action": "MOVE", + * "isLoggingRelated": false, + * "motionEvent": { + * "pointerIds": [ + * 0 + * ], + * "xyt": [ + * { + * "t": 4382551, + * "d": [ + * { + * "x": 141.25, + * "y": 151.8485107421875, + * "toma": 101.82337188720703, + * "tomi": 101.82337188720703, + * "o": 0.0 + * } + * ] + * }, + * { + * "t": 4382559, + * "d": [ + * { + * "x": 140.7266082763672, + * "y": 151.8485107421875, + * "toma": 101.82337188720703, + * "tomi": 101.82337188720703, + * "o": 0.0 + * } + * ] + * } + * ] + * } + * }, + * </pre> + */ + /* package for test */ void readLogStatement(final JsonReader jsonReader, + final ReplayData replayData) throws IOException { String logStatementType = null; - Integer actionType = null; - Integer x = null; - Integer y = null; - Long time = null; - boolean loggingRelated = false; + int actionType = UNINITIALIZED_ACTION; + int x = UNINITIALIZED_INT; + int y = UNINITIALIZED_INT; + long time = UNINITIALIZED_LONG; + boolean isLoggingRelated = false; jsonReader.beginObject(); while (jsonReader.hasNext()) { @@ -90,7 +163,18 @@ public class MotionEventReader { actionType = MotionEvent.ACTION_MOVE; } } else if (key.equals("loggingRelated")) { - loggingRelated = jsonReader.nextBoolean(); + isLoggingRelated = jsonReader.nextBoolean(); + } else if (logStatementType != null && logStatementType.equals("MotionEvent") + && key.equals("motionEvent")) { + if (actionType == UNINITIALIZED_ACTION) { + Log.e(TAG, "no actionType assigned in MotionEvent json"); + } + // Second variant of LogStatement. + if (isLoggingRelated) { + jsonReader.skipValue(); + } else { + readEmbeddedMotionEvent(jsonReader, replayData, actionType); + } } else { if (DEBUG) { Log.w(TAG, "Unknown JSON key in LogStatement: " + key); @@ -100,14 +184,149 @@ public class MotionEventReader { } jsonReader.endObject(); - if (logStatementType != null && time != null && x != null && y != null && actionType != null - && logStatementType.equals("MainKeyboardViewProcessMotionEvent") - && !loggingRelated) { - replayData.mActions.add(actionType); - replayData.mXCoords.add(x); - replayData.mYCoords.add(y); - replayData.mTimes.add(time); + if (logStatementType != null && time != UNINITIALIZED_LONG && x != UNINITIALIZED_INT + && y != UNINITIALIZED_INT && actionType != UNINITIALIZED_ACTION + && logStatementType.equals("MotionEvent") && !isLoggingRelated) { + // First variant of LogStatement. + final PointerProperties pointerProperties = new PointerProperties(); + pointerProperties.id = 0; + pointerProperties.toolType = MotionEvent.TOOL_TYPE_UNKNOWN; + final PointerProperties[] pointerPropertiesArray = { + pointerProperties + }; + final PointerCoords pointerCoords = new PointerCoords(); + pointerCoords.x = x; + pointerCoords.y = y; + pointerCoords.pressure = 1.0f; + pointerCoords.size = 1.0f; + final PointerCoords[] pointerCoordsArray = { + pointerCoords + }; + addMotionEventData(replayData, actionType, time, pointerPropertiesArray, + pointerCoordsArray); + } + } + + private void readEmbeddedMotionEvent(final JsonReader jsonReader, final ReplayData replayData, + final int actionType) throws IOException { + jsonReader.beginObject(); + PointerProperties[] pointerPropertiesArray = null; + while (jsonReader.hasNext()) { // pointerIds/xyt + final String name = jsonReader.nextName(); + if (name.equals("pointerIds")) { + pointerPropertiesArray = readPointerProperties(jsonReader); + } else if (name.equals("xyt")) { + readPointerData(jsonReader, replayData, actionType, pointerPropertiesArray); + } + } + jsonReader.endObject(); + } + + private PointerProperties[] readPointerProperties(final JsonReader jsonReader) + throws IOException { + final ArrayList<PointerProperties> pointerPropertiesArrayList = + new ArrayList<PointerProperties>(); + jsonReader.beginArray(); + while (jsonReader.hasNext()) { + final PointerProperties pointerProperties = new PointerProperties(); + pointerProperties.id = jsonReader.nextInt(); + pointerProperties.toolType = MotionEvent.TOOL_TYPE_UNKNOWN; + pointerPropertiesArrayList.add(pointerProperties); + } + jsonReader.endArray(); + return pointerPropertiesArrayList.toArray( + new PointerProperties[pointerPropertiesArrayList.size()]); + } + + private void readPointerData(final JsonReader jsonReader, final ReplayData replayData, + final int actionType, final PointerProperties[] pointerPropertiesArray) + throws IOException { + if (pointerPropertiesArray == null) { + Log.e(TAG, "PointerIDs must be given before xyt data in json for MotionEvent"); + jsonReader.skipValue(); + return; + } + long time = UNINITIALIZED_LONG; + jsonReader.beginArray(); + while (jsonReader.hasNext()) { // Array of historical data + jsonReader.beginObject(); + final ArrayList<PointerCoords> pointerCoordsArrayList = new ArrayList<PointerCoords>(); + while (jsonReader.hasNext()) { // Time/data object + final String name = jsonReader.nextName(); + if (name.equals("t")) { + time = jsonReader.nextLong(); + } else if (name.equals("d")) { + jsonReader.beginArray(); + while (jsonReader.hasNext()) { // array of data per pointer + final PointerCoords pointerCoords = readPointerCoords(jsonReader); + if (pointerCoords != null) { + pointerCoordsArrayList.add(pointerCoords); + } + } + jsonReader.endArray(); + } else { + jsonReader.skipValue(); + } + } + jsonReader.endObject(); + // Data was recorded as historical events, but must be split apart into + // separate MotionEvents for replaying + if (time != UNINITIALIZED_LONG) { + addMotionEventData(replayData, actionType, time, pointerPropertiesArray, + pointerCoordsArrayList.toArray( + new PointerCoords[pointerCoordsArrayList.size()])); + } else { + Log.e(TAG, "Time not assigned in json for MotionEvent"); + } + } + jsonReader.endArray(); + } + + private PointerCoords readPointerCoords(final JsonReader jsonReader) throws IOException { + jsonReader.beginObject(); + float x = UNINITIALIZED_FLOAT; + float y = UNINITIALIZED_FLOAT; + while (jsonReader.hasNext()) { // x,y + final String name = jsonReader.nextName(); + if (name.equals("x")) { + x = (float) jsonReader.nextDouble(); + } else if (name.equals("y")) { + y = (float) jsonReader.nextDouble(); + } else { + jsonReader.skipValue(); + } + } + jsonReader.endObject(); + + if (Float.compare(x, UNINITIALIZED_FLOAT) == 0 + || Float.compare(y, UNINITIALIZED_FLOAT) == 0) { + Log.w(TAG, "missing x or y value in MotionEvent json"); + return null; } + final PointerCoords pointerCoords = new PointerCoords(); + pointerCoords.x = x; + pointerCoords.y = y; + pointerCoords.pressure = 1.0f; + pointerCoords.size = 1.0f; + return pointerCoords; + } + + /** + * Tests that {@code x} is uninitialized. + * + * Assumes that {@code x} will never be given a valid value less than 0, and that + * UNINITIALIZED_FLOAT is less than 0.0f. + */ + private boolean isUninitializedFloat(final float x) { + return x < 0.0f; } + private void addMotionEventData(final ReplayData replayData, final int actionType, + final long time, final PointerProperties[] pointerProperties, + final PointerCoords[] pointerCoords) { + replayData.mActions.add(actionType); + replayData.mTimes.add(time); + replayData.mPointerPropertiesArrays.add(pointerProperties); + replayData.mPointerCoordsArrays.add(pointerCoords); + } } diff --git a/java/src/com/android/inputmethod/research/Replayer.java b/java/src/com/android/inputmethod/research/Replayer.java index 4cc2a5814..a9b7a9d0c 100644 --- a/java/src/com/android/inputmethod/research/Replayer.java +++ b/java/src/com/android/inputmethod/research/Replayer.java @@ -1,26 +1,29 @@ /* * 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 + * 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 + * 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. + * 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.research; import android.os.Handler; +import android.os.Looper; import android.os.Message; import android.os.SystemClock; import android.util.Log; import android.view.MotionEvent; +import android.view.MotionEvent.PointerCoords; +import android.view.MotionEvent.PointerProperties; import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.MainKeyboardView; @@ -40,6 +43,14 @@ public class Replayer { private boolean mIsReplaying = false; private KeyboardSwitcher mKeyboardSwitcher; + private Replayer() { + } + + private static final Replayer sInstance = new Replayer(); + public static Replayer getInstance() { + return sInstance; + } + public void setKeyboardSwitcher(final KeyboardSwitcher keyboardSwitcher) { mKeyboardSwitcher = keyboardSwitcher; } @@ -49,11 +60,10 @@ public class Replayer { private static final int COMPLETION_TIME_MS = 500; // TODO: Support historical events and multi-touch. - public void replay(final ReplayData replayData) { + public void replay(final ReplayData replayData, final Runnable callback) { if (mIsReplaying) { return; } - mIsReplaying = true; final int numActions = replayData.mActions.size(); if (DEBUG) { @@ -72,7 +82,7 @@ public class Replayer { // The adjustment needed to translate times from the original recorded time to the current // time. final long timeAdjustment = currentStartTime - origStartTime; - final Handler handler = new Handler() { + final Handler handler = new Handler(Looper.getMainLooper()) { // Track the time of the most recent DOWN event, to be passed as a parameter when // constructing a MotionEvent. It's initialized here to the origStartTime, but this is // only a precaution. The value should be overwritten by the first ACTION_DOWN event @@ -86,25 +96,36 @@ public class Replayer { case MSG_MOTION_EVENT: final int index = msg.arg1; final int action = replayData.mActions.get(index); - final int x = replayData.mXCoords.get(index); - final int y = replayData.mYCoords.get(index); + final PointerProperties[] pointerPropertiesArray = + replayData.mPointerPropertiesArrays.get(index); + final PointerCoords[] pointerCoordsArray = + replayData.mPointerCoordsArrays.get(index); final long origTime = replayData.mTimes.get(index); if (action == MotionEvent.ACTION_DOWN) { mOrigDownTime = origTime; } final MotionEvent me = MotionEvent.obtain(mOrigDownTime + timeAdjustment, - origTime + timeAdjustment, action, x, y, 0); + origTime + timeAdjustment, action, + pointerPropertiesArray.length, pointerPropertiesArray, + pointerCoordsArray, 0, 0, 1.0f, 1.0f, 0, 0, 0, 0); mainKeyboardView.processMotionEvent(me); me.recycle(); break; case MSG_DONE: mIsReplaying = false; + ResearchLogger.getInstance().requestIndicatorRedraw(); break; } } }; + handler.post(new Runnable() { + @Override + public void run() { + ResearchLogger.getInstance().requestIndicatorRedraw(); + } + }); for (int i = 0; i < numActions; i++) { final Message msg = Message.obtain(handler, MSG_MOTION_EVENT, i, 0); final long msgTime = replayData.mTimes.get(i) + timeAdjustment; @@ -113,8 +134,16 @@ public class Replayer { Log.d(TAG, "queuing event at " + msgTime); } } + final long presentDoneTime = replayData.mTimes.get(numActions - 1) + timeAdjustment + COMPLETION_TIME_MS; handler.sendMessageAtTime(Message.obtain(handler, MSG_DONE), presentDoneTime); + if (callback != null) { + handler.postAtTime(callback, presentDoneTime + 1); + } + } + + public boolean isReplaying() { + return mIsReplaying; } } diff --git a/java/src/com/android/inputmethod/research/ReplayerService.java b/java/src/com/android/inputmethod/research/ReplayerService.java new file mode 100644 index 000000000..88d9033cf --- /dev/null +++ b/java/src/com/android/inputmethod/research/ReplayerService.java @@ -0,0 +1,65 @@ +/* + * 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.research; + +import android.app.IntentService; +import android.content.Intent; +import android.util.Log; + +import com.android.inputmethod.research.MotionEventReader.ReplayData; + +import java.io.File; +import java.util.concurrent.TimeUnit; + +/** + * Provide a mechanism to invoke the replayer from outside. + * + * In particular, makes access from a host possible through {@code adb am startservice}. + */ +public class ReplayerService extends IntentService { + private static final String TAG = ReplayerService.class.getSimpleName(); + private static final String EXTRA_FILENAME = "com.android.inputmethod.research.extra.FILENAME"; + private static final long MAX_REPLAY_TIME = TimeUnit.SECONDS.toMillis(60); + + public ReplayerService() { + super(ReplayerService.class.getSimpleName()); + } + + @Override + protected void onHandleIntent(final Intent intent) { + final String filename = intent.getStringExtra(EXTRA_FILENAME); + if (filename == null) return; + + final ReplayData replayData = new MotionEventReader().readMotionEventData( + new File(filename)); + synchronized (this) { + Replayer.getInstance().replay(replayData, new Runnable() { + @Override + public void run() { + synchronized (ReplayerService.this) { + ReplayerService.this.notify(); + } + } + }); + try { + wait(MAX_REPLAY_TIME); + } catch (InterruptedException e) { + Log.e(TAG, "Timeout while replaying.", e); + } + } + } +} diff --git a/java/src/com/android/inputmethod/research/ResearchLogger.java b/java/src/com/android/inputmethod/research/ResearchLogger.java index 535ce28f4..8fc62ea7b 100644 --- a/java/src/com/android/inputmethod/research/ResearchLogger.java +++ b/java/src/com/android/inputmethod/research/ResearchLogger.java @@ -191,10 +191,10 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang private Suggest mSuggest; private MainKeyboardView mMainKeyboardView; // TODO: Check whether a superclass can be used instead of LatinIME. - private LatinIME mLatinIME; + /* package for test */ LatinIME mLatinIME; private final Statistics mStatistics; private final MotionEventReader mMotionEventReader = new MotionEventReader(); - private final Replayer mReplayer = new Replayer(); + private final Replayer mReplayer = Replayer.getInstance(); private Intent mUploadIntent; private Intent mUploadNowIntent; @@ -798,7 +798,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang public void run() { final ReplayData replayData = mMotionEventReader.readMotionEventData(mUserRecordingFile); - mReplayer.replay(replayData); + mReplayer.replay(replayData, null); } }, 1000); } @@ -846,7 +846,8 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang } private boolean isAllowedToLog() { - return !mIsPasswordView && !mIsLoggingSuspended && sIsLogging && !mInFeedbackDialog; + return !mIsPasswordView && !mIsLoggingSuspended && sIsLogging && !mInFeedbackDialog + && !isReplaying(); } public void requestIndicatorRedraw() { @@ -859,15 +860,30 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang mMainKeyboardView.invalidateAllKeys(); } + private boolean isReplaying() { + return mReplayer.isReplaying(); + } + + private int getIndicatorColor() { + if (isMakingUserRecording()) { + return Color.YELLOW; + } + if (isReplaying()) { + return Color.GREEN; + } + return Color.RED; + } + public void paintIndicator(KeyboardView view, Paint paint, Canvas canvas, int width, int height) { // TODO: Reimplement using a keyboard background image specific to the ResearchLogger // and remove this method. // The check for MainKeyboardView ensures that the indicator only decorates the main // keyboard, not every keyboard. - if (IS_SHOWING_INDICATOR && isAllowedToLog() && view instanceof MainKeyboardView) { + if (IS_SHOWING_INDICATOR && (isAllowedToLog() || isReplaying()) + && view instanceof MainKeyboardView) { final int savedColor = paint.getColor(); - paint.setColor(isMakingUserRecording() ? Color.YELLOW : Color.RED); + paint.setColor(getIndicatorColor()); final Style savedStyle = paint.getStyle(); paint.setStyle(Style.STROKE); final float savedStrokeWidth = paint.getStrokeWidth(); @@ -1189,7 +1205,8 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang * */ private static final LogStatement LOGSTATEMENT_MAIN_KEYBOARD_VIEW_PROCESS_MOTION_EVENT = - new LogStatement("MotionEvent", true, false, "action", "MotionEvent", "loggingRelated"); + new LogStatement("MotionEvent", true, false, "action", + LogStatement.KEY_IS_LOGGING_RELATED, "motionEvent"); public static void mainKeyboardView_processMotionEvent(final MotionEvent me, final int action, final long eventTime, final int index, final int id, final int x, final int y) { if (me != null) { @@ -1206,7 +1223,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang } final ResearchLogger researchLogger = getInstance(); researchLogger.enqueueEvent(LOGSTATEMENT_MAIN_KEYBOARD_VIEW_PROCESS_MOTION_EVENT, - actionString, MotionEvent.obtain(me), false); + actionString, false /* IS_LOGGING_RELATED */, MotionEvent.obtain(me)); if (action == MotionEvent.ACTION_DOWN) { // Subtract 1 from eventTime so the down event is included in the later // LogUnit, not the earlier (the test is for inequality). |