Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6f83f477 authored by Maryam Karimzadehgan's avatar Maryam Karimzadehgan
Browse files

Adding logging for the ML mBack gesture model.

Test: manual on device
Bug:150170384
Change-Id: Id6e71b806db3e7cc8bf0b98321c561a5d2e8fbca
parent 115113c6
Loading
Loading
Loading
Loading
+47 −18
Original line number Diff line number Diff line
@@ -76,6 +76,7 @@ import com.android.systemui.tracing.nano.EdgeBackGestureHandlerProto;
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;
@@ -207,10 +208,15 @@ public class EdgeBackGestureHandler extends CurrentUserTracker implements Displa
    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 =
@@ -302,6 +308,11 @@ public class EdgeBackGestureHandler extends CurrentUserTracker implements Displa
        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
@@ -494,6 +505,7 @@ public class EdgeBackGestureHandler extends CurrentUserTracker implements Displa
    private void updateMLModelState() {
        boolean newState = mIsEnabled && DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SYSTEMUI,
                SystemUiDeviceConfigFlags.USE_BACK_GESTURE_ML_MODEL, false);

        if (newState == mUseMLModel) {
            return;
        }
@@ -519,11 +531,10 @@ public class EdgeBackGestureHandler extends CurrentUserTracker implements Displa
        }
    }

    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;
        }

        int distanceFromEdge;
        int location;
        if (x <= mDisplaySize.x / 2.0) {
@@ -538,20 +549,18 @@ public class EdgeBackGestureHandler extends CurrentUserTracker implements Displa
            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;
@@ -570,8 +579,18 @@ public class EdgeBackGestureHandler extends CurrentUserTracker implements Displa
            return false;
        }

        if (mUseMLModel &&  (results = getBackGesturePredictionsCategory(x, y)) != -1) {
            withinRange = results == 1 ? true : false;
        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
@@ -579,6 +598,14 @@ public class EdgeBackGestureHandler extends CurrentUserTracker implements Displa
                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) {
@@ -774,6 +801,8 @@ public class EdgeBackGestureHandler extends CurrentUserTracker implements Displa
        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() {