| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.activityembedding; |
| 18 |
|
| 19 |
|
| 20 |
import static com.android.internal.policy.TransitionAnimation.WALLPAPER_TRANSITION_NONE; |
| 21 |
import static com.android.wm.shell.transition.TransitionAnimationHelper.loadAttributeAnimation; |
| 22 |
|
| 23 |
import android.content.Context; |
| 24 |
import android.graphics.Rect; |
| 25 |
import android.view.animation.AlphaAnimation; |
| 26 |
import android.view.animation.Animation; |
| 27 |
import android.view.animation.AnimationSet; |
| 28 |
import android.view.animation.AnimationUtils; |
| 29 |
import android.view.animation.ClipRectAnimation; |
| 30 |
import android.view.animation.Interpolator; |
| 31 |
import android.view.animation.LinearInterpolator; |
| 32 |
import android.view.animation.ScaleAnimation; |
| 33 |
import android.view.animation.TranslateAnimation; |
| 34 |
import android.window.TransitionInfo; |
| 35 |
|
| 36 |
import androidx.annotation.NonNull; |
| 37 |
|
| 38 |
import com.android.internal.policy.TransitionAnimation; |
| 39 |
import com.android.wm.shell.transition.Transitions; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class ActivityEmbeddingAnimationSpec { |
| 44 |
|
| 45 |
private static final String TAG = "ActivityEmbeddingAnimSpec"; |
| 46 |
private static final int CHANGE_ANIMATION_DURATION = 517; |
| 47 |
private static final int CHANGE_ANIMATION_FADE_DURATION = 80; |
| 48 |
private static final int CHANGE_ANIMATION_FADE_OFFSET = 30; |
| 49 |
|
| 50 |
private final Context mContext; |
| 51 |
private final TransitionAnimation mTransitionAnimation; |
| 52 |
private final Interpolator mFastOutExtraSlowInInterpolator; |
| 53 |
private final LinearInterpolator mLinearInterpolator; |
| 54 |
private float mTransitionAnimationScaleSetting; |
| 55 |
|
| 56 |
ActivityEmbeddingAnimationSpec(@NonNull Context context) { |
| 57 |
mContext = context; |
| 58 |
mTransitionAnimation = new TransitionAnimation(mContext, false , TAG); |
| 59 |
mFastOutExtraSlowInInterpolator = AnimationUtils.loadInterpolator( |
| 60 |
mContext, android.R.interpolator.fast_out_extra_slow_in); |
| 61 |
mLinearInterpolator = new LinearInterpolator(); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
void setAnimScaleSetting(float scale) { |
| 69 |
mTransitionAnimationScaleSetting = scale; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
@NonNull |
| 74 |
static Animation createNoopAnimation(@NonNull TransitionInfo.Change change) { |
| 75 |
|
| 76 |
final float alpha = Transitions.isClosingType(change.getMode()) ? 0f : 1f; |
| 77 |
return new AlphaAnimation(alpha, alpha); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
@NonNull |
| 82 |
Animation createChangeBoundsOpenAnimation(@NonNull TransitionInfo.Change change, |
| 83 |
@NonNull Rect parentBounds) { |
| 84 |
|
| 85 |
final Rect bounds = change.getEndAbsBounds(); |
| 86 |
final int startLeft; |
| 87 |
final int startTop; |
| 88 |
if (parentBounds.top == bounds.top && parentBounds.bottom == bounds.bottom) { |
| 89 |
|
| 90 |
startTop = 0; |
| 91 |
startLeft = parentBounds.left == bounds.left ? -bounds.width() : bounds.width(); |
| 92 |
} else { |
| 93 |
|
| 94 |
startTop = parentBounds.top == bounds.top ? -bounds.height() : bounds.height(); |
| 95 |
startLeft = 0; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
final Animation animation = new TranslateAnimation(startLeft, 0, startTop, 0); |
| 101 |
animation.setInterpolator(mFastOutExtraSlowInInterpolator); |
| 102 |
animation.setDuration(CHANGE_ANIMATION_DURATION); |
| 103 |
animation.initialize(bounds.width(), bounds.height(), bounds.width(), bounds.height()); |
| 104 |
animation.scaleCurrentDuration(mTransitionAnimationScaleSetting); |
| 105 |
return animation; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
@NonNull |
| 110 |
Animation createChangeBoundsCloseAnimation(@NonNull TransitionInfo.Change change, |
| 111 |
@NonNull Rect parentBounds) { |
| 112 |
|
| 113 |
final Rect bounds = change.getStartAbsBounds(); |
| 114 |
final int endTop; |
| 115 |
final int endLeft; |
| 116 |
if (parentBounds.top == bounds.top && parentBounds.bottom == bounds.bottom) { |
| 117 |
|
| 118 |
endTop = 0; |
| 119 |
endLeft = parentBounds.left == bounds.left ? -bounds.width() : bounds.width(); |
| 120 |
} else { |
| 121 |
|
| 122 |
endTop = parentBounds.top == bounds.top ? -bounds.height() : bounds.height(); |
| 123 |
endLeft = 0; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
final Animation animation = new TranslateAnimation(0, endLeft, 0, endTop); |
| 129 |
animation.setInterpolator(mFastOutExtraSlowInInterpolator); |
| 130 |
animation.setDuration(CHANGE_ANIMATION_DURATION); |
| 131 |
animation.initialize(bounds.width(), bounds.height(), bounds.width(), bounds.height()); |
| 132 |
animation.scaleCurrentDuration(mTransitionAnimationScaleSetting); |
| 133 |
return animation; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
@NonNull |
| 142 |
Animation[] createChangeBoundsChangeAnimations(@NonNull TransitionInfo.Change change, |
| 143 |
@NonNull Rect parentBounds) { |
| 144 |
|
| 145 |
|
| 146 |
final Rect startBounds = change.getStartAbsBounds(); |
| 147 |
final Rect endBounds = change.getEndAbsBounds(); |
| 148 |
float scaleX = ((float) startBounds.width()) / endBounds.width(); |
| 149 |
float scaleY = ((float) startBounds.height()) / endBounds.height(); |
| 150 |
|
| 151 |
|
| 152 |
float startScaleX = 1.f / scaleX; |
| 153 |
float startScaleY = 1.f / scaleY; |
| 154 |
|
| 155 |
|
| 156 |
final AnimationSet startSet = new AnimationSet(false ); |
| 157 |
final Animation startAlpha = new AlphaAnimation(1f, 0f); |
| 158 |
startAlpha.setInterpolator(mLinearInterpolator); |
| 159 |
startAlpha.setDuration(CHANGE_ANIMATION_FADE_DURATION); |
| 160 |
startAlpha.setStartOffset(CHANGE_ANIMATION_FADE_OFFSET); |
| 161 |
startSet.addAnimation(startAlpha); |
| 162 |
final Animation startScale = new ScaleAnimation(startScaleX, startScaleX, startScaleY, |
| 163 |
startScaleY); |
| 164 |
startScale.setInterpolator(mFastOutExtraSlowInInterpolator); |
| 165 |
startScale.setDuration(CHANGE_ANIMATION_DURATION); |
| 166 |
startSet.addAnimation(startScale); |
| 167 |
startSet.initialize(startBounds.width(), startBounds.height(), endBounds.width(), |
| 168 |
endBounds.height()); |
| 169 |
startSet.scaleCurrentDuration(mTransitionAnimationScaleSetting); |
| 170 |
|
| 171 |
|
| 172 |
final AnimationSet endSet = new AnimationSet(true ); |
| 173 |
endSet.setInterpolator(mFastOutExtraSlowInInterpolator); |
| 174 |
final Animation endScale = new ScaleAnimation(scaleX, 1, scaleY, 1); |
| 175 |
endScale.setDuration(CHANGE_ANIMATION_DURATION); |
| 176 |
endSet.addAnimation(endScale); |
| 177 |
|
| 178 |
|
| 179 |
final Animation endTranslate = new TranslateAnimation(startBounds.left - endBounds.left, 0, |
| 180 |
startBounds.top - endBounds.top, 0); |
| 181 |
endTranslate.setDuration(CHANGE_ANIMATION_DURATION); |
| 182 |
endSet.addAnimation(endTranslate); |
| 183 |
|
| 184 |
final Rect startClip = new Rect(startBounds); |
| 185 |
final Rect endClip = new Rect(endBounds); |
| 186 |
startClip.offsetTo(0, 0); |
| 187 |
endClip.offsetTo(0, 0); |
| 188 |
final Animation clipAnim = new ClipRectAnimation(startClip, endClip); |
| 189 |
clipAnim.setDuration(CHANGE_ANIMATION_DURATION); |
| 190 |
endSet.addAnimation(clipAnim); |
| 191 |
endSet.initialize(startBounds.width(), startBounds.height(), parentBounds.width(), |
| 192 |
parentBounds.height()); |
| 193 |
endSet.scaleCurrentDuration(mTransitionAnimationScaleSetting); |
| 194 |
|
| 195 |
return new Animation[]{startSet, endSet}; |
| 196 |
} |
| 197 |
|
| 198 |
@NonNull |
| 199 |
Animation loadOpenAnimation(@NonNull TransitionInfo info, |
| 200 |
@NonNull TransitionInfo.Change change, @NonNull Rect wholeAnimationBounds) { |
| 201 |
final boolean isEnter = Transitions.isOpeningType(change.getMode()); |
| 202 |
final Animation animation; |
| 203 |
if (shouldShowBackdrop(info, change)) { |
| 204 |
animation = mTransitionAnimation.loadDefaultAnimationRes(isEnter |
| 205 |
? com.android.internal.R.anim.task_fragment_clear_top_open_enter |
| 206 |
: com.android.internal.R.anim.task_fragment_clear_top_open_exit); |
| 207 |
} else { |
| 208 |
|
| 209 |
animation = mTransitionAnimation.loadDefaultAnimationRes(isEnter |
| 210 |
? com.android.internal.R.anim.activity_open_enter |
| 211 |
: com.android.internal.R.anim.activity_open_exit); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
animation.initialize(wholeAnimationBounds.width(), wholeAnimationBounds.height(), |
| 217 |
wholeAnimationBounds.width(), wholeAnimationBounds.height()); |
| 218 |
animation.scaleCurrentDuration(mTransitionAnimationScaleSetting); |
| 219 |
return animation; |
| 220 |
} |
| 221 |
|
| 222 |
@NonNull |
| 223 |
Animation loadCloseAnimation(@NonNull TransitionInfo info, |
| 224 |
@NonNull TransitionInfo.Change change, @NonNull Rect wholeAnimationBounds) { |
| 225 |
final boolean isEnter = Transitions.isOpeningType(change.getMode()); |
| 226 |
final Animation animation; |
| 227 |
if (shouldShowBackdrop(info, change)) { |
| 228 |
animation = mTransitionAnimation.loadDefaultAnimationRes(isEnter |
| 229 |
? com.android.internal.R.anim.task_fragment_clear_top_close_enter |
| 230 |
: com.android.internal.R.anim.task_fragment_clear_top_close_exit); |
| 231 |
} else { |
| 232 |
|
| 233 |
animation = mTransitionAnimation.loadDefaultAnimationRes(isEnter |
| 234 |
? com.android.internal.R.anim.activity_close_enter |
| 235 |
: com.android.internal.R.anim.activity_close_exit); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
animation.initialize(wholeAnimationBounds.width(), wholeAnimationBounds.height(), |
| 241 |
wholeAnimationBounds.width(), wholeAnimationBounds.height()); |
| 242 |
animation.scaleCurrentDuration(mTransitionAnimationScaleSetting); |
| 243 |
return animation; |
| 244 |
} |
| 245 |
|
| 246 |
private boolean shouldShowBackdrop(@NonNull TransitionInfo info, |
| 247 |
@NonNull TransitionInfo.Change change) { |
| 248 |
final Animation a = loadAttributeAnimation(info, change, WALLPAPER_TRANSITION_NONE, |
| 249 |
mTransitionAnimation); |
| 250 |
return a != null && a.getShowBackdrop(); |
| 251 |
} |
| 252 |
} |