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

Commit 5bbed60e authored by Tracy Zhou's avatar Tracy Zhou
Browse files

Clean up gesture handling enabled / disabled logic due to trackpad connecting and disconnecting

Bug: 335146977
Test: Make sure back gesture still works when trackpad connects and disconnects
Change-Id: I7ff065908f8a0c19b1ce195038e0f0e2690f2d48
parent 1baf5540
Loading
Loading
Loading
Loading
+33 −27
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ import static com.android.systemui.classifier.Classifier.BACK_GESTURE;
import static com.android.systemui.navigationbar.gestural.Utilities.isTrackpadScroll;
import static com.android.systemui.navigationbar.gestural.Utilities.isTrackpadThreeFingerSwipe;

import static java.util.stream.Collectors.joining;

import android.annotation.NonNull;
import android.app.ActivityManager;
import android.content.ComponentName;
@@ -47,6 +49,7 @@ import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.Trace;
import android.provider.DeviceConfig;
import android.util.ArraySet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
@@ -102,6 +105,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
@@ -255,7 +259,7 @@ public class EdgeBackGestureHandler implements PluginListener<NavigationEdgeBack

    private boolean mIsAttached;
    private boolean mIsGestureHandlingEnabled;
    private boolean mIsTrackpadConnected;
    private final Set<Integer> mTrackpadsConnected = new ArraySet<>();
    private boolean mInGestureNavMode;
    private boolean mUsingThreeButtonNav;
    private boolean mIsEnabled;
@@ -358,16 +362,14 @@ public class EdgeBackGestureHandler implements PluginListener<NavigationEdgeBack

    private final InputManager.InputDeviceListener mInputDeviceListener =
            new InputManager.InputDeviceListener() {

        // Only one trackpad can be connected to a device at a time, since it takes over the
        // only USB port.
        private int mTrackpadDeviceId;

        @Override
        public void onInputDeviceAdded(int deviceId) {
            if (isTrackpadDevice(deviceId)) {
                mTrackpadDeviceId = deviceId;
                update(true /* isTrackpadConnected */);
                boolean wasEmpty = mTrackpadsConnected.isEmpty();
                mTrackpadsConnected.add(deviceId);
                if (wasEmpty) {
                    update();
                }
            }
        }

@@ -376,18 +378,29 @@ public class EdgeBackGestureHandler implements PluginListener<NavigationEdgeBack

        @Override
        public void onInputDeviceRemoved(int deviceId) {
            if (mTrackpadDeviceId == deviceId) {
                update(false /* isTrackpadConnected */);
            mTrackpadsConnected.remove(deviceId);
            if (mTrackpadsConnected.isEmpty()) {
                update();
            }
        }

        private void update(boolean isTrackpadConnected) {
            boolean isPreviouslyTrackpadConnected = mIsTrackpadConnected;
            mIsTrackpadConnected = isTrackpadConnected;
            if (isPreviouslyTrackpadConnected != mIsTrackpadConnected) {
        private void update() {
            if (mIsEnabled && !mTrackpadsConnected.isEmpty()) {
                // Don't reinitialize gesture handling due to trackpad connecting when it's
                // already set up.
                return;
            }
            updateIsEnabled();
            updateCurrentUserResources();
        }

        private boolean isTrackpadDevice(int deviceId) {
            InputDevice inputDevice = mInputManager.getInputDevice(deviceId);
            if (inputDevice == null) {
                return false;
            }
            return inputDevice.getSources() == (InputDevice.SOURCE_MOUSE
                    | InputDevice.SOURCE_TOUCHPAD);
        }
    };

@@ -566,6 +579,7 @@ public class EdgeBackGestureHandler implements PluginListener<NavigationEdgeBack
        mOverviewProxyService.removeCallback(mQuickSwitchListener);
        mSysUiState.removeCallback(mSysUiStateCallback);
        mInputManager.unregisterInputDeviceListener(mInputDeviceListener);
        mTrackpadsConnected.clear();
        updateIsEnabled();
        mUserTracker.removeCallback(mUserChangedCallback);
    }
@@ -605,7 +619,7 @@ public class EdgeBackGestureHandler implements PluginListener<NavigationEdgeBack
            Trace.beginSection("EdgeBackGestureHandler#updateIsEnabled");

            mIsGestureHandlingEnabled = mInGestureNavMode || (mUsingThreeButtonNav
                    && mIsTrackpadConnected);
                    && !mTrackpadsConnected.isEmpty());
            boolean isEnabled = mIsAttached && mIsGestureHandlingEnabled;
            if (isEnabled == mIsEnabled) {
                return;
@@ -867,15 +881,6 @@ public class EdgeBackGestureHandler implements PluginListener<NavigationEdgeBack
                mDisplaySize.y - insets.bottom);
    }

    private boolean isTrackpadDevice(int deviceId) {
        InputDevice inputDevice = mInputManager.getInputDevice(deviceId);
        if (inputDevice == null) {
            return false;
        }
        return inputDevice.getSources() == (InputDevice.SOURCE_MOUSE
                | InputDevice.SOURCE_TOUCHPAD);
    }

    private boolean desktopExcludeRegionContains(int x, int y) {
        return mDesktopModeExcludeRegion.contains(x, y);
    }
@@ -1251,7 +1256,8 @@ public class EdgeBackGestureHandler implements PluginListener<NavigationEdgeBack
        pw.println("  mPredictionLog=" + String.join("\n", mPredictionLog));
        pw.println("  mGestureLogInsideInsets=" + String.join("\n", mGestureLogInsideInsets));
        pw.println("  mGestureLogOutsideInsets=" + String.join("\n", mGestureLogOutsideInsets));
        pw.println("  mIsTrackpadConnected=" + mIsTrackpadConnected);
        pw.println("  mTrackpadsConnected=" + mTrackpadsConnected.stream().map(
                String::valueOf).collect(joining()));
        pw.println("  mUsingThreeButtonNav=" + mUsingThreeButtonNav);
        pw.println("  mEdgeBackPlugin=" + mEdgeBackPlugin);
        if (mEdgeBackPlugin != null) {