| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package androidx.window.extensions.embedding; |
| 18 |
|
| 19 |
import static android.view.RemoteAnimationTarget.MODE_CLOSING; |
| 20 |
|
| 21 |
import android.app.ActivityThread; |
| 22 |
import android.content.ContentResolver; |
| 23 |
import android.content.Context; |
| 24 |
import android.database.ContentObserver; |
| 25 |
import android.graphics.Rect; |
| 26 |
import android.os.Handler; |
| 27 |
import android.provider.Settings; |
| 28 |
import android.view.RemoteAnimationTarget; |
| 29 |
import android.view.WindowManager; |
| 30 |
import android.view.animation.AlphaAnimation; |
| 31 |
import android.view.animation.Animation; |
| 32 |
import android.view.animation.AnimationSet; |
| 33 |
import android.view.animation.AnimationUtils; |
| 34 |
import android.view.animation.ClipRectAnimation; |
| 35 |
import android.view.animation.Interpolator; |
| 36 |
import android.view.animation.LinearInterpolator; |
| 37 |
import android.view.animation.ScaleAnimation; |
| 38 |
import android.view.animation.TranslateAnimation; |
| 39 |
|
| 40 |
import androidx.annotation.NonNull; |
| 41 |
|
| 42 |
import com.android.internal.R; |
| 43 |
import com.android.internal.policy.AttributeCache; |
| 44 |
import com.android.internal.policy.TransitionAnimation; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class TaskFragmentAnimationSpec { |
| 49 |
|
| 50 |
private static final String TAG = "TaskFragAnimationSpec"; |
| 51 |
private static final int CHANGE_ANIMATION_DURATION = 517; |
| 52 |
private static final int CHANGE_ANIMATION_FADE_DURATION = 80; |
| 53 |
private static final int CHANGE_ANIMATION_FADE_OFFSET = 30; |
| 54 |
|
| 55 |
private final Context mContext; |
| 56 |
private final TransitionAnimation mTransitionAnimation; |
| 57 |
private final Interpolator mFastOutExtraSlowInInterpolator; |
| 58 |
private final LinearInterpolator mLinearInterpolator; |
| 59 |
private float mTransitionAnimationScaleSetting; |
| 60 |
|
| 61 |
TaskFragmentAnimationSpec(@NonNull Handler handler) { |
| 62 |
mContext = ActivityThread.currentActivityThread().getApplication(); |
| 63 |
mTransitionAnimation = new TransitionAnimation(mContext, false , TAG); |
| 64 |
|
| 65 |
AttributeCache.init(mContext); |
| 66 |
mFastOutExtraSlowInInterpolator = AnimationUtils.loadInterpolator( |
| 67 |
mContext, android.R.interpolator.fast_out_extra_slow_in); |
| 68 |
mLinearInterpolator = new LinearInterpolator(); |
| 69 |
|
| 70 |
|
| 71 |
final ContentResolver resolver = mContext.getContentResolver(); |
| 72 |
mTransitionAnimationScaleSetting = getTransitionAnimationScaleSetting(); |
| 73 |
resolver.registerContentObserver( |
| 74 |
Settings.Global.getUriFor(Settings.Global.TRANSITION_ANIMATION_SCALE), false, |
| 75 |
new SettingsObserver(handler)); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
@NonNull |
| 80 |
static Animation createNoopAnimation(@NonNull RemoteAnimationTarget target) { |
| 81 |
|
| 82 |
final float alpha = target.mode == MODE_CLOSING ? 0f : 1f; |
| 83 |
return new AlphaAnimation(alpha, alpha); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
@NonNull |
| 88 |
Animation createChangeBoundsOpenAnimation(@NonNull RemoteAnimationTarget target) { |
| 89 |
final Rect parentBounds = target.taskInfo.configuration.windowConfiguration.getBounds(); |
| 90 |
final Rect bounds = target.screenSpaceBounds; |
| 91 |
final int startLeft; |
| 92 |
final int startTop; |
| 93 |
if (parentBounds.top == bounds.top && parentBounds.bottom == bounds.bottom) { |
| 94 |
|
| 95 |
startTop = 0; |
| 96 |
startLeft = parentBounds.left == bounds.left ? -bounds.width() : bounds.width(); |
| 97 |
} else { |
| 98 |
|
| 99 |
startTop = parentBounds.top == bounds.top ? -bounds.height() : bounds.height(); |
| 100 |
startLeft = 0; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
final Animation animation = new TranslateAnimation(startLeft, 0, startTop, 0); |
| 106 |
animation.setInterpolator(mFastOutExtraSlowInInterpolator); |
| 107 |
animation.setDuration(CHANGE_ANIMATION_DURATION); |
| 108 |
animation.initialize(bounds.width(), bounds.height(), bounds.width(), bounds.height()); |
| 109 |
animation.scaleCurrentDuration(mTransitionAnimationScaleSetting); |
| 110 |
return animation; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
@NonNull |
| 115 |
Animation createChangeBoundsCloseAnimation(@NonNull RemoteAnimationTarget target) { |
| 116 |
final Rect parentBounds = target.taskInfo.configuration.windowConfiguration.getBounds(); |
| 117 |
|
| 118 |
final Rect bounds = target.startBounds; |
| 119 |
final int endTop; |
| 120 |
final int endLeft; |
| 121 |
if (parentBounds.top == bounds.top && parentBounds.bottom == bounds.bottom) { |
| 122 |
|
| 123 |
endTop = 0; |
| 124 |
endLeft = parentBounds.left == bounds.left ? -bounds.width() : bounds.width(); |
| 125 |
} else { |
| 126 |
|
| 127 |
endTop = parentBounds.top == bounds.top ? -bounds.height() : bounds.height(); |
| 128 |
endLeft = 0; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
final Animation animation = new TranslateAnimation(0, endLeft, 0, endTop); |
| 134 |
animation.setInterpolator(mFastOutExtraSlowInInterpolator); |
| 135 |
animation.setDuration(CHANGE_ANIMATION_DURATION); |
| 136 |
animation.initialize(bounds.width(), bounds.height(), bounds.width(), bounds.height()); |
| 137 |
animation.scaleCurrentDuration(mTransitionAnimationScaleSetting); |
| 138 |
return animation; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
@NonNull |
| 147 |
Animation[] createChangeBoundsChangeAnimations(@NonNull RemoteAnimationTarget target) { |
| 148 |
|
| 149 |
|
| 150 |
final Rect startBounds = target.startBounds; |
| 151 |
final Rect parentBounds = target.taskInfo.configuration.windowConfiguration.getBounds(); |
| 152 |
final Rect endBounds = target.screenSpaceBounds; |
| 153 |
float scaleX = ((float) startBounds.width()) / endBounds.width(); |
| 154 |
float scaleY = ((float) startBounds.height()) / endBounds.height(); |
| 155 |
|
| 156 |
|
| 157 |
float startScaleX = 1.f / scaleX; |
| 158 |
float startScaleY = 1.f / scaleY; |
| 159 |
|
| 160 |
|
| 161 |
final AnimationSet startSet = new AnimationSet(false ); |
| 162 |
final Animation startAlpha = new AlphaAnimation(1f, 0f); |
| 163 |
startAlpha.setInterpolator(mLinearInterpolator); |
| 164 |
startAlpha.setDuration(CHANGE_ANIMATION_FADE_DURATION); |
| 165 |
startAlpha.setStartOffset(CHANGE_ANIMATION_FADE_OFFSET); |
| 166 |
startSet.addAnimation(startAlpha); |
| 167 |
final Animation startScale = new ScaleAnimation(startScaleX, startScaleX, startScaleY, |
| 168 |
startScaleY); |
| 169 |
startScale.setInterpolator(mFastOutExtraSlowInInterpolator); |
| 170 |
startScale.setDuration(CHANGE_ANIMATION_DURATION); |
| 171 |
startSet.addAnimation(startScale); |
| 172 |
startSet.initialize(startBounds.width(), startBounds.height(), endBounds.width(), |
| 173 |
endBounds.height()); |
| 174 |
startSet.scaleCurrentDuration(mTransitionAnimationScaleSetting); |
| 175 |
|
| 176 |
|
| 177 |
final AnimationSet endSet = new AnimationSet(true ); |
| 178 |
endSet.setInterpolator(mFastOutExtraSlowInInterpolator); |
| 179 |
final Animation endScale = new ScaleAnimation(scaleX, 1, scaleY, 1); |
| 180 |
endScale.setDuration(CHANGE_ANIMATION_DURATION); |
| 181 |
endSet.addAnimation(endScale); |
| 182 |
|
| 183 |
|
| 184 |
final Animation endTranslate = new TranslateAnimation(startBounds.left - endBounds.left, 0, |
| 185 |
startBounds.top - endBounds.top, 0); |
| 186 |
endTranslate.setDuration(CHANGE_ANIMATION_DURATION); |
| 187 |
endSet.addAnimation(endTranslate); |
| 188 |
|
| 189 |
final Rect startClip = new Rect(startBounds); |
| 190 |
final Rect endClip = new Rect(endBounds); |
| 191 |
startClip.offsetTo(0, 0); |
| 192 |
endClip.offsetTo(0, 0); |
| 193 |
final Animation clipAnim = new ClipRectAnimation(startClip, endClip); |
| 194 |
clipAnim.setDuration(CHANGE_ANIMATION_DURATION); |
| 195 |
endSet.addAnimation(clipAnim); |
| 196 |
endSet.initialize(startBounds.width(), startBounds.height(), parentBounds.width(), |
| 197 |
parentBounds.height()); |
| 198 |
endSet.scaleCurrentDuration(mTransitionAnimationScaleSetting); |
| 199 |
|
| 200 |
return new Animation[]{startSet, endSet}; |
| 201 |
} |
| 202 |
|
| 203 |
@NonNull |
| 204 |
Animation loadOpenAnimation(@NonNull RemoteAnimationTarget target, |
| 205 |
@NonNull Rect wholeAnimationBounds) { |
| 206 |
final boolean isEnter = target.mode != MODE_CLOSING; |
| 207 |
final Animation animation; |
| 208 |
|
| 209 |
|
| 210 |
if (target.showBackdrop) { |
| 211 |
animation = mTransitionAnimation.loadDefaultAnimationRes(isEnter |
| 212 |
? com.android.internal.R.anim.task_fragment_clear_top_open_enter |
| 213 |
: com.android.internal.R.anim.task_fragment_clear_top_open_exit); |
| 214 |
} else { |
| 215 |
animation = mTransitionAnimation.loadDefaultAnimationRes(isEnter |
| 216 |
? com.android.internal.R.anim.task_fragment_open_enter |
| 217 |
: com.android.internal.R.anim.task_fragment_open_exit); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
animation.initialize(wholeAnimationBounds.width(), wholeAnimationBounds.height(), |
| 223 |
wholeAnimationBounds.width(), wholeAnimationBounds.height()); |
| 224 |
animation.scaleCurrentDuration(mTransitionAnimationScaleSetting); |
| 225 |
return animation; |
| 226 |
} |
| 227 |
|
| 228 |
@NonNull |
| 229 |
Animation loadCloseAnimation(@NonNull RemoteAnimationTarget target, |
| 230 |
@NonNull Rect wholeAnimationBounds) { |
| 231 |
final boolean isEnter = target.mode != MODE_CLOSING; |
| 232 |
final Animation animation; |
| 233 |
if (target.showBackdrop) { |
| 234 |
animation = mTransitionAnimation.loadDefaultAnimationRes(isEnter |
| 235 |
? com.android.internal.R.anim.task_fragment_clear_top_close_enter |
| 236 |
: com.android.internal.R.anim.task_fragment_clear_top_close_exit); |
| 237 |
} else { |
| 238 |
animation = mTransitionAnimation.loadDefaultAnimationRes(isEnter |
| 239 |
? com.android.internal.R.anim.task_fragment_close_enter |
| 240 |
: com.android.internal.R.anim.task_fragment_close_exit); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
animation.initialize(wholeAnimationBounds.width(), wholeAnimationBounds.height(), |
| 246 |
wholeAnimationBounds.width(), wholeAnimationBounds.height()); |
| 247 |
animation.scaleCurrentDuration(mTransitionAnimationScaleSetting); |
| 248 |
return animation; |
| 249 |
} |
| 250 |
|
| 251 |
private float getTransitionAnimationScaleSetting() { |
| 252 |
return WindowManager.fixScale(Settings.Global.getFloat(mContext.getContentResolver(), |
| 253 |
Settings.Global.TRANSITION_ANIMATION_SCALE, mContext.getResources().getFloat( |
| 254 |
R.dimen.config_appTransitionAnimationDurationScaleDefault))); |
| 255 |
} |
| 256 |
|
| 257 |
private class SettingsObserver extends ContentObserver { |
| 258 |
SettingsObserver(@NonNull Handler handler) { |
| 259 |
super(handler); |
| 260 |
} |
| 261 |
|
| 262 |
@Override |
| 263 |
public void onChange(boolean selfChange) { |
| 264 |
mTransitionAnimationScaleSetting = getTransitionAnimationScaleSetting(); |
| 265 |
} |
| 266 |
} |
| 267 |
} |