Add debug logs to MotionPauseDetector

Flag: NONE
Bug: 321123090
Test: checked TIS logs
Change-Id: If389226c60b5115fcf90e76733383c74ceb7fbcd
diff --git a/quickstep/src/com/android/quickstep/util/ActiveGestureLog.java b/quickstep/src/com/android/quickstep/util/ActiveGestureLog.java
index 278ca56..1e05a69 100644
--- a/quickstep/src/com/android/quickstep/util/ActiveGestureLog.java
+++ b/quickstep/src/com/android/quickstep/util/ActiveGestureLog.java
@@ -67,15 +67,15 @@
     /**
      * Adds a log to be printed at log-dump-time.
      */
-    public void addLog(String event) {
+    public void addLog(@NonNull String event) {
         addLog(event, null);
     }
 
-    public void addLog(String event, int extras) {
+    public void addLog(@NonNull String event, int extras) {
         addLog(event, extras, null);
     }
 
-    public void addLog(String event, boolean extras) {
+    public void addLog(@NonNull String event, boolean extras) {
         addLog(event, extras, null);
     }
 
@@ -85,30 +85,30 @@
      * @param gestureEvent GestureEvent representing the event being logged.
      */
     public void addLog(
-            String event, @Nullable ActiveGestureErrorDetector.GestureEvent gestureEvent) {
+            @NonNull String event, @Nullable ActiveGestureErrorDetector.GestureEvent gestureEvent) {
         addLog(new CompoundString(event), gestureEvent);
     }
 
     public void addLog(
-            String event,
+            @NonNull String event,
             int extras,
             @Nullable ActiveGestureErrorDetector.GestureEvent gestureEvent) {
         addLog(new CompoundString(event).append(": ").append(extras), gestureEvent);
     }
 
     public void addLog(
-            String event,
+            @NonNull String event,
             boolean extras,
             @Nullable ActiveGestureErrorDetector.GestureEvent gestureEvent) {
         addLog(new CompoundString(event).append(": ").append(extras), gestureEvent);
     }
 
-    public void addLog(CompoundString compoundString) {
+    public void addLog(@NonNull CompoundString compoundString) {
         addLog(compoundString, null);
     }
 
     public void addLog(
-            CompoundString compoundString,
+            @NonNull CompoundString compoundString,
             @Nullable ActiveGestureErrorDetector.GestureEvent gestureEvent) {
         EventLog lastEventLog = logs[(nextIndex + logs.length - 1) % logs.length];
         if (lastEventLog == null || mCurrentLogId != lastEventLog.logId) {
@@ -259,21 +259,20 @@
 
         public CompoundString(String substring) {
             mIsNoOp = substring == null;
-            if (mIsNoOp) {
-                mSubstrings = null;
-                mArgs = null;
-                return;
+            mSubstrings = mIsNoOp ? null : new ArrayList<>();
+            mArgs = mIsNoOp ? null : new ArrayList<>();
+
+            if (!mIsNoOp) {
+                mSubstrings.add(substring);
             }
-            mSubstrings = new ArrayList<>();
-            mSubstrings.add(substring);
-            mArgs = new ArrayList<>();
         }
 
         public CompoundString append(CompoundString substring) {
-            if (mIsNoOp) {
+            if (mIsNoOp || substring.mIsNoOp) {
                 return this;
             }
             mSubstrings.addAll(substring.mSubstrings);
+            mArgs.addAll(substring.mArgs);
 
             return this;
         }
@@ -288,30 +287,53 @@
         }
 
         public CompoundString append(int num) {
+            if (mIsNoOp) {
+                return this;
+            }
+            mArgs.add(num);
+
+            return append("%d");
+        }
+
+        public CompoundString append(long num) {
+            if (mIsNoOp) {
+                return this;
+            }
             mArgs.add(num);
 
             return append("%d");
         }
 
         public CompoundString append(float num) {
+            if (mIsNoOp) {
+                return this;
+            }
             mArgs.add(num);
 
             return append("%.2f");
         }
 
         public CompoundString append(double num) {
+            if (mIsNoOp) {
+                return this;
+            }
             mArgs.add(num);
 
             return append("%.2f");
         }
 
         public CompoundString append(boolean bool) {
+            if (mIsNoOp) {
+                return this;
+            }
             mArgs.add(bool);
 
             return append("%b");
         }
 
-        public Object[] getArgs() {
+        private Object[] getArgs() {
+            Preconditions.assertTrue(!mIsNoOp);
+
             return mArgs.toArray();
         }
 
@@ -320,7 +342,7 @@
             return String.format(toUnformattedString(), getArgs());
         }
 
-        public String toUnformattedString() {
+        private String toUnformattedString() {
             Preconditions.assertTrue(!mIsNoOp);
 
             StringBuilder sb = new StringBuilder();
@@ -333,7 +355,7 @@
 
         @Override
         public int hashCode() {
-            return Objects.hash(mIsNoOp, mSubstrings);
+            return Objects.hash(mIsNoOp, mSubstrings, mArgs);
         }
 
         @Override
@@ -342,7 +364,9 @@
                 return false;
             }
             CompoundString other = (CompoundString) obj;
-            return (mIsNoOp == other.mIsNoOp) && Objects.equals(mSubstrings, other.mSubstrings);
+            return (mIsNoOp == other.mIsNoOp)
+                    && Objects.equals(mSubstrings, other.mSubstrings)
+                    && Objects.equals(mArgs, other.mArgs);
         }
     }
 }
diff --git a/quickstep/src/com/android/quickstep/util/MotionPauseDetector.java b/quickstep/src/com/android/quickstep/util/MotionPauseDetector.java
index a8a96ce..b8bc828 100644
--- a/quickstep/src/com/android/quickstep/util/MotionPauseDetector.java
+++ b/quickstep/src/com/android/quickstep/util/MotionPauseDetector.java
@@ -96,8 +96,14 @@
         mSpeedSomewhatFast = res.getDimension(R.dimen.motion_pause_detector_speed_somewhat_fast);
         mSpeedFast = res.getDimension(R.dimen.motion_pause_detector_speed_fast);
         mForcePauseTimeout = new Alarm();
-        mForcePauseTimeout.setOnAlarmListener(alarm -> updatePaused(true /* isPaused */,
-                "Force pause timeout after " +  alarm.getLastSetTimeout() + "ms" /* reason */));
+        mForcePauseTimeout.setOnAlarmListener(alarm -> {
+            ActiveGestureLog.CompoundString log =
+                    new ActiveGestureLog.CompoundString("Force pause timeout after ")
+                            .append(alarm.getLastSetTimeout())
+                            .append("ms");
+            addLogs(log);
+            updatePaused(true /* isPaused */, log);
+        });
         mMakePauseHarderToTrigger = makePauseHarderToTrigger;
         mVelocityProvider = new SystemVelocityProvider(axis);
     }
@@ -113,8 +119,14 @@
      * @param disallowPause If true, we will not detect any pauses until this is set to false again.
      */
     public void setDisallowPause(boolean disallowPause) {
+        ActiveGestureLog.CompoundString log =
+                new ActiveGestureLog.CompoundString("Set disallowPause=")
+                        .append(disallowPause);
+        if (mDisallowPause != disallowPause) {
+            addLogs(log);
+        }
         mDisallowPause = disallowPause;
-        updatePaused(mIsPaused, "Set disallowPause=" + disallowPause);
+        updatePaused(mIsPaused, log);
     }
 
     /**
@@ -148,27 +160,30 @@
         float speed = Math.abs(velocity);
         float previousSpeed = Math.abs(prevVelocity);
         boolean isPaused;
-        String isPausedReason = "";
+        ActiveGestureLog.CompoundString isPausedReason;
         if (mIsPaused) {
             // Continue to be paused until moving at a fast speed.
             isPaused = speed < mSpeedFast || previousSpeed < mSpeedFast;
-            isPausedReason = "Was paused, but started moving at a fast speed";
+            isPausedReason = new ActiveGestureLog.CompoundString(
+                    "Was paused, but started moving at a fast speed");
         } else {
             if (velocity < 0 != prevVelocity < 0) {
                 // We're just changing directions, not necessarily stopping.
                 isPaused = false;
-                isPausedReason = "Velocity changed directions";
+                isPausedReason = new ActiveGestureLog.CompoundString("Velocity changed directions");
             } else {
                 isPaused = speed < mSpeedVerySlow && previousSpeed < mSpeedVerySlow;
-                isPausedReason = "Pause requires back to back slow speeds";
+                isPausedReason = new ActiveGestureLog.CompoundString(
+                        "Pause requires back to back slow speeds");
                 if (!isPaused && !mHasEverBeenPaused) {
                     // We want to be more aggressive about detecting the first pause to ensure it
                     // feels as responsive as possible; getting two very slow speeds back to back
                     // takes too long, so also check for a rapid deceleration.
                     boolean isRapidDeceleration = speed < previousSpeed * RAPID_DECELERATION_FACTOR;
                     isPaused = isRapidDeceleration && speed < mSpeedSomewhatFast;
-                    isPausedReason = "Didn't have back to back slow speeds, checking for rapid"
-                            + " deceleration on first pause only";
+                    isPausedReason = new ActiveGestureLog.CompoundString(
+                            "Didn't have back to back slow speeds, checking for rapid ")
+                            .append(" deceleration on first pause only");
                 }
                 if (mMakePauseHarderToTrigger) {
                     if (speed < mSpeedSlow) {
@@ -176,12 +191,14 @@
                             mSlowStartTime = time;
                         }
                         isPaused = time - mSlowStartTime >= HARDER_TRIGGER_TIMEOUT;
-                        isPausedReason = "Maintained slow speed for sufficient duration when making"
-                                + " pause harder to trigger";
+                        isPausedReason = new ActiveGestureLog.CompoundString(
+                                "Maintained slow speed for sufficient duration when making")
+                                .append(" pause harder to trigger");
                     } else {
                         mSlowStartTime = 0;
                         isPaused = false;
-                        isPausedReason = "Intentionally making pause harder to trigger";
+                        isPausedReason = new ActiveGestureLog.CompoundString(
+                                "Intentionally making pause harder to trigger");
                     }
                 }
             }
@@ -189,18 +206,21 @@
         updatePaused(isPaused, isPausedReason);
     }
 
-    private void updatePaused(boolean isPaused, String reason) {
+    private void updatePaused(boolean isPaused, ActiveGestureLog.CompoundString reason) {
         if (mDisallowPause) {
-            reason = "Disallow pause; otherwise, would have been " + isPaused + " due to " + reason;
+            reason = new ActiveGestureLog.CompoundString(
+                    "Disallow pause; otherwise, would have been ")
+                    .append(isPaused)
+                    .append(" due to reason:")
+                    .append(reason);
             isPaused = false;
         }
         if (mIsPaused != isPaused) {
             mIsPaused = isPaused;
-            String logString = "onMotionPauseChanged, paused=" + mIsPaused + " reason=" + reason;
-            if (Utilities.isRunningInTestHarness()) {
-                Log.d(TAG, logString);
-            }
-            ActiveGestureLog.INSTANCE.addLog(logString);
+            addLogs(new ActiveGestureLog.CompoundString("onMotionPauseChanged triggered; paused=")
+                    .append(mIsPaused)
+                    .append(", reason=")
+                    .append(reason));
             boolean isFirstDetectedPause = !mHasEverBeenPaused && mIsPaused;
             if (mIsPaused) {
                 AccessibilityManagerCompat.sendTestProtocolEventToTest(mContext,
@@ -219,6 +239,16 @@
         }
     }
 
+    private void addLogs(ActiveGestureLog.CompoundString compoundString) {
+        ActiveGestureLog.CompoundString logString =
+                new ActiveGestureLog.CompoundString("MotionPauseDetector: ")
+                        .append(compoundString);
+        if (Utilities.isRunningInTestHarness()) {
+            Log.d(TAG, logString.toString());
+        }
+        ActiveGestureLog.INSTANCE.addLog(logString);
+    }
+
     public void clear() {
         mVelocityProvider.clear();
         mPreviousVelocity = null;