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

Commit 8b264568 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Waiting for a small timeout for launcher draw, before performing the swipe gesture.

This allow for a better synchronized motion, in case launcher draw was fast enough

Change-Id: Ie59aa1e8a2ffa94f3640b4ea08a0c23eeabafb54
parent 0ecca76f
Loading
Loading
Loading
Loading
+60 −31
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
@@ -56,12 +57,17 @@ import com.android.systemui.shared.system.RecentsAnimationListener;
import com.android.systemui.shared.system.RemoteAnimationTargetCompat;
import com.android.systemui.shared.system.WindowManagerWrapper;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Touch consumer for handling events originating from an activity other than Launcher
 */
public class OtherActivityTouchConsumer extends ContextWrapper implements TouchConsumer {
    private static final String TAG = "ActivityTouchConsumer";

    private static final long LAUNCHER_DRAW_TIMEOUT_MS = 150;

    private final RunningTaskInfo mRunningTask;
    private final RecentsModel mRecentsModel;
    private final Intent mHomeIntent;
@@ -111,7 +117,6 @@ public class OtherActivityTouchConsumer extends ContextWrapper implements TouchC
                    startTouchTrackingForWindowAnimation();
                }

                mVelocityTracker.addMovement(ev);
                Display display = getSystemService(WindowManager.class).getDefaultDisplay();
                mDisplayRotation = display.getRotation();
                WindowManagerWrapper.getInstance().getStableInsets(mStableInsets);
@@ -127,7 +132,6 @@ public class OtherActivityTouchConsumer extends ContextWrapper implements TouchC
                            ev.getY(newPointerIdx) - (mLastPos.y - mDownPos.y));
                    mLastPos.set(ev.getX(newPointerIdx), ev.getY(newPointerIdx));
                    mActivePointerId = ev.getPointerId(newPointerIdx);
                    mVelocityTracker.clear();
                }
                break;
            }
@@ -136,7 +140,6 @@ public class OtherActivityTouchConsumer extends ContextWrapper implements TouchC
                if (pointerIndex == INVALID_POINTER_ID) {
                    break;
                }
                mVelocityTracker.addMovement(ev);
                mLastPos.set(ev.getX(pointerIndex), ev.getY(pointerIndex));

                float displacement = ev.getY(pointerIndex) - mDownPos.y;
@@ -250,8 +253,23 @@ public class OtherActivityTouchConsumer extends ContextWrapper implements TouchC
        // Create the shared handler
        final WindowTransformSwipeHandler handler =
                new WindowTransformSwipeHandler(mRunningTask, this);
        BackgroundExecutor.get().submit(() -> {
            ActivityManagerWrapper.getInstance().startRecentsActivity(mHomeIntent,

        // Preload the plan
        mRecentsModel.loadTasks(mRunningTask.id, null);
        mInteractionHandler = handler;
        handler.setGestureEndCallback(this::onFinish);

        CountDownLatch drawWaitLock = new CountDownLatch(1);
        handler.setLauncherOnDrawCallback(() -> {
            drawWaitLock.countDown();
            if (handler == mInteractionHandler) {
                switchToMainConsumer();
            }
        });
        handler.initWhenReady(mMainThreadExecutor);

        Runnable startActivity = () -> ActivityManagerWrapper.getInstance()
                .startRecentsActivity(mHomeIntent,
                new AssistDataReceiver() {
                    @Override
                    public void onHandleAssistData(Bundle bundle) {
@@ -276,18 +294,19 @@ public class OtherActivityTouchConsumer extends ContextWrapper implements TouchC
                        }
                    }
                }, null, null);
        });

        // Preload the plan
        mRecentsModel.loadTasks(mRunningTask.id, null);
        mInteractionHandler = handler;
        handler.setGestureEndCallback(this::onFinish);
        handler.setLauncherOnDrawCallback(() -> {
            if (handler == mInteractionHandler) {
                switchToMainConsumer();
        if (Looper.myLooper() != Looper.getMainLooper()) {
            startActivity.run();
            try {
                drawWaitLock.await(LAUNCHER_DRAW_TIMEOUT_MS, TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                // We have waited long enough for launcher to draw
            }
        } else {
            // We should almost always get touch-town on background thread. This is an edge case
            // when the background Choreographer has not yet initialized.
            BackgroundExecutor.get().submit(startActivity);
        }
        });
        mMainThreadExecutor.execute(handler::initWhenReady);
    }

    /**
@@ -360,4 +379,14 @@ public class OtherActivityTouchConsumer extends ContextWrapper implements TouchC
    public void onTouchTrackingComplete() { }

    public void switchToMainConsumer() { }

    @Override
    public void preProcessMotionEvent(MotionEvent ev) {
        if (mVelocityTracker != null) {
           mVelocityTracker.addMovement(ev);
           if (ev.getActionMasked() == ACTION_POINTER_UP) {
               mVelocityTracker.clear();
           }
        }
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -55,4 +55,10 @@ public interface TouchConsumer extends Consumer<MotionEvent> {
    default void onQuickScrubEnd() { }

    default void onQuickScrubProgress(float progress) { }

    /**
     * Called on the binder thread to allow the consumer to process the motion event before it is
     * posted on a handler thread.
     */
    default void preProcessMotionEvent(MotionEvent ev) { }
}
+1 −0
Original line number Diff line number Diff line
@@ -178,6 +178,7 @@ public class TouchInteractionService extends Service {
            mEventQueue.setInterimChoreographer(mCurrentConsumer.shouldUseBackgroundConsumer()
                    ? mBackgroundThreadChoreographer : null);
        }
        mCurrentConsumer.preProcessMotionEvent(ev);
        mEventQueue.queue(ev);
    }

