| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.unfold; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.app.ActivityManager.RunningTaskInfo; |
| 21 |
import android.app.TaskInfo; |
| 22 |
import android.util.SparseArray; |
| 23 |
import android.view.SurfaceControl; |
| 24 |
|
| 25 |
import com.android.wm.shell.common.ShellExecutor; |
| 26 |
import com.android.wm.shell.common.TransactionPool; |
| 27 |
import com.android.wm.shell.sysui.ShellInit; |
| 28 |
import com.android.wm.shell.unfold.ShellUnfoldProgressProvider.UnfoldListener; |
| 29 |
import com.android.wm.shell.unfold.animation.UnfoldTaskAnimator; |
| 30 |
|
| 31 |
import java.util.List; |
| 32 |
import java.util.Optional; |
| 33 |
|
| 34 |
import dagger.Lazy; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class UnfoldAnimationController implements UnfoldListener { |
| 47 |
|
| 48 |
private final ShellUnfoldProgressProvider mUnfoldProgressProvider; |
| 49 |
private final ShellExecutor mExecutor; |
| 50 |
private final TransactionPool mTransactionPool; |
| 51 |
private final List<UnfoldTaskAnimator> mAnimators; |
| 52 |
private final Lazy<Optional<UnfoldTransitionHandler>> mUnfoldTransitionHandler; |
| 53 |
|
| 54 |
private final SparseArray<SurfaceControl> mTaskSurfaces = new SparseArray<>(); |
| 55 |
private final SparseArray<UnfoldTaskAnimator> mAnimatorsByTaskId = new SparseArray<>(); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private boolean mIsInStageChange; |
| 62 |
|
| 63 |
public UnfoldAnimationController( |
| 64 |
@NonNull ShellInit shellInit, |
| 65 |
@NonNull TransactionPool transactionPool, |
| 66 |
@NonNull ShellUnfoldProgressProvider unfoldProgressProvider, |
| 67 |
@NonNull List<UnfoldTaskAnimator> animators, |
| 68 |
@NonNull Lazy<Optional<UnfoldTransitionHandler>> unfoldTransitionHandler, |
| 69 |
@NonNull ShellExecutor executor) { |
| 70 |
mUnfoldProgressProvider = unfoldProgressProvider; |
| 71 |
mUnfoldTransitionHandler = unfoldTransitionHandler; |
| 72 |
mTransactionPool = transactionPool; |
| 73 |
mExecutor = executor; |
| 74 |
mAnimators = animators; |
| 75 |
|
| 76 |
|
| 77 |
if (unfoldProgressProvider != ShellUnfoldProgressProvider.NO_PROVIDER) { |
| 78 |
shellInit.addInitCallback(this::onInit, this); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public void onInit() { |
| 86 |
mUnfoldProgressProvider.addListener(mExecutor, this); |
| 87 |
|
| 88 |
for (int i = 0; i < mAnimators.size(); i++) { |
| 89 |
final UnfoldTaskAnimator animator = mAnimators.get(i); |
| 90 |
animator.init(); |
| 91 |
|
| 92 |
mExecutor.executeDelayed(animator::start, 0); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public void onTaskAppeared(RunningTaskInfo taskInfo, SurfaceControl leash) { |
| 102 |
mTaskSurfaces.put(taskInfo.taskId, leash); |
| 103 |
|
| 104 |
|
| 105 |
for (int i = 0; i < mAnimators.size(); i++) { |
| 106 |
final UnfoldTaskAnimator animator = mAnimators.get(i); |
| 107 |
if (animator.isApplicableTask(taskInfo)) { |
| 108 |
mAnimatorsByTaskId.put(taskInfo.taskId, animator); |
| 109 |
animator.onTaskAppeared(taskInfo, leash); |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public void onTaskInfoChanged(RunningTaskInfo taskInfo) { |
| 120 |
final UnfoldTaskAnimator animator = mAnimatorsByTaskId.get(taskInfo.taskId); |
| 121 |
final boolean isCurrentlyApplicable = animator != null; |
| 122 |
|
| 123 |
if (isCurrentlyApplicable) { |
| 124 |
final boolean isApplicable = animator.isApplicableTask(taskInfo); |
| 125 |
if (isApplicable) { |
| 126 |
|
| 127 |
animator.onTaskChanged(taskInfo); |
| 128 |
} else { |
| 129 |
|
| 130 |
maybeResetTask(animator, taskInfo); |
| 131 |
animator.onTaskVanished(taskInfo); |
| 132 |
mAnimatorsByTaskId.remove(taskInfo.taskId); |
| 133 |
} |
| 134 |
} else { |
| 135 |
|
| 136 |
for (int i = 0; i < mAnimators.size(); i++) { |
| 137 |
final UnfoldTaskAnimator currentAnimator = mAnimators.get(i); |
| 138 |
if (currentAnimator.isApplicableTask(taskInfo)) { |
| 139 |
|
| 140 |
mAnimatorsByTaskId.put(taskInfo.taskId, currentAnimator); |
| 141 |
|
| 142 |
SurfaceControl leash = mTaskSurfaces.get(taskInfo.taskId); |
| 143 |
currentAnimator.onTaskAppeared(taskInfo, leash); |
| 144 |
break; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public void onTaskVanished(RunningTaskInfo taskInfo) { |
| 155 |
mTaskSurfaces.remove(taskInfo.taskId); |
| 156 |
|
| 157 |
final UnfoldTaskAnimator animator = mAnimatorsByTaskId.get(taskInfo.taskId); |
| 158 |
final boolean isCurrentlyApplicable = animator != null; |
| 159 |
|
| 160 |
if (isCurrentlyApplicable) { |
| 161 |
maybeResetTask(animator, taskInfo); |
| 162 |
animator.onTaskVanished(taskInfo); |
| 163 |
mAnimatorsByTaskId.remove(taskInfo.taskId); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public void onStateChangeStarted() { |
| 169 |
if (mUnfoldTransitionHandler.get().get().willHandleTransition()) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
mIsInStageChange = true; |
| 174 |
SurfaceControl.Transaction transaction = null; |
| 175 |
for (int i = 0; i < mAnimators.size(); i++) { |
| 176 |
final UnfoldTaskAnimator animator = mAnimators.get(i); |
| 177 |
if (animator.hasActiveTasks()) { |
| 178 |
if (transaction == null) transaction = mTransactionPool.acquire(); |
| 179 |
animator.prepareStartTransaction(transaction); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
if (transaction != null) { |
| 184 |
transaction.apply(); |
| 185 |
mTransactionPool.release(transaction); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public void onStateChangeProgress(float progress) { |
| 191 |
if (mUnfoldTransitionHandler.get().get().willHandleTransition()) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
SurfaceControl.Transaction transaction = null; |
| 196 |
for (int i = 0; i < mAnimators.size(); i++) { |
| 197 |
final UnfoldTaskAnimator animator = mAnimators.get(i); |
| 198 |
if (animator.hasActiveTasks()) { |
| 199 |
if (transaction == null) transaction = mTransactionPool.acquire(); |
| 200 |
animator.applyAnimationProgress(progress, transaction); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if (transaction != null) { |
| 205 |
transaction.apply(); |
| 206 |
mTransactionPool.release(transaction); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public void onStateChangeFinished() { |
| 212 |
if (mUnfoldTransitionHandler.get().get().willHandleTransition()) { |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
final SurfaceControl.Transaction transaction = mTransactionPool.acquire(); |
| 217 |
|
| 218 |
for (int i = 0; i < mAnimators.size(); i++) { |
| 219 |
final UnfoldTaskAnimator animator = mAnimators.get(i); |
| 220 |
animator.resetAllSurfaces(transaction); |
| 221 |
animator.prepareFinishTransaction(transaction); |
| 222 |
} |
| 223 |
|
| 224 |
transaction.apply(); |
| 225 |
|
| 226 |
mTransactionPool.release(transaction); |
| 227 |
mIsInStageChange = false; |
| 228 |
} |
| 229 |
|
| 230 |
private void maybeResetTask(UnfoldTaskAnimator animator, TaskInfo taskInfo) { |
| 231 |
if (!mIsInStageChange) { |
| 232 |
|
| 233 |
return; |
| 234 |
} |
| 235 |
final SurfaceControl.Transaction transaction = mTransactionPool.acquire(); |
| 236 |
animator.resetSurface(taskInfo, transaction); |
| 237 |
transaction.apply(); |
| 238 |
mTransactionPool.release(transaction); |
| 239 |
} |
| 240 |
} |