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

Commit 2df947c6 authored by Ats Jenk's avatar Ats Jenk
Browse files

Refactor expanded view access in BubbleStackView

Create a method to access the expanded view that first checks if the
expanded bubble is null or not.
Also assign the expanded view to a variable in places it is used after
null check.

Bug: 342694122
Flag: EXEMPT refactor
Test: m
Test: manual test to expand and collapse bubbles
Change-Id: I9181b53537ebf3d14b5cefaf6265bbd2e557dd36
parent 40349d86
Loading
Loading
Loading
Loading
+77 −71
Original line number Diff line number Diff line
@@ -335,7 +335,7 @@ public class BubbleStackView extends FrameLayout
            pw.println("Expanded bubble state:");
            pw.println("  expandedBubbleKey: " + mExpandedBubble.getKey());

            final BubbleExpandedView expandedView = mExpandedBubble.getExpandedView();
            final BubbleExpandedView expandedView = getExpandedView();

            if (expandedView != null) {
                pw.println("  expandedViewVis:    " + expandedView.getVisibility());
@@ -799,10 +799,11 @@ public class BubbleStackView extends FrameLayout

        private float getScrimAlphaForDrag(float dragAmount) {
            // dragAmount should be negative as we allow scroll up only
            if (mExpandedBubble != null && mExpandedBubble.getExpandedView() != null) {
            BubbleExpandedView expandedView = getExpandedView();
            if (expandedView != null) {
                float alphaRange = BUBBLE_EXPANDED_SCRIM_ALPHA - MIN_SCRIM_ALPHA_FOR_DRAG;

                int dragMax = mExpandedBubble.getExpandedView().getContentHeight();
                int dragMax = expandedView.getContentHeight();
                float dragFraction = dragAmount / dragMax;

                return Math.max(BUBBLE_EXPANDED_SCRIM_ALPHA - alphaRange * dragFraction,
@@ -1104,33 +1105,35 @@ public class BubbleStackView extends FrameLayout
        mExpandedViewAlphaAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                if (mExpandedBubble != null && mExpandedBubble.getExpandedView() != null) {
                BubbleExpandedView expandedView = getExpandedView();
                if (expandedView != null) {
                    // We need to be Z ordered on top in order for alpha animations to work.
                    mExpandedBubble.getExpandedView().setSurfaceZOrderedOnTop(true);
                    mExpandedBubble.getExpandedView().setAnimating(true);
                    expandedView.setSurfaceZOrderedOnTop(true);
                    expandedView.setAnimating(true);
                    mExpandedViewContainer.setVisibility(VISIBLE);
                }
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (mExpandedBubble != null
                        && mExpandedBubble.getExpandedView() != null
                BubbleExpandedView expandedView = getExpandedView();
                if (expandedView != null
                        // The surface needs to be Z ordered on top for alpha values to work on the
                        // TaskView, and if we're temporarily hidden, we are still on the screen
                        // with alpha = 0f until we animate back. Stay Z ordered on top so the alpha
                        // = 0f remains in effect.
                        && !mExpandedViewTemporarilyHidden) {
                    mExpandedBubble.getExpandedView().setSurfaceZOrderedOnTop(false);
                    mExpandedBubble.getExpandedView().setAnimating(false);
                    expandedView.setSurfaceZOrderedOnTop(false);
                    expandedView.setAnimating(false);
                }
            }
        });
        mExpandedViewAlphaAnimator.addUpdateListener(valueAnimator -> {
            if (mExpandedBubble != null && mExpandedBubble.getExpandedView() != null) {
            BubbleExpandedView expandedView = getExpandedView();
            if (expandedView != null) {
                float alpha = (float) valueAnimator.getAnimatedValue();
                mExpandedBubble.getExpandedView().setContentAlpha(alpha);
                mExpandedBubble.getExpandedView().setBackgroundAlpha(alpha);
                expandedView.setContentAlpha(alpha);
                expandedView.setBackgroundAlpha(alpha);
            }
        });

@@ -1334,7 +1337,7 @@ public class BubbleStackView extends FrameLayout
        }
        final boolean seen = getPrefBoolean(ManageEducationView.PREF_MANAGED_EDUCATION);
        final boolean shouldShow = (!seen || BubbleDebugConfig.forceShowUserEducation(mContext))
                && mExpandedBubble != null && mExpandedBubble.getExpandedView() != null;
                && getExpandedView() != null;
        ProtoLog.d(WM_SHELL_BUBBLES, "Show manage edu=%b", shouldShow);
        if (shouldShow && BubbleDebugConfig.neverShowUserEducation(mContext)) {
            Log.w(TAG, "Want to show manage edu, but it is forced hidden");
@@ -1361,9 +1364,9 @@ public class BubbleStackView extends FrameLayout
     * Show manage education if was not showing before.
     */
    private void showManageEdu() {
        if (mExpandedBubble == null || mExpandedBubble.getExpandedView() == null) return;
        mManageEduView.show(mExpandedBubble.getExpandedView(),
                mStackAnimationController.isStackOnLeftSide());
        BubbleExpandedView expandedView = getExpandedView();
        if (expandedView == null) return;
        mManageEduView.show(expandedView, mStackAnimationController.isStackOnLeftSide());
    }

    @VisibleForTesting
@@ -1875,6 +1878,11 @@ public class BubbleStackView extends FrameLayout
        return mExpandedBubble;
    }

    @Nullable
    private BubbleExpandedView getExpandedView() {
        return mExpandedBubble != null ? mExpandedBubble.getExpandedView() : null;
    }

    // via BubbleData.Listener
    @SuppressLint("ClickableViewAccessibility")
    void addBubble(Bubble bubble) {
@@ -2054,13 +2062,11 @@ public class BubbleStackView extends FrameLayout

        // If we're expanded, screenshot the currently expanded bubble (before expanding the newly
        // selected bubble) so we can animate it out.
        if (mIsExpanded && mExpandedBubble != null && mExpandedBubble.getExpandedView() != null
                && !mExpandedViewTemporarilyHidden) {
            if (mExpandedBubble != null && mExpandedBubble.getExpandedView() != null) {
        BubbleExpandedView expandedView = getExpandedView();
        if (mIsExpanded && expandedView != null && !mExpandedViewTemporarilyHidden) {
            // Before screenshotting, have the real TaskView show on top of other surfaces
            // so that the screenshot doesn't flicker on top of it.
                mExpandedBubble.getExpandedView().setSurfaceZOrderedOnTop(true);
            }
            expandedView.setSurfaceZOrderedOnTop(true);

            try {
                screenshotAnimatingOutBubbleIntoSurface((success) -> {
@@ -2080,7 +2086,7 @@ public class BubbleStackView extends FrameLayout
    private void showNewlySelectedBubble(BubbleViewProvider bubbleToSelect) {
        final BubbleViewProvider previouslySelected = mExpandedBubble;
        mExpandedBubble = bubbleToSelect;
        mExpandedViewAnimationController.setExpandedView(mExpandedBubble.getExpandedView());
        mExpandedViewAnimationController.setExpandedView(getExpandedView());

        if (mIsExpanded) {
            hideCurrentInputMethod();
@@ -2386,8 +2392,7 @@ public class BubbleStackView extends FrameLayout
        updateOverflowVisibility();
        updatePointerPosition(false /* forIme */);
        mExpandedAnimationController.expandFromStack(() -> {
            if (mIsExpanded && mExpandedBubble != null
                    && mExpandedBubble.getExpandedView() != null) {
            if (mIsExpanded && getExpandedView() != null) {
                maybeShowManageEdu();
            }
            updateOverflowDotVisibility(true /* expanding */);
@@ -2450,13 +2455,14 @@ public class BubbleStackView extends FrameLayout
        }
        mExpandedViewContainer.setAnimationMatrix(mExpandedViewContainerMatrix);

        if (mExpandedBubble != null && mExpandedBubble.getExpandedView() != null) {
            mExpandedBubble.getExpandedView().setContentAlpha(0f);
            mExpandedBubble.getExpandedView().setBackgroundAlpha(0f);
        BubbleExpandedView expandedView = getExpandedView();
        if (expandedView != null) {
            expandedView.setContentAlpha(0f);
            expandedView.setBackgroundAlpha(0f);

            // We'll be starting the alpha animation after a slight delay, so set this flag early
            // here.
            mExpandedBubble.getExpandedView().setAnimating(true);
            expandedView.setAnimating(true);
        }

        mDelayedAnimation = () -> {
@@ -2486,10 +2492,9 @@ public class BubbleStackView extends FrameLayout
                    .withEndActions(() -> {
                        mExpandedViewContainer.setAnimationMatrix(null);
                        afterExpandedViewAnimation();
                        if (mExpandedBubble != null
                                && mExpandedBubble.getExpandedView() != null) {
                            mExpandedBubble.getExpandedView()
                                    .setSurfaceZOrderedOnTop(false);
                        BubbleExpandedView expView = getExpandedView();
                        if (expView != null) {
                            expView.setSurfaceZOrderedOnTop(false);
                        }
                    })
                    .start();
@@ -2553,12 +2558,13 @@ public class BubbleStackView extends FrameLayout
        };
        mExpandedViewAnimationController.animateCollapse(collapseBackToStack, after,
                collapsePosition);
        if (mExpandedBubble != null && mExpandedBubble.getExpandedView() != null) {
        BubbleExpandedView expandedView = getExpandedView();
        if (expandedView != null) {
            // When the animation completes, we should no longer be showing the content.
            // This won't actually update content visibility immediately, if we are currently
            // animating. But updates the internal state for the content to be hidden after
            // animation completes.
            mExpandedBubble.getExpandedView().setContentVisibility(false);
            expandedView.setContentVisibility(false);
        }
    }

@@ -2650,10 +2656,10 @@ public class BubbleStackView extends FrameLayout
                        // expanded view animation might not actually set the z ordering for the
                        // expanded view correctly, because the view may still be temporarily
                        // hidden. So set it again here.
                        BubbleExpandedView bev = mExpandedBubble.getExpandedView();
                        if (bev != null) {
                            mExpandedBubble.getExpandedView().setSurfaceZOrderedOnTop(false);
                            mExpandedBubble.getExpandedView().setAnimating(false);
                        BubbleExpandedView expandedView = getExpandedView();
                        if (expandedView != null) {
                            expandedView.setSurfaceZOrderedOnTop(false);
                            expandedView.setAnimating(false);
                        }
                    })
                    .start();
@@ -2725,13 +2731,13 @@ public class BubbleStackView extends FrameLayout

        if (mIsExpanded) {
            mExpandedViewAnimationController.animateForImeVisibilityChange(visible);
            if (mPositioner.showBubblesVertically()
                    && mExpandedBubble != null && mExpandedBubble.getExpandedView() != null) {
            BubbleExpandedView expandedView = getExpandedView();
            if (mPositioner.showBubblesVertically() && expandedView != null) {
                float selectedY = mPositioner.getExpandedBubbleXY(getState().selectedIndex,
                        getState()).y;
                float newExpandedViewTop = mPositioner.getExpandedViewY(mExpandedBubble, selectedY);
                mExpandedBubble.getExpandedView().setImeVisible(visible);
                if (!mExpandedBubble.getExpandedView().isUsingMaxHeight()) {
                expandedView.setImeVisible(visible);
                if (!expandedView.isUsingMaxHeight()) {
                    mExpandedViewContainer.animate().translationY(newExpandedViewTop);
                }
                List<Animator> animList = new ArrayList<>();
@@ -3080,7 +3086,8 @@ public class BubbleStackView extends FrameLayout

        // This should not happen, since the manage menu is only visible when there's an expanded
        // bubble. If we end up in this state, just hide the menu immediately.
        if (mExpandedBubble == null || mExpandedBubble.getExpandedView() == null) {
        BubbleExpandedView expandedView = getExpandedView();
        if (expandedView == null) {
            mManageMenu.setVisibility(View.INVISIBLE);
            mManageMenuScrim.setVisibility(INVISIBLE);
            mSysuiProxyProvider.getSysuiProxy().onManageMenuExpandChanged(false /* show */);
@@ -3126,8 +3133,8 @@ public class BubbleStackView extends FrameLayout
            }
        }

        if (mExpandedBubble.getExpandedView().getTaskView() != null) {
            mExpandedBubble.getExpandedView().getTaskView().setObscuredTouchRect(mShowingManage
        if (expandedView.getTaskView() != null) {
            expandedView.getTaskView().setObscuredTouchRect(mShowingManage
                    ? new Rect(0, 0, getWidth(), getHeight())
                    : null);
        }
@@ -3137,8 +3144,8 @@ public class BubbleStackView extends FrameLayout

        // When the menu is open, it should be at these coordinates. The menu pops out to the right
        // in LTR and to the left in RTL.
        mExpandedBubble.getExpandedView().getManageButtonBoundsOnScreen(mTempRect);
        final float margin = mExpandedBubble.getExpandedView().getManageButtonMargin();
        expandedView.getManageButtonBoundsOnScreen(mTempRect);
        final float margin = expandedView.getManageButtonMargin();
        final float targetX = isLtr
                ? mTempRect.left - margin
                : mTempRect.right + margin - mManageMenu.getWidth();
@@ -3162,9 +3169,10 @@ public class BubbleStackView extends FrameLayout
                    .withEndActions(() -> {
                        View child = mManageMenu.getChildAt(0);
                        child.requestAccessibilityFocus();
                        if (mExpandedBubble != null && mExpandedBubble.getExpandedView() != null) {
                        BubbleExpandedView expView = getExpandedView();
                        if (expView != null) {
                            // Update the AV's obscured touchable region for the new state.
                            mExpandedBubble.getExpandedView().updateObscuredTouchableRegion();
                            expView.updateObscuredTouchableRegion();
                        }
                    })
                    .start();
@@ -3179,9 +3187,10 @@ public class BubbleStackView extends FrameLayout
                    .spring(DynamicAnimation.TRANSLATION_Y, targetY + menuHeight / 4f)
                    .withEndActions(() -> {
                        mManageMenu.setVisibility(View.INVISIBLE);
                        if (mExpandedBubble != null && mExpandedBubble.getExpandedView() != null) {
                        BubbleExpandedView expView = getExpandedView();
                        if (expView != null) {
                            // Update the AV's obscured touchable region for the new state.
                            mExpandedBubble.getExpandedView().updateObscuredTouchableRegion();
                            expView.updateObscuredTouchableRegion();
                        }
                    })
                    .start();
@@ -3208,9 +3217,8 @@ public class BubbleStackView extends FrameLayout

    private void updateExpandedBubble() {
        mExpandedViewContainer.removeAllViews();
        if (mIsExpanded && mExpandedBubble != null
                && mExpandedBubble.getExpandedView() != null) {
            BubbleExpandedView bev = mExpandedBubble.getExpandedView();
        BubbleExpandedView bev = getExpandedView();
        if (mIsExpanded && bev != null) {
            bev.setContentVisibility(false);
            bev.setAnimating(!mIsExpansionAnimating);
            mExpandedViewContainerMatrix.setScaleX(0f);
@@ -3238,9 +3246,8 @@ public class BubbleStackView extends FrameLayout
    }

    private void updateManageButtonListener() {
        if (mIsExpanded && mExpandedBubble != null
                && mExpandedBubble.getExpandedView() != null) {
            BubbleExpandedView bev = mExpandedBubble.getExpandedView();
        BubbleExpandedView bev = getExpandedView();
        if (mIsExpanded && bev != null) {
            bev.setManageClickListener((view) -> {
                showManageMenu(true /* show */);
            });
@@ -3257,14 +3264,13 @@ public class BubbleStackView extends FrameLayout
     *                   expanded bubble.
     */
    private void screenshotAnimatingOutBubbleIntoSurface(Consumer<Boolean> onComplete) {
        if (!mIsExpanded || mExpandedBubble == null || mExpandedBubble.getExpandedView() == null) {
        final BubbleExpandedView animatingOutExpandedView = getExpandedView();
        if (!mIsExpanded || animatingOutExpandedView == null) {
            // You can't animate null.
            onComplete.accept(false);
            return;
        }

        final BubbleExpandedView animatingOutExpandedView = mExpandedBubble.getExpandedView();

        // Release the previous screenshot if it hasn't been released already.
        if (mAnimatingOutBubbleBuffer != null) {
            releaseAnimatingOutBubbleBuffer();
@@ -3296,8 +3302,7 @@ public class BubbleStackView extends FrameLayout
        mAnimatingOutSurfaceContainer.setTranslationX(translationX);
        mAnimatingOutSurfaceContainer.setTranslationY(0);

        final int[] taskViewLocation =
                mExpandedBubble.getExpandedView().getTaskViewLocationOnScreen();
        final int[] taskViewLocation = animatingOutExpandedView.getTaskViewLocationOnScreen();
        final int[] surfaceViewLocation = mAnimatingOutSurfaceView.getLocationOnScreen();

        // Translate the surface to overlap the real TaskView.
@@ -3359,15 +3364,15 @@ public class BubbleStackView extends FrameLayout
        int[] paddings = mPositioner.getExpandedViewContainerPadding(
                mStackAnimationController.isStackOnLeftSide(), isOverflowExpanded);
        mExpandedViewContainer.setPadding(paddings[0], paddings[1], paddings[2], paddings[3]);
        if (mExpandedBubble != null && mExpandedBubble.getExpandedView() != null) {
        BubbleExpandedView expandedView = getExpandedView();
        if (expandedView != null) {
            PointF p = mPositioner.getExpandedBubbleXY(getBubbleIndex(mExpandedBubble),
                    getState());
            mExpandedViewContainer.setTranslationY(mPositioner.getExpandedViewY(mExpandedBubble,
                    mPositioner.showBubblesVertically() ? p.y : p.x));
            mExpandedViewContainer.setTranslationX(0f);
            mExpandedBubble.getExpandedView().updateTaskViewContentWidth();
            mExpandedBubble.getExpandedView().updateView(
                    mExpandedViewContainer.getLocationOnScreen());
            expandedView.updateTaskViewContentWidth();
            expandedView.updateView(mExpandedViewContainer.getLocationOnScreen());
            updatePointerPosition(false /* forIme */);
        }

@@ -3440,7 +3445,8 @@ public class BubbleStackView extends FrameLayout
     *               the pointer is animated to the location.
     */
    private void updatePointerPosition(boolean forIme) {
        if (mExpandedBubble == null || mExpandedBubble.getExpandedView() == null) {
        BubbleExpandedView expandedView = getExpandedView();
        if (mExpandedBubble == null || expandedView == null) {
            return;
        }
        int index = getBubbleIndex(mExpandedBubble);
@@ -3451,7 +3457,7 @@ public class BubbleStackView extends FrameLayout
        float bubblePosition = mPositioner.showBubblesVertically()
                ? position.y
                : position.x;
        mExpandedBubble.getExpandedView().setPointerPosition(bubblePosition,
        expandedView.setPointerPosition(bubblePosition,
                mStackOnLeftOrWillBe, forIme /* animate */);
    }