| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.compatui; |
| 18 |
|
| 19 |
import static com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation; |
| 20 |
import static com.android.internal.R.styleable.WindowAnimation_windowExitAnimation; |
| 21 |
import static com.android.wm.shell.transition.Transitions.ENABLE_SHELL_TRANSITIONS; |
| 22 |
|
| 23 |
import android.animation.Animator; |
| 24 |
import android.animation.AnimatorListenerAdapter; |
| 25 |
import android.animation.ObjectAnimator; |
| 26 |
import android.annotation.AnyRes; |
| 27 |
import android.annotation.NonNull; |
| 28 |
import android.annotation.Nullable; |
| 29 |
import android.content.Context; |
| 30 |
import android.graphics.drawable.Drawable; |
| 31 |
import android.util.IntProperty; |
| 32 |
import android.util.Log; |
| 33 |
import android.util.Property; |
| 34 |
import android.view.ContextThemeWrapper; |
| 35 |
import android.view.View; |
| 36 |
import android.view.animation.Animation; |
| 37 |
|
| 38 |
import com.android.internal.policy.TransitionAnimation; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class DialogAnimationController<T extends DialogContainerSupplier> { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static final int BACKGROUND_DIM_ALPHA = 204; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private static final int ENTER_ANIM_START_DELAY_MILLIS = ENABLE_SHELL_TRANSITIONS ? 300 : 500; |
| 54 |
|
| 55 |
private final TransitionAnimation mTransitionAnimation; |
| 56 |
private final String mPackageName; |
| 57 |
private final String mTag; |
| 58 |
@AnyRes |
| 59 |
private final int mAnimStyleResId; |
| 60 |
|
| 61 |
@Nullable |
| 62 |
private Animation mDialogAnimation; |
| 63 |
@Nullable |
| 64 |
private Animator mBackgroundDimAnimator; |
| 65 |
|
| 66 |
public DialogAnimationController(Context context, String tag) { |
| 67 |
mTransitionAnimation = new TransitionAnimation(context, false, tag); |
| 68 |
mAnimStyleResId = (new ContextThemeWrapper(context, |
| 69 |
android.R.style.ThemeOverlay_Material_Dialog).getTheme()).obtainStyledAttributes( |
| 70 |
com.android.internal.R.styleable.Window).getResourceId( |
| 71 |
com.android.internal.R.styleable.Window_windowAnimationStyle, 0); |
| 72 |
mPackageName = context.getPackageName(); |
| 73 |
mTag = tag; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public void startEnterAnimation(@NonNull T layout, Runnable endCallback) { |
| 80 |
|
| 81 |
cancelAnimation(); |
| 82 |
|
| 83 |
final View dialogContainer = layout.getDialogContainerView(); |
| 84 |
mDialogAnimation = loadAnimation(WindowAnimation_windowEnterAnimation); |
| 85 |
if (mDialogAnimation == null) { |
| 86 |
endCallback.run(); |
| 87 |
return; |
| 88 |
} |
| 89 |
mDialogAnimation.setAnimationListener(getAnimationListener( |
| 90 |
() -> dialogContainer.setAlpha(1), |
| 91 |
() -> { |
| 92 |
mDialogAnimation = null; |
| 93 |
endCallback.run(); |
| 94 |
})); |
| 95 |
|
| 96 |
mBackgroundDimAnimator = getAlphaAnimator(layout.getBackgroundDimDrawable(), |
| 97 |
BACKGROUND_DIM_ALPHA, |
| 98 |
mDialogAnimation.getDuration()); |
| 99 |
mBackgroundDimAnimator.addListener(getDimAnimatorListener()); |
| 100 |
|
| 101 |
mDialogAnimation.setStartOffset(ENTER_ANIM_START_DELAY_MILLIS); |
| 102 |
mBackgroundDimAnimator.setStartDelay(ENTER_ANIM_START_DELAY_MILLIS); |
| 103 |
|
| 104 |
dialogContainer.startAnimation(mDialogAnimation); |
| 105 |
mBackgroundDimAnimator.start(); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public void startExitAnimation(@NonNull T layout, Runnable endCallback) { |
| 112 |
|
| 113 |
cancelAnimation(); |
| 114 |
|
| 115 |
final View dialogContainer = layout.getDialogContainerView(); |
| 116 |
mDialogAnimation = loadAnimation(WindowAnimation_windowExitAnimation); |
| 117 |
if (mDialogAnimation == null) { |
| 118 |
endCallback.run(); |
| 119 |
return; |
| 120 |
} |
| 121 |
mDialogAnimation.setAnimationListener(getAnimationListener( |
| 122 |
() -> {}, |
| 123 |
() -> { |
| 124 |
dialogContainer.setAlpha(0); |
| 125 |
mDialogAnimation = null; |
| 126 |
endCallback.run(); |
| 127 |
})); |
| 128 |
|
| 129 |
mBackgroundDimAnimator = getAlphaAnimator(layout.getBackgroundDimDrawable(), |
| 130 |
0, mDialogAnimation.getDuration()); |
| 131 |
mBackgroundDimAnimator.addListener(getDimAnimatorListener()); |
| 132 |
|
| 133 |
dialogContainer.startAnimation(mDialogAnimation); |
| 134 |
mBackgroundDimAnimator.start(); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public void cancelAnimation() { |
| 141 |
if (mDialogAnimation != null) { |
| 142 |
mDialogAnimation.cancel(); |
| 143 |
mDialogAnimation = null; |
| 144 |
} |
| 145 |
if (mBackgroundDimAnimator != null) { |
| 146 |
mBackgroundDimAnimator.cancel(); |
| 147 |
mBackgroundDimAnimator = null; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
private Animation loadAnimation(int animAttr) { |
| 152 |
Animation animation = mTransitionAnimation.loadAnimationAttr(mPackageName, mAnimStyleResId, |
| 153 |
animAttr, false); |
| 154 |
if (animation == null) { |
| 155 |
Log.e(mTag, "Failed to load animation " + animAttr); |
| 156 |
} |
| 157 |
return animation; |
| 158 |
} |
| 159 |
|
| 160 |
private Animation.AnimationListener getAnimationListener(Runnable startCallback, |
| 161 |
Runnable endCallback) { |
| 162 |
return new Animation.AnimationListener() { |
| 163 |
@Override |
| 164 |
public void onAnimationStart(Animation animation) { |
| 165 |
startCallback.run(); |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public void onAnimationEnd(Animation animation) { |
| 170 |
endCallback.run(); |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public void onAnimationRepeat(Animation animation) {} |
| 175 |
}; |
| 176 |
} |
| 177 |
|
| 178 |
private AnimatorListenerAdapter getDimAnimatorListener() { |
| 179 |
return new AnimatorListenerAdapter() { |
| 180 |
@Override |
| 181 |
public void onAnimationEnd(Animator animation) { |
| 182 |
mBackgroundDimAnimator = null; |
| 183 |
} |
| 184 |
}; |
| 185 |
} |
| 186 |
|
| 187 |
private static Animator getAlphaAnimator( |
| 188 |
Drawable drawable, int endAlpha, long duration) { |
| 189 |
Animator animator = ObjectAnimator.ofInt(drawable, DRAWABLE_ALPHA, endAlpha); |
| 190 |
animator.setDuration(duration); |
| 191 |
return animator; |
| 192 |
} |
| 193 |
|
| 194 |
private static final Property<Drawable, Integer> DRAWABLE_ALPHA = new IntProperty<Drawable>( |
| 195 |
"alpha") { |
| 196 |
@Override |
| 197 |
public void setValue(Drawable object, int value) { |
| 198 |
object.setAlpha(value); |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public Integer get(Drawable object) { |
| 203 |
return object.getAlpha(); |
| 204 |
} |
| 205 |
}; |
| 206 |
} |