| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import static android.view.Display.INVALID_DISPLAY; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.RequiresPermission; |
| 24 |
import android.annotation.SystemService; |
| 25 |
import android.annotation.TestApi; |
| 26 |
import android.compat.annotation.UnsupportedAppUsage; |
| 27 |
import android.content.Context; |
| 28 |
import android.content.pm.PackageManager; |
| 29 |
import android.content.res.Configuration; |
| 30 |
import android.content.res.Resources; |
| 31 |
import android.graphics.Rect; |
| 32 |
import android.os.Build; |
| 33 |
import android.os.IBinder; |
| 34 |
import android.os.Parcel; |
| 35 |
import android.os.Parcelable; |
| 36 |
import android.os.RemoteException; |
| 37 |
import android.os.ServiceManager; |
| 38 |
import android.util.DisplayMetrics; |
| 39 |
import android.util.Singleton; |
| 40 |
import android.view.RemoteAnimationDefinition; |
| 41 |
import android.window.SplashScreenView.SplashScreenViewParcelable; |
| 42 |
|
| 43 |
import java.util.List; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
@TestApi |
| 52 |
@SystemService(Context.ACTIVITY_TASK_SERVICE) |
| 53 |
public class ActivityTaskManager { |
| 54 |
|
| 55 |
|
| 56 |
public static final int INVALID_STACK_ID = -1; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public static final int INVALID_TASK_ID = -1; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static final int RESIZE_MODE_SYSTEM = 0; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static final int RESIZE_MODE_PRESERVE_WINDOW = (0x1 << 0); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public static final int RESIZE_MODE_USER = RESIZE_MODE_PRESERVE_WINDOW; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public static final int RESIZE_MODE_SYSTEM_SCREEN_ROTATION = RESIZE_MODE_PRESERVE_WINDOW; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public static final int RESIZE_MODE_FORCED = (0x1 << 1); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static final int RESIZE_MODE_USER_FORCED = |
| 108 |
RESIZE_MODE_PRESERVE_WINDOW | RESIZE_MODE_FORCED; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public static final String EXTRA_OPTIONS = "android.app.extra.OPTIONS"; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public static final String EXTRA_IGNORE_TARGET_SECURITY = |
| 125 |
"android.app.extra.EXTRA_IGNORE_TARGET_SECURITY"; |
| 126 |
|
| 127 |
|
| 128 |
public static final int DEFAULT_MINIMAL_SPLIT_SCREEN_DISPLAY_SIZE_DP = 440; |
| 129 |
|
| 130 |
private static int sMaxRecentTasks = -1; |
| 131 |
|
| 132 |
private static final Singleton<ActivityTaskManager> sInstance = |
| 133 |
new Singleton<ActivityTaskManager>() { |
| 134 |
@Override |
| 135 |
protected ActivityTaskManager create() { |
| 136 |
return new ActivityTaskManager(); |
| 137 |
} |
| 138 |
}; |
| 139 |
|
| 140 |
private ActivityTaskManager() { |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
public static ActivityTaskManager getInstance() { |
| 145 |
return sInstance.get(); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
public static IActivityTaskManager getService() { |
| 150 |
return IActivityTaskManagerSingleton.get(); |
| 151 |
} |
| 152 |
|
| 153 |
@UnsupportedAppUsage(trackingBug = 129726065) |
| 154 |
private static final Singleton<IActivityTaskManager> IActivityTaskManagerSingleton = |
| 155 |
new Singleton<IActivityTaskManager>() { |
| 156 |
@Override |
| 157 |
protected IActivityTaskManager create() { |
| 158 |
final IBinder b = ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE); |
| 159 |
return IActivityTaskManager.Stub.asInterface(b); |
| 160 |
} |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
@RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) |
| 168 |
public void removeRootTasksInWindowingModes(@NonNull int[] windowingModes) { |
| 169 |
try { |
| 170 |
getService().removeRootTasksInWindowingModes(windowingModes); |
| 171 |
} catch (RemoteException e) { |
| 172 |
throw e.rethrowFromSystemServer(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
@RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) |
| 178 |
public void removeRootTasksWithActivityTypes(@NonNull int[] activityTypes) { |
| 179 |
try { |
| 180 |
getService().removeRootTasksWithActivityTypes(activityTypes); |
| 181 |
} catch (RemoteException e) { |
| 182 |
throw e.rethrowFromSystemServer(); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
@RequiresPermission(android.Manifest.permission.REMOVE_TASKS) |
| 191 |
public void removeAllVisibleRecentTasks() { |
| 192 |
try { |
| 193 |
getService().removeAllVisibleRecentTasks(); |
| 194 |
} catch (RemoteException e) { |
| 195 |
throw e.rethrowFromSystemServer(); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public static int getMaxRecentTasksStatic() { |
| 204 |
if (sMaxRecentTasks < 0) { |
| 205 |
return sMaxRecentTasks = ActivityManager.isLowRamDeviceStatic() ? 36 : 48; |
| 206 |
} |
| 207 |
return sMaxRecentTasks; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public void onSplashScreenViewCopyFinished(int taskId, |
| 218 |
@Nullable SplashScreenViewParcelable parcelable) { |
| 219 |
try { |
| 220 |
getService().onSplashScreenViewCopyFinished(taskId, parcelable); |
| 221 |
} catch (RemoteException e) { |
| 222 |
throw e.rethrowFromSystemServer(); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public static int getDefaultAppRecentsLimitStatic() { |
| 231 |
return getMaxRecentTasksStatic() / 6; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public static int getMaxAppRecentsLimitStatic() { |
| 239 |
return getMaxRecentTasksStatic() / 2; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public static boolean supportsMultiWindow(Context context) { |
| 247 |
|
| 248 |
|
| 249 |
boolean isWatch = context.getPackageManager().hasSystemFeature( |
| 250 |
PackageManager.FEATURE_WATCH); |
| 251 |
return (!ActivityManager.isLowRamDeviceStatic() || isWatch) |
| 252 |
&& Resources.getSystem().getBoolean( |
| 253 |
com.android.internal.R.bool.config_supportsMultiWindow); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public static boolean supportsSplitScreenMultiWindow(Context context) { |
| 264 |
DisplayMetrics dm = new DisplayMetrics(); |
| 265 |
context.getDisplay().getRealMetrics(dm); |
| 266 |
|
| 267 |
int widthDp = (int) (dm.widthPixels / dm.density); |
| 268 |
int heightDp = (int) (dm.heightPixels / dm.density); |
| 269 |
if (Math.max(widthDp, heightDp) < DEFAULT_MINIMAL_SPLIT_SCREEN_DISPLAY_SIZE_DP) { |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
return supportsMultiWindow(context) |
| 274 |
&& Resources.getSystem().getBoolean( |
| 275 |
com.android.internal.R.bool.config_supportsSplitScreenMultiWindow); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
@RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) |
| 283 |
public void startSystemLockTaskMode(int taskId) { |
| 284 |
try { |
| 285 |
getService().startSystemLockTaskMode(taskId); |
| 286 |
} catch (RemoteException e) { |
| 287 |
throw e.rethrowFromSystemServer(); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
@RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) |
| 295 |
public void stopSystemLockTaskMode() { |
| 296 |
try { |
| 297 |
getService().stopSystemLockTaskMode(); |
| 298 |
} catch (RemoteException e) { |
| 299 |
throw e.rethrowFromSystemServer(); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
@RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) |
| 310 |
public void moveTaskToRootTask(int taskId, int rootTaskId, boolean toTop) { |
| 311 |
try { |
| 312 |
getService().moveTaskToRootTask(taskId, rootTaskId, toTop); |
| 313 |
} catch (RemoteException e) { |
| 314 |
throw e.rethrowFromSystemServer(); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
@RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) |
| 324 |
public void resizeTask(int taskId, Rect bounds) { |
| 325 |
try { |
| 326 |
getService().resizeTask(taskId, bounds, RESIZE_MODE_SYSTEM); |
| 327 |
} catch (RemoteException e) { |
| 328 |
throw e.rethrowFromSystemServer(); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
@RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) |
| 337 |
public void clearLaunchParamsForPackages(List<String> packageNames) { |
| 338 |
try { |
| 339 |
getService().clearLaunchParamsForPackages(packageNames); |
| 340 |
} catch (RemoteException e) { |
| 341 |
e.rethrowFromSystemServer(); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public static boolean currentUiModeSupportsErrorDialogs(@NonNull Configuration config) { |
| 350 |
int modeType = config.uiMode & Configuration.UI_MODE_TYPE_MASK; |
| 351 |
return (modeType != Configuration.UI_MODE_TYPE_CAR |
| 352 |
&& !(modeType == Configuration.UI_MODE_TYPE_WATCH && Build.IS_USER) |
| 353 |
&& modeType != Configuration.UI_MODE_TYPE_TELEVISION |
| 354 |
&& modeType != Configuration.UI_MODE_TYPE_VR_HEADSET); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
public static boolean currentUiModeSupportsErrorDialogs(@NonNull Context context) { |
| 359 |
final Configuration config = context.getResources().getConfiguration(); |
| 360 |
return currentUiModeSupportsErrorDialogs(config); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
public static int getMaxNumPictureInPictureActions(@NonNull Context context) { |
| 365 |
return context.getResources().getInteger( |
| 366 |
com.android.internal.R.integer.config_pictureInPictureMaxNumberOfActions); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum) { |
| 374 |
return getTasks(maxNum, false , false , |
| 375 |
INVALID_DISPLAY); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
public List<ActivityManager.RunningTaskInfo> getTasks( |
| 383 |
int maxNum, boolean filterOnlyVisibleRecents) { |
| 384 |
return getTasks(maxNum, filterOnlyVisibleRecents, false , |
| 385 |
INVALID_DISPLAY); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public List<ActivityManager.RunningTaskInfo> getTasks( |
| 394 |
int maxNum, boolean filterOnlyVisibleRecents, boolean keepIntentExtra) { |
| 395 |
return getTasks(maxNum, filterOnlyVisibleRecents, keepIntentExtra, INVALID_DISPLAY); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public List<ActivityManager.RunningTaskInfo> getTasks( |
| 405 |
int maxNum, boolean filterOnlyVisibleRecents, boolean keepIntentExtra, int displayId) { |
| 406 |
try { |
| 407 |
return getService().getTasks(maxNum, filterOnlyVisibleRecents, keepIntentExtra, |
| 408 |
displayId); |
| 409 |
} catch (RemoteException e) { |
| 410 |
throw e.rethrowFromSystemServer(); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public List<ActivityManager.RecentTaskInfo> getRecentTasks( |
| 419 |
int maxNum, int flags, int userId) { |
| 420 |
try { |
| 421 |
return getService().getRecentTasks(maxNum, flags, userId).getList(); |
| 422 |
} catch (RemoteException e) { |
| 423 |
throw e.rethrowFromSystemServer(); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
public void registerTaskStackListener(TaskStackListener listener) { |
| 429 |
try { |
| 430 |
getService().registerTaskStackListener(listener); |
| 431 |
} catch (RemoteException e) { |
| 432 |
throw e.rethrowFromSystemServer(); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
public void unregisterTaskStackListener(TaskStackListener listener) { |
| 438 |
try { |
| 439 |
getService().unregisterTaskStackListener(listener); |
| 440 |
} catch (RemoteException e) { |
| 441 |
throw e.rethrowFromSystemServer(); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
public Rect getTaskBounds(int taskId) { |
| 447 |
try { |
| 448 |
return getService().getTaskBounds(taskId); |
| 449 |
} catch (RemoteException e) { |
| 450 |
throw e.rethrowFromSystemServer(); |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
public void registerRemoteAnimationsForDisplay( |
| 459 |
int displayId, RemoteAnimationDefinition definition) { |
| 460 |
try { |
| 461 |
getService().registerRemoteAnimationsForDisplay(displayId, definition); |
| 462 |
} catch (RemoteException e) { |
| 463 |
throw e.rethrowFromSystemServer(); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
public boolean isInLockTaskMode() { |
| 469 |
try { |
| 470 |
return getService().isInLockTaskMode(); |
| 471 |
} catch (RemoteException e) { |
| 472 |
throw e.rethrowFromSystemServer(); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
@RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_TASKS) |
| 478 |
public boolean removeTask(int taskId) { |
| 479 |
try { |
| 480 |
return getService().removeTask(taskId); |
| 481 |
} catch (RemoteException e) { |
| 482 |
throw e.rethrowFromSystemServer(); |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
@RequiresPermission(android.Manifest.permission.CONTROL_REMOTE_APP_TRANSITION_ANIMATIONS) |
| 491 |
public void detachNavigationBarFromApp(@NonNull IBinder transition) { |
| 492 |
try { |
| 493 |
getService().detachNavigationBarFromApp(transition); |
| 494 |
} catch (RemoteException e) { |
| 495 |
throw e.rethrowFromSystemServer(); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
public static class RootTaskInfo extends TaskInfo implements Parcelable { |
| 504 |
|
| 505 |
public Rect bounds = new Rect(); |
| 506 |
public int[] childTaskIds; |
| 507 |
public String[] childTaskNames; |
| 508 |
public Rect[] childTaskBounds; |
| 509 |
public int[] childTaskUserIds; |
| 510 |
public boolean visible; |
| 511 |
|
| 512 |
public int position; |
| 513 |
|
| 514 |
@Override |
| 515 |
public int describeContents() { |
| 516 |
return 0; |
| 517 |
} |
| 518 |
|
| 519 |
@Override |
| 520 |
public void writeToParcel(Parcel dest, int flags) { |
| 521 |
dest.writeTypedObject(bounds, flags); |
| 522 |
dest.writeIntArray(childTaskIds); |
| 523 |
dest.writeStringArray(childTaskNames); |
| 524 |
dest.writeTypedArray(childTaskBounds, flags); |
| 525 |
dest.writeIntArray(childTaskUserIds); |
| 526 |
dest.writeInt(visible ? 1 : 0); |
| 527 |
dest.writeInt(position); |
| 528 |
super.writeToParcel(dest, flags); |
| 529 |
} |
| 530 |
|
| 531 |
@Override |
| 532 |
void readFromParcel(Parcel source) { |
| 533 |
bounds = source.readTypedObject(Rect.CREATOR); |
| 534 |
childTaskIds = source.createIntArray(); |
| 535 |
childTaskNames = source.createStringArray(); |
| 536 |
childTaskBounds = source.createTypedArray(Rect.CREATOR); |
| 537 |
childTaskUserIds = source.createIntArray(); |
| 538 |
visible = source.readInt() > 0; |
| 539 |
position = source.readInt(); |
| 540 |
super.readFromParcel(source); |
| 541 |
} |
| 542 |
|
| 543 |
public static final @NonNull Creator<RootTaskInfo> CREATOR = new Creator<>() { |
| 544 |
@Override |
| 545 |
public RootTaskInfo createFromParcel(Parcel source) { |
| 546 |
return new RootTaskInfo(source); |
| 547 |
} |
| 548 |
|
| 549 |
@Override |
| 550 |
public RootTaskInfo[] newArray(int size) { |
| 551 |
return new RootTaskInfo[size]; |
| 552 |
} |
| 553 |
}; |
| 554 |
|
| 555 |
public RootTaskInfo() { |
| 556 |
} |
| 557 |
|
| 558 |
private RootTaskInfo(Parcel source) { |
| 559 |
readFromParcel(source); |
| 560 |
} |
| 561 |
|
| 562 |
@Override |
| 563 |
public String toString() { |
| 564 |
StringBuilder sb = new StringBuilder(256); |
| 565 |
sb.append("RootTask id="); sb.append(taskId); |
| 566 |
sb.append(" bounds="); sb.append(bounds.toShortString()); |
| 567 |
sb.append(" displayId="); sb.append(displayId); |
| 568 |
sb.append(" userId="); sb.append(userId); |
| 569 |
sb.append("\n"); |
| 570 |
|
| 571 |
sb.append(" configuration="); sb.append(configuration); |
| 572 |
sb.append("\n"); |
| 573 |
|
| 574 |
for (int i = 0; i < childTaskIds.length; ++i) { |
| 575 |
sb.append(" taskId="); sb.append(childTaskIds[i]); |
| 576 |
sb.append(": "); sb.append(childTaskNames[i]); |
| 577 |
if (childTaskBounds != null) { |
| 578 |
sb.append(" bounds="); sb.append(childTaskBounds[i].toShortString()); |
| 579 |
} |
| 580 |
sb.append(" userId=").append(childTaskUserIds[i]); |
| 581 |
sb.append(" visible=").append(visible); |
| 582 |
if (topActivity != null) { |
| 583 |
sb.append(" topActivity=").append(topActivity); |
| 584 |
} |
| 585 |
sb.append("\n"); |
| 586 |
} |
| 587 |
return sb.toString(); |
| 588 |
} |
| 589 |
} |
| 590 |
} |