From 8463dc47eba11cf248cc979112994eb28b6c5c42 Mon Sep 17 00:00:00 2001 From: Schneider Victor-Tulias Date: Tue, 22 Oct 2024 15:29:59 -0400 Subject: [PATCH] Generalize ActivityTracker to support RecentsWindowManager Continued quick switch couldn't work using existing infrastructure until RecentsWindowManager could be properly tracked by ActivityTracker. Refactored ActivityTracker into ContextTracker to support this. Also refactored ActivityInitListener into ContextInitListener to fit the updated pattern. Flag: com.android.launcher3.enable_fallback_overview_in_window Fixes: 366023051 Test: RecentsWindowSwipeHandlerTestCase; Attempted continued quick switch from home and launched app Change-Id: Ic38ebf3611ef22fbfd1ddeb79d72d8a3621940a0 --- .../launcher3/LauncherInitListener.java | 8 +- .../DesktopVisibilityController.java | 4 +- .../android/quickstep/AbsSwipeUpHandler.java | 30 ++-- .../quickstep/BaseContainerInterface.java | 4 +- .../quickstep/FallbackActivityInterface.java | 8 +- .../quickstep/FallbackWindowInterface.java | 10 +- .../quickstep/LauncherActivityInterface.java | 5 +- .../android/quickstep/RecentsActivity.java | 8 +- .../fallback/window/RecentsWindowManager.kt | 12 +- ...Listener.java => ContextInitListener.java} | 32 ++--- .../quickstep/AbsSwipeUpHandlerTestCase.java | 14 +- .../quickstep/FallbackRecentsTest.java | 6 +- src/com/android/launcher3/Launcher.java | 7 +- .../dragndrop/BaseItemDragListener.java | 8 +- .../launcher3/model/ItemInstallQueue.java | 2 +- .../launcher3/states/RotationHelper.java | 3 +- .../testing/TestInformationHandler.java | 10 +- .../launcher3/util/ActivityTracker.java | 117 ---------------- .../launcher3/util/ContextTracker.java | 129 ++++++++++++++++++ .../launcher3/ui/AbstractLauncherUiTest.java | 4 +- .../ui/widget/TaplAddConfigWidgetTest.java | 2 +- .../ui/widget/TaplRequestPinItemTest.java | 2 +- 22 files changed, 222 insertions(+), 203 deletions(-) rename quickstep/src/com/android/quickstep/util/{ActivityInitListener.java => ContextInitListener.java} (63%) delete mode 100644 src/com/android/launcher3/util/ActivityTracker.java create mode 100644 src/com/android/launcher3/util/ContextTracker.java diff --git a/quickstep/src/com/android/launcher3/LauncherInitListener.java b/quickstep/src/com/android/launcher3/LauncherInitListener.java index 523923df6a..4e4ffe7799 100644 --- a/quickstep/src/com/android/launcher3/LauncherInitListener.java +++ b/quickstep/src/com/android/launcher3/LauncherInitListener.java @@ -15,11 +15,11 @@ */ package com.android.launcher3; -import com.android.quickstep.util.ActivityInitListener; +import com.android.quickstep.util.ContextInitListener; import java.util.function.BiPredicate; -public class LauncherInitListener extends ActivityInitListener { +public class LauncherInitListener extends ContextInitListener { /** * @param onInitListener a callback made when the activity is initialized. The callback should @@ -31,8 +31,8 @@ public class LauncherInitListener extends ActivityInitListener { } @Override - public boolean handleInit(Launcher launcher, boolean alreadyOnHome) { + public boolean handleInit(Launcher launcher, boolean isHomeStarted) { launcher.deferOverlayCallbacksUntilNextResumeOrStop(); - return super.handleInit(launcher, alreadyOnHome); + return super.handleInit(launcher, isHomeStarted); } } diff --git a/quickstep/src/com/android/launcher3/statehandlers/DesktopVisibilityController.java b/quickstep/src/com/android/launcher3/statehandlers/DesktopVisibilityController.java index 2ac87ffee4..57444648ea 100644 --- a/quickstep/src/com/android/launcher3/statehandlers/DesktopVisibilityController.java +++ b/quickstep/src/com/android/launcher3/statehandlers/DesktopVisibilityController.java @@ -384,7 +384,7 @@ public class DesktopVisibilityController { Log.d(TAG, "markLauncherPaused " + Debug.getCaller()); } StatefulActivity activity = - QuickstepLauncher.ACTIVITY_TRACKER.getCreatedActivity(); + QuickstepLauncher.ACTIVITY_TRACKER.getCreatedContext(); if (activity != null) { activity.setPaused(); } @@ -404,7 +404,7 @@ public class DesktopVisibilityController { Log.d(TAG, "markLauncherResumed " + Debug.getCaller()); } StatefulActivity activity = - QuickstepLauncher.ACTIVITY_TRACKER.getCreatedActivity(); + QuickstepLauncher.ACTIVITY_TRACKER.getCreatedContext(); // Check activity state before calling setResumed(). Launcher may have been actually // paused (eg fullscreen task moved to front). // In this case we should not mark the activity as resumed. diff --git a/quickstep/src/com/android/quickstep/AbsSwipeUpHandler.java b/quickstep/src/com/android/quickstep/AbsSwipeUpHandler.java index fbb2c06753..97d71799fd 100644 --- a/quickstep/src/com/android/quickstep/AbsSwipeUpHandler.java +++ b/quickstep/src/com/android/quickstep/AbsSwipeUpHandler.java @@ -100,7 +100,6 @@ import com.android.internal.jank.Cuj; import com.android.internal.util.LatencyTracker; import com.android.launcher3.AbstractFloatingView; import com.android.launcher3.DeviceProfile; -import com.android.launcher3.Flags; import com.android.launcher3.R; import com.android.launcher3.Utilities; import com.android.launcher3.anim.AnimationSuccessListener; @@ -125,8 +124,8 @@ import com.android.quickstep.fallback.window.RecentsWindowManager; import com.android.quickstep.util.ActiveGestureErrorDetector; import com.android.quickstep.util.ActiveGestureLog; import com.android.quickstep.util.ActiveGestureProtoLogProxy; -import com.android.quickstep.util.ActivityInitListener; import com.android.quickstep.util.AnimatorControllerWithResistance; +import com.android.quickstep.util.ContextInitListener; import com.android.quickstep.util.InputConsumerProxy; import com.android.quickstep.util.InputProxyHandlerFactory; import com.android.quickstep.util.MotionPauseDetector; @@ -156,8 +155,6 @@ import com.android.wm.shell.shared.TransactionPool; import com.android.wm.shell.shared.desktopmode.DesktopModeStatus; import com.android.wm.shell.shared.startingsurface.SplashScreenExitAnimationUtils; -import kotlin.Unit; - import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -167,6 +164,8 @@ import java.util.Optional; import java.util.OptionalInt; import java.util.function.Consumer; +import kotlin.Unit; + /** * Handles the navigation gestures when Launcher is the default home activity. */ @@ -184,7 +183,7 @@ public abstract class AbsSwipeUpHandler< protected final BaseContainerInterface mContainerInterface; protected final InputConsumerProxy mInputConsumerProxy; - protected final ActivityInitListener mActivityInitListener; + protected final ContextInitListener mContextInitListener; // Callbacks to be made once the recents animation starts private final ArrayList mRecentsAnimationStartCallbacks = new ArrayList<>(); private final OnScrollChangedListener mOnRecentsScrollListener = this::onRecentsViewScroll; @@ -357,10 +356,7 @@ public abstract class AbsSwipeUpHandler< InputConsumerController inputConsumer, RecentsWindowManager recentsWindowManager) { super(context, deviceState, gestureState); mContainerInterface = gestureState.getContainerInterface(); - if (recentsWindowManager != null && Flags.enableFallbackOverviewInWindow()) { - recentsWindowManager.registerInitListener(this::onActivityInit); - } - mActivityInitListener = + mContextInitListener = mContainerInterface.createActivityInitListener(this::onActivityInit); mInputConsumerProxy = new InputConsumerProxy(context, /* rotationSupplier = */ () -> { @@ -482,7 +478,7 @@ public abstract class AbsSwipeUpHandler< this::resetStateForAnimationCancel); } - protected boolean onActivityInit(Boolean alreadyOnHome) { + protected boolean onActivityInit(Boolean isHomeStarted) { if (mStateCallback.hasStates(STATE_HANDLER_INVALIDATED)) { return false; } @@ -510,11 +506,11 @@ public abstract class AbsSwipeUpHandler< initStateCallbacks(); mStateCallback.setState(oldState); } - mWasLauncherAlreadyVisible = alreadyOnHome; + mWasLauncherAlreadyVisible = isHomeStarted; mContainer = container; // Override the visibility of the activity until the gesture actually starts and we swipe // up, or until we transition home and the home animation is composed - if (alreadyOnHome) { + if (isHomeStarted) { mContainer.clearForceInvisibleFlag(STATE_HANDLER_INVISIBILITY_FLAGS); } else { mContainer.addForceInvisibleFlag(STATE_HANDLER_INVISIBILITY_FLAGS); @@ -524,7 +520,7 @@ public abstract class AbsSwipeUpHandler< mRecentsView.setOnPageTransitionEndCallback(null); mStateCallback.setState(STATE_LAUNCHER_PRESENT); - if (alreadyOnHome) { + if (isHomeStarted) { onLauncherStart(); } else { container.addEventCallback(EVENT_STARTED, mLauncherOnStartCallback); @@ -985,7 +981,7 @@ public abstract class AbsSwipeUpHandler< @Override public void onRecentsAnimationCanceled(HashMap thumbnailDatas) { ActiveGestureProtoLogProxy.logAbsSwipeUpHandlerOnRecentsAnimationCanceled(); - mActivityInitListener.unregister("AbsSwipeUpHandler.onRecentsAnimationCanceled"); + mContextInitListener.unregister("AbsSwipeUpHandler.onRecentsAnimationCanceled"); mStateCallback.setStateOnUiThread(STATE_GESTURE_CANCELLED | STATE_HANDLER_INVALIDATED); // Defer clearing the controller and the targets until after we've updated the state mRecentsAnimationController = null; @@ -1989,7 +1985,7 @@ public abstract class AbsSwipeUpHandler< // Cleanup when switching handlers mInputConsumerProxy.unregisterOnTouchDownCallback(); - mActivityInitListener.unregister("AbsSwipeUpHandler.cancelCurrentAnimation"); + mContextInitListener.unregister("AbsSwipeUpHandler.cancelCurrentAnimation"); TaskStackChangeListeners.getInstance().unregisterTaskStackListener( mActivityRestartListener); mTaskSnapshotCache.clear(); @@ -2007,7 +2003,7 @@ public abstract class AbsSwipeUpHandler< mGestureEndCallback.run(); } - mActivityInitListener.unregister("AbsSwipeUpHandler.invalidateHandler"); + mContextInitListener.unregister("AbsSwipeUpHandler.invalidateHandler"); TaskStackChangeListeners.getInstance().unregisterTaskStackListener( mActivityRestartListener); mTaskSnapshotCache.clear(); @@ -2516,7 +2512,7 @@ public abstract class AbsSwipeUpHandler< // Preload the plan RecentsModel.INSTANCE.get(mContext).getTasks(null); - mActivityInitListener.register(reasonString); + mContextInitListener.register(reasonString); } private boolean shouldFadeOutTargetsForKeyboardQuickSwitch( diff --git a/quickstep/src/com/android/quickstep/BaseContainerInterface.java b/quickstep/src/com/android/quickstep/BaseContainerInterface.java index 143ef1214c..a3953ca33b 100644 --- a/quickstep/src/com/android/quickstep/BaseContainerInterface.java +++ b/quickstep/src/com/android/quickstep/BaseContainerInterface.java @@ -47,8 +47,8 @@ import com.android.launcher3.util.DisplayController; import com.android.launcher3.util.WindowBounds; import com.android.launcher3.views.ScrimView; import com.android.quickstep.orientation.RecentsPagedOrientationHandler; -import com.android.quickstep.util.ActivityInitListener; import com.android.quickstep.util.AnimatorControllerWithResistance; +import com.android.quickstep.util.ContextInitListener; import com.android.quickstep.views.RecentsView; import com.android.quickstep.views.RecentsViewContainer; import com.android.systemui.shared.recents.model.ThumbnailData; @@ -134,7 +134,7 @@ public abstract class BaseContainerInterface callback); - public abstract ActivityInitListener createActivityInitListener( + public abstract ContextInitListener createActivityInitListener( Predicate onInitListener); /** * Returns the expected STATE_TYPE from the provided GestureEndTarget. diff --git a/quickstep/src/com/android/quickstep/FallbackActivityInterface.java b/quickstep/src/com/android/quickstep/FallbackActivityInterface.java index df83eb2fdb..b78739918b 100644 --- a/quickstep/src/com/android/quickstep/FallbackActivityInterface.java +++ b/quickstep/src/com/android/quickstep/FallbackActivityInterface.java @@ -36,8 +36,8 @@ import com.android.launcher3.util.DisplayController; import com.android.quickstep.GestureState.GestureEndTarget; import com.android.quickstep.fallback.RecentsState; import com.android.quickstep.orientation.RecentsPagedOrientationHandler; -import com.android.quickstep.util.ActivityInitListener; import com.android.quickstep.util.AnimatorControllerWithResistance; +import com.android.quickstep.util.ContextInitListener; import com.android.quickstep.views.RecentsView; import java.util.function.Consumer; @@ -88,16 +88,16 @@ public final class FallbackActivityInterface extends } @Override - public ActivityInitListener createActivityInitListener( + public ContextInitListener createActivityInitListener( Predicate onInitListener) { - return new ActivityInitListener<>((activity, alreadyOnHome) -> + return new ContextInitListener<>((activity, alreadyOnHome) -> onInitListener.test(alreadyOnHome), RecentsActivity.ACTIVITY_TRACKER); } @Nullable @Override public RecentsActivity getCreatedContainer() { - return RecentsActivity.ACTIVITY_TRACKER.getCreatedActivity(); + return RecentsActivity.ACTIVITY_TRACKER.getCreatedContext(); } @Override diff --git a/quickstep/src/com/android/quickstep/FallbackWindowInterface.java b/quickstep/src/com/android/quickstep/FallbackWindowInterface.java index ea478dd334..832c093855 100644 --- a/quickstep/src/com/android/quickstep/FallbackWindowInterface.java +++ b/quickstep/src/com/android/quickstep/FallbackWindowInterface.java @@ -37,8 +37,8 @@ import com.android.quickstep.GestureState.GestureEndTarget; import com.android.quickstep.fallback.RecentsState; import com.android.quickstep.fallback.window.RecentsWindowManager; import com.android.quickstep.orientation.RecentsPagedOrientationHandler; -import com.android.quickstep.util.ActivityInitListener; import com.android.quickstep.util.AnimatorControllerWithResistance; +import com.android.quickstep.util.ContextInitListener; import com.android.quickstep.views.RecentsView; import java.util.function.Consumer; @@ -111,11 +111,11 @@ public final class FallbackWindowInterface extends BaseWindowInterface{ } @Override - public ActivityInitListener createActivityInitListener( + public ContextInitListener createActivityInitListener( Predicate onInitListener) { - //todo figure out how to properly replace this - return new ActivityInitListener<>((activity, alreadyOnHome) -> - onInitListener.test(alreadyOnHome), RecentsActivity.ACTIVITY_TRACKER); + return new ContextInitListener<>( + (activity, alreadyOnHome) -> onInitListener.test(alreadyOnHome), + RecentsWindowManager.getRecentsWindowTracker()); } @Nullable diff --git a/quickstep/src/com/android/quickstep/LauncherActivityInterface.java b/quickstep/src/com/android/quickstep/LauncherActivityInterface.java index 85312e4988..ef6a09dba8 100644 --- a/quickstep/src/com/android/quickstep/LauncherActivityInterface.java +++ b/quickstep/src/com/android/quickstep/LauncherActivityInterface.java @@ -47,7 +47,6 @@ import com.android.launcher3.util.DisplayController; import com.android.launcher3.util.NavigationMode; import com.android.quickstep.GestureState.GestureEndTarget; import com.android.quickstep.orientation.RecentsPagedOrientationHandler; -import com.android.quickstep.util.ActivityInitListener; import com.android.quickstep.util.AnimatorControllerWithResistance; import com.android.quickstep.util.LayoutUtils; import com.android.quickstep.views.RecentsView; @@ -134,7 +133,7 @@ public final class LauncherActivityInterface extends } @Override - public ActivityInitListener createActivityInitListener(Predicate onInitListener) { + public LauncherInitListener createActivityInitListener(Predicate onInitListener) { return new LauncherInitListener((activity, alreadyOnHome) -> onInitListener.test(alreadyOnHome)); } @@ -151,7 +150,7 @@ public final class LauncherActivityInterface extends @Nullable @Override public QuickstepLauncher getCreatedContainer() { - return QuickstepLauncher.ACTIVITY_TRACKER.getCreatedActivity(); + return QuickstepLauncher.ACTIVITY_TRACKER.getCreatedContext(); } @Nullable diff --git a/quickstep/src/com/android/quickstep/RecentsActivity.java b/quickstep/src/com/android/quickstep/RecentsActivity.java index 9ac4141932..324ca1bdf0 100644 --- a/quickstep/src/com/android/quickstep/RecentsActivity.java +++ b/quickstep/src/com/android/quickstep/RecentsActivity.java @@ -71,7 +71,7 @@ import com.android.launcher3.statemanager.StatefulActivity; import com.android.launcher3.taskbar.FallbackTaskbarUIController; import com.android.launcher3.taskbar.TaskbarManager; import com.android.launcher3.util.ActivityOptionsWrapper; -import com.android.launcher3.util.ActivityTracker; +import com.android.launcher3.util.ContextTracker; import com.android.launcher3.util.RunnableList; import com.android.launcher3.util.SystemUiController; import com.android.launcher3.util.Themes; @@ -102,8 +102,8 @@ public final class RecentsActivity extends StatefulActivity implem RecentsViewContainer { private static final String TAG = "RecentsActivity"; - public static final ActivityTracker ACTIVITY_TRACKER = - new ActivityTracker<>(); + public static final ContextTracker.ActivityTracker ACTIVITY_TRACKER = + new ContextTracker.ActivityTracker<>(); private Handler mUiHandler = new Handler(Looper.getMainLooper()); @@ -421,7 +421,7 @@ public final class RecentsActivity extends StatefulActivity implem @Override protected void onDestroy() { super.onDestroy(); - ACTIVITY_TRACKER.onActivityDestroyed(this); + ACTIVITY_TRACKER.onContextDestroyed(this); mActivityLaunchAnimationRunner = null; mSplitSelectStateController.onDestroy(); mTISBindHelper.onDestroy(); diff --git a/quickstep/src/com/android/quickstep/fallback/window/RecentsWindowManager.kt b/quickstep/src/com/android/quickstep/fallback/window/RecentsWindowManager.kt index fbf671fbf7..3017df2481 100644 --- a/quickstep/src/com/android/quickstep/fallback/window/RecentsWindowManager.kt +++ b/quickstep/src/com/android/quickstep/fallback/window/RecentsWindowManager.kt @@ -39,6 +39,7 @@ import com.android.launcher3.statehandlers.DesktopVisibilityController import com.android.launcher3.statemanager.StateManager import com.android.launcher3.statemanager.StateManager.AtomicAnimationFactory import com.android.launcher3.statemanager.StatefulContainer +import com.android.launcher3.util.ContextTracker import com.android.launcher3.util.DisplayController import com.android.launcher3.util.RunnableList import com.android.launcher3.util.SystemUiController @@ -89,6 +90,14 @@ class RecentsWindowManager(context: Context) : companion object { private const val HOME_APPEAR_DURATION: Long = 250 private const val TAG = "RecentsWindowManager" + + class RecentsWindowTracker : ContextTracker() { + override fun isHomeStarted(context: RecentsWindowManager?): Boolean { + return true + } + } + + @JvmStatic val recentsWindowTracker = RecentsWindowTracker() } protected var recentsView: FallbackRecentsView? = null @@ -147,6 +156,7 @@ class RecentsWindowManager(context: Context) : FallbackWindowInterface.getInstance()?.destroy() TaskStackChangeListeners.getInstance().unregisterTaskStackListener(taskStackChangeListener) callbacks?.removeListener(recentsAnimationListener) + recentsWindowTracker.onContextDestroyed(this) } override fun startHome() { @@ -254,7 +264,7 @@ class RecentsWindowManager(context: Context) : actionsView?.updateVerticalMargin(DisplayController.getNavigationMode(this)) mSystemUiController = SystemUiController(windowView) - onInitListener?.test(true) + recentsWindowTracker.handleCreate(this) this.callbacks = callbacks callbacks?.addListener(recentsAnimationListener) diff --git a/quickstep/src/com/android/quickstep/util/ActivityInitListener.java b/quickstep/src/com/android/quickstep/util/ContextInitListener.java similarity index 63% rename from quickstep/src/com/android/quickstep/util/ActivityInitListener.java rename to quickstep/src/com/android/quickstep/util/ContextInitListener.java index 5efbb4074a..49f1463c9d 100644 --- a/quickstep/src/com/android/quickstep/util/ActivityInitListener.java +++ b/quickstep/src/com/android/quickstep/util/ContextInitListener.java @@ -15,17 +15,17 @@ */ package com.android.quickstep.util; -import com.android.launcher3.BaseActivity; -import com.android.launcher3.util.ActivityTracker; -import com.android.launcher3.util.ActivityTracker.SchedulerCallback; +import com.android.launcher3.util.ContextTracker; +import com.android.launcher3.util.ContextTracker.SchedulerCallback; +import com.android.launcher3.views.ActivityContext; import java.util.function.BiPredicate; -public class ActivityInitListener implements - SchedulerCallback { +public class ContextInitListener implements + SchedulerCallback { - private BiPredicate mOnInitListener; - private final ActivityTracker mActivityTracker; + private BiPredicate mOnInitListener; + private final ContextTracker mContextTracker; private boolean mIsRegistered = false; @@ -34,23 +34,23 @@ public class ActivityInitListener implements * return true to continue receiving callbacks (ie. for if the activity is * recreated). */ - public ActivityInitListener(BiPredicate onInitListener, - ActivityTracker tracker) { + public ContextInitListener(BiPredicate onInitListener, + ContextTracker tracker) { mOnInitListener = onInitListener; - mActivityTracker = tracker; + mContextTracker = tracker; } @Override - public final boolean init(T activity, boolean alreadyOnHome) { + public final boolean init(CONTEXT activity, boolean isHomeStarted) { if (!mIsRegistered) { // Don't receive any more updates return false; } - return handleInit(activity, alreadyOnHome); + return handleInit(activity, isHomeStarted); } - protected boolean handleInit(T activity, boolean alreadyOnHome) { - return mOnInitListener.test(activity, alreadyOnHome); + protected boolean handleInit(CONTEXT activity, boolean isHomeStarted) { + return mOnInitListener.test(activity, isHomeStarted); } /** @@ -59,14 +59,14 @@ public class ActivityInitListener implements */ public void register(String reasonString) { mIsRegistered = true; - mActivityTracker.registerCallback(this, reasonString); + mContextTracker.registerCallback(this, reasonString); } /** * After calling this, we won't {@link #init} even when the activity is ready. */ public void unregister(String reasonString) { - mActivityTracker.unregisterCallback(this, reasonString); + mContextTracker.unregisterCallback(this, reasonString); mIsRegistered = false; mOnInitListener = null; } diff --git a/quickstep/tests/multivalentTests/src/com/android/quickstep/AbsSwipeUpHandlerTestCase.java b/quickstep/tests/multivalentTests/src/com/android/quickstep/AbsSwipeUpHandlerTestCase.java index 0bf988678b..dc5223c2a7 100644 --- a/quickstep/tests/multivalentTests/src/com/android/quickstep/AbsSwipeUpHandlerTestCase.java +++ b/quickstep/tests/multivalentTests/src/com/android/quickstep/AbsSwipeUpHandlerTestCase.java @@ -55,7 +55,7 @@ import com.android.launcher3.statemanager.BaseState; import com.android.launcher3.statemanager.StatefulContainer; import com.android.launcher3.util.SystemUiController; import com.android.quickstep.fallback.window.RecentsWindowManager; -import com.android.quickstep.util.ActivityInitListener; +import com.android.quickstep.util.ContextInitListener; import com.android.quickstep.views.RecentsView; import com.android.quickstep.views.RecentsViewContainer; import com.android.systemui.shared.system.InputConsumerController; @@ -116,7 +116,7 @@ public abstract class AbsSwipeUpHandlerTestCase< protected TaskAnimationManager mTaskAnimationManager; @Mock protected CONTAINER_INTERFACE mActivityInterface; - @Mock protected ActivityInitListener mActivityInitListener; + @Mock protected ContextInitListener mContextInitListener; @Mock protected RecentsAnimationController mRecentsAnimationController; @Mock protected STATE_TYPE mState; @Mock protected ViewTreeObserver mViewTreeObserver; @@ -168,7 +168,7 @@ public abstract class AbsSwipeUpHandlerTestCase< when(recentsContainer.getRootView()).thenReturn(mRootView); when(recentsContainer.getSystemUiController()).thenReturn(mSystemUiController); when(mActivityInterface.createActivityInitListener(any())) - .thenReturn(mActivityInitListener); + .thenReturn(mContextInitListener); doReturn(recentsContainer).when(mActivityInterface).getCreatedContainer(); doAnswer(answer -> { answer.getArgument(0).run(); @@ -181,7 +181,7 @@ public abstract class AbsSwipeUpHandlerTestCase< String reasonString = "because i said so"; createSwipeHandler().initWhenReady(reasonString); - verify(mActivityInitListener).register(eq(reasonString)); + verify(mContextInitListener).register(eq(reasonString)); } @Test @@ -189,7 +189,7 @@ public abstract class AbsSwipeUpHandlerTestCase< createSwipeHandler() .onRecentsAnimationCanceled(new HashMap<>()); - runOnMainSync(() -> verify(mActivityInitListener) + runOnMainSync(() -> verify(mContextInitListener) .unregister(eq("AbsSwipeUpHandler.onRecentsAnimationCanceled"))); } @@ -197,7 +197,7 @@ public abstract class AbsSwipeUpHandlerTestCase< public void testOnConsumerAboutToBeSwitched_unregistersActivityInitListener() { createSwipeHandler().onConsumerAboutToBeSwitched(); - runOnMainSync(() -> verify(mActivityInitListener) + runOnMainSync(() -> verify(mContextInitListener) .unregister("AbsSwipeUpHandler.invalidateHandler")); } @@ -206,7 +206,7 @@ public abstract class AbsSwipeUpHandlerTestCase< createSwipeUpHandlerForGesture(GestureState.GestureEndTarget.NEW_TASK) .onConsumerAboutToBeSwitched(); - runOnMainSync(() -> verify(mActivityInitListener) + runOnMainSync(() -> verify(mContextInitListener) .unregister(eq("AbsSwipeUpHandler.cancelCurrentAnimation"))); } diff --git a/quickstep/tests/src/com/android/quickstep/FallbackRecentsTest.java b/quickstep/tests/src/com/android/quickstep/FallbackRecentsTest.java index 28589291fd..1f11c14837 100644 --- a/quickstep/tests/src/com/android/quickstep/FallbackRecentsTest.java +++ b/quickstep/tests/src/com/android/quickstep/FallbackRecentsTest.java @@ -142,7 +142,7 @@ public class FallbackRecentsTest { }; final ViewCaptureRule viewCaptureRule = new ViewCaptureRule( - RecentsActivity.ACTIVITY_TRACKER::getCreatedActivity); + RecentsActivity.ACTIVITY_TRACKER::getCreatedContext); mOrderSensitiveRules = RuleChain .outerRule(new SamplerRule()) .around(new TestStabilityRule()) @@ -208,7 +208,7 @@ public class FallbackRecentsTest { if (!TestHelpers.isInLauncherProcess()) return null; Object[] result = new Object[1]; Wait.atMost("Failed to get from recents", () -> MAIN_EXECUTOR.submit(() -> { - RecentsActivity activity = RecentsActivity.ACTIVITY_TRACKER.getCreatedActivity(); + RecentsActivity activity = RecentsActivity.ACTIVITY_TRACKER.getCreatedContext(); if (activity == null) { return false; } @@ -231,7 +231,7 @@ public class FallbackRecentsTest { private void waitForRecentsActivityStop() { try { final boolean recentsActivityIsNull = MAIN_EXECUTOR.submit( - () -> RecentsActivity.ACTIVITY_TRACKER.getCreatedActivity() == null).get(); + () -> RecentsActivity.ACTIVITY_TRACKER.getCreatedContext() == null).get(); if (recentsActivityIsNull) { // Null activity counts as a "stopped" one. return; diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index a8840fe8f9..8547eb4bf0 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -224,10 +224,10 @@ import com.android.launcher3.testing.shared.TestProtocol; import com.android.launcher3.touch.AllAppsSwipeController; import com.android.launcher3.touch.ItemLongClickListener; import com.android.launcher3.util.ActivityResultInfo; -import com.android.launcher3.util.ActivityTracker; import com.android.launcher3.util.BackPressHandler; import com.android.launcher3.util.CannedAnimationCoordinator; import com.android.launcher3.util.ComponentKey; +import com.android.launcher3.util.ContextTracker; import com.android.launcher3.util.IntArray; import com.android.launcher3.util.IntSet; import com.android.launcher3.util.ItemInflater; @@ -292,7 +292,8 @@ public class Launcher extends StatefulActivity PluginListener { public static final String TAG = "Launcher"; - public static final ActivityTracker ACTIVITY_TRACKER = new ActivityTracker<>(); + public static final ContextTracker.ActivityTracker ACTIVITY_TRACKER = + new ContextTracker.ActivityTracker<>(); static final boolean LOGD = false; @@ -1778,7 +1779,7 @@ public class Launcher extends StatefulActivity @Override public void onDestroy() { super.onDestroy(); - ACTIVITY_TRACKER.onActivityDestroyed(this); + ACTIVITY_TRACKER.onContextDestroyed(this); SettingsCache.INSTANCE.get(this).unregister(TOUCHPAD_NATURAL_SCROLLING, mNaturalScrollingChangedListener); diff --git a/src/com/android/launcher3/dragndrop/BaseItemDragListener.java b/src/com/android/launcher3/dragndrop/BaseItemDragListener.java index 981e3a65a4..43c148a762 100644 --- a/src/com/android/launcher3/dragndrop/BaseItemDragListener.java +++ b/src/com/android/launcher3/dragndrop/BaseItemDragListener.java @@ -34,7 +34,7 @@ import com.android.launcher3.AbstractFloatingView; import com.android.launcher3.DragSource; import com.android.launcher3.DropTarget.DragObject; import com.android.launcher3.Launcher; -import com.android.launcher3.util.ActivityTracker.SchedulerCallback; +import com.android.launcher3.util.ContextTracker.SchedulerCallback; import com.android.launcher3.widget.PendingItemDragHelper; import java.util.UUID; @@ -74,9 +74,9 @@ public abstract class BaseItemDragListener implements View.OnDragListener, DragS } @Override - public boolean init(Launcher launcher, boolean alreadyOnHome) { - AbstractFloatingView.closeAllOpenViews(launcher, alreadyOnHome); - launcher.getStateManager().goToState(NORMAL, alreadyOnHome /* animated */); + public boolean init(Launcher launcher, boolean isHomeStarted) { + AbstractFloatingView.closeAllOpenViews(launcher, /* animate= */ isHomeStarted); + launcher.getStateManager().goToState(NORMAL, /* animated= */ isHomeStarted); launcher.getDragLayer().setOnDragListener(this); launcher.getRotationHelper().setStateHandlerRequest(REQUEST_LOCK); diff --git a/src/com/android/launcher3/model/ItemInstallQueue.java b/src/com/android/launcher3/model/ItemInstallQueue.java index 59d1d00204..49f75eb167 100644 --- a/src/com/android/launcher3/model/ItemInstallQueue.java +++ b/src/com/android/launcher3/model/ItemInstallQueue.java @@ -121,7 +121,7 @@ public class ItemInstallQueue implements SafeCloseable { @WorkerThread private void flushQueueInBackground() { - Launcher launcher = Launcher.ACTIVITY_TRACKER.getCreatedActivity(); + Launcher launcher = Launcher.ACTIVITY_TRACKER.getCreatedContext(); if (launcher == null) { // Launcher not loaded return; diff --git a/src/com/android/launcher3/states/RotationHelper.java b/src/com/android/launcher3/states/RotationHelper.java index b3bcada148..4c9da5d457 100644 --- a/src/com/android/launcher3/states/RotationHelper.java +++ b/src/com/android/launcher3/states/RotationHelper.java @@ -36,6 +36,7 @@ import com.android.launcher3.BaseActivity; import com.android.launcher3.DeviceProfile; import com.android.launcher3.LauncherPrefChangeListener; import com.android.launcher3.LauncherPrefs; +import com.android.launcher3.util.ContextTracker; import com.android.launcher3.util.DisplayController; /** @@ -72,7 +73,7 @@ public class RotationHelper implements LauncherPrefChangeListener, /** * Rotation request made by - * {@link com.android.launcher3.util.ActivityTracker.SchedulerCallback}. + * {@link ContextTracker.SchedulerCallback}. * This supersedes any other request. */ private int mStateHandlerRequest = REQUEST_NONE; diff --git a/src/com/android/launcher3/testing/TestInformationHandler.java b/src/com/android/launcher3/testing/TestInformationHandler.java index 3a93981f0a..aa3f2f291a 100644 --- a/src/com/android/launcher3/testing/TestInformationHandler.java +++ b/src/com/android/launcher3/testing/TestInformationHandler.java @@ -246,12 +246,12 @@ public class TestInformationHandler implements ResourceBasedOverride { case TestProtocol.REQUEST_GET_SPLIT_SELECTION_ACTIVE: response.putBoolean(TEST_INFO_RESPONSE_FIELD, enableSplitContextually() - && Launcher.ACTIVITY_TRACKER.getCreatedActivity().isSplitSelectionActive()); + && Launcher.ACTIVITY_TRACKER.getCreatedContext().isSplitSelectionActive()); return response; case TestProtocol.REQUEST_ENABLE_ROTATION: MAIN_EXECUTOR.submit(() -> - Launcher.ACTIVITY_TRACKER.getCreatedActivity().getRotationHelper() + Launcher.ACTIVITY_TRACKER.getCreatedContext().getRotationHelper() .forceAllowRotationForTesting(Boolean.parseBoolean(arg))); return response; @@ -475,12 +475,12 @@ public class TestInformationHandler implements ResourceBasedOverride { } protected boolean isLauncherInitialized() { - return Launcher.ACTIVITY_TRACKER.getCreatedActivity() == null + return Launcher.ACTIVITY_TRACKER.getCreatedContext() == null || LauncherAppState.getInstance(mContext).getModel().isModelLoaded(); } protected WindowInsets getWindowInsets(){ - return Launcher.ACTIVITY_TRACKER.getCreatedActivity().getWindow().getDecorView() + return Launcher.ACTIVITY_TRACKER.getCreatedContext().getWindow().getDecorView() .getRootWindowInsets(); } @@ -489,7 +489,7 @@ public class TestInformationHandler implements ResourceBasedOverride { */ public static Bundle getLauncherUIProperty( BundleSetter bundleSetter, Function provider) { - return getUIProperty(bundleSetter, provider, Launcher.ACTIVITY_TRACKER::getCreatedActivity); + return getUIProperty(bundleSetter, provider, Launcher.ACTIVITY_TRACKER::getCreatedContext); } /** diff --git a/src/com/android/launcher3/util/ActivityTracker.java b/src/com/android/launcher3/util/ActivityTracker.java deleted file mode 100644 index b2d0d75dc4..0000000000 --- a/src/com/android/launcher3/util/ActivityTracker.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.android.launcher3.util; - -import android.util.Log; - -import androidx.annotation.Nullable; - -import com.android.launcher3.BaseActivity; - -import java.io.PrintWriter; -import java.lang.ref.WeakReference; -import java.util.concurrent.CopyOnWriteArrayList; - -/** - * Helper class to statically track activity creation - * @param The activity type to track - */ -public final class ActivityTracker { - - private static final String TAG = "ActivityTracker"; - - private WeakReference mCurrentActivity = new WeakReference<>(null); - private CopyOnWriteArrayList> mCallbacks = new CopyOnWriteArrayList<>(); - - @Nullable - public R getCreatedActivity() { - return (R) mCurrentActivity.get(); - } - - public void onActivityDestroyed(T activity) { - if (mCurrentActivity.get() == activity) { - mCurrentActivity.clear(); - } - } - - /** - * Call {@link SchedulerCallback#init(BaseActivity, boolean)} when the - * activity is ready. If the activity is already created, this is called immediately. - * - * The tracker maintains a strong ref to the callback, so it is up to the caller to return - * {@code false} in the callback OR to unregister the callback explicitly. - * - * @param callback The callback to call init() on when the activity is ready. - */ - public void registerCallback(SchedulerCallback callback, String reasonString) { - Log.d(TAG, "Registering callback: " + callback + ", reason=" + reasonString); - T activity = mCurrentActivity.get(); - mCallbacks.add(callback); - if (activity != null) { - if (!callback.init(activity, activity.isStarted())) { - unregisterCallback(callback, "ActivityTracker.registerCallback: Intent handled"); - } - } - } - - /** - * Unregisters a registered callback. - */ - public void unregisterCallback(SchedulerCallback callback, String reasonString) { - Log.d(TAG, "Unregistering callback: " + callback + ", reason=" + reasonString); - mCallbacks.remove(callback); - } - - public boolean handleCreate(T activity) { - mCurrentActivity = new WeakReference<>(activity); - return handleIntent(activity, false /* alreadyOnHome */); - } - - public boolean handleNewIntent(T activity) { - return handleIntent(activity, activity.isStarted()); - } - - private boolean handleIntent(T activity, boolean alreadyOnHome) { - boolean handled = false; - if (!mCallbacks.isEmpty()) { - Log.d(TAG, "handleIntent: mCallbacks=" + mCallbacks); - } - for (SchedulerCallback cb : mCallbacks) { - if (!cb.init(activity, alreadyOnHome)) { - // Callback doesn't want any more updates - unregisterCallback(cb, "ActivityTracker.handleIntent: Intent handled"); - } - handled = true; - } - return handled; - } - - public void dump(String prefix, PrintWriter writer) { - writer.println(prefix + "ActivityTracker:"); - writer.println(prefix + "\tmCurrentActivity=" + mCurrentActivity.get()); - writer.println(prefix + "\tmCallbacks=" + mCallbacks); - } - - public interface SchedulerCallback { - - /** - * Called when the activity is ready. - * @param alreadyOnHome Whether the activity is already started. - * @return Whether to continue receiving callbacks (i.e. if the activity is recreated). - */ - boolean init(T activity, boolean alreadyOnHome); - } -} diff --git a/src/com/android/launcher3/util/ContextTracker.java b/src/com/android/launcher3/util/ContextTracker.java new file mode 100644 index 0000000000..c729b4b625 --- /dev/null +++ b/src/com/android/launcher3/util/ContextTracker.java @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.launcher3.util; + +import android.util.Log; + +import androidx.annotation.Nullable; + +import com.android.launcher3.BaseActivity; +import com.android.launcher3.views.ActivityContext; + +import java.io.PrintWriter; +import java.lang.ref.WeakReference; +import java.util.concurrent.CopyOnWriteArrayList; + +/** + * Helper class to statically track activity creation + * @param The context type to track + */ +public abstract class ContextTracker { + + private static final String TAG = "ContextTracker"; + + private WeakReference mCurrentContext = new WeakReference<>(null); + private CopyOnWriteArrayList> mCallbacks = + new CopyOnWriteArrayList<>(); + + @Nullable + public R getCreatedContext() { + return (R) mCurrentContext.get(); + } + + public void onContextDestroyed(CONTEXT context) { + if (mCurrentContext.get() == context) { + mCurrentContext.clear(); + } + } + + public abstract boolean isHomeStarted(CONTEXT context); + + /** + * Call {@link SchedulerCallback#init(ActivityContext, boolean)} when the + * context is ready. If the context is already created, this is called immediately. + * + * The tracker maintains a strong ref to the callback, so it is up to the caller to return + * {@code false} in the callback OR to unregister the callback explicitly. + * + * @param callback The callback to call init() on when the context is ready. + */ + public void registerCallback(SchedulerCallback callback, String reasonString) { + Log.d(TAG, "Registering callback: " + callback + ", reason=" + reasonString); + CONTEXT context = mCurrentContext.get(); + mCallbacks.add(callback); + if (context != null) { + if (!callback.init(context, isHomeStarted(context))) { + unregisterCallback(callback, "ContextTracker.registerCallback: Intent handled"); + } + } + } + + /** + * Unregisters a registered callback. + */ + public void unregisterCallback(SchedulerCallback callback, String reasonString) { + Log.d(TAG, "Unregistering callback: " + callback + ", reason=" + reasonString); + mCallbacks.remove(callback); + } + + public boolean handleCreate(CONTEXT context) { + mCurrentContext = new WeakReference<>(context); + return handleCreate(context, /* alreadyOnHome= */ false); + } + + public boolean handleNewIntent(CONTEXT context) { + return handleCreate(context, isHomeStarted(context)); + } + + private boolean handleCreate(CONTEXT context, boolean isHomeStarted) { + boolean handled = false; + if (!mCallbacks.isEmpty()) { + Log.d(TAG, "handleIntent: mCallbacks=" + mCallbacks); + } + for (SchedulerCallback cb : mCallbacks) { + if (!cb.init(context, isHomeStarted)) { + // Callback doesn't want any more updates + unregisterCallback(cb, "ContextTracker.handleIntent: Intent handled"); + } + handled = true; + } + return handled; + } + + public void dump(String prefix, PrintWriter writer) { + writer.println(prefix + "ContextTracker:"); + writer.println(prefix + "\tmCurrentContext=" + mCurrentContext.get()); + writer.println(prefix + "\tmCallbacks=" + mCallbacks); + } + + public interface SchedulerCallback { + + /** + * Called when the context is ready. + * @param isHomeStarted Whether the home activity is already started. + * @return Whether to continue receiving callbacks (i.e. if the context is recreated). + */ + boolean init(T context, boolean isHomeStarted); + } + + public static final class ActivityTracker extends ContextTracker { + + @Override + public boolean isHomeStarted(T context) { + return context.isStarted(); + } + } +} diff --git a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java index 4ca6da8a47..206647a19c 100644 --- a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java +++ b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java @@ -257,7 +257,7 @@ public abstract class AbstractLauncherUiTest { protected TestRule getRulesInsideActivityMonitor() { final ViewCaptureRule viewCaptureRule = new ViewCaptureRule( - Launcher.ACTIVITY_TRACKER::getCreatedActivity); + Launcher.ACTIVITY_TRACKER::getCreatedContext); final RuleChain inner = RuleChain .outerRule(new PortraitLandscapeRunner(this)) .around(new FailureWatcher(mLauncher, viewCaptureRule::getViewCaptureData)) @@ -456,7 +456,7 @@ public abstract class AbstractLauncherUiTest { protected T getFromLauncher(Function f) { if (!TestHelpers.isInLauncherProcess()) return null; - return getOnUiThread(() -> f.apply(Launcher.ACTIVITY_TRACKER.getCreatedActivity())); + return getOnUiThread(() -> f.apply(Launcher.ACTIVITY_TRACKER.getCreatedContext())); } protected void executeOnLauncher(Consumer f) { diff --git a/tests/src/com/android/launcher3/ui/widget/TaplAddConfigWidgetTest.java b/tests/src/com/android/launcher3/ui/widget/TaplAddConfigWidgetTest.java index 961e7fc5e6..7ff4f22e3d 100644 --- a/tests/src/com/android/launcher3/ui/widget/TaplAddConfigWidgetTest.java +++ b/tests/src/com/android/launcher3/ui/widget/TaplAddConfigWidgetTest.java @@ -136,7 +136,7 @@ public class TaplAddConfigWidgetTest extends AbstractLauncherUiTest { @Override public boolean isTrue() throws Throwable { return mMainThreadExecutor.submit(() -> { - Launcher l = Launcher.ACTIVITY_TRACKER.getCreatedActivity(); + Launcher l = Launcher.ACTIVITY_TRACKER.getCreatedContext(); return l != null && l.getWorkspace().getFirstMatch(this) != null; }).get(); } diff --git a/tests/src/com/android/launcher3/ui/widget/TaplRequestPinItemTest.java b/tests/src/com/android/launcher3/ui/widget/TaplRequestPinItemTest.java index 74047f0d01..35c7cabb61 100644 --- a/tests/src/com/android/launcher3/ui/widget/TaplRequestPinItemTest.java +++ b/tests/src/com/android/launcher3/ui/widget/TaplRequestPinItemTest.java @@ -187,7 +187,7 @@ public class TaplRequestPinItemTest extends AbstractLauncherUiTest { @Override public boolean isTrue() throws Throwable { return mMainThreadExecutor.submit(() -> { - Launcher l = Launcher.ACTIVITY_TRACKER.getCreatedActivity(); + Launcher l = Launcher.ACTIVITY_TRACKER.getCreatedContext(); return l != null && l.getWorkspace().getFirstMatch(mOp) != null; }).get(); }