diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.kt b/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.kt new file mode 100644 index 0000000000..6506c99fa4 --- /dev/null +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.kt @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2025 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.taskbar + +import android.app.PendingIntent +import com.android.launcher3.anim.AnimatorPlaybackController +import com.android.launcher3.statemanager.StatefulActivity +import com.android.quickstep.SystemDecorationChangeObserver +import com.android.quickstep.views.RecentsViewContainer +import com.android.systemui.shared.statusbar.phone.BarTransitions +import com.android.systemui.shared.system.QuickStepContract.SystemUiStateFlags +import java.io.PrintWriter + +interface TaskbarManager : SystemDecorationChangeObserver.DisplayDecorationListener { + + fun createLauncherStartFromSuwAnim(duration: Int): AnimatorPlaybackController? + + fun onUserUnlocked() + + fun setActivity(activity: StatefulActivity<*>) + + fun setRecentsViewContainer(recentsViewContainer: RecentsViewContainer) + + fun recreateTaskbars() + + fun onSystemUiFlagsChanged(@SystemUiStateFlags systemUiStateFlags: Long, displayId: Int) + + fun onLongPressHomeEnabled(assistantLongPressEnabled: Boolean) + + fun setSetupUIVisible(isVisible: Boolean) + + fun setWallpaperVisible(displayId: Int, isVisible: Boolean) + + fun checkNavBarModes(displayId: Int) + + fun finishBarAnimations(displayId: Int) + + fun touchAutoDim(displayId: Int, reset: Boolean) + + fun transitionTo(displayId: Int, @BarTransitions.TransitionMode barMode: Int, animate: Boolean) + + fun appTransitionPending(pending: Boolean) + + fun onRotationProposal(rotation: Int, isValid: Boolean) + + fun disableNavBarElements(displayId: Int, state1: Int, state2: Int, animate: Boolean) + + fun onSystemBarAttributesChanged(displayId: Int, behavior: Int) + + fun onTransitionModeUpdated(barMode: Int, checkBarModes: Boolean) + + fun onNavButtonsDarkIntensityChanged(darkIntensity: Float) + + fun onNavigationBarLumaSamplingEnabled(displayId: Int, enable: Boolean) + + fun destroy() + + fun getCurrentActivityContext(): TaskbarActivityContext? + + fun dumpLogs(prefix: String, pw: PrintWriter) + + fun getUIControllerForDisplay(displayId: Int): TaskbarUIController? + + fun getTaskbarForDisplay(displayId: Int): TaskbarActivityContext? + + fun createAllAppsPendingIntent(): PendingIntent + + fun debugPrimaryTaskbar(debugReason: String, verbose: Boolean) +} diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarManagerImpl.java similarity index 98% rename from quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java rename to quickstep/src/com/android/launcher3/taskbar/TaskbarManagerImpl.java index 315757371a..dabad1f73a 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarManagerImpl.java @@ -19,6 +19,7 @@ import static android.content.Context.RECEIVER_EXPORTED; import static android.content.Context.RECEIVER_NOT_EXPORTED; import static android.content.pm.PackageManager.FEATURE_FREEFORM_WINDOW_MANAGEMENT; import static android.content.pm.PackageManager.FEATURE_PC; +import static android.os.Process.THREAD_PRIORITY_FOREGROUND; import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR; import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL; @@ -35,7 +36,6 @@ import static com.android.launcher3.util.DisplayController.CHANGE_DESKTOP_MODE; import static com.android.launcher3.util.DisplayController.CHANGE_NAVIGATION_MODE; import static com.android.launcher3.util.DisplayController.CHANGE_SHOW_LOCKED_TASKBAR; import static com.android.launcher3.util.DisplayController.CHANGE_TASKBAR_PINNING; -import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR; import static com.android.launcher3.util.FlagDebugUtils.formatFlagChange; import static com.android.quickstep.util.SystemActionConstants.ACTION_SHOW_TASKBAR; @@ -85,6 +85,7 @@ import com.android.launcher3.taskbar.TaskbarNavButtonController.TaskbarNavButton import com.android.launcher3.taskbar.unfold.NonDestroyableScopedUnfoldTransitionProgressProvider; import com.android.launcher3.uioverrides.QuickstepLauncher; import com.android.launcher3.util.DisplayController; +import com.android.launcher3.util.LooperExecutor; import com.android.launcher3.util.SettingsCache; import com.android.launcher3.util.SimpleBroadcastReceiver; import com.android.quickstep.AllAppsActionManager; @@ -114,11 +115,12 @@ import java.util.Map; import java.util.Set; import java.util.StringJoiner; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executor; /** * Class to manage taskbar lifecycle */ -public class TaskbarManager implements DisplayDecorationListener { +public class TaskbarManagerImpl implements DisplayDecorationListener { private static final String TAG = "TaskbarManager"; private static final boolean DEBUG = false; private static final int TASKBAR_DESTROY_DURATION = 100; @@ -159,6 +161,9 @@ public class TaskbarManager implements DisplayDecorationListener { private final SimpleBroadcastReceiver mShutdownReceiver; + private final LooperExecutor mPerWindowUiExecutor = + new LooperExecutor("TaskbarUiThread", THREAD_PRIORITY_FOREGROUND); + // The source for this provider is set when Launcher is available // We use 'non-destroyable' version here so the original provider won't be destroyed // as it is tied to the activity lifecycle, not the taskbar lifecycle. @@ -437,7 +442,7 @@ public class TaskbarManager implements DisplayDecorationListener { }; @SuppressLint("WrongConstant") - public TaskbarManager( + public TaskbarManagerImpl( Context context, AllAppsActionManager allAppsActionManager, TaskbarNavButtonCallbacks navCallbacks, @@ -507,6 +512,10 @@ public class TaskbarManager implements DisplayDecorationListener { debugPrimaryTaskbar("TaskbarManager created"); } + public LooperExecutor getPerWindowUiExecutor() { + return mPerWindowUiExecutor; + } + private void handleDisplayUpdatesForPerceptibleTasks() { // 1. When desktop mode changes, detect eligibility for perceptible tasks. // 2. When no longer eligible for perceptible tasks, turn off and clean up. @@ -591,7 +600,7 @@ public class TaskbarManager implements DisplayDecorationListener { * Shows or hides the All Apps view in the Taskbar or Launcher, based on its current * visibility on the System UI tracked focused display. */ - public void toggleAllAppsSearch() { + private void toggleAllAppsSearch() { TaskbarActivityContext taskbar = getTaskbarForDisplay(getFocusedDisplayId()); if (taskbar == null) { // Home All Apps should be toggled from this class, because the controllers are not @@ -613,9 +622,7 @@ public class TaskbarManager implements DisplayDecorationListener { return taskbar == null ? null : taskbar.createLauncherStartFromSuwAnim(duration); } - /** - * Called when the user is unlocked - */ + /** Called when the user is unlocked */ public void onUserUnlocked() { debugPrimaryTaskbar("onUserUnlocked"); mUserUnlocked = true; @@ -743,7 +750,6 @@ public class TaskbarManager implements DisplayDecorationListener { * we fully want to destroy existing taskbars and create all desired new ones. * In other case (folding/unfolding) we don't need to remove and add window. */ - @VisibleForTesting public synchronized void recreateTaskbars() { for (int i = 0; i < mWindowContexts.size(); i++) { int displayId = mWindowContexts.keyAt(i); @@ -1698,7 +1704,7 @@ public class TaskbarManager implements DisplayDecorationListener { * @param displayId The ID of the display for which to log debug information. * @param verbose Indicates whether or not to debug with detail. */ - public void debugTaskbarManager(String debugReason, int displayId, boolean verbose) { + private void debugTaskbarManager(String debugReason, int displayId, boolean verbose) { StringJoiner log = new StringJoiner("\n"); log.add(debugReason + " displayId=" + displayId + " isDefaultDisplay=" + isDefaultDisplay( displayId)); @@ -1713,7 +1719,7 @@ public class TaskbarManager implements DisplayDecorationListener { * @param debugReason A string describing the reason for the debug log. * */ - public void debugPrimaryTaskbar(String debugReason) { + private void debugPrimaryTaskbar(String debugReason) { debugTaskbarManager(debugReason, mPrimaryDisplayId, false); } @@ -1727,13 +1733,13 @@ public class TaskbarManager implements DisplayDecorationListener { } /** Creates a {@link PendingIntent} for showing / hiding the all apps UI. */ - public PendingIntent createAllAppsPendingIntent() { + public PendingIntent createAllAppsPendingIntent(Executor uiExecutor) { return new PendingIntent(new IIntentSender.Stub() { @Override public void send(int code, Intent intent, String resolvedType, IBinder allowlistToken, IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) { - MAIN_EXECUTOR.execute(TaskbarManager.this::toggleAllAppsSearch); + uiExecutor.execute(TaskbarManagerImpl.this::toggleAllAppsSearch); } }); } diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarManagerImplWrapper.kt b/quickstep/src/com/android/launcher3/taskbar/TaskbarManagerImplWrapper.kt new file mode 100644 index 0000000000..ce0863f209 --- /dev/null +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarManagerImplWrapper.kt @@ -0,0 +1,280 @@ +/* + * Copyright (C) 2025 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.taskbar + +import android.app.PendingIntent +import android.os.Looper +import com.android.launcher3.Flags.enableTaskbarUiThread +import com.android.launcher3.anim.AnimatorPlaybackController +import com.android.launcher3.statemanager.StatefulActivity +import com.android.launcher3.util.Executors +import com.android.quickstep.views.RecentsViewContainer +import java.io.PrintWriter + +/** + * Wrapper of [TaskbarManagerImpl], this class controls which thread the invocation happens. The + * goal of this class is to minimize the changes to [TaskbarManagerImpl] during migration of + * rendering taskbar in per-window ui thread. + */ +class TaskbarManagerImplWrapper(private val impl: TaskbarManagerImpl) : TaskbarManager { + + override fun onUserUnlocked() { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute(impl::onUserUnlocked) + } else { + impl.onUserUnlocked() + } + } + + override fun setActivity(activity: StatefulActivity<*>) { + // TODO(b/404636836) internal invocation on activity should be posted back to main thread + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.setActivity(activity) } + } else { + impl.setActivity(activity) + } + } + + override fun setRecentsViewContainer(recentsViewContainer: RecentsViewContainer) { + // TODO(b/404636836) internal invocation on recentsViewContainer should be posted back to + // main thread + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.setRecentsViewContainer(recentsViewContainer) } + } else { + impl.setRecentsViewContainer(recentsViewContainer) + } + } + + override fun recreateTaskbars() { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute(impl::recreateTaskbars) + } else { + impl.recreateTaskbars() + } + } + + override fun onSystemUiFlagsChanged(systemUiStateFlags: Long, displayId: Int) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { + impl.onSystemUiFlagsChanged(systemUiStateFlags, displayId) + } + } else { + impl.onSystemUiFlagsChanged(systemUiStateFlags, displayId) + } + } + + override fun onLongPressHomeEnabled(assistantLongPressEnabled: Boolean) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { + impl.onLongPressHomeEnabled(assistantLongPressEnabled) + } + } else { + impl.onLongPressHomeEnabled(assistantLongPressEnabled) + } + } + + override fun setSetupUIVisible(isVisible: Boolean) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.setSetupUIVisible(isVisible) } + } else { + impl.setSetupUIVisible(isVisible) + } + } + + override fun setWallpaperVisible(displayId: Int, isVisible: Boolean) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.setWallpaperVisible(displayId, isVisible) } + } else { + impl.setWallpaperVisible(displayId, isVisible) + } + } + + override fun checkNavBarModes(displayId: Int) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.checkNavBarModes(displayId) } + } else { + impl.checkNavBarModes(displayId) + } + } + + override fun finishBarAnimations(displayId: Int) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.finishBarAnimations(displayId) } + } else { + impl.finishBarAnimations(displayId) + } + } + + override fun touchAutoDim(displayId: Int, reset: Boolean) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.touchAutoDim(displayId, reset) } + } else { + impl.touchAutoDim(displayId, reset) + } + } + + override fun transitionTo(displayId: Int, barMode: Int, animate: Boolean) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.transitionTo(displayId, barMode, animate) } + } else { + impl.transitionTo(displayId, barMode, animate) + } + } + + override fun appTransitionPending(pending: Boolean) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.appTransitionPending(pending) } + } else { + impl.appTransitionPending(pending) + } + } + + override fun onRotationProposal(rotation: Int, isValid: Boolean) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.onRotationProposal(rotation, isValid) } + } else { + impl.onRotationProposal(rotation, isValid) + } + } + + override fun disableNavBarElements(displayId: Int, state1: Int, state2: Int, animate: Boolean) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { + impl.disableNavBarElements(displayId, state1, state2, animate) + } + } else { + impl.disableNavBarElements(displayId, state1, state2, animate) + } + } + + override fun onSystemBarAttributesChanged(displayId: Int, behavior: Int) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { + impl.onSystemBarAttributesChanged(displayId, behavior) + } + } else { + impl.onSystemBarAttributesChanged(displayId, behavior) + } + } + + override fun onTransitionModeUpdated(barMode: Int, checkBarModes: Boolean) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { + impl.onTransitionModeUpdated(barMode, checkBarModes) + } + } else { + impl.onTransitionModeUpdated(barMode, checkBarModes) + } + } + + override fun onNavButtonsDarkIntensityChanged(darkIntensity: Float) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { + impl.onNavButtonsDarkIntensityChanged(darkIntensity) + } + } else { + impl.onNavButtonsDarkIntensityChanged(darkIntensity) + } + } + + override fun onNavigationBarLumaSamplingEnabled(displayId: Int, enable: Boolean) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { + impl.onNavigationBarLumaSamplingEnabled(displayId, enable) + } + } else { + impl.onNavigationBarLumaSamplingEnabled(displayId, enable) + } + } + + override fun onDisplayAddSystemDecorations(displayId: Int) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.onDisplayAddSystemDecorations(displayId) } + } else { + impl.onDisplayAddSystemDecorations(displayId) + } + } + + override fun onDisplayRemoved(displayId: Int) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.onDisplayRemoved(displayId) } + } else { + impl.onDisplayRemoved(displayId) + } + } + + override fun onDisplayRemoveSystemDecorations(displayId: Int) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.onDisplayRemoveSystemDecorations(displayId) } + } else { + impl.onDisplayRemoveSystemDecorations(displayId) + } + } + + override fun destroy() { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.destroy() } + } else { + impl.destroy() + } + } + + override fun createLauncherStartFromSuwAnim(duration: Int): AnimatorPlaybackController? { + // TODO(b/404636836): Evaluate if internal impl taskbar.createLauncherStartFromSuwAnim() is + // thread safe + return impl.createLauncherStartFromSuwAnim(duration) + } + + override fun getCurrentActivityContext(): TaskbarActivityContext? { + // Thread safe + return impl.currentActivityContext + } + + override fun getUIControllerForDisplay(displayId: Int): TaskbarUIController? { + // Thread safe + return impl.getUIControllerForDisplay(displayId) + } + + override fun getTaskbarForDisplay(displayId: Int): TaskbarActivityContext? { + // Thread safe + return impl.getTaskbarForDisplay(displayId) + } + + override fun createAllAppsPendingIntent(): PendingIntent { + // Thread safe + return impl.createAllAppsPendingIntent( + if (enableTaskbarUiThread()) impl.perWindowUiExecutor else Executors.MAIN_EXECUTOR + ) + } + + override fun dumpLogs(prefix: String, pw: PrintWriter) { + // Stay on caller thread because PrinterWriter is not thread safe. + impl.dumpLogs(prefix, pw) + } + + override fun debugPrimaryTaskbar(debugReason: String, verbose: Boolean) { + if (shouldPostToTaskbarUiThread()) { + impl.perWindowUiExecutor.execute { impl.debugPrimaryTaskbar(debugReason, verbose) } + } else { + impl.debugPrimaryTaskbar(debugReason, verbose) + } + } + + private fun shouldPostToTaskbarUiThread(): Boolean { + return enableTaskbarUiThread() && Looper.getMainLooper() == Looper.myLooper() + } +} diff --git a/quickstep/src/com/android/quickstep/InputConsumerUtils.kt b/quickstep/src/com/android/quickstep/InputConsumerUtils.kt index 0bbd5a8f89..dde180c848 100644 --- a/quickstep/src/com/android/quickstep/InputConsumerUtils.kt +++ b/quickstep/src/com/android/quickstep/InputConsumerUtils.kt @@ -74,7 +74,7 @@ object InputConsumerUtils { event: MotionEvent, rotationTouchHelper: RotationTouchHelper, ): InputConsumer where T : RecentsViewContainer, T : StatefulContainer { - val tac = taskbarManager.currentActivityContext + val tac = taskbarManager.getCurrentActivityContext() val bubbleControllers = tac?.bubbleControllers if (bubbleControllers != null && BubbleBarInputConsumer.isEventOnBubbles(tac, event)) { val consumer: InputConsumer = diff --git a/quickstep/src/com/android/quickstep/TouchInteractionService.java b/quickstep/src/com/android/quickstep/TouchInteractionService.java index 2f6a35c141..11bc370f56 100644 --- a/quickstep/src/com/android/quickstep/TouchInteractionService.java +++ b/quickstep/src/com/android/quickstep/TouchInteractionService.java @@ -79,6 +79,8 @@ import com.android.launcher3.statehandlers.DesktopVisibilityController; import com.android.launcher3.statemanager.StatefulActivity; import com.android.launcher3.taskbar.TaskbarActivityContext; import com.android.launcher3.taskbar.TaskbarManager; +import com.android.launcher3.taskbar.TaskbarManagerImpl; +import com.android.launcher3.taskbar.TaskbarManagerImplWrapper; import com.android.launcher3.taskbar.TaskbarNavButtonController.TaskbarNavButtonCallbacks; import com.android.launcher3.taskbar.bubbles.BubbleControllers; import com.android.launcher3.testing.TestLogging; @@ -673,8 +675,9 @@ public class TouchInteractionService extends Service { initInputMonitor("onTrackpadConnected()"); }); - mTaskbarManager = new TaskbarManager(this, mAllAppsActionManager, mNavCallbacks, - mRecentsWindowManagerRepository); + mTaskbarManager = new TaskbarManagerImplWrapper( + new TaskbarManagerImpl(this, mAllAppsActionManager, mNavCallbacks, + mRecentsWindowManagerRepository)); mDesktopAppLaunchTransitionManager = new DesktopAppLaunchTransitionManager(this, SystemUiProxy.INSTANCE.get(this)); mDesktopAppLaunchTransitionManager.registerTransitions(); diff --git a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarUnitTestRule.kt b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarUnitTestRule.kt index 7e9f27235e..8827836d07 100644 --- a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarUnitTestRule.kt +++ b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarUnitTestRule.kt @@ -26,7 +26,7 @@ import androidx.test.platform.app.InstrumentationRegistry import com.android.launcher3.LauncherAppState import com.android.launcher3.taskbar.TaskbarActivityContext import com.android.launcher3.taskbar.TaskbarControllers -import com.android.launcher3.taskbar.TaskbarManager +import com.android.launcher3.taskbar.TaskbarManagerImpl import com.android.launcher3.taskbar.TaskbarNavButtonController.TaskbarNavButtonCallbacks import com.android.launcher3.taskbar.TaskbarUIController import com.android.launcher3.taskbar.bubbles.BubbleControllers @@ -79,7 +79,7 @@ class TaskbarUnitTestRule( private val instrumentation = InstrumentationRegistry.getInstrumentation() - private lateinit var taskbarManager: TaskbarManager + private lateinit var taskbarManager: TaskbarManagerImpl val activityContext: TaskbarActivityContext get() { @@ -107,7 +107,7 @@ class TaskbarUnitTestRule( taskbarManager = TestUtil.getOnUiThread { object : - TaskbarManager( + TaskbarManagerImpl( context, AllAppsActionManager( context,