blob: 679f7d401be74ab2211d5b4860f496d39040d33e [file] [log] [blame]
Sebastian Francobd7919c2023-09-19 10:55:37 -07001package com.android.launcher3
2
3import androidx.annotation.UiThread
4import com.android.launcher3.model.BgDataModel
5import com.android.launcher3.model.data.AppInfo
6import com.android.launcher3.model.data.ItemInfo
7import com.android.launcher3.model.data.LauncherAppWidgetInfo
8import com.android.launcher3.model.data.WorkspaceItemInfo
9import com.android.launcher3.popup.PopupContainerWithArrow
10import com.android.launcher3.util.ComponentKey
11import com.android.launcher3.util.PackageUserKey
12import com.android.launcher3.util.Preconditions
13import com.android.launcher3.widget.model.WidgetsListBaseEntry
14import java.util.function.Predicate
15
16class ModelCallbacks(private var launcher: Launcher) : BgDataModel.Callbacks {
17 override fun preAddApps() {
18 // If there's an undo snackbar, force it to complete to ensure empty screens are removed
19 // before trying to add new items.
20 launcher.modelWriter.commitDelete()
21 val snackbar =
22 AbstractFloatingView.getOpenView<AbstractFloatingView>(
23 launcher,
24 AbstractFloatingView.TYPE_SNACKBAR
25 )
26 snackbar?.post { snackbar.close(true) }
27 }
28
29 @UiThread
30 override fun bindAllApplications(
31 apps: Array<AppInfo?>?,
32 flags: Int,
33 packageUserKeytoUidMap: Map<PackageUserKey?, Int?>?
34 ) {
35 Preconditions.assertUIThread()
36 val hadWorkApps = launcher.appsView.shouldShowTabs()
37 launcher.appsView.appsStore.setApps(apps, flags, packageUserKeytoUidMap)
38 PopupContainerWithArrow.dismissInvalidPopup(launcher)
39 if (hadWorkApps != launcher.appsView.shouldShowTabs()) {
40 launcher.stateManager.goToState(LauncherState.NORMAL)
41 }
42 }
43
44 /**
45 * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary
46 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
47 */
48 override fun bindDeepShortcutMap(deepShortcutMapCopy: HashMap<ComponentKey?, Int?>?) {
49 launcher.popupDataProvider.setDeepShortcutMap(deepShortcutMapCopy)
50 }
51
52 override fun bindIncrementalDownloadProgressUpdated(app: AppInfo?) {
53 launcher.appsView.appsStore.updateProgressBar(app)
54 }
55
56 override fun bindWidgetsRestored(widgets: ArrayList<LauncherAppWidgetInfo?>?) {
57 launcher.workspace.widgetsRestored(widgets)
58 }
59
60 /**
61 * Some shortcuts were updated in the background. Implementation of the method from
62 * LauncherModel.Callbacks.
63 *
64 * @param updated list of shortcuts which have changed.
65 */
66 override fun bindWorkspaceItemsChanged(updated: List<WorkspaceItemInfo?>) {
67 if (updated.isNotEmpty()) {
68 launcher.workspace.updateWorkspaceItems(updated, launcher)
69 PopupContainerWithArrow.dismissInvalidPopup(launcher)
70 }
71 }
72
73 /**
74 * Update the state of a package, typically related to install state. Implementation of the
75 * method from LauncherModel.Callbacks.
76 */
77 override fun bindRestoreItemsChange(updates: HashSet<ItemInfo?>?) {
78 launcher.workspace.updateRestoreItems(updates, launcher)
79 }
80
81 /**
82 * A package was uninstalled/updated. We take both the super set of packageNames in addition to
83 * specific applications to remove, the reason being that this can be called when a package is
84 * updated as well. In that scenario, we only remove specific components from the workspace and
85 * hotseat, where as package-removal should clear all items by package name.
86 */
87 override fun bindWorkspaceComponentsRemoved(matcher: Predicate<ItemInfo?>?) {
88 launcher.workspace.removeItemsByMatcher(matcher)
89 launcher.dragController.onAppsRemoved(matcher)
90 PopupContainerWithArrow.dismissInvalidPopup(launcher)
91 }
92
93 override fun bindAllWidgets(allWidgets: List<WidgetsListBaseEntry?>?) {
94 launcher.popupDataProvider.allWidgets = allWidgets
95 }
96}