+14 −5
Original line number Diff line number Diff line
@@ -119,6 +119,7 @@ public class WindowTransformSwipeHandler extends BaseSwipeInteractionHandler {
    private boolean mWasLauncherAlreadyVisible;

    private float mCurrentDisplacement;
    private boolean mGestureStarted;

    private @InteractionType int mInteractionType = INTERACTION_NORMAL;
    private boolean mStartedQuickScrubFromHome;
@@ -216,6 +217,7 @@ public class WindowTransformSwipeHandler extends BaseSwipeInteractionHandler {
            long accuracy = 2 * Math.max(dp.widthPx, dp.heightPx);
            mLauncherTransitionController = launcher.getStateManager()
                    .createAnimationToNewWorkspace(OVERVIEW, accuracy);
            mLauncherTransitionController.dispatchOnStart();
            mLauncherTransitionController.setPlayFraction(mCurrentShift.value);

            state = STATE_ACTIVITY_MULTIPLIER_COMPLETE | STATE_LAUNCHER_DRAWN
@@ -273,10 +275,15 @@ public class WindowTransformSwipeHandler extends BaseSwipeInteractionHandler {
    private void launcherFrameDrawn() {
        View rootView = mLauncher.getRootView();
        if (rootView.getAlpha() < 1) {
            if (mGestureStarted) {
                final MultiStateCallback callback = mStateCallback;
                rootView.animate().alpha(1)
                        .setDuration(getFadeInDuration())
                        .withEndAction(() -> callback.setState(STATE_ACTIVITY_MULTIPLIER_COMPLETE));
            } else {
                rootView.setAlpha(1);
                mStateCallback.setState(STATE_ACTIVITY_MULTIPLIER_COMPLETE);
            }
        }
        mLauncherLayoutListener.setHandler(this);
        onLauncherLayoutChanged();
@@ -416,7 +423,9 @@ public class WindowTransformSwipeHandler extends BaseSwipeInteractionHandler {
        setStateOnUiThread(STATE_APP_CONTROLLER_RECEIVED);
    }

    public void onGestureStarted() { }
    public void onGestureStarted() {
        mGestureStarted = true;
    }

    @WorkerThread
    public void onGestureEnded(float endVelocity) {
+1 −1
Original line number Diff line number Diff line
@@ -1256,7 +1256,7 @@ public class Launcher extends BaseActivity
                && AbstractFloatingView.getTopOpenView(this) == null;
        boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
        boolean internalStateHandled = InternalStateHandler
                .handleNewIntent(this, intent, alreadyOnHome);
                .handleNewIntent(this, intent, isStarted());

        if (isActionMain) {
            if (!internalStateHandled) {
Loading