From cc1dad0cae1547c3aad40af8676d9e5312e4564b Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Thu, 15 Feb 2024 12:57:06 -0800 Subject: [PATCH] Merging multiple implementations of CancellableTask Bug: 318539160 Flag: None Test: Manual Change-Id: Ie855427c2676e0ce76655e4dd67472c0b5e435d8 --- .../com/android/quickstep/TaskIconCache.java | 29 ++++---- .../android/quickstep/TaskThumbnailCache.java | 49 +++++++------ .../quickstep/util/CancellableTask.java | 68 ------------------- .../quickstep/views/DesktopTaskView.java | 3 +- .../quickstep/views/GroupedTaskView.java | 3 +- .../android/quickstep/views/RecentsView.java | 5 +- .../com/android/quickstep/views/TaskView.java | 5 +- src/com/android/launcher3/BubbleTextView.java | 4 +- .../android/launcher3/icons/IconCache.java | 15 ++-- .../recyclerview/AllAppsRecyclerViewPool.kt | 18 ++--- ...ExecutorRunnable.kt => CancellableTask.kt} | 50 ++++++-------- .../widget/DatabaseWidgetPreviewLoader.java | 6 +- .../android/launcher3/widget/WidgetCell.java | 4 +- .../widget/picker/WidgetsListHeader.java | 4 +- ...RunnableTest.kt => CancellableTaskTest.kt} | 16 ++--- 15 files changed, 96 insertions(+), 183 deletions(-) delete mode 100644 quickstep/src/com/android/quickstep/util/CancellableTask.java rename src/com/android/launcher3/util/{ExecutorRunnable.kt => CancellableTask.kt} (52%) rename tests/multivalentTests/src/com/android/launcher3/util/{ExecutorRunnableTest.kt => CancellableTaskTest.kt} (92%) diff --git a/quickstep/src/com/android/quickstep/TaskIconCache.java b/quickstep/src/com/android/quickstep/TaskIconCache.java index 4e6b23f50c..e6febff5f7 100644 --- a/quickstep/src/com/android/quickstep/TaskIconCache.java +++ b/quickstep/src/com/android/quickstep/TaskIconCache.java @@ -17,6 +17,7 @@ package com.android.quickstep; import static com.android.launcher3.Flags.enableOverviewIconMenu; import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY; +import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; import android.annotation.Nullable; import android.app.ActivityManager; @@ -41,12 +42,12 @@ import com.android.launcher3.icons.BaseIconFactory.IconOptions; import com.android.launcher3.icons.BitmapInfo; import com.android.launcher3.icons.IconProvider; import com.android.launcher3.pm.UserCache; +import com.android.launcher3.util.CancellableTask; import com.android.launcher3.util.DisplayController; import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener; import com.android.launcher3.util.DisplayController.Info; import com.android.launcher3.util.FlagOp; import com.android.launcher3.util.Preconditions; -import com.android.quickstep.util.CancellableTask; import com.android.quickstep.util.TaskKeyLruCache; import com.android.quickstep.util.TaskVisualsChangeListener; import com.android.systemui.shared.recents.model.Task; @@ -109,21 +110,17 @@ public class TaskIconCache implements DisplayInfoChangeListener { callback.accept(task); return null; } - CancellableTask request = new CancellableTask() { - @Override - public TaskCacheEntry getResultOnBg() { - return getCacheEntry(task); - } - - @Override - public void handleResult(TaskCacheEntry result) { - task.icon = result.icon; - task.titleDescription = result.contentDescription; - task.title = result.title; - callback.accept(task); - dispatchIconUpdate(task.key.id); - } - }; + CancellableTask request = new CancellableTask<>( + () -> getCacheEntry(task), + MAIN_EXECUTOR, + result -> { + task.icon = result.icon; + task.titleDescription = result.contentDescription; + task.title = result.title; + callback.accept(task); + dispatchIconUpdate(task.key.id); + } + ); mBgExecutor.execute(request); return request; } diff --git a/quickstep/src/com/android/quickstep/TaskThumbnailCache.java b/quickstep/src/com/android/quickstep/TaskThumbnailCache.java index 9e2159516f..b7cbb47e63 100644 --- a/quickstep/src/com/android/quickstep/TaskThumbnailCache.java +++ b/quickstep/src/com/android/quickstep/TaskThumbnailCache.java @@ -16,6 +16,7 @@ package com.android.quickstep; import static com.android.launcher3.Flags.enableGridOnlyOverview; +import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; import android.content.Context; import android.content.res.Resources; @@ -23,8 +24,8 @@ import android.content.res.Resources; import androidx.annotation.VisibleForTesting; import com.android.launcher3.R; +import com.android.launcher3.util.CancellableTask; import com.android.launcher3.util.Preconditions; -import com.android.quickstep.util.CancellableTask; import com.android.quickstep.util.TaskKeyByLastActiveTimeCache; import com.android.quickstep.util.TaskKeyCache; import com.android.quickstep.util.TaskKeyLruCache; @@ -195,33 +196,29 @@ public class TaskThumbnailCache { return null; } - CancellableTask request = new CancellableTask<>() { - @Override - public ThumbnailData getResultOnBg() { - ThumbnailData thumbnailData = ActivityManagerWrapper.getInstance().getTaskThumbnail( - key.id, lowResolution); - if (thumbnailData.thumbnail != null) { - return thumbnailData; - } - return ActivityManagerWrapper.getInstance().takeTaskThumbnail(key.id); - } - - @Override - public void handleResult(ThumbnailData result) { - // Avoid an async timing issue that a low res entry replaces an existing high res - // entry in high res enabled state, so we check before putting it to cache - if (enableGridOnlyOverview() && result.reducedResolution - && getHighResLoadingState().isEnabled()) { - ThumbnailData cachedThumbnail = mCache.getAndInvalidateIfModified(key); - if (cachedThumbnail != null && cachedThumbnail.thumbnail != null - && !cachedThumbnail.reducedResolution) { - return; + CancellableTask request = new CancellableTask<>( + () -> { + ThumbnailData thumbnailData = ActivityManagerWrapper.getInstance() + .getTaskThumbnail(key.id, lowResolution); + return thumbnailData.thumbnail != null ? thumbnailData + : ActivityManagerWrapper.getInstance().takeTaskThumbnail(key.id); + }, + MAIN_EXECUTOR, + result -> { + // Avoid an async timing issue that a low res entry replaces an existing high + // res entry in high res enabled state, so we check before putting it to cache + if (enableGridOnlyOverview() && result.reducedResolution + && getHighResLoadingState().isEnabled()) { + ThumbnailData newCachedThumbnail = mCache.getAndInvalidateIfModified(key); + if (newCachedThumbnail != null && newCachedThumbnail.thumbnail != null + && !newCachedThumbnail.reducedResolution) { + return; + } } + mCache.put(key, result); + callback.accept(result); } - mCache.put(key, result); - callback.accept(result); - } - }; + ); mBgExecutor.execute(request); return request; } diff --git a/quickstep/src/com/android/quickstep/util/CancellableTask.java b/quickstep/src/com/android/quickstep/util/CancellableTask.java deleted file mode 100644 index a6e2e81589..0000000000 --- a/quickstep/src/com/android/quickstep/util/CancellableTask.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.android.quickstep.util; - -import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; - -import androidx.annotation.UiThread; -import androidx.annotation.WorkerThread; - -/** - * Utility class to executore a task on background and post the result on UI thread - */ -public abstract class CancellableTask implements Runnable { - - private boolean mCancelled = false; - - @Override - public final void run() { - if (mCancelled) { - return; - } - T result = getResultOnBg(); - if (mCancelled) { - return; - } - MAIN_EXECUTOR.execute(() -> { - if (mCancelled) { - return; - } - handleResult(result); - }); - } - - /** - * Called on the worker thread to process the request. The return object is passed to - * {@link #handleResult(Object)} - */ - @WorkerThread - public abstract T getResultOnBg(); - - /** - * Called on the UI thread to handle the final result. - * @param result - */ - @UiThread - public abstract void handleResult(T result); - - /** - * Cancels the request. If it is called before {@link #handleResult(Object)}, that method - * will not be called - */ - public void cancel() { - mCancelled = true; - } -} diff --git a/quickstep/src/com/android/quickstep/views/DesktopTaskView.java b/quickstep/src/com/android/quickstep/views/DesktopTaskView.java index 6b00473b73..4c7d5c49b9 100644 --- a/quickstep/src/com/android/quickstep/views/DesktopTaskView.java +++ b/quickstep/src/com/android/quickstep/views/DesktopTaskView.java @@ -44,10 +44,10 @@ import com.android.launcher3.R; import com.android.launcher3.Utilities; import com.android.launcher3.desktop.DesktopRecentsTransitionController; import com.android.launcher3.icons.IconProvider; +import com.android.launcher3.util.CancellableTask; import com.android.launcher3.util.RunnableList; import com.android.quickstep.RecentsModel; import com.android.quickstep.TaskThumbnailCache; -import com.android.quickstep.util.CancellableTask; import com.android.quickstep.util.RecentsOrientedState; import com.android.systemui.shared.recents.model.Task; import com.android.systemui.shared.recents.model.ThumbnailData; @@ -63,7 +63,6 @@ import java.util.HashMap; import java.util.List; import java.util.function.Consumer; - /** * TaskView that contains all tasks that are part of the desktop. */ diff --git a/quickstep/src/com/android/quickstep/views/GroupedTaskView.java b/quickstep/src/com/android/quickstep/views/GroupedTaskView.java index ad4b29214f..10ef47c268 100644 --- a/quickstep/src/com/android/quickstep/views/GroupedTaskView.java +++ b/quickstep/src/com/android/quickstep/views/GroupedTaskView.java @@ -24,6 +24,7 @@ import com.android.internal.jank.Cuj; import com.android.launcher3.DeviceProfile; import com.android.launcher3.R; import com.android.launcher3.Utilities; +import com.android.launcher3.util.CancellableTask; import com.android.launcher3.util.RunnableList; import com.android.launcher3.util.SplitConfigurationOptions; import com.android.launcher3.util.SplitConfigurationOptions.SplitBounds; @@ -31,7 +32,6 @@ import com.android.launcher3.util.TransformingTouchDelegate; import com.android.quickstep.RecentsModel; import com.android.quickstep.TaskIconCache; import com.android.quickstep.TaskThumbnailCache; -import com.android.quickstep.util.CancellableTask; import com.android.quickstep.util.RecentsOrientedState; import com.android.quickstep.util.SplitSelectStateController; import com.android.systemui.shared.recents.model.Task; @@ -45,7 +45,6 @@ import kotlin.Unit; import java.util.HashMap; import java.util.function.Consumer; - /** * TaskView that contains and shows thumbnails for not one, BUT TWO(!!) tasks * diff --git a/quickstep/src/com/android/quickstep/views/RecentsView.java b/quickstep/src/com/android/quickstep/views/RecentsView.java index f6afaf0043..1cdb661e55 100644 --- a/quickstep/src/com/android/quickstep/views/RecentsView.java +++ b/quickstep/src/com/android/quickstep/views/RecentsView.java @@ -148,7 +148,6 @@ import com.android.launcher3.anim.SpringProperty; import com.android.launcher3.compat.AccessibilityManagerCompat; import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.desktop.DesktopRecentsTransitionController; -import com.android.launcher3.icons.cache.HandlerRunnable; import com.android.launcher3.logging.StatsLogManager; import com.android.launcher3.statehandlers.DepthController; import com.android.launcher3.statemanager.BaseState; @@ -156,6 +155,7 @@ import com.android.launcher3.statemanager.StatefulActivity; import com.android.launcher3.testing.TestLogging; import com.android.launcher3.testing.shared.TestProtocol; import com.android.launcher3.touch.OverScroll; +import com.android.launcher3.util.CancellableTask; import com.android.launcher3.util.DynamicResource; import com.android.launcher3.util.IntArray; import com.android.launcher3.util.IntSet; @@ -588,8 +588,7 @@ public abstract class RecentsView( - UI_HELPER_EXECUTOR.getHandler(), + UI_HELPER_EXECUTOR.execute(new CancellableTask<>( () -> PackageManagerWrapper.getInstance() .getActivityInfo(taskKey.getComponent(), taskKey.userId) == null, MAIN_EXECUTOR, diff --git a/quickstep/src/com/android/quickstep/views/TaskView.java b/quickstep/src/com/android/quickstep/views/TaskView.java index 55da1601b6..1cbf890dac 100644 --- a/quickstep/src/com/android/quickstep/views/TaskView.java +++ b/quickstep/src/com/android/quickstep/views/TaskView.java @@ -54,7 +54,6 @@ import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.PointF; import android.graphics.Rect; -import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.AttributeSet; @@ -89,6 +88,7 @@ import com.android.launcher3.statemanager.StatefulActivity; import com.android.launcher3.testing.TestLogging; import com.android.launcher3.testing.shared.TestProtocol; import com.android.launcher3.util.ActivityOptionsWrapper; +import com.android.launcher3.util.CancellableTask; import com.android.launcher3.util.ComponentKey; import com.android.launcher3.util.RunnableList; import com.android.launcher3.util.SplitConfigurationOptions; @@ -107,7 +107,6 @@ import com.android.quickstep.TaskViewUtils; import com.android.quickstep.orientation.RecentsPagedOrientationHandler; import com.android.quickstep.util.ActiveGestureLog; import com.android.quickstep.util.BorderAnimator; -import com.android.quickstep.util.CancellableTask; import com.android.quickstep.util.RecentsOrientedState; import com.android.quickstep.util.SplitSelectStateController; import com.android.quickstep.util.TaskCornerRadius; @@ -135,8 +134,6 @@ public class TaskView extends FrameLayout implements Reusable { private static final String TAG = TaskView.class.getSimpleName(); private static final boolean DEBUG = false; - private static final RectF EMPTY_RECT_F = new RectF(); - public static final int FLAG_UPDATE_ICON = 1; public static final int FLAG_UPDATE_THUMBNAIL = FLAG_UPDATE_ICON << 1; public static final int FLAG_UPDATE_CORNER_RADIUS = FLAG_UPDATE_THUMBNAIL << 1; diff --git a/src/com/android/launcher3/BubbleTextView.java b/src/com/android/launcher3/BubbleTextView.java index 2f0c0968e9..cab71cf95f 100644 --- a/src/com/android/launcher3/BubbleTextView.java +++ b/src/com/android/launcher3/BubbleTextView.java @@ -69,13 +69,13 @@ import com.android.launcher3.icons.DotRenderer; import com.android.launcher3.icons.FastBitmapDrawable; import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver; import com.android.launcher3.icons.PlaceHolderIconDrawable; -import com.android.launcher3.icons.cache.HandlerRunnable; import com.android.launcher3.model.data.AppInfo; import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.model.data.ItemInfoWithIcon; import com.android.launcher3.model.data.WorkspaceItemInfo; import com.android.launcher3.popup.PopupContainerWithArrow; import com.android.launcher3.search.StringMatcherUtility; +import com.android.launcher3.util.CancellableTask; import com.android.launcher3.util.IntArray; import com.android.launcher3.util.MultiTranslateDelegate; import com.android.launcher3.util.SafeCloseable; @@ -190,7 +190,7 @@ public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, @ViewDebug.ExportedProperty(category = "launcher") private boolean mDisableRelayout = false; - private HandlerRunnable mIconLoadRequest; + private CancellableTask mIconLoadRequest; private boolean mEnableIconUpdateAnimation = false; diff --git a/src/com/android/launcher3/icons/IconCache.java b/src/com/android/launcher3/icons/IconCache.java index d8fa90ae82..ee66a60c03 100644 --- a/src/com/android/launcher3/icons/IconCache.java +++ b/src/com/android/launcher3/icons/IconCache.java @@ -54,7 +54,6 @@ import com.android.launcher3.Utilities; import com.android.launcher3.icons.ComponentWithLabel.ComponentCachingLogic; import com.android.launcher3.icons.cache.BaseIconCache; import com.android.launcher3.icons.cache.CachingLogic; -import com.android.launcher3.icons.cache.HandlerRunnable; import com.android.launcher3.model.data.AppInfo; import com.android.launcher3.model.data.IconRequestInfo; import com.android.launcher3.model.data.ItemInfoWithIcon; @@ -63,6 +62,7 @@ import com.android.launcher3.model.data.WorkspaceItemInfo; import com.android.launcher3.pm.InstallSessionHelper; import com.android.launcher3.pm.UserCache; import com.android.launcher3.shortcuts.ShortcutKey; +import com.android.launcher3.util.CancellableTask; import com.android.launcher3.util.InstantAppResolver; import com.android.launcher3.util.PackageUserKey; import com.android.launcher3.util.Preconditions; @@ -100,7 +100,7 @@ public class IconCache extends BaseIconCache { private final UserCache mUserManager; private final InstantAppResolver mInstantAppResolver; private final IconProvider mIconProvider; - private final HandlerRunnable mCancelledRunnable; + private final CancellableTask mCancelledTask; private final SparseArray mWidgetCategoryBitmapInfos; @@ -119,9 +119,8 @@ public class IconCache extends BaseIconCache { mIconProvider = iconProvider; mWidgetCategoryBitmapInfos = new SparseArray<>(); - mCancelledRunnable = new HandlerRunnable( - mWorkerHandler, () -> null, MAIN_EXECUTOR, c -> { }); - mCancelledRunnable.cancel(); + mCancelledTask = new CancellableTask(() -> null, MAIN_EXECUTOR, c -> { }); + mCancelledTask.cancel(); } @Override @@ -174,7 +173,7 @@ public class IconCache extends BaseIconCache { * * @return a request ID that can be used to cancel the request. */ - public HandlerRunnable updateIconInBackground(final ItemInfoUpdateReceiver caller, + public CancellableTask updateIconInBackground(final ItemInfoUpdateReceiver caller, final ItemInfoWithIcon info) { Preconditions.assertUIThread(); Supplier task; @@ -191,7 +190,7 @@ public class IconCache extends BaseIconCache { } else { Log.i(TAG, "Icon update not supported for " + info == null ? "null" : info.getClass().getName()); - return mCancelledRunnable; + return mCancelledTask; } if (mPendingIconRequestCount <= 0) { @@ -199,7 +198,7 @@ public class IconCache extends BaseIconCache { } mPendingIconRequestCount++; - HandlerRunnable request = new HandlerRunnable<>(mWorkerHandler, + CancellableTask request = new CancellableTask<>( task, MAIN_EXECUTOR, caller::reapplyItemInfo, this::onIconRequestEnd); Utilities.postAsyncCallback(mWorkerHandler, request); return request; diff --git a/src/com/android/launcher3/recyclerview/AllAppsRecyclerViewPool.kt b/src/com/android/launcher3/recyclerview/AllAppsRecyclerViewPool.kt index 71957e165b..43027da5fd 100644 --- a/src/com/android/launcher3/recyclerview/AllAppsRecyclerViewPool.kt +++ b/src/com/android/launcher3/recyclerview/AllAppsRecyclerViewPool.kt @@ -23,7 +23,7 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder import com.android.launcher3.BubbleTextView import com.android.launcher3.allapps.BaseAllAppsAdapter import com.android.launcher3.config.FeatureFlags -import com.android.launcher3.util.ExecutorRunnable +import com.android.launcher3.util.CancellableTask import com.android.launcher3.util.Executors.MAIN_EXECUTOR import com.android.launcher3.util.Executors.VIEW_PREINFLATION_EXECUTOR import com.android.launcher3.views.ActivityContext @@ -39,7 +39,7 @@ const val EXTRA_ICONS_COUNT = 2 class AllAppsRecyclerViewPool : RecycledViewPool() { var hasWorkProfile = false - var executorRunnable: ExecutorRunnable>? = null + private var mCancellableTask: CancellableTask>? = null /** * Preinflate app icons. If all apps RV cannot be scrolled down, we don't need to preinflate. @@ -62,14 +62,14 @@ class AllAppsRecyclerViewPool : RecycledViewPool() { override fun getLayoutManager(): RecyclerView.LayoutManager? = null } - executorRunnable?.cancel(/* interrupt= */ false) - executorRunnable = - ExecutorRunnable.createAndExecute( - VIEW_PREINFLATION_EXECUTOR, + mCancellableTask?.cancel() + var task: CancellableTask>? = null + task = + CancellableTask( { val list: ArrayList = ArrayList() for (i in 0 until preInflateCount) { - if (Thread.interrupted()) { + if (task?.canceled == true) { break } list.add( @@ -85,6 +85,8 @@ class AllAppsRecyclerViewPool : RecycledViewPool() { } } ) + mCancellableTask = task + VIEW_PREINFLATION_EXECUTOR.submit(mCancellableTask) } /** @@ -93,7 +95,7 @@ class AllAppsRecyclerViewPool : RecycledViewPool() { */ override fun clear() { super.clear() - executorRunnable?.cancel(/* interrupt= */ true) + mCancellableTask?.cancel() } /** diff --git a/src/com/android/launcher3/util/ExecutorRunnable.kt b/src/com/android/launcher3/util/CancellableTask.kt similarity index 52% rename from src/com/android/launcher3/util/ExecutorRunnable.kt rename to src/com/android/launcher3/util/CancellableTask.kt index 49cf5925b8..49ef020ff0 100644 --- a/src/com/android/launcher3/util/ExecutorRunnable.kt +++ b/src/com/android/launcher3/util/CancellableTask.kt @@ -17,62 +17,54 @@ package com.android.launcher3.util import java.util.concurrent.Executor -import java.util.concurrent.ExecutorService -import java.util.concurrent.Future import java.util.function.Consumer import java.util.function.Supplier /** A [Runnable] that can be posted to a [Executor] that can be cancelled. */ -class ExecutorRunnable -private constructor( +class CancellableTask +@JvmOverloads +constructor( private val task: Supplier, // Executor where consumer needs to be executed on. Typically UI executor. private val callbackExecutor: Executor, // Consumer that needs to be accepted upon completion of the task. Typically work that needs to // be done in UI thread after task completes. - private val callback: Consumer + private val callback: Consumer, + // Callback to be executed on callbackExecutor at the end irrespective of the task being + // completed or cancelled + private val endRunnable: Runnable = Runnable {} ) : Runnable { - // future of this runnable that will used for cancellation. - lateinit var future: Future<*> - // flag to cancel the callback var canceled = false + private set + + private var ended = false override fun run() { - val value: T = task.get() + if (canceled) return + val value = task.get() callbackExecutor.execute { if (!canceled) { callback.accept(value) } + onEnd() } } /** - * Cancel the [ExecutorRunnable] if not scheduled. If [ExecutorRunnable] has started execution - * at this time, we will try to cancel the callback if not executed yet. + * Cancel the [CancellableTask] if not scheduled. If [CancellableTask] has started execution at + * this time, we will try to cancel the callback if not executed yet. */ - fun cancel(interrupt: Boolean) { - future.cancel(interrupt) + fun cancel() { canceled = true + callbackExecutor.execute(this::onEnd) } - companion object { - /** - * Create [ExecutorRunnable] and execute it on task [Executor]. It will also save the - * [Future] into this [ExecutorRunnable] to be used for cancellation. - */ - fun createAndExecute( - // Executor where task will be executed, typically an Executor running on background - // thread. - taskExecutor: ExecutorService, - task: Supplier, - callbackExecutor: Executor, - callback: Consumer - ): ExecutorRunnable { - val executorRunnable = ExecutorRunnable(task, callbackExecutor, callback) - executorRunnable.future = taskExecutor.submit(executorRunnable) - return executorRunnable + private fun onEnd() { + if (!ended) { + ended = true + endRunnable.run() } } } diff --git a/src/com/android/launcher3/widget/DatabaseWidgetPreviewLoader.java b/src/com/android/launcher3/widget/DatabaseWidgetPreviewLoader.java index 99485bec2e..aab78bd8c0 100644 --- a/src/com/android/launcher3/widget/DatabaseWidgetPreviewLoader.java +++ b/src/com/android/launcher3/widget/DatabaseWidgetPreviewLoader.java @@ -41,9 +41,9 @@ import com.android.launcher3.Utilities; import com.android.launcher3.icons.BitmapRenderer; import com.android.launcher3.icons.LauncherIcons; import com.android.launcher3.icons.ShadowGenerator; -import com.android.launcher3.icons.cache.HandlerRunnable; import com.android.launcher3.model.WidgetItem; import com.android.launcher3.pm.ShortcutConfigActivityInfo; +import com.android.launcher3.util.CancellableTask; import com.android.launcher3.util.Executors; import com.android.launcher3.views.ActivityContext; import com.android.launcher3.widget.util.WidgetSizes; @@ -74,12 +74,12 @@ public class DatabaseWidgetPreviewLoader { * @return a request id which can be used to cancel the request. */ @NonNull - public HandlerRunnable loadPreview( + public CancellableTask loadPreview( @NonNull WidgetItem item, @NonNull Size previewSize, @NonNull Consumer callback) { Handler handler = Executors.UI_HELPER_EXECUTOR.getHandler(); - HandlerRunnable request = new HandlerRunnable<>(handler, + CancellableTask request = new CancellableTask<>( () -> generatePreview(item, previewSize.getWidth(), previewSize.getHeight()), MAIN_EXECUTOR, callback); diff --git a/src/com/android/launcher3/widget/WidgetCell.java b/src/com/android/launcher3/widget/WidgetCell.java index c75f9d114e..74101d7b8a 100644 --- a/src/com/android/launcher3/widget/WidgetCell.java +++ b/src/com/android/launcher3/widget/WidgetCell.java @@ -51,8 +51,8 @@ import com.android.launcher3.Launcher; import com.android.launcher3.R; import com.android.launcher3.icons.FastBitmapDrawable; import com.android.launcher3.icons.RoundDrawableWrapper; -import com.android.launcher3.icons.cache.HandlerRunnable; import com.android.launcher3.model.WidgetItem; +import com.android.launcher3.util.CancellableTask; import com.android.launcher3.views.ActivityContext; import java.util.function.Consumer; @@ -90,7 +90,7 @@ public class WidgetCell extends LinearLayout { private final DatabaseWidgetPreviewLoader mWidgetPreviewLoader; - protected HandlerRunnable mActiveRequest; + protected CancellableTask mActiveRequest; private boolean mAnimatePreview = true; protected final ActivityContext mActivity; diff --git a/src/com/android/launcher3/widget/picker/WidgetsListHeader.java b/src/com/android/launcher3/widget/picker/WidgetsListHeader.java index 3383299136..d373a3b327 100644 --- a/src/com/android/launcher3/widget/picker/WidgetsListHeader.java +++ b/src/com/android/launcher3/widget/picker/WidgetsListHeader.java @@ -35,9 +35,9 @@ import com.android.launcher3.LauncherAppState; import com.android.launcher3.R; import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver; import com.android.launcher3.icons.PlaceHolderIconDrawable; -import com.android.launcher3.icons.cache.HandlerRunnable; import com.android.launcher3.model.data.ItemInfoWithIcon; import com.android.launcher3.model.data.PackageItemInfo; +import com.android.launcher3.util.CancellableTask; import com.android.launcher3.views.ActivityContext; import com.android.launcher3.widget.model.WidgetsListHeaderEntry; @@ -57,7 +57,7 @@ public final class WidgetsListHeader extends LinearLayout implements ItemInfoUpd * widget apps aren't collapsable. */ private final boolean mIsCollapsable; - @Nullable private HandlerRunnable mIconLoadRequest; + @Nullable private CancellableTask mIconLoadRequest; @Nullable private Drawable mIconDrawable; @Nullable private WidgetsListDrawableState mListDrawableState; private ImageView mAppIcon; diff --git a/tests/multivalentTests/src/com/android/launcher3/util/ExecutorRunnableTest.kt b/tests/multivalentTests/src/com/android/launcher3/util/CancellableTaskTest.kt similarity index 92% rename from tests/multivalentTests/src/com/android/launcher3/util/ExecutorRunnableTest.kt rename to tests/multivalentTests/src/com/android/launcher3/util/CancellableTaskTest.kt index 6634577dc8..eb58096c1c 100644 --- a/tests/multivalentTests/src/com/android/launcher3/util/ExecutorRunnableTest.kt +++ b/tests/multivalentTests/src/com/android/launcher3/util/CancellableTaskTest.kt @@ -29,12 +29,12 @@ import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith -/** Unit test for [ExecutorRunnable] */ +/** Unit test for [CancellableTask] */ @SmallTest @RunWith(AndroidJUnit4::class) -class ExecutorRunnableTest { +class CancellableTaskTest { - private lateinit var underTest: ExecutorRunnable + private lateinit var underTest: CancellableTask private val lock = ReentrantLock() private var result: Int = -1 @@ -51,8 +51,7 @@ class ExecutorRunnableTest { private fun submitJob() { underTest = - ExecutorRunnable.createAndExecute( - Executors.UI_HELPER_EXECUTOR, + CancellableTask( { isTaskExecuted = true 1 @@ -63,6 +62,7 @@ class ExecutorRunnableTest { result = it + 1 } ) + Executors.UI_HELPER_EXECUTOR.execute(underTest) } @Test @@ -82,7 +82,7 @@ class ExecutorRunnableTest { Executors.UI_HELPER_EXECUTOR.submit { lock.lock() } submitJob() - underTest.cancel(false) + underTest.cancel() lock.unlock() // unblock task on UI_HELPER_EXECUTOR awaitAllExecutorCompleted() @@ -101,7 +101,7 @@ class ExecutorRunnableTest { awaitExecutorCompleted(Executors.UI_HELPER_EXECUTOR) assertTrue("task should be executed.", isTaskExecuted) - underTest.cancel(false) + underTest.cancel() lock.unlock() // unblock callback on VIEW_PREINFLATION_EXECUTOR awaitExecutorCompleted(Executors.VIEW_PREINFLATION_EXECUTOR) @@ -113,7 +113,7 @@ class ExecutorRunnableTest { fun run_and_cancelAfterCompletion_executeAll() { awaitAllExecutorCompleted() - underTest.cancel(false) + underTest.cancel() assertTrue("task should be executed", isTaskExecuted) assertTrue("callback should be executed", isCallbackExecuted)