Adding logging for the ML mBack gesture model.

Test: manual on device
Bug:150170384
Change-Id: Id6e71b806db3e7cc8bf0b98321c561a5d2e8fbca
Merged-In: Id6e71b806db3e7cc8bf0b98321c561a5d2e8fbca
(cherry picked from commit 6f83f477b9b44a4511f0bc817233a23b76be3654)
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/EdgeBackGestureHandler.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/EdgeBackGestureHandler.java
index dc42997..603679a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/EdgeBackGestureHandler.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/EdgeBackGestureHandler.java
@@ -75,6 +75,7 @@
 import com.android.systemui.tracing.nano.SystemUiTraceProto;
 
 import java.io.PrintWriter;
+import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -204,10 +205,15 @@
     private BackGestureTfClassifierProvider mBackGestureTfClassifierProvider;
     private Map<String, Integer> mVocab;
     private boolean mUseMLModel;
+    // minimum width below which we do not run the model
+    private int mMLEnableWidth;
     private float mMLModelThreshold;
     private String mPackageName;
     private float mMLResults;
 
+    private static final int MAX_LOGGED_PREDICTIONS = 10;
+    private ArrayDeque<String> mPredictionLog = new ArrayDeque<>();
+
     private final GestureNavigationSettingsObserver mGestureNavigationSettingsObserver;
 
     private final NavigationEdgeBackPlugin.BackCallback mBackCallback =
@@ -292,6 +298,11 @@
         mBottomGestureHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, gestureHeight,
                 dm);
 
+        // Set the minimum bounds to activate ML to 12dp or the minimum of configured values
+        mMLEnableWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12.0f, dm);
+        if (mMLEnableWidth > mEdgeWidthRight) mMLEnableWidth = mEdgeWidthRight;
+        if (mMLEnableWidth > mEdgeWidthLeft) mMLEnableWidth = mEdgeWidthLeft;
+
         // Reduce the default touch slop to ensure that we can intercept the gesture
         // before the app starts to react to it.
         // TODO(b/130352502) Tune this value and extract into a constant
@@ -500,8 +511,8 @@
         }
     }
 
-    private float getBackGesturePredictionsCategory(int x, int y) {
-        if (!mVocab.containsKey(mPackageName)) {
+    private int getBackGesturePredictionsCategory(int x, int y, int app) {
+        if (app == -1) {
             return -1;
         }
 
@@ -519,20 +530,19 @@
             new long[]{(long) mDisplaySize.x},
             new long[]{(long) distanceFromEdge},
             new long[]{(long) location},
-            new long[]{(long) mVocab.get(mPackageName)},
+            new long[]{(long) app},
             new long[]{(long) y},
         };
 
         mMLResults = mBackGestureTfClassifierProvider.predict(featuresVector);
-        if (mMLResults == -1) return -1;
+        if (mMLResults == -1) {
+            return -1;
+        }
 
         return mMLResults >= mMLModelThreshold ? 1 : 0;
     }
 
     private boolean isWithinTouchRegion(int x, int y) {
-        boolean withinRange = false;
-        float results = -1;
-
         // Disallow if we are in the bottom gesture area
         if (y >= (mDisplaySize.y - mBottomGestureHeight)) {
             return false;
@@ -546,15 +556,33 @@
             return false;
         }
 
-        if (mUseMLModel &&  (results = getBackGesturePredictionsCategory(x, y)) != -1) {
-            withinRange = results == 1 ? true : false;
-        } else {
-            // Denotes whether we should proceed with the gesture.
-            // Even if it is false, we may want to log it assuming
-            // it is not invalid due to exclusion.
-            withinRange = x <= mEdgeWidthLeft + mLeftInset
-                    || x >= (mDisplaySize.x - mEdgeWidthRight - mRightInset);
+        int app = -1;
+        if (mVocab != null) {
+            app = mVocab.getOrDefault(mPackageName, -1);
         }
+        // Check if we are within the tightest bounds beyond which
+        // we would not need to run the ML model.
+        boolean withinRange = x <= mMLEnableWidth + mLeftInset
+                || x >= (mDisplaySize.x - mMLEnableWidth - mRightInset);
+        if (!withinRange) {
+            int results = -1;
+            if (mUseMLModel && (results = getBackGesturePredictionsCategory(x, y, app)) != -1) {
+                withinRange = results == 1;
+            } else {
+                // Denotes whether we should proceed with the gesture.
+                // Even if it is false, we may want to log it assuming
+                // it is not invalid due to exclusion.
+                withinRange = x <= mEdgeWidthLeft + mLeftInset
+                        || x >= (mDisplaySize.x - mEdgeWidthRight - mRightInset);
+            }
+        }
+
+        // For debugging purposes
+        if (mPredictionLog.size() >= MAX_LOGGED_PREDICTIONS) {
+            mPredictionLog.removeFirst();
+        }
+        mPredictionLog.addLast(String.format("[%d,%d,%d,%f,%d]",
+                x, y, app, mMLResults, withinRange ? 1 : 0));
 
         // Always allow if the user is in a transient sticky immersive state
         if (mIsNavBarShownTransiently) {
@@ -753,6 +781,8 @@
         pw.println("  mIsAttached=" + mIsAttached);
         pw.println("  mEdgeWidthLeft=" + mEdgeWidthLeft);
         pw.println("  mEdgeWidthRight=" + mEdgeWidthRight);
+        pw.println("  mIsNavBarShownTransiently=" + mIsNavBarShownTransiently);
+        pw.println("  mPredictionLog=" + String.join(";", mPredictionLog));
     }
 
     private boolean isGestureBlockingActivityRunning() {