Introduce RecentsView.getTaskViews for easy for-each looping

- This vastly simplified the need to call do a indexed loop with getTaskViewCount() and calls requireTaskViewAt() each time, useful for code that needs to loop all TaskViews
- removeTasksViewsAndClearAllButton and resetTaskVisuals have no reason to loop reversed, so I just loop them in order

Fix: 365534984
Test: presubmit, manual
Flag: EXEMPT REFACTOR
Change-Id: Ib69f8439f3a7509fc57ec2430affa207c038bf46
This commit is contained in:
Alex Chau
2024-09-09 16:22:35 +01:00
parent 02cc26f055
commit a7c90bbcd1
2 changed files with 73 additions and 101 deletions
@@ -841,8 +841,7 @@ public abstract class RecentsView<
private final RecentsViewModel mRecentsViewModel;
private final RecentsViewModelHelper mHelper;
private final RecentsViewUtils mRecentsViewUtils = new RecentsViewUtils();
private final RecentsViewUtils mUtils = new RecentsViewUtils();
public RecentsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
BaseContainerInterface sizeStrategy) {
@@ -1103,14 +1102,13 @@ public abstract class RecentsView<
@Override
public void onTaskIconChanged(@NonNull String pkg, @NonNull UserHandle user) {
for (int i = 0; i < getTaskViewCount(); i++) {
TaskView tv = requireTaskViewAt(i);
Task task = tv.getFirstTask();
for (TaskView taskView : getTaskViews()) {
Task task = taskView.getFirstTask();
if (pkg.equals(task.key.getPackageName()) && task.key.userId == user.getIdentifier()) {
task.icon = null;
if (tv.getTaskContainers().stream().anyMatch(
if (taskView.getTaskContainers().stream().anyMatch(
container -> container.getIconView().getDrawable() != null)) {
tv.onTaskListVisibilityChanged(true /* visible */);
taskView.onTaskListVisibilityChanged(true /* visible */);
}
}
}
@@ -1499,8 +1497,7 @@ public abstract class RecentsView<
return null;
}
for (int i = 0; i < getTaskViewCount(); i++) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
if (taskView.containsTaskId(taskId)) {
return taskView;
}
@@ -1521,8 +1518,7 @@ public abstract class RecentsView<
int[] taskIdsCopy = Arrays.copyOf(taskIds, taskIds.length);
Arrays.sort(taskIdsCopy);
for (int i = 0; i < getTaskViewCount(); i++) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
int[] taskViewIdsCopy = taskView.getTaskIds();
Arrays.sort(taskViewIdsCopy);
if (Arrays.equals(taskIdsCopy, taskViewIdsCopy)) {
@@ -1558,9 +1554,7 @@ public abstract class RecentsView<
*/
public void setTaskBorderEnabled(boolean enabled) {
mBorderEnabled = enabled;
int taskCount = getTaskViewCount();
for (int i = 0; i < taskCount; i++) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
taskView.setBorderEnabled(enabled);
}
mClearAllButton.setBorderEnabled(enabled);
@@ -1627,9 +1621,7 @@ public abstract class RecentsView<
super.onTouchEvent(ev);
if (showAsGrid()) {
int taskCount = getTaskViewCount();
for (int i = 0; i < taskCount; i++) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
if (isTaskViewVisible(taskView) && taskView.offerTouchToChildren(ev)) {
// Keep consuming events to pass to delegate
return true;
@@ -1883,7 +1875,7 @@ public abstract class RecentsView<
// Move Desktop Tasks to the end of the list
if (enableLargeDesktopWindowingTile()) {
taskGroups = mRecentsViewUtils.sortDesktopTasksToFront(taskGroups);
taskGroups = mUtils.sortDesktopTasksToFront(taskGroups);
}
// Add views as children based on whether it's grouped or single task. Looping through
@@ -1939,7 +1931,7 @@ public abstract class RecentsView<
// Keep same previous focused task
TaskView newFocusedTaskView = getTaskViewByTaskIds(focusedTaskIds);
// If the list changed, maybe the focused task doesn't exist anymore
int newFocusedTaskViewIndex = mRecentsViewUtils.getFocusedTaskIndex(taskGroups);
int newFocusedTaskViewIndex = mUtils.getFocusedTaskIndex(taskGroups);
if (newFocusedTaskView == null && getTaskViewCount() > newFocusedTaskViewIndex) {
newFocusedTaskView = getTaskViewAt(newFocusedTaskViewIndex);
}
@@ -2030,15 +2022,13 @@ public abstract class RecentsView<
}
private void removeTasksViewsAndClearAllButton() {
// This handles an edge case where applyLoadPlan happens during a gesture when the
// only Task is one with excludeFromRecents, in which case we should not remove it.
final int stubRunningTaskIndex = isGestureActive() ? getRunningTaskIndex() : -1;
for (int i = getTaskViewCount() - 1; i >= 0; i--) {
if (i == stubRunningTaskIndex) {
for (TaskView taskView : getTaskViews()) {
if (isGestureActive() && taskView.isRunningTask()) {
// This handles an edge case where applyLoadPlan happens during a gesture when the
// only Task is one with excludeFromRecents, in which case we should not remove it.
continue;
}
removeView(requireTaskViewAt(i));
removeView(taskView);
}
if (getTaskViewCount() == 0 && indexOfChild(mClearAllButton) != -1) {
removeView(mClearAllButton);
@@ -2059,7 +2049,7 @@ public abstract class RecentsView<
* @return Number of children that are instances of DesktopTaskView
*/
private int getDesktopTaskViewCount() {
return mRecentsViewUtils.getDesktopTaskViewCount(getChildCount(), this::getTaskViewAt);
return mUtils.getDesktopTaskViewCount(getTaskViews());
}
/**
@@ -2082,8 +2072,7 @@ public abstract class RecentsView<
}
public void resetTaskVisuals() {
for (int i = getTaskViewCount() - 1; i >= 0; i--) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
if (Arrays.stream(taskView.getTaskIds()).noneMatch(
taskId -> taskId == mIgnoreResetTaskId)) {
taskView.resetViewTransforms();
@@ -2126,9 +2115,8 @@ public abstract class RecentsView<
if (enableRefactorTaskThumbnail()) {
mRecentsViewModel.updateFullscreenProgress(mFullscreenProgress);
}
int taskCount = getTaskViewCount();
for (int i = 0; i < taskCount; i++) {
requireTaskViewAt(i).setFullscreenProgress(mFullscreenProgress);
for (TaskView taskView : getTaskViews()) {
taskView.setFullscreenProgress(mFullscreenProgress);
}
mClearAllButton.setFullscreenProgress(fullscreenProgress);
@@ -2274,8 +2262,7 @@ public abstract class RecentsView<
? mLastComputedCarouselTaskSize.right - mLastComputedTaskSize.right
: mLastComputedCarouselTaskSize.left - mLastComputedTaskSize.left;
}
for (int i = 0; i < taskCount; i++) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
taskView.updateTaskSize(mLastComputedTaskSize, mLastComputedGridTaskSize,
mLastComputedCarouselTaskSize);
taskView.setNonGridTranslationX(accumulatedTranslationX);
@@ -2664,7 +2651,7 @@ public abstract class RecentsView<
}
private @Nullable TaskView getFirstLargeTaskView() {
return mRecentsViewUtils.getFirstLargeTaskView(getChildCount(), this::getTaskViewAt);
return mUtils.getFirstLargeTaskView(getTaskViews());
}
@Nullable
@@ -2673,8 +2660,7 @@ public abstract class RecentsView<
return null;
}
for (int i = 0; i < getTaskViewCount(); i++) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
if (taskView.getTaskViewId() == taskViewId) {
return taskView;
}
@@ -2823,8 +2809,8 @@ public abstract class RecentsView<
}
private void updateChildTaskOrientations() {
for (int i = 0; i < getTaskViewCount(); i++) {
requireTaskViewAt(i).setOrientationState(mOrientationState);
for (TaskView taskView : getTaskViews()) {
taskView.setOrientationState(mOrientationState);
}
boolean shouldRotateMenuForFakeRotation =
!mOrientationState.isRecentsActivityRotationAllowed();
@@ -3083,9 +3069,8 @@ public abstract class RecentsView<
public void setTaskIconScaledDown(boolean isScaledDown) {
if (mTaskIconScaledDown != isScaledDown) {
mTaskIconScaledDown = isScaledDown;
int taskCount = getTaskViewCount();
for (int i = 0; i < taskCount; i++) {
requireTaskViewAt(i).setIconScaleAndDim(mTaskIconScaledDown ? 0 : 1);
for (TaskView taskView : getTaskViews()) {
taskView.setIconScaleAndDim(mTaskIconScaledDown ? 0 : 1);
}
}
}
@@ -3098,9 +3083,7 @@ public abstract class RecentsView<
public void animateUpTaskIconScale() {
mTaskIconScaledDown = false;
int taskCount = getTaskViewCount();
for (int i = 0; i < taskCount; i++) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
taskView.animateIconScaleAndDimIntoView();
}
}
@@ -3395,9 +3378,8 @@ public abstract class RecentsView<
private void setGridProgress(float gridProgress) {
mGridProgress = gridProgress;
int taskCount = getTaskViewCount();
for (int i = 0; i < taskCount; i++) {
requireTaskViewAt(i).setGridProgress(gridProgress);
for (TaskView taskView : getTaskViews()) {
taskView.setGridProgress(gridProgress);
}
mClearAllButton.setGridProgress(gridProgress);
}
@@ -3407,14 +3389,10 @@ public abstract class RecentsView<
mRecentsViewModel.updateThumbnailSplashProgress(taskThumbnailSplashAlpha);
return;
}
int taskCount = getTaskViewCount();
if (taskCount == 0) {
return;
}
mTaskThumbnailSplashAlpha = taskThumbnailSplashAlpha;
for (int i = 0; i < taskCount; i++) {
requireTaskViewAt(i).setTaskThumbnailSplashAlpha(taskThumbnailSplashAlpha);
for (TaskView taskView : getTaskViews()) {
taskView.setTaskThumbnailSplashAlpha(taskThumbnailSplashAlpha);
}
}
@@ -3637,8 +3615,7 @@ public abstract class RecentsView<
nextFocusedTaskFromTop =
!mTopRowIdSet.isEmpty() && mTopRowIdSet.size() >= (taskCount - 1) / 2f;
// Pick the next focused task from the preferred row.
for (int i = 0; i < taskCount; i++) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
if (taskView == dismissedTaskView || taskView.isLargeTile()) {
continue;
}
@@ -3766,8 +3743,7 @@ public abstract class RecentsView<
END_DISMISS_TRANSLATION_INTERPOLATION_OFFSET
+ (taskCount - 1) * halfAdditionalDismissTranslationOffset,
END_DISMISS_TRANSLATION_INTERPOLATION_OFFSET, 1);
for (int i = 0; i < taskCount; i++) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
anim.setFloat(taskView, TaskView.GRID_END_TRANSLATION_X, longGridRowWidthDiff,
clampToProgress(LINEAR, dismissTranslationInterpolationEnd, 1));
dismissTranslationInterpolationEnd = Utilities.boundToRange(
@@ -4227,9 +4203,8 @@ public abstract class RecentsView<
return new IntArray(0);
}
IntArray topArray = new IntArray(mTopRowIdSet.size());
int taskViewCount = getTaskViewCount();
for (int i = 0; i < taskViewCount; i++) {
int taskViewId = requireTaskViewAt(i).getTaskViewId();
for (TaskView taskView : getTaskViews()) {
int taskViewId = taskView.getTaskViewId();
if (mTopRowIdSet.contains(taskViewId)) {
topArray.add(taskViewId);
}
@@ -4246,9 +4221,7 @@ public abstract class RecentsView<
return new IntArray(0);
}
IntArray bottomArray = new IntArray(bottomRowIdArraySize);
int taskViewCount = getTaskViewCount();
for (int i = 0; i < taskViewCount; i++) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
int taskViewId = taskView.getTaskViewId();
if (!mTopRowIdSet.contains(taskViewId) && !taskView.isLargeTile()) {
bottomArray.add(taskViewId);
@@ -4308,9 +4281,8 @@ public abstract class RecentsView<
}
PendingAnimation anim = new PendingAnimation(duration);
int count = getTaskViewCount();
for (int i = 0; i < count; i++) {
addDismissedTaskAnimations(requireTaskViewAt(i), duration, anim);
for (TaskView taskView : getTaskViews()) {
addDismissedTaskAnimations(taskView, duration, anim);
}
mPendingAnimation = anim;
@@ -4477,12 +4449,11 @@ public abstract class RecentsView<
mContentAlpha = alpha;
TaskView runningTaskView = getRunningTaskView();
for (int i = getTaskViewCount() - 1; i >= 0; i--) {
TaskView child = requireTaskViewAt(i);
if (runningTaskView != null && mRunningTaskTileHidden && child == runningTaskView) {
for (TaskView taskView : getTaskViews()) {
if (runningTaskView != null && mRunningTaskTileHidden && taskView == runningTaskView) {
continue;
}
child.setStableAlpha(alpha);
taskView.setStableAlpha(alpha);
}
mClearAllButton.setContentAlpha(mContentAlpha);
int alphaInt = Math.round(alpha * 255);
@@ -4591,6 +4562,13 @@ public abstract class RecentsView<
return Objects.requireNonNull(getTaskViewAt(index));
}
/**
* Returns the current list of [TaskView] children.
*/
private List<TaskView> getTaskViews() {
return mUtils.getTaskViews(getTaskViewCount(), this::requireTaskViewAt);
}
public void setOnEmptyMessageUpdatedListener(OnEmptyMessageUpdatedListener listener) {
mOnEmptyMessageUpdatedListener = listener;
}
@@ -4895,9 +4873,9 @@ public abstract class RecentsView<
protected void setTaskViewsResistanceTranslation(float translation) {
mTaskViewsSecondaryTranslation = translation;
for (int i = 0; i < getTaskViewCount(); i++) {
TaskView task = requireTaskViewAt(i);
task.getTaskResistanceTranslationProperty().set(task, translation / getScaleY());
for (TaskView taskView : getTaskViews()) {
taskView.getTaskResistanceTranslationProperty().set(taskView,
translation / getScaleY());
}
runActionOnRemoteHandles(
remoteTargetHandle -> remoteTargetHandle.getTaskViewSimulator()
@@ -4905,23 +4883,21 @@ public abstract class RecentsView<
}
private void updateTaskViewsSnapshotRadius() {
for (int i = 0; i < getTaskViewCount(); i++) {
requireTaskViewAt(i).updateSnapshotRadius();
for (TaskView taskView : getTaskViews()) {
taskView.updateSnapshotRadius();
}
}
protected void setTaskViewsPrimarySplitTranslation(float translation) {
mTaskViewsPrimarySplitTranslation = translation;
for (int i = 0; i < getTaskViewCount(); i++) {
TaskView task = requireTaskViewAt(i);
task.getPrimarySplitTranslationProperty().set(task, translation);
for (TaskView taskView : getTaskViews()) {
taskView.getPrimarySplitTranslationProperty().set(taskView, translation);
}
}
protected void setTaskViewsSecondarySplitTranslation(float translation) {
mTaskViewsSecondarySplitTranslation = translation;
for (int i = 0; i < getTaskViewCount(); i++) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
if (taskView == mSplitHiddenTaskView && !taskView.containsMultipleTasks()) {
continue;
}
@@ -6088,9 +6064,7 @@ public abstract class RecentsView<
private void updateEnabledOverlays() {
TaskView focusedTaskView = getFocusedTaskView();
int taskCount = getTaskViewCount();
for (int i = 0; i < taskCount; i++) {
TaskView taskView = requireTaskViewAt(i);
for (TaskView taskView : getTaskViews()) {
if (taskView == focusedTaskView) {
continue;
}
@@ -6164,7 +6138,7 @@ public abstract class RecentsView<
return;
}
Map<Integer, ThumbnailData> updatedThumbnails = mRecentsViewUtils.screenshotTasks(taskView,
Map<Integer, ThumbnailData> updatedThumbnails = mUtils.screenshotTasks(taskView,
mRecentsAnimationController);
if (enableRefactorTaskThumbnail()) {
mHelper.switchToScreenshot(taskView, updatedThumbnails, onFinishRunnable);
@@ -6262,8 +6236,8 @@ public abstract class RecentsView<
mRecentsViewModel.setTintAmount(tintAmount);
}
for (int i = 0; i < getTaskViewCount(); i++) {
requireTaskViewAt(i).setColorTint(mColorTint, mTintingColor);
for (TaskView taskView : getTaskViews()) {
taskView.setColorTint(mColorTint, mTintingColor);
}
Drawable scrimBg = mContainer.getScrimView().getBackground();