Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 93221e00 authored by Sunny Goyal's avatar Sunny Goyal Committed by Android (Google) Code Review
Browse files

Merge "Converting ModelTask to an interface instead of an abstract class" into main

parents f4b51c60 99389389
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -15,8 +15,8 @@
 */
package com.android.launcher3.model;

import static com.android.launcher3.LauncherPrefs.nonRestorableItem;
import static com.android.launcher3.EncryptionType.ENCRYPTED;
import static com.android.launcher3.LauncherPrefs.nonRestorableItem;
import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT;
import static com.android.quickstep.InstantAppResolverImpl.COMPONENT_CLASS_MARKER;

@@ -32,6 +32,7 @@ import androidx.annotation.NonNull;

import com.android.launcher3.ConstantItem;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherModel.ModelUpdateTask;
import com.android.launcher3.LauncherPrefs;
import com.android.launcher3.model.BgDataModel.FixedContainerItems;
import com.android.launcher3.model.QuickstepModelDelegate.PredictorState;
@@ -47,7 +48,7 @@ import java.util.stream.Collectors;
/**
 * Task to update model as a result of predicted apps update
 */
public class PredictionUpdateTask extends BaseModelUpdateTask {
public class PredictionUpdateTask implements ModelUpdateTask {

    public static final ConstantItem<Boolean> LAST_PREDICTION_ENABLED =
            nonRestorableItem("last_prediction_enabled_state", true, ENCRYPTED);
@@ -61,8 +62,9 @@ public class PredictionUpdateTask extends BaseModelUpdateTask {
    }

