Merge "Import translations. DO NOT MERGE"
diff --git a/java/res/values/research_strings.xml b/java/res/values/research_strings.xml
index 2d9dad1..f67943e 100644
--- a/java/res/values/research_strings.xml
+++ b/java/res/values/research_strings.xml
@@ -23,4 +23,7 @@
     <string name="research_splash_content" translatable="false"></string>
     <string name="research_account_type" translatable="false"></string>
     <string name="research_allowed_account_domain" translatable="false"></string>
+    <!-- Message informing the user that the feedback string must not be empty [CHAR LIMIT=100] -->
+    <!-- TODO: remove translatable=false attribute once text is stable -->
+    <string name="research_feedback_empty_feedback_error_message" translatable="false">The feedback field must not be empty.</string>
 </resources>
diff --git a/java/res/values/strings.xml b/java/res/values/strings.xml
index 8822e8d..bab612b 100644
--- a/java/res/values/strings.xml
+++ b/java/res/values/strings.xml
@@ -313,6 +313,10 @@
     <!-- TODO: remove translatable=false attribute once text is stable -->
     <string name="research_log_uploader_name" translatable="false">Research Uploader Service</string>
 
+    <!-- Name for the research replaying service to be displayed to users.  [CHAR LIMIT=50] -->
+    <!-- TODO: remove translatable=false attribute once text is stable -->
+    <string name="research_log_replayer_name" translatable="false">Research Replayer Service</string>
+
     <!-- Preference for input language selection -->
     <string name="select_language">Input languages</string>
 
diff --git a/java/src/com/android/inputmethod/research/FeedbackFragment.java b/java/src/com/android/inputmethod/research/FeedbackFragment.java
index 11a833a..69ddf82 100644
--- a/java/src/com/android/inputmethod/research/FeedbackFragment.java
+++ b/java/src/com/android/inputmethod/research/FeedbackFragment.java
@@ -20,6 +20,7 @@
 import android.app.Fragment;
 import android.os.Bundle;
 import android.text.Editable;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -28,6 +29,7 @@
 import android.widget.Button;
 import android.widget.CheckBox;
 import android.widget.EditText;
+import android.widget.Toast;
 
 import com.android.inputmethod.latin.R;
 
@@ -96,12 +98,18 @@
         } else if (view == mSendButton) {
             final Editable editable = mEditText.getText();
             final String feedbackContents = editable.toString();
-            final boolean isIncludingAccountName = isIncludingAccountName();
-            researchLogger.sendFeedback(feedbackContents,
-                    false /* isIncludingHistory */, isIncludingAccountName, hasUserRecording());
-            getActivity().finish();
-            researchLogger.setFeedbackDialogBundle(null);
-            researchLogger.onLeavingSendFeedbackDialog();
+            if (TextUtils.isEmpty(feedbackContents)) {
+                Toast.makeText(getActivity(),
+                        R.string.research_feedback_empty_feedback_error_message,
+                        Toast.LENGTH_LONG).show();
+            } else {
+                final boolean isIncludingAccountName = isIncludingAccountName();
+                researchLogger.sendFeedback(feedbackContents,
+                        false /* isIncludingHistory */, isIncludingAccountName, hasUserRecording());
+                getActivity().finish();
+                researchLogger.setFeedbackDialogBundle(null);
+                researchLogger.onLeavingSendFeedbackDialog();
+            }
         } else if (view == mCancelButton) {
             Log.d(TAG, "Finishing");
             getActivity().finish();
diff --git a/java/src/com/android/inputmethod/research/LogStatement.java b/java/src/com/android/inputmethod/research/LogStatement.java
index 090c58e..1d83e1a 100644
--- a/java/src/com/android/inputmethod/research/LogStatement.java
+++ b/java/src/com/android/inputmethod/research/LogStatement.java
@@ -29,13 +29,12 @@
             "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 608fab3..2e732fc 100644
--- a/java/src/com/android/inputmethod/research/LogUnit.java
+++ b/java/src/com/android/inputmethod/research/LogUnit.java
@@ -453,13 +453,12 @@
 
         // 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 @@
             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 36e75be..26a1d7f 100644
--- a/java/src/com/android/inputmethod/research/MotionEventReader.java
+++ b/java/src/com/android/inputmethod/research/MotionEventReader.java
@@ -101,7 +101,7 @@
         jsonReader.endObject();
 
         if (logStatementType != null && time != null && x != null && y != null && actionType != null
-                && logStatementType.equals("MainKeyboardViewProcessMotionEvent")
+                && logStatementType.equals("MotionEvent")
                 && !loggingRelated) {
             replayData.mActions.add(actionType);
             replayData.mXCoords.add(x);
diff --git a/java/src/com/android/inputmethod/research/Replayer.java b/java/src/com/android/inputmethod/research/Replayer.java
index 4cc2a58..611abb2 100644
--- a/java/src/com/android/inputmethod/research/Replayer.java
+++ b/java/src/com/android/inputmethod/research/Replayer.java
@@ -1,22 +1,23 @@
 /*
  * 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;
@@ -40,6 +41,14 @@
     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,7 +58,7 @@
     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;
         }
@@ -72,7 +81,7 @@
         // 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
@@ -113,8 +122,12 @@
                 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);
+        }
     }
 }
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 0000000..88d9033
--- /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 e6bc2fd..da41001 100644
--- a/java/src/com/android/inputmethod/research/ResearchLogger.java
+++ b/java/src/com/android/inputmethod/research/ResearchLogger.java
@@ -187,7 +187,7 @@
     /* 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;
@@ -783,7 +783,7 @@
                 public void run() {
                     final ReplayData replayData =
                             mMotionEventReader.readMotionEventData(mUserRecordingFile);
-                    mReplayer.replay(replayData);
+                    mReplayer.replay(replayData, null);
                 }
             }, 1000);
         }
@@ -1162,7 +1162,8 @@
      *
      */
     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) {
@@ -1179,7 +1180,7 @@
             }
             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).