From c2fcb4099bd4fad19d9bcff4ca197f0ab0c904a2 Mon Sep 17 00:00:00 2001 From: Joshua Trask Date: Tue, 26 Sep 2023 13:53:36 +0000 Subject: ResolverListAdapter: Switch `Handler`->`Executor` With the new `mDestroyed` bookkeeping in ag/24854314, we no longer need the richer (but less-testable/etc) API of a `Handler`. In the first snapshot, this retains the ad-hoc flow control mechanisms that direct the `testPostListReadyAtEndOfRebuild_` tests to show the direct before-and-after equivalence against the `Handler` model. All the existing tests still pass on the new code. The second snapshot removes those ad-hoc mechanisms because the tests now have complete control of the execution via `TestExecutor`. Bug: (general code cleanup) Test: `IntentResolverUnitTests` / `CtsSharesheetDeviceTest` Change-Id: I3063d45aedec47a81bf9bc7f76c26873de1383af --- .../intentresolver/ResolverListAdapter.java | 23 +++++++++++----------- 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'java/src') diff --git a/java/src/com/android/intentresolver/ResolverListAdapter.java b/java/src/com/android/intentresolver/ResolverListAdapter.java index 4ad8d926..0d199fa3 100644 --- a/java/src/com/android/intentresolver/ResolverListAdapter.java +++ b/java/src/com/android/intentresolver/ResolverListAdapter.java @@ -28,7 +28,6 @@ import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.drawable.Drawable; import android.os.AsyncTask; -import android.os.Handler; import android.os.RemoteException; import android.os.Trace; import android.os.UserHandle; @@ -82,7 +81,7 @@ public class ResolverListAdapter extends BaseAdapter { private final Set mRequestedIcons = new HashSet<>(); private final Set mRequestedLabels = new HashSet<>(); private final Executor mBgExecutor; - private final Handler mMainHandler; + private final Executor mCallbackExecutor; private final AtomicBoolean mDestroyed = new AtomicBoolean(); private ResolveInfo mLastChosen; @@ -124,7 +123,7 @@ public class ResolverListAdapter extends BaseAdapter { initialIntentsUserSpace, targetDataLoader, AsyncTask.SERIAL_EXECUTOR, - context.getMainThreadHandler()); + runnable -> context.getMainThreadHandler().post(runnable)); } @VisibleForTesting @@ -141,7 +140,7 @@ public class ResolverListAdapter extends BaseAdapter { UserHandle initialIntentsUserSpace, TargetDataLoader targetDataLoader, Executor bgExecutor, - Handler mainHandler) { + Executor callbackExecutor) { mContext = context; mIntents = payloadIntents; mInitialIntents = initialIntents; @@ -157,7 +156,7 @@ public class ResolverListAdapter extends BaseAdapter { mResolverListCommunicator = resolverListCommunicator; mInitialIntentsUserSpace = initialIntentsUserSpace; mBgExecutor = bgExecutor; - mMainHandler = mainHandler; + mCallbackExecutor = callbackExecutor; } public final DisplayResolveInfo getFirstDisplayResolveInfo() { @@ -236,12 +235,12 @@ public class ResolverListAdapter extends BaseAdapter { /** * Rebuild the list of resolvers. When rebuilding is complete, queue the {@code onPostListReady} - * callback on the main handler with {@code rebuildCompleted} true. + * callback on the callback executor with {@code rebuildCompleted} true. * * In some cases some parts will need some asynchronous work to complete. Then this will first - * immediately queue {@code onPostListReady} (on the main handler) with {@code rebuildCompleted} - * false; only when the asynchronous work completes will this then go on to queue another - * {@code onPostListReady} callback with {@code rebuildCompleted} true. + * immediately queue {@code onPostListReady} (on the callback executor) with + * {@code rebuildCompleted} false; only when the asynchronous work completes will this then go + * on to queue another {@code onPostListReady} callback with {@code rebuildCompleted} true. * * The {@code doPostProcessing} parameter is used to specify whether to update the UI and * load additional targets (e.g. direct share) after the list has been rebuilt. We may choose @@ -456,7 +455,7 @@ public class ResolverListAdapter extends BaseAdapter { throw t; } finally { final List result = sortedComponents; - mMainHandler.post(() -> onComponentsSorted(result, doPostProcessing)); + mCallbackExecutor.execute(() -> onComponentsSorted(result, doPostProcessing)); } }); return false; @@ -541,7 +540,7 @@ public class ResolverListAdapter extends BaseAdapter { /** * Some necessary methods for creating the list are initiated in onCreate and will also * determine the layout known. We therefore can't update the UI inline and post to the - * handler thread to update after the current task is finished. + * callback executor to update after the current task is finished. * @param doPostProcessing Whether to update the UI and load additional direct share targets * after the list has been rebuilt * @param rebuildCompleted Whether the list has been completely rebuilt @@ -557,7 +556,7 @@ public class ResolverListAdapter extends BaseAdapter { doPostProcessing, rebuildCompleted); } }; - mMainHandler.post(listReadyRunnable); + mCallbackExecutor.execute(listReadyRunnable); } private void addResolveInfoWithAlternates(ResolvedComponentInfo rci) { -- cgit v1.2.3-59-g8ed1b