Fix NPE and make getTask return Optional

Fix an NPE when attempting to update the thumbnail in IconRecentsView.
If the task item is binding the loading UI, it has no task so the app
will crash, so we just need to ensure the task is actually present
first.

While we're here, also change the API to return an Optional<Task> and
make a tighter contract, making it harder to make this mistake in the
future.

Bug: 130746661
Fix: 130746661
Test: Go from app => overview on recents Go
Change-Id: I1402fcd2e58bdeb703c7dcc1b9dcf0d258808b3d
(cherry picked from commit 77f01524bd)
This commit is contained in:
Kevin 2019-04-17 12:04:35 -07:00 committed by Kevin Han
parent 532fb48331
commit 1866ecf638
4 changed files with 18 additions and 12 deletions

View File

@ -42,7 +42,7 @@ public final class TaskActionController {
* @param viewHolder the task view holder to launch
*/
public void launchTask(TaskHolder viewHolder) {
if (viewHolder.getTask() == null) {
if (!viewHolder.getTask().isPresent()) {
return;
}
TaskItemView itemView = (TaskItemView) (viewHolder.itemView);
@ -53,8 +53,9 @@ public final class TaskActionController {
int height = v.getMeasuredHeight();
ActivityOptions opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
ActivityManagerWrapper.getInstance().startActivityFromRecentsAsync(viewHolder.getTask().key,
opts, null /* resultCallback */, null /* resultCallbackHandler */);
ActivityManagerWrapper.getInstance().startActivityFromRecentsAsync(
viewHolder.getTask().get().key, opts, null /* resultCallback */,
null /* resultCallbackHandler */);
}
/**
@ -63,11 +64,11 @@ public final class TaskActionController {
* @param viewHolder the task view holder to remove
*/
public void removeTask(TaskHolder viewHolder) {
if (viewHolder.getTask() == null) {
if (!viewHolder.getTask().isPresent()) {
return;
}
int position = viewHolder.getAdapterPosition();
Task task = viewHolder.getTask();
Task task = viewHolder.getTask().get();
ActivityManagerWrapper.getInstance().removeTask(task.key.id);
mLoader.removeTask(task);
mAdapter.notifyItemRemoved(position);

View File

@ -28,6 +28,7 @@ import com.android.systemui.shared.recents.model.Task;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* Recycler view adapter that dynamically inflates and binds {@link TaskHolder} instances with the
@ -109,13 +110,13 @@ public final class TaskAdapter extends Adapter<TaskHolder> {
holder.bindTask(task, willAnimate /* willAnimate */);
mLoader.loadTaskIconAndLabel(task, () -> {
// Ensure holder still has the same task.
if (Objects.equals(task, holder.getTask())) {
if (Objects.equals(Optional.of(task), holder.getTask())) {
holder.getTaskItemView().setIcon(task.icon);
holder.getTaskItemView().setLabel(task.titleDescription);
}
});
mLoader.loadTaskThumbnail(task, () -> {
if (Objects.equals(task, holder.getTask())) {
if (Objects.equals(Optional.of(task), holder.getTask())) {
holder.getTaskItemView().setThumbnail(task.thumbnail.thumbnail);
}
});

View File

@ -18,12 +18,13 @@ package com.android.quickstep;
import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.android.quickstep.views.TaskItemView;
import com.android.systemui.shared.recents.model.Task;
import java.util.Optional;
/**
* A recycler view holder that holds the task view and binds {@link Task} content (app title, icon,
* etc.) to the view.
@ -81,7 +82,7 @@ public final class TaskHolder extends ViewHolder {
*
* @return the current task
*/
public @Nullable Task getTask() {
return mTask;
public Optional<Task> getTask() {
return Optional.ofNullable(mTask);
}
}

View File

@ -61,6 +61,8 @@ import com.android.quickstep.TaskListLoader;
import com.android.quickstep.TaskSwipeCallback;
import com.android.systemui.shared.recents.model.Task;
import java.util.Optional;
/**
* Root view for the icon recents view. Acts as the main interface to the rest of the Launcher code
* base.
@ -120,8 +122,9 @@ public final class IconRecentsView extends FrameLayout {
TaskItemView[] itemViews = getTaskViews();
for (TaskItemView taskView : itemViews) {
TaskHolder taskHolder = (TaskHolder) mTaskRecyclerView.getChildViewHolder(taskView);
Task task = taskHolder.getTask();
if (taskHolder.getTask().key.id == taskId) {
Optional<Task> optTask = taskHolder.getTask();
if (optTask.filter(task -> task.key.id == taskId).isPresent()) {
Task task = optTask.get();
// Update thumbnail on the task.
task.thumbnail = thumbnailData;
taskView.setThumbnail(thumbnailData.thumbnail);