Simplifying Launcher binding callbacks
> Making all methods as default
> Removing obsolete logic around synchronous binding
> Removing some UI dependencies from bind callbacks
Bug: 187353581
Test: Manual
Change-Id: I0d2bbb060af2cab7c64541d7695055629dfaf0b8
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 5a9257d..5720a8c 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -118,6 +118,7 @@
import com.android.launcher3.allapps.AllAppsStore;
import com.android.launcher3.allapps.AllAppsTransitionController;
import com.android.launcher3.allapps.DiscoveryBounce;
+import com.android.launcher3.anim.AnimatorListeners;
import com.android.launcher3.anim.PropertyListBuilder;
import com.android.launcher3.compat.AccessibilityManagerCompat;
import com.android.launcher3.config.FeatureFlags;
@@ -174,6 +175,7 @@
import com.android.launcher3.util.PackageManagerHelper;
import com.android.launcher3.util.PackageUserKey;
import com.android.launcher3.util.PendingRequestArgs;
+import com.android.launcher3.util.RunnableList;
import com.android.launcher3.util.SafeCloseable;
import com.android.launcher3.util.SystemUiController;
import com.android.launcher3.util.Themes;
@@ -2060,7 +2062,7 @@
@Override
public void clearPendingBinds() {
if (mPendingExecutor != null) {
- mPendingExecutor.markCompleted();
+ mPendingExecutor.cancel();
mPendingExecutor = null;
// We might have set this flag previously and forgot to clear it.
@@ -2482,25 +2484,6 @@
return info;
}
- public void onPagesBoundSynchronously(IntSet pages) {
- mSynchronouslyBoundPages = pages;
- mWorkspace.setCurrentPage(pages.getArray().get(0));
- mPagesToBindSynchronously = new IntSet();
- }
-
- @Override
- public void executeOnNextDraw(ViewOnDrawExecutor executor) {
- clearPendingBinds();
- mPendingExecutor = executor;
- if (!isInState(ALL_APPS)) {
- mAppsView.getAppsStore().enableDeferUpdates(AllAppsStore.DEFER_UPDATES_NEXT_DRAW);
- mPendingExecutor.execute(() -> mAppsView.getAppsStore().disableDeferUpdates(
- AllAppsStore.DEFER_UPDATES_NEXT_DRAW));
- }
-
- executor.attachTo(this);
- }
-
public void clearPendingExecutor(ViewOnDrawExecutor executor) {
if (mPendingExecutor == executor) {
mPendingExecutor = null;
@@ -2508,22 +2491,31 @@
}
@Override
- public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
+ public void onInitialBindComplete(IntSet boundPages, RunnableList pendingTasks) {
+ mSynchronouslyBoundPages = boundPages;
+ if (!boundPages.isEmpty()) {
+ mWorkspace.setCurrentPage(boundPages.getArray().get(0));
+ }
+ mPagesToBindSynchronously = new IntSet();
+
+ clearPendingBinds();
+ ViewOnDrawExecutor executor = new ViewOnDrawExecutor(pendingTasks);
+ mPendingExecutor = executor;
+ if (!isInState(ALL_APPS)) {
+ mAppsView.getAppsStore().enableDeferUpdates(AllAppsStore.DEFER_UPDATES_NEXT_DRAW);
+ pendingTasks.add(() -> mAppsView.getAppsStore().disableDeferUpdates(
+ AllAppsStore.DEFER_UPDATES_NEXT_DRAW));
+ }
+
AlphaProperty property = mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD);
if (property.getValue() < 1) {
ObjectAnimator anim = ObjectAnimator.ofFloat(property, MultiValueAlpha.VALUE, 1);
- if (executor != null) {
- anim.addListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator animation) {
- executor.onLoadAnimationCompleted();
- }
- });
- }
+ anim.addListener(AnimatorListeners.forEndCallback(executor::onLoadAnimationCompleted));
anim.start();
- } else if (executor != null) {
+ } else {
executor.onLoadAnimationCompleted();
}
+ executor.attachTo(this);
}
/**
diff --git a/src/com/android/launcher3/model/BaseLoaderResults.java b/src/com/android/launcher3/model/BaseLoaderResults.java
index 12ee676..30755e3 100644
--- a/src/com/android/launcher3/model/BaseLoaderResults.java
+++ b/src/com/android/launcher3/model/BaseLoaderResults.java
@@ -18,7 +18,9 @@
import static com.android.launcher3.model.ModelUtils.filterCurrentWorkspaceItems;
import static com.android.launcher3.model.ModelUtils.sortWorkspaceItemsSpatially;
+import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
+import android.os.Process;
import android.util.Log;
import com.android.launcher3.InvariantDeviceProfile;
@@ -33,7 +35,7 @@
import com.android.launcher3.util.IntSet;
import com.android.launcher3.util.LooperExecutor;
import com.android.launcher3.util.LooperIdleLock;
-import com.android.launcher3.util.ViewOnDrawExecutor;
+import com.android.launcher3.util.RunnableList;
import java.util.ArrayList;
import java.util.Collections;
@@ -175,7 +177,6 @@
currentScreenIndices = screenIndices;
}
- final boolean validFirstPage = !currentScreenIndices.isEmpty();
IntSet currentScreenIds = new IntSet();
currentScreenIndices.forEach(
@@ -204,40 +205,25 @@
// Bind workspace screens
executeCallbacksTask(c -> c.bindScreens(mOrderedScreenIds), mUiExecutor);
- Executor mainExecutor = mUiExecutor;
// Load items on the current page.
- bindWorkspaceItems(currentWorkspaceItems, mainExecutor);
- bindAppWidgets(currentAppWidgets, mainExecutor);
+ bindWorkspaceItems(currentWorkspaceItems, mUiExecutor);
+ bindAppWidgets(currentAppWidgets, mUiExecutor);
mExtraItems.forEach(item ->
- executeCallbacksTask(c -> c.bindExtraContainerItems(item), mainExecutor));
+ executeCallbacksTask(c -> c.bindExtraContainerItems(item), mUiExecutor));
- // In case of validFirstPage, only bind the first screen, and defer binding the
- // remaining screens after first onDraw (and an optional the fade animation whichever
- // happens later).
- // This ensures that the first screen is immediately visible (eg. during rotation)
- // In case of !validFirstPage, bind all pages one after other.
+ RunnableList pendingTasks = new RunnableList();
+ Executor pendingExecutor = pendingTasks::add;
+ bindWorkspaceItems(otherWorkspaceItems, pendingExecutor);
+ bindAppWidgets(otherAppWidgets, pendingExecutor);
+ executeCallbacksTask(c -> c.finishBindingItems(currentScreenIndices), pendingExecutor);
+ pendingExecutor.execute(
+ () -> MODEL_EXECUTOR.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT));
- final Executor deferredExecutor =
- validFirstPage ? new ViewOnDrawExecutor() : mainExecutor;
-
- executeCallbacksTask(c -> c.finishFirstPageBind(
- validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null), mainExecutor);
-
- bindWorkspaceItems(otherWorkspaceItems, deferredExecutor);
- bindAppWidgets(otherAppWidgets, deferredExecutor);
- // Tell the workspace that we're done binding items
- executeCallbacksTask(c -> c.finishBindingItems(currentScreenIndices), deferredExecutor);
-
- if (validFirstPage) {
- executeCallbacksTask(c -> {
- // We are loading synchronously, which means, some of the pages will be
- // bound after first draw. Inform the mCallbacks that page binding is
- // not complete, and schedule the remaining pages.
- c.onPagesBoundSynchronously(currentScreenIndices);
- c.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
-
- }, mUiExecutor);
- }
+ executeCallbacksTask(
+ c -> {
+ MODEL_EXECUTOR.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
+ c.onInitialBindComplete(currentScreenIndices, pendingTasks);
+ }, mUiExecutor);
}
private void bindWorkspaceItems(
diff --git a/src/com/android/launcher3/model/BgDataModel.java b/src/com/android/launcher3/model/BgDataModel.java
index 0740a30..ba825ca 100644
--- a/src/com/android/launcher3/model/BgDataModel.java
+++ b/src/com/android/launcher3/model/BgDataModel.java
@@ -49,7 +49,7 @@
import com.android.launcher3.util.IntSet;
import com.android.launcher3.util.IntSparseArrayMap;
import com.android.launcher3.util.ItemInfoMatcher;
-import com.android.launcher3.util.ViewOnDrawExecutor;
+import com.android.launcher3.util.RunnableList;
import com.android.launcher3.widget.model.WidgetsListBaseEntry;
import java.io.FileDescriptor;
@@ -462,35 +462,41 @@
* Returns an IntSet of page numbers to bind first, synchronously if possible
* or an empty IntSet
*/
- IntSet getPagesToBindSynchronously();
- void clearPendingBinds();
- void startBinding();
- void bindItems(List<ItemInfo> shortcuts, boolean forceAnimateIcons);
- void bindScreens(IntArray orderedScreenIds);
- void finishFirstPageBind(ViewOnDrawExecutor executor);
- void finishBindingItems(IntSet pagesBoundFirst);
- void preAddApps();
- void bindAppsAdded(IntArray newScreens,
- ArrayList<ItemInfo> addNotAnimated, ArrayList<ItemInfo> addAnimated);
+ default IntSet getPagesToBindSynchronously() {
+ return new IntSet();
+ }
+
+ default void clearPendingBinds() { }
+ default void startBinding() { }
+
+ default void bindItems(List<ItemInfo> shortcuts, boolean forceAnimateIcons) { }
+ default void bindScreens(IntArray orderedScreenIds) { }
+ default void finishBindingItems(IntSet pagesBoundFirst) { }
+ default void preAddApps() { }
+ default void bindAppsAdded(IntArray newScreens,
+ ArrayList<ItemInfo> addNotAnimated, ArrayList<ItemInfo> addAnimated) { }
/**
* Binds updated incremental download progress
*/
- void bindIncrementalDownloadProgressUpdated(AppInfo app);
- void bindWorkspaceItemsChanged(List<WorkspaceItemInfo> updated);
- void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
- void bindRestoreItemsChange(HashSet<ItemInfo> updates);
- void bindWorkspaceComponentsRemoved(ItemInfoMatcher matcher);
- void bindAllWidgets(List<WidgetsListBaseEntry> widgets);
- void onPagesBoundSynchronously(IntSet pages);
- void executeOnNextDraw(ViewOnDrawExecutor executor);
- void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMap);
+ default void bindIncrementalDownloadProgressUpdated(AppInfo app) { }
+ default void bindWorkspaceItemsChanged(List<WorkspaceItemInfo> updated) { }
+ default void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) { }
+ default void bindRestoreItemsChange(HashSet<ItemInfo> updates) { }
+ default void bindWorkspaceComponentsRemoved(ItemInfoMatcher matcher) { }
+ default void bindAllWidgets(List<WidgetsListBaseEntry> widgets) { }
+
+ default void onInitialBindComplete(IntSet boundPages, RunnableList pendingTasks) {
+ pendingTasks.executeAllAndDestroy();
+ }
+
+ default void bindDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMap) { }
/**
* Binds extra item provided any external source
*/
default void bindExtraContainerItems(FixedContainerItems item) { }
- void bindAllApplications(AppInfo[] apps, int flags);
+ default void bindAllApplications(AppInfo[] apps, int flags) { }
}
}
diff --git a/src/com/android/launcher3/secondarydisplay/SecondaryDisplayLauncher.java b/src/com/android/launcher3/secondarydisplay/SecondaryDisplayLauncher.java
index b271a6a..1a96c23 100644
--- a/src/com/android/launcher3/secondarydisplay/SecondaryDisplayLauncher.java
+++ b/src/com/android/launcher3/secondarydisplay/SecondaryDisplayLauncher.java
@@ -35,23 +35,13 @@
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.LauncherAppWidgetInfo;
-import com.android.launcher3.model.data.WorkspaceItemInfo;
import com.android.launcher3.popup.PopupContainerWithArrow;
import com.android.launcher3.popup.PopupDataProvider;
import com.android.launcher3.util.ComponentKey;
-import com.android.launcher3.util.IntArray;
-import com.android.launcher3.util.IntSet;
-import com.android.launcher3.util.ItemInfoMatcher;
import com.android.launcher3.util.Themes;
-import com.android.launcher3.util.ViewOnDrawExecutor;
import com.android.launcher3.views.BaseDragLayer;
-import com.android.launcher3.widget.model.WidgetsListBaseEntry;
-import java.util.ArrayList;
import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
/**
* Launcher activity for secondary displays
@@ -176,67 +166,10 @@
}
@Override
- public IntSet getPagesToBindSynchronously() {
- return new IntSet();
- }
-
- @Override
- public void clearPendingBinds() { }
-
- @Override
- public void startBinding() { }
-
- @Override
- public void bindItems(List<ItemInfo> shortcuts, boolean forceAnimateIcons) { }
-
- @Override
- public void bindScreens(IntArray orderedScreenIds) { }
-
- @Override
- public void finishFirstPageBind(ViewOnDrawExecutor executor) {
- if (executor != null) {
- executor.onLoadAnimationCompleted();
- }
- }
-
- @Override
- public void finishBindingItems(IntSet pagesBoundFirst) { }
-
- @Override
- public void preAddApps() { }
-
- @Override
- public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
- ArrayList<ItemInfo> addAnimated) { }
-
- @Override
public void bindIncrementalDownloadProgressUpdated(AppInfo app) {
mAppsView.getAppsStore().updateProgressBar(app);
}
- @Override
- public void bindWorkspaceItemsChanged(List<WorkspaceItemInfo> updated) { }
-
- @Override
- public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets) { }
-
- @Override
- public void bindRestoreItemsChange(HashSet<ItemInfo> updates) { }
-
- @Override
- public void bindWorkspaceComponentsRemoved(ItemInfoMatcher matcher) { }
-
- @Override
- public void bindAllWidgets(List<WidgetsListBaseEntry> widgets) { }
-
- @Override
- public void onPagesBoundSynchronously(IntSet pages) { }
-
- @Override
- public void executeOnNextDraw(ViewOnDrawExecutor executor) {
- executor.attachTo(getDragLayer(), false, null);
- }
-
/**
* Called when apps-button is clicked
*/
diff --git a/src/com/android/launcher3/util/ViewOnDrawExecutor.java b/src/com/android/launcher3/util/ViewOnDrawExecutor.java
index 82e24c2..5d90291 100644
--- a/src/com/android/launcher3/util/ViewOnDrawExecutor.java
+++ b/src/com/android/launcher3/util/ViewOnDrawExecutor.java
@@ -16,28 +16,21 @@
package com.android.launcher3.util;
-import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
-
-import android.os.Process;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.ViewTreeObserver.OnDrawListener;
-import androidx.annotation.VisibleForTesting;
-
import com.android.launcher3.Launcher;
-import java.util.ArrayList;
-import java.util.concurrent.Executor;
import java.util.function.Consumer;
/**
* An executor which runs all the tasks after the first onDraw is called on the target view.
*/
-public class ViewOnDrawExecutor implements Executor, OnDrawListener, Runnable,
+public class ViewOnDrawExecutor implements OnDrawListener, Runnable,
OnAttachStateChangeListener {
- private final ArrayList<Runnable> mTasks = new ArrayList<>();
+ private final RunnableList mTasks;
private Consumer<ViewOnDrawExecutor> mOnClearCallback;
private View mAttachedView;
@@ -46,22 +39,16 @@
private boolean mLoadAnimationCompleted;
private boolean mFirstDrawCompleted;
- public void attachTo(Launcher launcher) {
- attachTo(launcher.getWorkspace(), true /* waitForLoadAnimation */,
- launcher::clearPendingExecutor);
+ private boolean mCancelled;
+
+ public ViewOnDrawExecutor(RunnableList tasks) {
+ mTasks = tasks;
}
- /**
- * Attached the executor to the existence of the view
- */
- public void attachTo(View attachedView, boolean waitForLoadAnimation,
- Consumer<ViewOnDrawExecutor> onClearCallback) {
- mOnClearCallback = onClearCallback;
- mAttachedView = attachedView;
+ public void attachTo(Launcher launcher) {
+ mOnClearCallback = launcher::clearPendingExecutor;
+ mAttachedView = launcher.getWorkspace();
mAttachedView.addOnAttachStateChangeListener(this);
- if (!waitForLoadAnimation) {
- mLoadAnimationCompleted = true;
- }
if (mAttachedView.isAttachedToWindow()) {
attachObserver();
@@ -75,12 +62,6 @@
}
@Override
- public void execute(Runnable command) {
- mTasks.add(command);
- MODEL_EXECUTOR.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
- }
-
- @Override
public void onViewAttachedToWindow(View v) {
attachObserver();
}
@@ -105,12 +86,17 @@
public void run() {
// Post the pending tasks after both onDraw and onLoadAnimationCompleted have been called.
if (mLoadAnimationCompleted && mFirstDrawCompleted && !mCompleted) {
- runAllTasks();
+ markCompleted();
}
}
+ /**
+ * Executes all tasks immediately
+ */
public void markCompleted() {
- mTasks.clear();
+ if (!mCancelled) {
+ mTasks.executeAllAndDestroy();
+ }
mCompleted = true;
if (mAttachedView != null) {
mAttachedView.getViewTreeObserver().removeOnDrawListener(this);
@@ -119,21 +105,10 @@
if (mOnClearCallback != null) {
mOnClearCallback.accept(this);
}
- MODEL_EXECUTOR.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
}
- protected boolean isCompleted() {
- return mCompleted;
- }
-
- /**
- * Executes all tasks immediately
- */
- @VisibleForTesting
- public void runAllTasks() {
- for (final Runnable r : mTasks) {
- r.run();
- }
+ public void cancel() {
+ mCancelled = true;
markCompleted();
}
}