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

Commit 8c556702 authored by Eric Lin's avatar Eric Lin
Browse files

Refactor BubblesTransitionObserver#onTransitionReady.

Moved loop-invariant checks out of the main loop in `onTransitionReady`.
Conditions related to the overall bubble state are now evaluated once
upfront, allowing for an early return if no further processing is
needed.

Additionally, values constant across loop iterations, such as the
expanded bubble's task ID and the bubble view's display ID, are now
pre-computed. This avoids redundant calculations and improves
performance and readability.

BUG: 387193964
Test: atest WMShellUnitTests:BubblesTransitionObserverTest
Flag: EXEMPT refactor
Change-Id: Ib13534ec28b9b2a5bde19a7cfbda75ced48b0f6a
parent e3c98685
Loading
Loading
Loading
Loading
+30 −13
Original line number Diff line number Diff line
@@ -13,9 +13,11 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wm.shell.bubbles;

import static android.app.ActivityTaskManager.INVALID_TASK_ID;

import static com.android.wm.shell.protolog.ShellProtoLogGroup.WM_SHELL_BUBBLES_NOISY;

import android.app.ActivityManager;
@@ -51,38 +53,53 @@ public class BubblesTransitionObserver implements Transitions.TransitionObserver
    public void onTransitionReady(@NonNull IBinder transition, @NonNull TransitionInfo info,
            @NonNull SurfaceControl.Transaction startTransaction,
            @NonNull SurfaceControl.Transaction finishTransaction) {
        for (TransitionInfo.Change change : info.getChanges()) {
            final ActivityManager.RunningTaskInfo taskInfo = change.getTaskInfo();
            // We only care about opens / move to fronts when bubbles are expanded & not animating.
            if (taskInfo == null
                    || taskInfo.taskId == INVALID_TASK_ID
                    || !TransitionUtil.isOpeningType(change.getMode())
                    || mBubbleController.isStackAnimating()

        // --- Pre-conditions (Loop-invariant checks) ---
        // If bubbles aren't expanded, are animating, or no bubble is selected,
        // we don't need to process any transitions for collapsing.
        if (mBubbleController.isStackAnimating()
                || !mBubbleData.isExpanded()
                || mBubbleData.getSelectedBubble() == null) {
            return;
        }

        final int expandedTaskId = mBubbleData.getSelectedBubble().getTaskId();
        // If expanded task id is invalid, we don't need to process any transitions for collapsing.
        if (expandedTaskId == INVALID_TASK_ID) {
            return;
        }

        final int bubbleViewDisplayId = mBubbleController.getCurrentViewDisplayId();
        for (TransitionInfo.Change change : info.getChanges()) {
            // We only care about opens / move to fronts.
            if (!TransitionUtil.isOpeningType(change.getMode())) {
                continue;
            }
            final ActivityManager.RunningTaskInfo taskInfo = change.getTaskInfo();
            // We only handle task transitions.
            if (taskInfo == null || taskInfo.taskId == INVALID_TASK_ID) {
                continue;
            }
            final int expandedId = mBubbleData.getSelectedBubble().getTaskId();
            // If the opening task id is the same as the expanded bubble, skip collapsing
            // because it is our bubble that is opening.
            if (expandedId == INVALID_TASK_ID || expandedId == taskInfo.taskId) {
            if (taskInfo.taskId == expandedTaskId) {
                continue;
            }
            // If the opening task is on a different display, skip collapsing because the task
            // opening does not visually overlap with the bubbles.
            final int bubbleViewDisplayId = mBubbleController.getCurrentViewDisplayId();
            if (taskInfo.displayId != bubbleViewDisplayId) {
                continue;
            }
            // If the opening task was launched by another bubble, skip collapsing the existing one
            // since BubbleTransitions will start a new bubble for it
            if (BubbleAnythingFlagHelper.enableCreateAnyBubble() && taskInfo.isAppBubble) {
                ProtoLog.d(WM_SHELL_BUBBLES_NOISY,
                        "TransitionObserver.onTransitionReady(): skipping app bubble for taskId=%d",
                        taskInfo.taskId);
                ProtoLog.d(WM_SHELL_BUBBLES_NOISY, "TransitionObserver.onTransitionReady(): "
                        + "skipping app bubble for taskId=%d", taskInfo.taskId);
                continue;
            }

            mBubbleData.setExpanded(false);
            return;
        }
    }