    @Override
    public void execute(@NonNull final LauncherAppState app, @NonNull final BgDataModel dataModel,
            @NonNull final AllAppsList apps) {
    public void execute(@NonNull ModelTaskController taskController, @NonNull BgDataModel dataModel,
            @NonNull AllAppsList apps) {
        LauncherAppState app = taskController.getApp();
        Context context = app.getContext();

        // TODO: remove this
@@ -119,7 +121,7 @@ public class PredictionUpdateTask extends BaseModelUpdateTask {

        FixedContainerItems fci = new FixedContainerItems(mPredictorState.containerId, items);
        dataModel.extraItems.put(fci.containerId, fci);
        bindExtraContainerItems(fci);
        taskController.bindExtraContainerItems(fci);
        usersForChangedShortcuts.forEach(
                u -> dataModel.updateShortcutPinnedState(app.getContext(), u));

+6 −6
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherModel.ModelUpdateTask;
import com.android.launcher3.model.BgDataModel.FixedContainerItems;
import com.android.launcher3.model.QuickstepModelDelegate.PredictorState;
import com.android.launcher3.model.data.ItemInfo;
@@ -41,7 +41,7 @@ import java.util.function.Predicate;
import java.util.stream.Collectors;

/** Task to update model as a result of predicted widgets update */
public final class WidgetsPredictionUpdateTask extends BaseModelUpdateTask {
public final class WidgetsPredictionUpdateTask implements ModelUpdateTask {
    private final PredictorState mPredictorState;
    private final List<AppTarget> mTargets;

@@ -58,8 +58,8 @@ public final class WidgetsPredictionUpdateTask extends BaseModelUpdateTask {
     * workspace.
     */
    @Override
    public void execute(@NonNull final LauncherAppState appState,
            @NonNull final BgDataModel dataModel, @NonNull final AllAppsList apps) {
    public void execute(@NonNull ModelTaskController taskController, @NonNull BgDataModel dataModel,
            @NonNull AllAppsList apps) {
        Set<ComponentKey> widgetsInWorkspace = dataModel.appWidgets.stream().map(
                widget -> new ComponentKey(widget.providerName, widget.user)).collect(
                Collectors.toSet());
@@ -98,7 +98,7 @@ public final class WidgetsPredictionUpdateTask extends BaseModelUpdateTask {

        List<ItemInfo> items;
        if (enableCategorizedWidgetSuggestions()) {
            Context context = appState.getContext();
            Context context = taskController.getApp().getContext();
            WidgetRecommendationCategoryProvider categoryProvider =
                    WidgetRecommendationCategoryProvider.newInstance(context);
            items = servicePredictedItems.stream()
@@ -115,7 +115,7 @@ public final class WidgetsPredictionUpdateTask extends BaseModelUpdateTask {
                new FixedContainerItems(mPredictorState.containerId, items);

        dataModel.extraItems.put(mPredictorState.containerId, fixedContainerItems);
        bindExtraContainerItems(fixedContainerItems);
        taskController.bindExtraContainerItems(fixedContainerItems);

        // Don't store widgets prediction to disk because it is not used frequently.
    }
+3 −15
Original line number Diff line number Diff line
@@ -58,10 +58,6 @@ import com.android.launcher3.logger.LauncherAtomExtensions.DeviceSearchResultCon
import com.android.launcher3.logger.LauncherAtomExtensions.ExtendedContainers;
import com.android.launcher3.logging.InstanceId;
import com.android.launcher3.logging.StatsLogManager;
import com.android.launcher3.model.AllAppsList;
import com.android.launcher3.model.BaseModelUpdateTask;
import com.android.launcher3.model.BgDataModel;
import com.android.launcher3.model.data.CollectionInfo;
import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.util.Executors;
import com.android.launcher3.util.LogConfig;
@@ -371,17 +367,9 @@ public class StatsLogCompatManager extends StatsLogManager {
            if (mItemInfo.container < 0 || !LauncherAppState.INSTANCE.executeIfCreated(app -> {
                // Item is inside a collection, fetch collection info in a BG thread
                // and then write to StatsLog.
                app.getModel().enqueueModelUpdateTask(
                        new BaseModelUpdateTask() {
                            @Override
                            public void execute(@NonNull final LauncherAppState app,
                                    @NonNull final BgDataModel dataModel,
                                    @NonNull final AllAppsList apps) {
                                CollectionInfo collectionInfo =
                                        dataModel.collections.get(mItemInfo.container);
                                write(event, applyOverwrites(mItemInfo.buildProto(collectionInfo)));
                            }
                        });
                app.getModel().enqueueModelUpdateTask((taskController, dataModel, apps) ->
                        write(event, applyOverwrites(mItemInfo.buildProto(
                                dataModel.collections.get(mItemInfo.container)))));
            })) {
                // Write log on the model thread so that logs do not go out of order
                // (for eg: drop comes after drag)
+70 −92
Original line number Diff line number Diff line
@@ -48,7 +48,6 @@ import com.android.launcher3.icons.IconCache;
import com.android.launcher3.model.AddWorkspaceItemsTask;
import com.android.launcher3.model.AllAppsList;
import com.android.launcher3.model.BaseLauncherBinder;
import com.android.launcher3.model.BaseModelUpdateTask;
import com.android.launcher3.model.BgDataModel;
import com.android.launcher3.model.BgDataModel.Callbacks;
import com.android.launcher3.model.CacheDataUpdatedTask;
@@ -57,6 +56,7 @@ import com.android.launcher3.model.LoaderTask;
import com.android.launcher3.model.ModelDbController;
import com.android.launcher3.model.ModelDelegate;
import com.android.launcher3.model.ModelLauncherCallbacks;
import com.android.launcher3.model.ModelTaskController;
import com.android.launcher3.model.ModelWriter;
import com.android.launcher3.model.PackageInstallStateChangedTask;
import com.android.launcher3.model.PackageUpdatedTask;
@@ -82,7 +82,6 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Supplier;

@@ -426,13 +425,9 @@ public class LauncherModel implements InstallSessionTracker.Callback {
    @Override
    public void onInstallSessionCreated(@NonNull final PackageInstallInfo sessionInfo) {
        if (FeatureFlags.PROMISE_APPS_IN_ALL_APPS.get()) {
            enqueueModelUpdateTask(new BaseModelUpdateTask() {
                @Override
                public void execute(@NonNull final LauncherAppState app,
                        @NonNull final BgDataModel dataModel, @NonNull final AllAppsList apps) {
                    apps.addPromiseApp(app.getContext(), sessionInfo);
                    bindApplicationsIfNeeded();
                }
            enqueueModelUpdateTask((taskController, dataModel, apps) -> {
                apps.addPromiseApp(mApp.getContext(), sessionInfo);
                taskController.bindApplicationsIfNeeded();
            });
        }
    }
@@ -440,11 +435,8 @@ public class LauncherModel implements InstallSessionTracker.Callback {
    @Override
    public void onSessionFailure(@NonNull final String packageName,
            @NonNull final UserHandle user) {
        enqueueModelUpdateTask(new BaseModelUpdateTask() {
            @Override
            public void execute(@NonNull final LauncherAppState app,
                    @NonNull final BgDataModel dataModel, @NonNull final AllAppsList apps) {
                IconCache iconCache = app.getIconCache();
        enqueueModelUpdateTask((taskController, dataModel, apps) -> {
            IconCache iconCache = mApp.getIconCache();
            final IntSet removedIds = new IntSet();
            HashSet<WorkspaceItemInfo> archivedWorkspaceItemsToCacheRefresh = new HashSet<>();
            boolean isAppArchived = PackageManagerHelper.INSTANCE.get(mApp.getContext())
@@ -482,17 +474,16 @@ public class LauncherModel implements InstallSessionTracker.Callback {
            }

            if (!removedIds.isEmpty()) {
                    deleteAndBindComponentsRemoved(
                taskController.deleteAndBindComponentsRemoved(
                        ItemInfoMatcher.ofItemIds(removedIds),
                        "removed because install session failed");
            }
            if (!archivedWorkspaceItemsToCacheRefresh.isEmpty()) {
                    bindUpdatedWorkspaceItems(
                taskController.bindUpdatedWorkspaceItems(
                        archivedWorkspaceItemsToCacheRefresh.stream().toList());
            }
            if (isAppArchived) {
                    bindApplicationsIfNeeded();
                }
                taskController.bindApplicationsIfNeeded();
            }
        });
    }
@@ -583,13 +574,9 @@ public class LauncherModel implements InstallSessionTracker.Callback {
     */
    public void onWidgetLabelsUpdated(@NonNull final HashSet<String> updatedPackages,
            @NonNull final UserHandle user) {
        enqueueModelUpdateTask(new BaseModelUpdateTask() {
            @Override
            public void execute(@NonNull final LauncherAppState app,
                    @NonNull final BgDataModel dataModel, @NonNull final AllAppsList apps) {
                dataModel.widgetsModel.onPackageIconsUpdated(updatedPackages, user, app);
                bindUpdatedWidgets(dataModel);
            }
        enqueueModelUpdateTask((taskController, dataModel, apps) ->  {
            dataModel.widgetsModel.onPackageIconsUpdated(updatedPackages, user, mApp);
            taskController.bindUpdatedWidgets(dataModel);
        });
    }

@@ -597,8 +584,15 @@ public class LauncherModel implements InstallSessionTracker.Callback {
        if (mModelDestroyed) {
            return;
        }
        task.init(mApp, this, mBgDataModel, mBgAllAppsList, MAIN_EXECUTOR);
        MODEL_EXECUTOR.execute(task);
        MODEL_EXECUTOR.execute(() -> {
            if (!isModelLoaded()) {
                // Loader has not yet run.
                return;
            }
            ModelTaskController controller = new ModelTaskController(
                    mApp, mBgDataModel, mBgAllAppsList, this, MAIN_EXECUTOR);
            task.execute(controller, mBgDataModel, mBgAllAppsList);
        });
    }

    /**
@@ -610,18 +604,10 @@ public class LauncherModel implements InstallSessionTracker.Callback {
        void execute(@NonNull Callbacks callbacks);
    }

    /**
     * A runnable which changes/updates the data model of the launcher based on certain events.
     */
    public interface ModelUpdateTask extends Runnable {

        /**
         * Called before the task is posted to initialize the internal state.
         */
        void init(@NonNull LauncherAppState app, @NonNull LauncherModel model,
                @NonNull BgDataModel dataModel, @NonNull AllAppsList allAppsList,
                @NonNull Executor uiExecutor);
    public interface ModelUpdateTask {

        void execute(@NonNull ModelTaskController taskController,
                @NonNull BgDataModel dataModel, @NonNull AllAppsList apps);
    }

    public void updateAndBindWorkspaceItem(@NonNull final WorkspaceItemInfo si,
@@ -638,27 +624,19 @@ public class LauncherModel implements InstallSessionTracker.Callback {
     */
    public void updateAndBindWorkspaceItem(
            @NonNull final Supplier<WorkspaceItemInfo> itemProvider) {
        enqueueModelUpdateTask(new BaseModelUpdateTask() {
            @Override
            public void execute(@NonNull final LauncherAppState app,
                    @NonNull final BgDataModel dataModel, @NonNull final AllAppsList apps) {
        enqueueModelUpdateTask((taskController, dataModel, apps) ->  {
            WorkspaceItemInfo info = itemProvider.get();
                getModelWriter().updateItemInDatabase(info);
            taskController.getModelWriter().updateItemInDatabase(info);
            ArrayList<WorkspaceItemInfo> update = new ArrayList<>();
            update.add(info);
                bindUpdatedWorkspaceItems(update);
            }
            taskController.bindUpdatedWorkspaceItems(update);
        });
    }

    public void refreshAndBindWidgetsAndShortcuts(@Nullable final PackageUserKey packageUser) {
        enqueueModelUpdateTask(new BaseModelUpdateTask() {
            @Override
            public void execute(@NonNull final LauncherAppState app,
                    @NonNull final BgDataModel dataModel, @NonNull final AllAppsList apps) {
                dataModel.widgetsModel.update(app, packageUser);
                bindUpdatedWidgets(dataModel);
            }
        enqueueModelUpdateTask((taskController, dataModel, apps) ->  {
            dataModel.widgetsModel.update(taskController.getApp(), packageUser);
            taskController.bindUpdatedWidgets(dataModel);
        });
    }

+5 −13
Original line number Diff line number Diff line
@@ -22,13 +22,9 @@ import android.content.Context;
import android.os.Handler;

import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;

import com.android.launcher3.LauncherAppState;
import com.android.launcher3.allapps.BaseAllAppsAdapter.AdapterItem;
import com.android.launcher3.model.AllAppsList;
import com.android.launcher3.model.BaseModelUpdateTask;
import com.android.launcher3.model.BgDataModel;
import com.android.launcher3.model.data.AppInfo;
import com.android.launcher3.search.SearchAlgorithm;
import com.android.launcher3.search.SearchCallback;
@@ -67,16 +63,12 @@ public class DefaultAppSearchAlgorithm implements SearchAlgorithm<AdapterItem> {

    @Override
    public void doSearch(String query, SearchCallback<AdapterItem> callback) {
        mAppState.getModel().enqueueModelUpdateTask(new BaseModelUpdateTask() {
            @Override
            public void execute(@NonNull final LauncherAppState app,
                    @NonNull final BgDataModel dataModel, @NonNull final AllAppsList apps) {
        mAppState.getModel().enqueueModelUpdateTask((taskController, dataModel, apps) ->  {
            ArrayList<AdapterItem> result = getTitleMatchResult(apps.data, query);
            if (mAddNoResultsMessage && result.isEmpty()) {
                result.add(getEmptyMessageAdapterItem(query));
            }
            mResultHandler.post(() -> callback.onSearchResult(query, result));
            }
        });
    }

Loading