| 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.graphics.Matrix.MTRANS_X; |
| 20 |
import static android.graphics.Matrix.MTRANS_Y; |
| 21 |
import static android.view.RemoteAnimationTarget.MODE_CLOSING; |
| 22 |
|
| 23 |
import android.graphics.Point; |
| 24 |
import android.graphics.Rect; |
| 25 |
import android.view.Choreographer; |
| 26 |
import android.view.RemoteAnimationTarget; |
| 27 |
import android.view.SurfaceControl; |
| 28 |
import android.view.animation.Animation; |
| 29 |
import android.view.animation.Transformation; |
| 30 |
|
| 31 |
import androidx.annotation.NonNull; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class TaskFragmentAnimationAdapter { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private static final int LAYER_NO_OVERRIDE = -1; |
| 44 |
|
| 45 |
@NonNull |
| 46 |
final Animation mAnimation; |
| 47 |
@NonNull |
| 48 |
final RemoteAnimationTarget mTarget; |
| 49 |
@NonNull |
| 50 |
final SurfaceControl mLeash; |
| 51 |
|
| 52 |
@NonNull |
| 53 |
private final Rect mWholeAnimationBounds = new Rect(); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
@NonNull |
| 60 |
private final Rect mContentBounds = new Rect(); |
| 61 |
|
| 62 |
@NonNull |
| 63 |
private final Point mContentRelOffset = new Point(); |
| 64 |
|
| 65 |
@NonNull |
| 66 |
final Transformation mTransformation = new Transformation(); |
| 67 |
@NonNull |
| 68 |
final float[] mMatrix = new float[9]; |
| 69 |
@NonNull |
| 70 |
final float[] mVecs = new float[4]; |
| 71 |
@NonNull |
| 72 |
final Rect mRect = new Rect(); |
| 73 |
private boolean mIsFirstFrame = true; |
| 74 |
private int mOverrideLayer = LAYER_NO_OVERRIDE; |
| 75 |
|
| 76 |
TaskFragmentAnimationAdapter(@NonNull Animation animation, |
| 77 |
@NonNull RemoteAnimationTarget target) { |
| 78 |
this(animation, target, target.leash, target.screenSpaceBounds); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
TaskFragmentAnimationAdapter(@NonNull Animation animation, |
| 87 |
@NonNull RemoteAnimationTarget target, @NonNull SurfaceControl leash, |
| 88 |
@NonNull Rect wholeAnimationBounds) { |
| 89 |
mAnimation = animation; |
| 90 |
mTarget = target; |
| 91 |
mLeash = leash; |
| 92 |
mWholeAnimationBounds.set(wholeAnimationBounds); |
| 93 |
if (target.mode == MODE_CLOSING) { |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
final Rect startBounds = target.startBounds; |
| 98 |
final Rect endBounds = target.screenSpaceBounds; |
| 99 |
mContentBounds.set(startBounds); |
| 100 |
mContentRelOffset.set(target.localBounds.left, target.localBounds.top); |
| 101 |
mContentRelOffset.offset( |
| 102 |
startBounds.left - endBounds.left, |
| 103 |
startBounds.top - endBounds.top); |
| 104 |
} else { |
| 105 |
mContentBounds.set(target.screenSpaceBounds); |
| 106 |
mContentRelOffset.set(target.localBounds.left, target.localBounds.top); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
final void overrideLayer(int layer) { |
| 115 |
mOverrideLayer = layer; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
final void onAnimationUpdate(@NonNull SurfaceControl.Transaction t, long currentPlayTime) { |
| 120 |
if (mIsFirstFrame) { |
| 121 |
t.show(mLeash); |
| 122 |
if (mOverrideLayer != LAYER_NO_OVERRIDE) { |
| 123 |
t.setLayer(mLeash, mOverrideLayer); |
| 124 |
} |
| 125 |
mIsFirstFrame = false; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
mAnimation.getTransformation(Math.min(currentPlayTime, mAnimation.getDuration()), |
| 130 |
mTransformation); |
| 131 |
t.setFrameTimelineVsync(Choreographer.getInstance().getVsyncId()); |
| 132 |
onAnimationUpdateInner(t); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
void onAnimationUpdateInner(@NonNull SurfaceControl.Transaction t) { |
| 137 |
|
| 138 |
mTransformation.getMatrix().postTranslate(mContentRelOffset.x, mContentRelOffset.y); |
| 139 |
t.setMatrix(mLeash, mTransformation.getMatrix(), mMatrix); |
| 140 |
t.setAlpha(mLeash, mTransformation.getAlpha()); |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
final int positionX = Math.round(mMatrix[MTRANS_X]); |
| 145 |
final int positionY = Math.round(mMatrix[MTRANS_Y]); |
| 146 |
final Rect cropRect = new Rect(mContentBounds); |
| 147 |
cropRect.offset(positionX - mContentRelOffset.x, positionY - mContentRelOffset.y); |
| 148 |
|
| 149 |
|
| 150 |
final int offsetX = cropRect.left; |
| 151 |
final int offsetY = cropRect.top; |
| 152 |
|
| 153 |
|
| 154 |
if (!cropRect.intersect(mWholeAnimationBounds)) { |
| 155 |
|
| 156 |
t.setAlpha(mLeash, 0); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
cropRect.offset(-offsetX, -offsetY); |
| 161 |
t.setCrop(mLeash, cropRect); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
final void onAnimationEnd(@NonNull SurfaceControl.Transaction t) { |
| 166 |
onAnimationUpdate(t, mAnimation.getDuration()); |
| 167 |
} |
| 168 |
|
| 169 |
final long getDurationHint() { |
| 170 |
return mAnimation.computeDurationHint(); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
static class SnapshotAdapter extends TaskFragmentAnimationAdapter { |
| 178 |
|
| 179 |
SnapshotAdapter(@NonNull Animation animation, @NonNull RemoteAnimationTarget target) { |
| 180 |
|
| 181 |
super(animation, target, target.startLeash, target.screenSpaceBounds); |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
void onAnimationUpdateInner(@NonNull SurfaceControl.Transaction t) { |
| 186 |
|
| 187 |
mTransformation.getMatrix().postTranslate(0, 0); |
| 188 |
t.setMatrix(mLeash, mTransformation.getMatrix(), mMatrix); |
| 189 |
t.setAlpha(mLeash, mTransformation.getAlpha()); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
static class BoundsChangeAdapter extends TaskFragmentAnimationAdapter { |
| 197 |
|
| 198 |
BoundsChangeAdapter(@NonNull Animation animation, @NonNull RemoteAnimationTarget target) { |
| 199 |
super(animation, target); |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
void onAnimationUpdateInner(@NonNull SurfaceControl.Transaction t) { |
| 204 |
mTransformation.getMatrix().postTranslate( |
| 205 |
mTarget.localBounds.left, mTarget.localBounds.top); |
| 206 |
t.setMatrix(mLeash, mTransformation.getMatrix(), mMatrix); |
| 207 |
t.setAlpha(mLeash, mTransformation.getAlpha()); |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
mVecs[1] = mVecs[2] = 0; |
| 212 |
mVecs[0] = mVecs[3] = 1; |
| 213 |
mTransformation.getMatrix().mapVectors(mVecs); |
| 214 |
mVecs[0] = 1.f / mVecs[0]; |
| 215 |
mVecs[3] = 1.f / mVecs[3]; |
| 216 |
final Rect clipRect = mTransformation.getClipRect(); |
| 217 |
mRect.left = (int) (clipRect.left * mVecs[0] + 0.5f); |
| 218 |
mRect.right = (int) (clipRect.right * mVecs[0] + 0.5f); |
| 219 |
mRect.top = (int) (clipRect.top * mVecs[3] + 0.5f); |
| 220 |
mRect.bottom = (int) (clipRect.bottom * mVecs[3] + 0.5f); |
| 221 |
t.setWindowCrop(mLeash, mRect); |
| 222 |
} |
| 223 |
} |
| 224 |
} |