| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.window; |
| 18 |
|
| 19 |
import static android.view.WindowManager.TRANSIT_CHANGE; |
| 20 |
import static android.view.WindowManager.TRANSIT_CLOSE; |
| 21 |
import static android.view.WindowManager.TRANSIT_NONE; |
| 22 |
import static android.view.WindowManager.TRANSIT_OPEN; |
| 23 |
import static android.window.TaskFragmentTransaction.TYPE_ACTIVITY_REPARENTED_TO_TASK; |
| 24 |
import static android.window.TaskFragmentTransaction.TYPE_TASK_FRAGMENT_APPEARED; |
| 25 |
import static android.window.TaskFragmentTransaction.TYPE_TASK_FRAGMENT_ERROR; |
| 26 |
import static android.window.TaskFragmentTransaction.TYPE_TASK_FRAGMENT_INFO_CHANGED; |
| 27 |
import static android.window.TaskFragmentTransaction.TYPE_TASK_FRAGMENT_PARENT_INFO_CHANGED; |
| 28 |
import static android.window.TaskFragmentTransaction.TYPE_TASK_FRAGMENT_VANISHED; |
| 29 |
import static android.window.WindowContainerTransaction.HierarchyOp.HIERARCHY_OP_TYPE_CREATE_TASK_FRAGMENT; |
| 30 |
import static android.window.WindowContainerTransaction.HierarchyOp.HIERARCHY_OP_TYPE_DELETE_TASK_FRAGMENT; |
| 31 |
import static android.window.WindowContainerTransaction.HierarchyOp.HIERARCHY_OP_TYPE_REPARENT_ACTIVITY_TO_TASK_FRAGMENT; |
| 32 |
|
| 33 |
import android.annotation.CallSuper; |
| 34 |
import android.annotation.NonNull; |
| 35 |
import android.annotation.Nullable; |
| 36 |
import android.annotation.TestApi; |
| 37 |
import android.app.WindowConfiguration; |
| 38 |
import android.content.res.Configuration; |
| 39 |
import android.os.Bundle; |
| 40 |
import android.os.IBinder; |
| 41 |
import android.os.RemoteException; |
| 42 |
import android.util.SparseArray; |
| 43 |
import android.view.RemoteAnimationDefinition; |
| 44 |
import android.view.WindowManager; |
| 45 |
|
| 46 |
import java.util.ArrayList; |
| 47 |
import java.util.List; |
| 48 |
import java.util.concurrent.Executor; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
@TestApi |
| 55 |
public class TaskFragmentOrganizer extends WindowOrganizer { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static final String KEY_ERROR_CALLBACK_THROWABLE = "fragment_throwable"; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static final String KEY_ERROR_CALLBACK_TASK_FRAGMENT_INFO = "task_fragment_info"; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static final String KEY_ERROR_CALLBACK_OP_TYPE = "operation_type"; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static @NonNull Bundle putErrorInfoInBundle(@NonNull Throwable exception, |
| 83 |
@Nullable TaskFragmentInfo info, int opType) { |
| 84 |
final Bundle errorBundle = new Bundle(); |
| 85 |
errorBundle.putSerializable(KEY_ERROR_CALLBACK_THROWABLE, exception); |
| 86 |
if (info != null) { |
| 87 |
errorBundle.putParcelable(KEY_ERROR_CALLBACK_TASK_FRAGMENT_INFO, info); |
| 88 |
} |
| 89 |
errorBundle.putInt(KEY_ERROR_CALLBACK_OP_TYPE, opType); |
| 90 |
return errorBundle; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private final Executor mExecutor; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private final SparseArray<List<IBinder>> mTaskIdToFragmentTokens = new SparseArray<>(); |
| 101 |
|
| 102 |
private final SparseArray<Configuration> mTaskIdToConfigurations = new SparseArray<>(); |
| 103 |
|
| 104 |
public TaskFragmentOrganizer(@NonNull Executor executor) { |
| 105 |
mExecutor = executor; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
@NonNull |
| 112 |
public Executor getExecutor() { |
| 113 |
return mExecutor; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
@CallSuper |
| 120 |
public void registerOrganizer() { |
| 121 |
try { |
| 122 |
getController().registerOrganizer(mInterface); |
| 123 |
} catch (RemoteException e) { |
| 124 |
throw e.rethrowFromSystemServer(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
@CallSuper |
| 132 |
public void unregisterOrganizer() { |
| 133 |
try { |
| 134 |
getController().unregisterOrganizer(mInterface); |
| 135 |
} catch (RemoteException e) { |
| 136 |
throw e.rethrowFromSystemServer(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
@CallSuper |
| 147 |
public void registerRemoteAnimations(@NonNull RemoteAnimationDefinition definition) { |
| 148 |
try { |
| 149 |
getController().registerRemoteAnimations(mInterface, definition); |
| 150 |
} catch (RemoteException e) { |
| 151 |
throw e.rethrowFromSystemServer(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
@CallSuper |
| 160 |
public void unregisterRemoteAnimations() { |
| 161 |
try { |
| 162 |
getController().unregisterRemoteAnimations(mInterface); |
| 163 |
} catch (RemoteException e) { |
| 164 |
throw e.rethrowFromSystemServer(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public void onTransactionHandled(@NonNull IBinder transactionToken, |
| 187 |
@NonNull WindowContainerTransaction wct, |
| 188 |
@WindowManager.TransitionType int transitionType, boolean shouldApplyIndependently) { |
| 189 |
wct.setTaskFragmentOrganizer(mInterface); |
| 190 |
try { |
| 191 |
getController().onTransactionHandled(transactionToken, wct, transitionType, |
| 192 |
shouldApplyIndependently); |
| 193 |
} catch (RemoteException e) { |
| 194 |
throw e.rethrowFromSystemServer(); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
@Override |
| 205 |
public void applyTransaction(@NonNull WindowContainerTransaction wct) { |
| 206 |
|
| 207 |
applyTransaction(wct, getTransitionType(wct), false ); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public void applyTransaction(@NonNull WindowContainerTransaction wct, |
| 225 |
@WindowManager.TransitionType int transitionType, boolean shouldApplyIndependently) { |
| 226 |
if (wct.isEmpty()) { |
| 227 |
return; |
| 228 |
} |
| 229 |
wct.setTaskFragmentOrganizer(mInterface); |
| 230 |
try { |
| 231 |
getController().applyTransaction(wct, transitionType, shouldApplyIndependently); |
| 232 |
} catch (RemoteException e) { |
| 233 |
throw e.rethrowFromSystemServer(); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
@WindowManager.TransitionType |
| 244 |
public static int getTransitionType(@NonNull WindowContainerTransaction wct) { |
| 245 |
if (wct.isEmpty()) { |
| 246 |
return TRANSIT_NONE; |
| 247 |
} |
| 248 |
for (WindowContainerTransaction.Change change : wct.getChanges().values()) { |
| 249 |
if ((change.getWindowSetMask() & WindowConfiguration.WINDOW_CONFIG_BOUNDS) != 0) { |
| 250 |
|
| 251 |
return TRANSIT_CHANGE; |
| 252 |
} |
| 253 |
} |
| 254 |
boolean containsCreatingTaskFragment = false; |
| 255 |
boolean containsDeleteTaskFragment = false; |
| 256 |
final List<WindowContainerTransaction.HierarchyOp> ops = wct.getHierarchyOps(); |
| 257 |
for (int i = ops.size() - 1; i >= 0; i--) { |
| 258 |
final int type = ops.get(i).getType(); |
| 259 |
if (type == HIERARCHY_OP_TYPE_REPARENT_ACTIVITY_TO_TASK_FRAGMENT) { |
| 260 |
|
| 261 |
return TRANSIT_CHANGE; |
| 262 |
} |
| 263 |
if (type == HIERARCHY_OP_TYPE_CREATE_TASK_FRAGMENT) { |
| 264 |
containsCreatingTaskFragment = true; |
| 265 |
} else if (type == HIERARCHY_OP_TYPE_DELETE_TASK_FRAGMENT) { |
| 266 |
containsDeleteTaskFragment = true; |
| 267 |
} |
| 268 |
} |
| 269 |
if (containsCreatingTaskFragment) { |
| 270 |
return TRANSIT_OPEN; |
| 271 |
} |
| 272 |
if (containsDeleteTaskFragment) { |
| 273 |
return TRANSIT_CLOSE; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
return TRANSIT_CHANGE; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
@Deprecated |
| 287 |
public void onTaskFragmentAppeared(@NonNull TaskFragmentInfo taskFragmentInfo) {} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
@Deprecated |
| 296 |
public void onTaskFragmentInfoChanged(@NonNull TaskFragmentInfo taskFragmentInfo) {} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
@Deprecated |
| 305 |
public void onTaskFragmentVanished(@NonNull TaskFragmentInfo taskFragmentInfo) {} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
@Deprecated |
| 321 |
public void onTaskFragmentParentInfoChanged( |
| 322 |
@NonNull IBinder fragmentToken, @NonNull Configuration parentConfig) {} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
@Deprecated |
| 334 |
public void onTaskFragmentError( |
| 335 |
@NonNull IBinder errorCallbackToken, @NonNull Throwable exception) {} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public void onTransactionReady(@NonNull TaskFragmentTransaction transaction) { |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
final WindowContainerTransaction wct = new WindowContainerTransaction(); |
| 347 |
final List<TaskFragmentTransaction.Change> changes = transaction.getChanges(); |
| 348 |
for (TaskFragmentTransaction.Change change : changes) { |
| 349 |
final int taskId = change.getTaskId(); |
| 350 |
switch (change.getType()) { |
| 351 |
case TYPE_TASK_FRAGMENT_APPEARED: |
| 352 |
if (!mTaskIdToFragmentTokens.contains(taskId)) { |
| 353 |
mTaskIdToFragmentTokens.put(taskId, new ArrayList<>()); |
| 354 |
} |
| 355 |
mTaskIdToFragmentTokens.get(taskId).add(change.getTaskFragmentToken()); |
| 356 |
onTaskFragmentParentInfoChanged(change.getTaskFragmentToken(), |
| 357 |
mTaskIdToConfigurations.get(taskId)); |
| 358 |
onTaskFragmentAppeared(change.getTaskFragmentInfo()); |
| 359 |
break; |
| 360 |
case TYPE_TASK_FRAGMENT_INFO_CHANGED: |
| 361 |
onTaskFragmentInfoChanged(change.getTaskFragmentInfo()); |
| 362 |
break; |
| 363 |
case TYPE_TASK_FRAGMENT_VANISHED: |
| 364 |
if (mTaskIdToFragmentTokens.contains(taskId)) { |
| 365 |
final List<IBinder> tokens = mTaskIdToFragmentTokens.get(taskId); |
| 366 |
tokens.remove(change.getTaskFragmentToken()); |
| 367 |
if (tokens.isEmpty()) { |
| 368 |
mTaskIdToFragmentTokens.remove(taskId); |
| 369 |
mTaskIdToConfigurations.remove(taskId); |
| 370 |
} |
| 371 |
} |
| 372 |
onTaskFragmentVanished(change.getTaskFragmentInfo()); |
| 373 |
break; |
| 374 |
case TYPE_TASK_FRAGMENT_PARENT_INFO_CHANGED: |
| 375 |
final Configuration parentConfig = change.getTaskConfiguration(); |
| 376 |
mTaskIdToConfigurations.put(taskId, parentConfig); |
| 377 |
final List<IBinder> tokens = mTaskIdToFragmentTokens.get(taskId); |
| 378 |
if (tokens == null || tokens.isEmpty()) { |
| 379 |
break; |
| 380 |
} |
| 381 |
for (int i = tokens.size() - 1; i >= 0; i--) { |
| 382 |
onTaskFragmentParentInfoChanged(tokens.get(i), parentConfig); |
| 383 |
} |
| 384 |
break; |
| 385 |
case TYPE_TASK_FRAGMENT_ERROR: |
| 386 |
final Bundle errorBundle = change.getErrorBundle(); |
| 387 |
onTaskFragmentError( |
| 388 |
change.getErrorCallbackToken(), |
| 389 |
errorBundle.getSerializable(KEY_ERROR_CALLBACK_THROWABLE, |
| 390 |
java.lang.Throwable.class)); |
| 391 |
break; |
| 392 |
case TYPE_ACTIVITY_REPARENTED_TO_TASK: |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
break; |
| 397 |
default: |
| 398 |
throw new IllegalArgumentException( |
| 399 |
"Unknown TaskFragmentEvent=" + change.getType()); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
onTransactionHandled(transaction.getTransactionToken(), wct, getTransitionType(wct), |
| 405 |
false ); |
| 406 |
} |
| 407 |
|
| 408 |
private final ITaskFragmentOrganizer mInterface = new ITaskFragmentOrganizer.Stub() { |
| 409 |
@Override |
| 410 |
public void onTransactionReady(@NonNull TaskFragmentTransaction transaction) { |
| 411 |
mExecutor.execute(() -> TaskFragmentOrganizer.this.onTransactionReady(transaction)); |
| 412 |
} |
| 413 |
}; |
| 414 |
|
| 415 |
private final TaskFragmentOrganizerToken mToken = new TaskFragmentOrganizerToken(mInterface); |
| 416 |
|
| 417 |
@NonNull |
| 418 |
public TaskFragmentOrganizerToken getOrganizerToken() { |
| 419 |
return mToken; |
| 420 |
} |
| 421 |
|
| 422 |
private ITaskFragmentOrganizerController getController() { |
| 423 |
try { |
| 424 |
return getWindowOrganizerController().getTaskFragmentOrganizerController(); |
| 425 |
} catch (RemoteException e) { |
| 426 |
return null; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
public boolean isActivityEmbedded(@NonNull IBinder activityToken) { |
| 436 |
try { |
| 437 |
return getController().isActivityEmbedded(activityToken); |
| 438 |
} catch (RemoteException e) { |
| 439 |
throw e.rethrowFromSystemServer(); |
| 440 |
} |
| 441 |
} |
| 442 |
} |