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

Commit 33cf8ca0 authored by Massimo Carli's avatar Massimo Carli
Browse files

[2/n] Makes EduAnimationController generic

Use generics to reuse EduAnimationController

Fixes: 263113045
Test: Manual

Change-Id: Ifdcf879f3c3f6050622bc893ad639ccbc05d7f23
parent 75845880
Loading
Loading
Loading
Loading
+23 −16
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@
 * limitations under the License.
 */

package com.android.wm.shell.compatui.letterboxedu;
package com.android.wm.shell.compatui;

import static com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
import static com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
@@ -38,10 +38,15 @@ import android.view.animation.Animation;
import com.android.internal.policy.TransitionAnimation;

/**
 * Controls the enter/exit animations of the letterbox education.
 * Controls the enter/exit a dialog.
 *
 * @param <T> The {@link DialogContainerSupplier} to use
 */
class LetterboxEduAnimationController {
    private static final String TAG = "LetterboxEduAnimation";
public class DialogAnimationController<T extends DialogContainerSupplier> {

    // The alpha of a background is a number between 0 (fully transparent) to 255 (fully opaque).
    // 204 is simply 255 * 0.8.
    static final int BACKGROUND_DIM_ALPHA = 204;

    // If shell transitions are enabled, startEnterAnimation will be called after all transitions
    // have finished, and therefore the start delay should be shorter.
@@ -49,6 +54,7 @@ class LetterboxEduAnimationController {

    private final TransitionAnimation mTransitionAnimation;
    private final String mPackageName;
    private final String mTag;
    @AnyRes
    private final int mAnimStyleResId;

@@ -57,23 +63,24 @@ class LetterboxEduAnimationController {
    @Nullable
    private Animator mBackgroundDimAnimator;

    LetterboxEduAnimationController(Context context) {
        mTransitionAnimation = new TransitionAnimation(context, /* debug= */ false, TAG);
    public DialogAnimationController(Context context, String tag) {
        mTransitionAnimation = new TransitionAnimation(context, /* debug= */ false, tag);
        mAnimStyleResId = (new ContextThemeWrapper(context,
                android.R.style.ThemeOverlay_Material_Dialog).getTheme()).obtainStyledAttributes(
                com.android.internal.R.styleable.Window).getResourceId(
                com.android.internal.R.styleable.Window_windowAnimationStyle, 0);
        mPackageName = context.getPackageName();
        mTag = tag;
    }

    /**
     * Starts both background dim fade-in animation and the dialog enter animation.
     */
    void startEnterAnimation(@NonNull LetterboxEduDialogLayout layout, Runnable endCallback) {
    public void startEnterAnimation(@NonNull T layout, Runnable endCallback) {
        // Cancel any previous animation if it's still running.
        cancelAnimation();

        final View dialogContainer = layout.getDialogContainer();
        final View dialogContainer = layout.getDialogContainerView();
        mDialogAnimation = loadAnimation(WindowAnimation_windowEnterAnimation);
        if (mDialogAnimation == null) {
            endCallback.run();
@@ -86,8 +93,8 @@ class LetterboxEduAnimationController {
                    endCallback.run();
                }));

        mBackgroundDimAnimator = getAlphaAnimator(layout.getBackgroundDim(),
                /* endAlpha= */ LetterboxEduDialogLayout.BACKGROUND_DIM_ALPHA,
        mBackgroundDimAnimator = getAlphaAnimator(layout.getBackgroundDimDrawable(),
                /* endAlpha= */ BACKGROUND_DIM_ALPHA,
                mDialogAnimation.getDuration());
        mBackgroundDimAnimator.addListener(getDimAnimatorListener());

@@ -101,11 +108,11 @@ class LetterboxEduAnimationController {
    /**
     * Starts both the background dim fade-out animation and the dialog exit animation.
     */
    void startExitAnimation(@NonNull LetterboxEduDialogLayout layout, Runnable endCallback) {
    public void startExitAnimation(@NonNull T layout, Runnable endCallback) {
        // Cancel any previous animation if it's still running.
        cancelAnimation();

        final View dialogContainer = layout.getDialogContainer();
        final View dialogContainer = layout.getDialogContainerView();
        mDialogAnimation = loadAnimation(WindowAnimation_windowExitAnimation);
        if (mDialogAnimation == null) {
            endCallback.run();
@@ -119,8 +126,8 @@ class LetterboxEduAnimationController {
                    endCallback.run();
                }));

        mBackgroundDimAnimator = getAlphaAnimator(layout.getBackgroundDim(), /* endAlpha= */ 0,
                mDialogAnimation.getDuration());
        mBackgroundDimAnimator = getAlphaAnimator(layout.getBackgroundDimDrawable(),
                /* endAlpha= */ 0, mDialogAnimation.getDuration());
        mBackgroundDimAnimator.addListener(getDimAnimatorListener());

        dialogContainer.startAnimation(mDialogAnimation);
@@ -130,7 +137,7 @@ class LetterboxEduAnimationController {
    /**
     * Cancels all animations and resets the state of the controller.
     */
    void cancelAnimation() {
    public void cancelAnimation() {
        if (mDialogAnimation != null) {
            mDialogAnimation.cancel();
            mDialogAnimation = null;
@@ -145,7 +152,7 @@ class LetterboxEduAnimationController {
        Animation animation = mTransitionAnimation.loadAnimationAttr(mPackageName, mAnimStyleResId,
                animAttr, /* translucent= */ false);
        if (animation == null) {
            Log.e(TAG, "Failed to load animation " + animAttr);
            Log.e(mTag, "Failed to load animation " + animAttr);
        }
        return animation;
    }
+36 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.wm.shell.compatui;

import android.graphics.drawable.Drawable;
import android.view.View;

/**
 * A component which can provide a {@link View} to use as a container for a Dialog
 */
public interface DialogContainerSupplier {

    /**
     * @return The {@link View} to use as a container for a Dialog
     */
    View getDialogContainerView();

    /**
     * @return The {@link Drawable} to use as background of the dialog.
     */
    Drawable getBackgroundDimDrawable();
}
+9 −10
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.android.wm.shell.R;
import com.android.wm.shell.compatui.DialogContainerSupplier;

/**
 * Container for Letterbox Education Dialog and background dim.
@@ -33,11 +34,7 @@ import com.android.wm.shell.R;
 * <p>This layout should fill the entire task and the background around the dialog acts as the
 * background dim which dismisses the dialog when clicked.
 */
class LetterboxEduDialogLayout extends ConstraintLayout {

    // The alpha of a background is a number between 0 (fully transparent) to 255 (fully opaque).
    // 204 is simply 255 * 0.8.
    static final int BACKGROUND_DIM_ALPHA = 204;
class LetterboxEduDialogLayout extends ConstraintLayout implements DialogContainerSupplier {

    private View mDialogContainer;
    private TextView mDialogTitle;
@@ -60,16 +57,18 @@ class LetterboxEduDialogLayout extends ConstraintLayout {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    View getDialogContainer() {
    @Override
    public View getDialogContainerView() {
        return mDialogContainer;
    }

    TextView getDialogTitle() {
        return mDialogTitle;
    @Override
    public Drawable getBackgroundDimDrawable() {
        return mBackgroundDim;
    }

    Drawable getBackgroundDim() {
        return mBackgroundDim;
    TextView getDialogTitle() {
        return mDialogTitle;
    }

    /**
+8 −4
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import com.android.wm.shell.common.DisplayLayout;
import com.android.wm.shell.common.DockStateReader;
import com.android.wm.shell.common.SyncTransactionQueue;
import com.android.wm.shell.compatui.CompatUIWindowManagerAbstract;
import com.android.wm.shell.compatui.DialogAnimationController;
import com.android.wm.shell.transition.Transitions;

/**
@@ -63,7 +64,7 @@ public class LetterboxEduWindowManager extends CompatUIWindowManagerAbstract {
     */
    private final SharedPreferences mSharedPreferences;

    private final LetterboxEduAnimationController mAnimationController;
    private final DialogAnimationController<LetterboxEduDialogLayout> mAnimationController;

    private final Transitions mTransitions;

@@ -96,14 +97,17 @@ public class LetterboxEduWindowManager extends CompatUIWindowManagerAbstract {
            DisplayLayout displayLayout, Transitions transitions,
            Runnable onDismissCallback, DockStateReader dockStateReader) {
        this(context, taskInfo, syncQueue, taskListener, displayLayout, transitions,
                onDismissCallback, new LetterboxEduAnimationController(context), dockStateReader);
                onDismissCallback,
                new DialogAnimationController<>(context, /* tag */ "LetterboxEduWindowManager"),
                dockStateReader);
    }

    @VisibleForTesting
    LetterboxEduWindowManager(Context context, TaskInfo taskInfo,
            SyncTransactionQueue syncQueue, ShellTaskOrganizer.TaskListener taskListener,
            DisplayLayout displayLayout, Transitions transitions, Runnable onDismissCallback,
            LetterboxEduAnimationController animationController, DockStateReader dockStateReader) {
            DialogAnimationController<LetterboxEduDialogLayout> animationController,
            DockStateReader dockStateReader) {
        super(context, taskInfo, syncQueue, taskListener, displayLayout);
        mTransitions = transitions;
        mOnDismissCallback = onDismissCallback;
@@ -160,7 +164,7 @@ public class LetterboxEduWindowManager extends CompatUIWindowManagerAbstract {
        if (mLayout == null) {
            return;
        }
        final View dialogContainer = mLayout.getDialogContainer();
        final View dialogContainer = mLayout.getDialogContainerView();
        MarginLayoutParams marginParams = (MarginLayoutParams) dialogContainer.getLayoutParams();

        final Rect taskBounds = getTaskBounds();
+2 −2
Original line number Diff line number Diff line
@@ -68,11 +68,11 @@ public class LetterboxEduDialogLayoutTest extends ShellTestCase {

    @Test
    public void testOnFinishInflate() {
        assertEquals(mLayout.getDialogContainer(),
        assertEquals(mLayout.getDialogContainerView(),
                mLayout.findViewById(R.id.letterbox_education_dialog_container));
        assertEquals(mLayout.getDialogTitle(),
                mLayout.findViewById(R.id.letterbox_education_dialog_title));
        assertEquals(mLayout.getBackgroundDim(), mLayout.getBackground());
        assertEquals(mLayout.getBackgroundDimDrawable(), mLayout.getBackground());
        assertEquals(mLayout.getBackground().getAlpha(), 0);
    }

Loading