Files
Lawnchair/src/com/android/launcher3/model/ModelTaskController.kt
T
Sunny Goyal 36c3112abf Unifying various model update callbacks into one
Making the BubbleTextView.applyItem stateless so that it does not depend on the order of events

Bug: 390572144
Flag: EXEMPT refactor
Test: atest LauncherBindableItemsContainerTest
      atest BubbleTextViewTest
Change-Id: Ib9c0ac6c330d6f4e08c3db5772d35787fa056b65
2025-01-28 16:03:01 -08:00

115 lines
4.3 KiB
Kotlin

/*
* Copyright (C) 2024 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.launcher3.model
import com.android.launcher3.LauncherAppState
import com.android.launcher3.LauncherModel
import com.android.launcher3.LauncherModel.CallbackTask
import com.android.launcher3.celllayout.CellPosMapper
import com.android.launcher3.model.BgDataModel.FixedContainerItems
import com.android.launcher3.model.data.ItemInfo
import com.android.launcher3.util.PackageUserKey
import com.android.launcher3.widget.model.WidgetsListBaseEntriesBuilder
import java.util.Objects
import java.util.concurrent.Executor
import java.util.function.Predicate
/** Class with utility methods and properties for running a LauncherModel Task */
class ModelTaskController(
val app: LauncherAppState,
val dataModel: BgDataModel,
val allAppsList: AllAppsList,
private val model: LauncherModel,
private val uiExecutor: Executor,
) {
/** Schedules a {@param task} to be executed on the current callbacks. */
fun scheduleCallbackTask(task: CallbackTask) {
for (cb in model.callbacks) {
uiExecutor.execute { task.execute(cb) }
}
}
/**
* Updates from model task, do not deal with icon position in hotseat. Also no need to verify
* changes as the ModelTasks always push the changes to callbacks
*/
fun getModelWriter() = model.getWriter(false /* verifyChanges */, CellPosMapper.DEFAULT, null)
fun bindUpdatedWorkspaceItems(allUpdates: Collection<ItemInfo>) {
// Bind workspace items
val workspaceUpdates = allUpdates.filter { it.id != ItemInfo.NO_ID }.toSet()
if (workspaceUpdates.isNotEmpty()) {
scheduleCallbackTask { it.bindItemsUpdated(workspaceUpdates) }
}
// Bind extra items if any
allUpdates
.stream()
.mapToInt { it.container }
.distinct()
.mapToObj { dataModel.extraItems.get(it) }
.filter { Objects.nonNull(it) }
.forEach { bindExtraContainerItems(it) }
}
fun bindExtraContainerItems(item: FixedContainerItems) {
scheduleCallbackTask { it.bindExtraContainerItems(item) }
}
fun bindDeepShortcuts(dataModel: BgDataModel) {
val shortcutMapCopy = HashMap(dataModel.deepShortcutMap)
scheduleCallbackTask { it.bindDeepShortcutMap(shortcutMapCopy) }
}
fun bindUpdatedWidgets(dataModel: BgDataModel) {
val widgetsByPackageItem = dataModel.widgetsModel.widgetsByPackageItem
val allWidgets = WidgetsListBaseEntriesBuilder(app.context).build(widgetsByPackageItem)
val defaultWidgetsFilter = dataModel.widgetsModel.defaultWidgetsFilter
val defaultWidgets =
if (defaultWidgetsFilter != null) {
WidgetsListBaseEntriesBuilder(app.context)
.build(widgetsByPackageItem, defaultWidgetsFilter)
} else {
emptyList()
}
scheduleCallbackTask { it.bindAllWidgets(allWidgets, defaultWidgets) }
}
fun deleteAndBindComponentsRemoved(matcher: Predicate<ItemInfo?>, reason: String?) {
getModelWriter().deleteItemsFromDatabase(matcher, reason)
// Call the components-removed callback
scheduleCallbackTask { it.bindWorkspaceComponentsRemoved(matcher) }
}
fun bindApplicationsIfNeeded() {
if (allAppsList.getAndResetChangeFlag()) {
val apps = allAppsList.copyData()
val flags = allAppsList.flags
val packageUserKeyToUidMap =
apps.associateBy(
keySelector = { PackageUserKey(it.componentName!!.packageName, it.user) },
valueTransform = { it.uid },
)
scheduleCallbackTask { it.bindAllApplications(apps, flags, packageUserKeyToUidMap) }
}
}
}