| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell; |
| 18 |
|
| 19 |
import static android.view.WindowManager.TRANSIT_OPEN; |
| 20 |
import static android.view.WindowManager.TRANSIT_TO_BACK; |
| 21 |
import static android.view.WindowManager.TRANSIT_TO_FRONT; |
| 22 |
|
| 23 |
import android.annotation.NonNull; |
| 24 |
import android.annotation.Nullable; |
| 25 |
import android.app.ActivityManager; |
| 26 |
import android.os.IBinder; |
| 27 |
import android.util.Slog; |
| 28 |
import android.view.SurfaceControl; |
| 29 |
import android.view.WindowManager; |
| 30 |
import android.window.TransitionInfo; |
| 31 |
import android.window.TransitionRequestInfo; |
| 32 |
import android.window.WindowContainerTransaction; |
| 33 |
|
| 34 |
import com.android.wm.shell.transition.Transitions; |
| 35 |
|
| 36 |
import java.util.ArrayList; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class TaskViewTransitions implements Transitions.TransitionHandler { |
| 42 |
private static final String TAG = "TaskViewTransitions"; |
| 43 |
|
| 44 |
private final ArrayList<TaskView> mTaskViews = new ArrayList<>(); |
| 45 |
private final ArrayList<PendingTransition> mPending = new ArrayList<>(); |
| 46 |
private final Transitions mTransitions; |
| 47 |
private final boolean[] mRegistered = new boolean[]{ false }; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private static class PendingTransition { |
| 55 |
final @WindowManager.TransitionType int mType; |
| 56 |
final WindowContainerTransaction mWct; |
| 57 |
final @NonNull TaskView mTaskView; |
| 58 |
IBinder mClaimed; |
| 59 |
|
| 60 |
PendingTransition(@WindowManager.TransitionType int type, |
| 61 |
@Nullable WindowContainerTransaction wct, @NonNull TaskView taskView) { |
| 62 |
mType = type; |
| 63 |
mWct = wct; |
| 64 |
mTaskView = taskView; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public TaskViewTransitions(Transitions transitions) { |
| 69 |
mTransitions = transitions; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
void addTaskView(TaskView tv) { |
| 76 |
synchronized (mRegistered) { |
| 77 |
if (!mRegistered[0]) { |
| 78 |
mRegistered[0] = true; |
| 79 |
mTransitions.addHandler(this); |
| 80 |
} |
| 81 |
} |
| 82 |
mTaskViews.add(tv); |
| 83 |
} |
| 84 |
|
| 85 |
void removeTaskView(TaskView tv) { |
| 86 |
mTaskViews.remove(tv); |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
boolean isEnabled() { |
| 91 |
return mTransitions.isRegistered(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
private PendingTransition findPending(TaskView taskView, boolean closing, boolean latest) { |
| 105 |
for (int i = mPending.size() - 1; i >= 0; --i) { |
| 106 |
if (mPending.get(i).mTaskView != taskView) continue; |
| 107 |
if (Transitions.isClosingType(mPending.get(i).mType) == closing) { |
| 108 |
return mPending.get(i); |
| 109 |
} |
| 110 |
if (latest) { |
| 111 |
return null; |
| 112 |
} |
| 113 |
} |
| 114 |
return null; |
| 115 |
} |
| 116 |
|
| 117 |
private PendingTransition findPending(IBinder claimed) { |
| 118 |
for (int i = 0; i < mPending.size(); ++i) { |
| 119 |
if (mPending.get(i).mClaimed != claimed) continue; |
| 120 |
return mPending.get(i); |
| 121 |
} |
| 122 |
return null; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
public boolean hasPending() { |
| 127 |
return !mPending.isEmpty(); |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public WindowContainerTransaction handleRequest(@NonNull IBinder transition, |
| 132 |
@Nullable TransitionRequestInfo request) { |
| 133 |
final ActivityManager.RunningTaskInfo triggerTask = request.getTriggerTask(); |
| 134 |
if (triggerTask == null) { |
| 135 |
return null; |
| 136 |
} |
| 137 |
final TaskView taskView = findTaskView(triggerTask); |
| 138 |
if (taskView == null) return null; |
| 139 |
|
| 140 |
if (!Transitions.isClosingType(request.getType())) return null; |
| 141 |
PendingTransition pending = findPending(taskView, true , false ); |
| 142 |
if (pending == null) { |
| 143 |
pending = new PendingTransition(request.getType(), null, taskView); |
| 144 |
} |
| 145 |
if (pending.mClaimed != null) { |
| 146 |
throw new IllegalStateException("Task is closing in 2 collecting transitions?" |
| 147 |
+ " This state doesn't make sense"); |
| 148 |
} |
| 149 |
pending.mClaimed = transition; |
| 150 |
return new WindowContainerTransaction(); |
| 151 |
} |
| 152 |
|
| 153 |
private TaskView findTaskView(ActivityManager.RunningTaskInfo taskInfo) { |
| 154 |
for (int i = 0; i < mTaskViews.size(); ++i) { |
| 155 |
if (mTaskViews.get(i).getTaskInfo() == null) continue; |
| 156 |
if (taskInfo.token.equals(mTaskViews.get(i).getTaskInfo().token)) { |
| 157 |
return mTaskViews.get(i); |
| 158 |
} |
| 159 |
} |
| 160 |
return null; |
| 161 |
} |
| 162 |
|
| 163 |
void startTaskView(WindowContainerTransaction wct, TaskView taskView) { |
| 164 |
mPending.add(new PendingTransition(TRANSIT_OPEN, wct, taskView)); |
| 165 |
startNextTransition(); |
| 166 |
} |
| 167 |
|
| 168 |
void setTaskViewVisible(TaskView taskView, boolean visible) { |
| 169 |
PendingTransition pending = findPending(taskView, !visible, true ); |
| 170 |
if (pending != null) { |
| 171 |
|
| 172 |
return; |
| 173 |
} |
| 174 |
if (taskView.getTaskInfo() == null) { |
| 175 |
|
| 176 |
return; |
| 177 |
} |
| 178 |
final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 179 |
wct.setHidden(taskView.getTaskInfo().token, !visible ); |
| 180 |
pending = new PendingTransition( |
| 181 |
visible ? TRANSIT_TO_FRONT : TRANSIT_TO_BACK, wct, taskView); |
| 182 |
mPending.add(pending); |
| 183 |
startNextTransition(); |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
private void startNextTransition() { |
| 188 |
if (mPending.isEmpty()) return; |
| 189 |
final PendingTransition pending = mPending.get(0); |
| 190 |
if (pending.mClaimed != null) { |
| 191 |
|
| 192 |
return; |
| 193 |
} |
| 194 |
pending.mClaimed = mTransitions.startTransition(pending.mType, pending.mWct, this); |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public boolean startAnimation(@NonNull IBinder transition, |
| 199 |
@NonNull TransitionInfo info, |
| 200 |
@NonNull SurfaceControl.Transaction startTransaction, |
| 201 |
@NonNull SurfaceControl.Transaction finishTransaction, |
| 202 |
@NonNull Transitions.TransitionFinishCallback finishCallback) { |
| 203 |
PendingTransition pending = findPending(transition); |
| 204 |
if (pending == null) return false; |
| 205 |
mPending.remove(pending); |
| 206 |
TaskView taskView = pending.mTaskView; |
| 207 |
final ArrayList<TransitionInfo.Change> tasks = new ArrayList<>(); |
| 208 |
for (int i = 0; i < info.getChanges().size(); ++i) { |
| 209 |
final TransitionInfo.Change chg = info.getChanges().get(i); |
| 210 |
if (chg.getTaskInfo() == null) continue; |
| 211 |
tasks.add(chg); |
| 212 |
} |
| 213 |
if (tasks.isEmpty()) { |
| 214 |
Slog.e(TAG, "Got a TaskView transition with no task."); |
| 215 |
return false; |
| 216 |
} |
| 217 |
WindowContainerTransaction wct = null; |
| 218 |
for (int i = 0; i < tasks.size(); ++i) { |
| 219 |
TransitionInfo.Change chg = tasks.get(i); |
| 220 |
if (Transitions.isClosingType(chg.getMode())) { |
| 221 |
final boolean isHide = chg.getMode() == TRANSIT_TO_BACK; |
| 222 |
TaskView tv = findTaskView(chg.getTaskInfo()); |
| 223 |
if (tv == null) { |
| 224 |
throw new IllegalStateException("TaskView transition is closing a " |
| 225 |
+ "non-taskview task "); |
| 226 |
} |
| 227 |
if (isHide) { |
| 228 |
tv.prepareHideAnimation(finishTransaction); |
| 229 |
} else { |
| 230 |
tv.prepareCloseAnimation(); |
| 231 |
} |
| 232 |
} else if (Transitions.isOpeningType(chg.getMode())) { |
| 233 |
final boolean taskIsNew = chg.getMode() == TRANSIT_OPEN; |
| 234 |
if (wct == null) wct = new WindowContainerTransaction(); |
| 235 |
TaskView tv = taskView; |
| 236 |
if (!taskIsNew) { |
| 237 |
tv = findTaskView(chg.getTaskInfo()); |
| 238 |
if (tv == null) { |
| 239 |
throw new IllegalStateException("TaskView transition is showing a " |
| 240 |
+ "non-taskview task "); |
| 241 |
} |
| 242 |
} |
| 243 |
tv.prepareOpenAnimation(taskIsNew, startTransaction, finishTransaction, |
| 244 |
chg.getTaskInfo(), chg.getLeash(), wct); |
| 245 |
} else { |
| 246 |
throw new IllegalStateException("Claimed transition isn't an opening or closing" |
| 247 |
+ " type: " + chg.getMode()); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
startTransaction.apply(); |
| 252 |
finishTransaction.apply(); |
| 253 |
finishCallback.onTransitionFinished(wct, null ); |
| 254 |
startNextTransition(); |
| 255 |
return true; |
| 256 |
} |
| 257 |
} |