Sebastian Franco | bd7919c | 2023-09-19 10:55:37 -0700 | [diff] [blame] | 1 | package com.android.launcher3 |
| 2 | |
Sebastian Franco | 0b461ef | 2023-12-08 12:20:07 -0600 | [diff] [blame] | 3 | import android.annotation.TargetApi |
| 4 | import android.os.Build |
| 5 | import android.os.Trace |
Sihua Ma | a44f4ac | 2024-06-03 18:35:39 +0000 | [diff] [blame] | 6 | import android.util.Log |
Sebastian Franco | bd7919c | 2023-09-19 10:55:37 -0700 | [diff] [blame] | 7 | import androidx.annotation.UiThread |
fbaron | 9d08e0f | 2024-04-24 11:26:01 -0700 | [diff] [blame] | 8 | import com.android.launcher3.Flags.enableSmartspaceRemovalToggle |
Sebastian Franco | 0b461ef | 2023-12-08 12:20:07 -0600 | [diff] [blame] | 9 | import com.android.launcher3.LauncherConstants.TraceEvents |
fbaron | 9d08e0f | 2024-04-24 11:26:01 -0700 | [diff] [blame] | 10 | import com.android.launcher3.Utilities.SHOULD_SHOW_FIRST_PAGE_WIDGET |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 11 | import com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID |
Sebastian Franco | e496512 | 2023-12-01 13:27:42 -0600 | [diff] [blame] | 12 | import com.android.launcher3.allapps.AllAppsStore |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 13 | import com.android.launcher3.config.FeatureFlags |
Sebastian Franco | 224f67a | 2024-04-18 18:40:40 -0700 | [diff] [blame] | 14 | import com.android.launcher3.debug.TestEvent |
| 15 | import com.android.launcher3.debug.TestEventEmitter |
Sebastian Franco | bd7919c | 2023-09-19 10:55:37 -0700 | [diff] [blame] | 16 | import com.android.launcher3.model.BgDataModel |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 17 | import com.android.launcher3.model.StringCache |
Sebastian Franco | bd7919c | 2023-09-19 10:55:37 -0700 | [diff] [blame] | 18 | import com.android.launcher3.model.data.AppInfo |
| 19 | import com.android.launcher3.model.data.ItemInfo |
| 20 | import com.android.launcher3.model.data.LauncherAppWidgetInfo |
| 21 | import com.android.launcher3.model.data.WorkspaceItemInfo |
| 22 | import com.android.launcher3.popup.PopupContainerWithArrow |
| 23 | import com.android.launcher3.util.ComponentKey |
Sebastian Franco | 69fd742 | 2023-10-06 16:48:58 -0700 | [diff] [blame] | 24 | import com.android.launcher3.util.IntArray as LIntArray |
| 25 | import com.android.launcher3.util.IntSet as LIntSet |
Sebastian Franco | bd7919c | 2023-09-19 10:55:37 -0700 | [diff] [blame] | 26 | import com.android.launcher3.util.PackageUserKey |
| 27 | import com.android.launcher3.util.Preconditions |
Sebastian Franco | 0b461ef | 2023-12-08 12:20:07 -0600 | [diff] [blame] | 28 | import com.android.launcher3.util.RunnableList |
Sebastian Franco | e496512 | 2023-12-01 13:27:42 -0600 | [diff] [blame] | 29 | import com.android.launcher3.util.TraceHelper |
| 30 | import com.android.launcher3.util.ViewOnDrawExecutor |
Sebastian Franco | 50b74c3 | 2023-11-16 11:36:49 -0600 | [diff] [blame] | 31 | import com.android.launcher3.widget.PendingAddWidgetInfo |
Sebastian Franco | bd7919c | 2023-09-19 10:55:37 -0700 | [diff] [blame] | 32 | import com.android.launcher3.widget.model.WidgetsListBaseEntry |
| 33 | import java.util.function.Predicate |
| 34 | |
Sihua Ma | a44f4ac | 2024-06-03 18:35:39 +0000 | [diff] [blame] | 35 | private const val TAG = "ModelCallbacks" |
| 36 | |
Sebastian Franco | bd7919c | 2023-09-19 10:55:37 -0700 | [diff] [blame] | 37 | class ModelCallbacks(private var launcher: Launcher) : BgDataModel.Callbacks { |
Sebastian Franco | 69fd742 | 2023-10-06 16:48:58 -0700 | [diff] [blame] | 38 | |
| 39 | var synchronouslyBoundPages = LIntSet() |
| 40 | var pagesToBindSynchronously = LIntSet() |
| 41 | |
Sebastian Franco | e496512 | 2023-12-01 13:27:42 -0600 | [diff] [blame] | 42 | private var isFirstPagePinnedItemEnabled = |
fbaron | 9d08e0f | 2024-04-24 11:26:01 -0700 | [diff] [blame] | 43 | (BuildConfig.QSB_ON_FIRST_SCREEN && !enableSmartspaceRemovalToggle()) |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 44 | |
| 45 | var stringCache: StringCache? = null |
| 46 | |
Sebastian Franco | e496512 | 2023-12-01 13:27:42 -0600 | [diff] [blame] | 47 | var pendingExecutor: ViewOnDrawExecutor? = null |
| 48 | |
| 49 | var workspaceLoading = true |
| 50 | |
| 51 | /** |
| 52 | * Refreshes the shortcuts shown on the workspace. |
| 53 | * |
| 54 | * Implementation of the method from LauncherModel.Callbacks. |
| 55 | */ |
| 56 | override fun startBinding() { |
| 57 | TraceHelper.INSTANCE.beginSection("startBinding") |
| 58 | // Floating panels (except the full widget sheet) are associated with individual icons. If |
| 59 | // we are starting a fresh bind, close all such panels as all the icons are about |
| 60 | // to go away. |
| 61 | AbstractFloatingView.closeOpenViews( |
| 62 | launcher, |
| 63 | true, |
| 64 | AbstractFloatingView.TYPE_ALL and AbstractFloatingView.TYPE_REBIND_SAFE.inv() |
| 65 | ) |
| 66 | workspaceLoading = true |
| 67 | |
| 68 | // Clear the workspace because it's going to be rebound |
| 69 | launcher.dragController.cancelDrag() |
| 70 | launcher.workspace.clearDropTargets() |
| 71 | launcher.workspace.removeAllWorkspaceScreens() |
Sihua Ma | f418b2e | 2024-03-13 12:17:27 -0700 | [diff] [blame] | 72 | // Avoid clearing the widget update listeners for staying up-to-date with widget info |
| 73 | launcher.appWidgetHolder.clearWidgetViews() |
Sihua Ma | a44f4ac | 2024-06-03 18:35:39 +0000 | [diff] [blame] | 74 | // TODO(b/335141365): Remove this log after the bug is fixed. |
| 75 | Log.d( |
| 76 | TAG, |
| 77 | "startBinding: " + |
| 78 | "hotseat layout was vertical: ${launcher.hotseat?.isHasVerticalHotseat}" + |
| 79 | " and is setting to ${launcher.deviceProfile.isVerticalBarLayout}" |
| 80 | ) |
Sebastian Franco | e496512 | 2023-12-01 13:27:42 -0600 | [diff] [blame] | 81 | launcher.hotseat?.resetLayout(launcher.deviceProfile.isVerticalBarLayout) |
| 82 | TraceHelper.INSTANCE.endSection() |
| 83 | } |
| 84 | |
Sebastian Franco | 0b461ef | 2023-12-08 12:20:07 -0600 | [diff] [blame] | 85 | @TargetApi(Build.VERSION_CODES.S) |
| 86 | override fun onInitialBindComplete( |
| 87 | boundPages: LIntSet, |
| 88 | pendingTasks: RunnableList, |
Sunny Goyal | 72a7466 | 2024-02-14 12:32:59 -0800 | [diff] [blame] | 89 | onCompleteSignal: RunnableList, |
Sebastian Franco | 0b461ef | 2023-12-08 12:20:07 -0600 | [diff] [blame] | 90 | workspaceItemCount: Int, |
| 91 | isBindSync: Boolean |
| 92 | ) { |
Sunny Goyal | e337a80 | 2024-02-14 15:07:26 -0800 | [diff] [blame] | 93 | if (Utilities.ATLEAST_S) { |
| 94 | Trace.endAsyncSection( |
| 95 | TraceEvents.DISPLAY_WORKSPACE_TRACE_METHOD_NAME, |
| 96 | TraceEvents.DISPLAY_WORKSPACE_TRACE_COOKIE |
| 97 | ) |
| 98 | } |
Sebastian Franco | 0b461ef | 2023-12-08 12:20:07 -0600 | [diff] [blame] | 99 | synchronouslyBoundPages = boundPages |
| 100 | pagesToBindSynchronously = LIntSet() |
| 101 | clearPendingBinds() |
Sebastian Franco | 0b461ef | 2023-12-08 12:20:07 -0600 | [diff] [blame] | 102 | if (!launcher.isInState(LauncherState.ALL_APPS)) { |
| 103 | launcher.appsView.appsStore.enableDeferUpdates(AllAppsStore.DEFER_UPDATES_NEXT_DRAW) |
| 104 | pendingTasks.add { |
| 105 | launcher.appsView.appsStore.disableDeferUpdates( |
| 106 | AllAppsStore.DEFER_UPDATES_NEXT_DRAW |
| 107 | ) |
| 108 | } |
| 109 | } |
Sunny Goyal | e337a80 | 2024-02-14 15:07:26 -0800 | [diff] [blame] | 110 | val executor = |
| 111 | ViewOnDrawExecutor(pendingTasks) { |
| 112 | if (pendingExecutor == it) { |
| 113 | pendingExecutor = null |
Sebastian Franco | 0b461ef | 2023-12-08 12:20:07 -0600 | [diff] [blame] | 114 | } |
| 115 | } |
Sunny Goyal | e337a80 | 2024-02-14 15:07:26 -0800 | [diff] [blame] | 116 | pendingExecutor = executor |
Sunny Goyal | 72a7466 | 2024-02-14 12:32:59 -0800 | [diff] [blame] | 117 | |
| 118 | if (Flags.enableWorkspaceInflation()) { |
| 119 | // Finish the executor as soon as the pending inflation is completed |
| 120 | onCompleteSignal.add(executor::markCompleted) |
| 121 | } else { |
| 122 | // Pending executor is already completed, wait until first draw to run the tasks |
| 123 | executor.attachTo(launcher) |
| 124 | } |
Sunny Goyal | e337a80 | 2024-02-14 15:07:26 -0800 | [diff] [blame] | 125 | launcher.bindComplete(workspaceItemCount, isBindSync) |
Sebastian Franco | 0b461ef | 2023-12-08 12:20:07 -0600 | [diff] [blame] | 126 | } |
| 127 | |
Sebastian Franco | e496512 | 2023-12-01 13:27:42 -0600 | [diff] [blame] | 128 | /** |
Sebastian Franco | 6196b90 | 2023-12-06 11:39:52 -0600 | [diff] [blame] | 129 | * Callback saying that there aren't any more items to bind. |
| 130 | * |
| 131 | * Implementation of the method from LauncherModel.Callbacks. |
| 132 | */ |
| 133 | override fun finishBindingItems(pagesBoundFirst: LIntSet?) { |
| 134 | TraceHelper.INSTANCE.beginSection("finishBindingItems") |
| 135 | val deviceProfile = launcher.deviceProfile |
| 136 | launcher.workspace.restoreInstanceStateForRemainingPages() |
| 137 | workspaceLoading = false |
| 138 | launcher.processActivityResult() |
| 139 | val currentPage = |
| 140 | if (pagesBoundFirst != null && !pagesBoundFirst.isEmpty) |
| 141 | launcher.workspace.getPageIndexForScreenId(pagesBoundFirst.array[0]) |
| 142 | else PagedView.INVALID_PAGE |
| 143 | // When undoing the removal of the last item on a page, return to that page. |
| 144 | // Since we are just resetting the current page without user interaction, |
| 145 | // override the previous page so we don't log the page switch. |
| 146 | launcher.workspace.setCurrentPage(currentPage, currentPage /* overridePrevPage */) |
Sebastian Franco | 0b461ef | 2023-12-08 12:20:07 -0600 | [diff] [blame] | 147 | pagesToBindSynchronously = LIntSet() |
Sebastian Franco | 6196b90 | 2023-12-06 11:39:52 -0600 | [diff] [blame] | 148 | |
| 149 | // Cache one page worth of icons |
| 150 | launcher.viewCache.setCacheSize( |
| 151 | R.layout.folder_application, |
Thales Lima | 1faa4ed | 2023-12-01 16:48:19 +0000 | [diff] [blame] | 152 | deviceProfile.numFolderColumns * deviceProfile.numFolderRows |
Sebastian Franco | 6196b90 | 2023-12-06 11:39:52 -0600 | [diff] [blame] | 153 | ) |
| 154 | launcher.viewCache.setCacheSize(R.layout.folder_page, 2) |
| 155 | TraceHelper.INSTANCE.endSection() |
| 156 | launcher.workspace.removeExtraEmptyScreen(/* stripEmptyScreens= */ true) |
Sihua Ma | a44f4ac | 2024-06-03 18:35:39 +0000 | [diff] [blame] | 157 | launcher.workspace.pageIndicator.setPauseScroll( |
| 158 | /*pause=*/ false, |
| 159 | deviceProfile.isTwoPanels |
| 160 | ) |
Sebastian Franco | 224f67a | 2024-04-18 18:40:40 -0700 | [diff] [blame] | 161 | TestEventEmitter.INSTANCE.get(launcher).sendEvent(TestEvent.WORKSPACE_FINISH_LOADING) |
Sebastian Franco | 6196b90 | 2023-12-06 11:39:52 -0600 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
Sebastian Franco | e496512 | 2023-12-01 13:27:42 -0600 | [diff] [blame] | 165 | * Clear any pending bind callbacks. This is called when is loader is planning to perform a full |
| 166 | * rebind from scratch. |
| 167 | */ |
| 168 | override fun clearPendingBinds() { |
| 169 | pendingExecutor?.cancel() ?: return |
| 170 | pendingExecutor = null |
| 171 | |
| 172 | // We might have set this flag previously and forgot to clear it. |
| 173 | launcher.appsView.appsStore.disableDeferUpdatesSilently( |
| 174 | AllAppsStore.DEFER_UPDATES_NEXT_DRAW |
| 175 | ) |
| 176 | } |
| 177 | |
Sebastian Franco | bd7919c | 2023-09-19 10:55:37 -0700 | [diff] [blame] | 178 | override fun preAddApps() { |
| 179 | // If there's an undo snackbar, force it to complete to ensure empty screens are removed |
| 180 | // before trying to add new items. |
| 181 | launcher.modelWriter.commitDelete() |
| 182 | val snackbar = |
| 183 | AbstractFloatingView.getOpenView<AbstractFloatingView>( |
| 184 | launcher, |
| 185 | AbstractFloatingView.TYPE_SNACKBAR |
| 186 | ) |
| 187 | snackbar?.post { snackbar.close(true) } |
| 188 | } |
| 189 | |
| 190 | @UiThread |
| 191 | override fun bindAllApplications( |
| 192 | apps: Array<AppInfo?>?, |
| 193 | flags: Int, |
| 194 | packageUserKeytoUidMap: Map<PackageUserKey?, Int?>? |
| 195 | ) { |
| 196 | Preconditions.assertUIThread() |
| 197 | val hadWorkApps = launcher.appsView.shouldShowTabs() |
| 198 | launcher.appsView.appsStore.setApps(apps, flags, packageUserKeytoUidMap) |
| 199 | PopupContainerWithArrow.dismissInvalidPopup(launcher) |
Alex Chau | 61d1938 | 2024-02-12 17:36:57 +0000 | [diff] [blame] | 200 | if ( |
| 201 | hadWorkApps != launcher.appsView.shouldShowTabs() && |
| 202 | launcher.stateManager.state == LauncherState.ALL_APPS |
| 203 | ) { |
Sebastian Franco | bd7919c | 2023-09-19 10:55:37 -0700 | [diff] [blame] | 204 | launcher.stateManager.goToState(LauncherState.NORMAL) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Copies LauncherModel's map of activities to shortcut counts to Launcher's. This is necessary |
| 210 | * because LauncherModel's map is updated in the background, while Launcher runs on the UI. |
| 211 | */ |
| 212 | override fun bindDeepShortcutMap(deepShortcutMapCopy: HashMap<ComponentKey?, Int?>?) { |
| 213 | launcher.popupDataProvider.setDeepShortcutMap(deepShortcutMapCopy) |
| 214 | } |
| 215 | |
| 216 | override fun bindIncrementalDownloadProgressUpdated(app: AppInfo?) { |
| 217 | launcher.appsView.appsStore.updateProgressBar(app) |
| 218 | } |
| 219 | |
| 220 | override fun bindWidgetsRestored(widgets: ArrayList<LauncherAppWidgetInfo?>?) { |
| 221 | launcher.workspace.widgetsRestored(widgets) |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Some shortcuts were updated in the background. Implementation of the method from |
| 226 | * LauncherModel.Callbacks. |
| 227 | * |
| 228 | * @param updated list of shortcuts which have changed. |
| 229 | */ |
| 230 | override fun bindWorkspaceItemsChanged(updated: List<WorkspaceItemInfo?>) { |
| 231 | if (updated.isNotEmpty()) { |
| 232 | launcher.workspace.updateWorkspaceItems(updated, launcher) |
| 233 | PopupContainerWithArrow.dismissInvalidPopup(launcher) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Update the state of a package, typically related to install state. Implementation of the |
| 239 | * method from LauncherModel.Callbacks. |
| 240 | */ |
| 241 | override fun bindRestoreItemsChange(updates: HashSet<ItemInfo?>?) { |
| 242 | launcher.workspace.updateRestoreItems(updates, launcher) |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * A package was uninstalled/updated. We take both the super set of packageNames in addition to |
| 247 | * specific applications to remove, the reason being that this can be called when a package is |
| 248 | * updated as well. In that scenario, we only remove specific components from the workspace and |
| 249 | * hotseat, where as package-removal should clear all items by package name. |
| 250 | */ |
| 251 | override fun bindWorkspaceComponentsRemoved(matcher: Predicate<ItemInfo?>?) { |
| 252 | launcher.workspace.removeItemsByMatcher(matcher) |
| 253 | launcher.dragController.onAppsRemoved(matcher) |
| 254 | PopupContainerWithArrow.dismissInvalidPopup(launcher) |
| 255 | } |
| 256 | |
Shamali P | 6661404 | 2024-07-19 21:29:39 +0000 | [diff] [blame^] | 257 | override fun bindAllWidgets(allWidgets: List<WidgetsListBaseEntry>) { |
| 258 | launcher.widgetPickerDataProvider.setWidgets(allWidgets, /* defaultWidgets= */ listOf()) |
Sebastian Franco | bd7919c | 2023-09-19 10:55:37 -0700 | [diff] [blame] | 259 | } |
Sebastian Franco | 69fd742 | 2023-10-06 16:48:58 -0700 | [diff] [blame] | 260 | |
| 261 | /** Returns the ids of the workspaces to bind. */ |
| 262 | override fun getPagesToBindSynchronously(orderedScreenIds: LIntArray): LIntSet { |
| 263 | // If workspace binding is still in progress, getCurrentPageScreenIds won't be |
| 264 | // accurate, and we should use mSynchronouslyBoundPages that's set during initial binding. |
| 265 | val visibleIds = |
| 266 | when { |
| 267 | !pagesToBindSynchronously.isEmpty -> pagesToBindSynchronously |
Sebastian Franco | e496512 | 2023-12-01 13:27:42 -0600 | [diff] [blame] | 268 | !workspaceLoading -> launcher.workspace.currentPageScreenIds |
Sebastian Franco | 69fd742 | 2023-10-06 16:48:58 -0700 | [diff] [blame] | 269 | else -> synchronouslyBoundPages |
| 270 | } |
| 271 | // Launcher IntArray has the same name as Kotlin IntArray |
| 272 | val result = LIntSet() |
| 273 | if (visibleIds.isEmpty) { |
| 274 | return result |
| 275 | } |
| 276 | val actualIds = orderedScreenIds.clone() |
| 277 | val firstId = visibleIds.first() |
| 278 | val pairId = launcher.workspace.getScreenPair(firstId) |
| 279 | // Double check that actual screenIds contains the visibleId, as empty screens are hidden |
| 280 | // in single panel. |
| 281 | if (actualIds.contains(firstId)) { |
| 282 | result.add(firstId) |
| 283 | if (launcher.deviceProfile.isTwoPanels && actualIds.contains(pairId)) { |
| 284 | result.add(pairId) |
| 285 | } |
| 286 | } else if ( |
| 287 | LauncherAppState.getIDP(launcher).supportedProfiles.any(DeviceProfile::isTwoPanels) && |
| 288 | actualIds.contains(pairId) |
| 289 | ) { |
| 290 | // Add the right panel if left panel is hidden when switching display, due to empty |
| 291 | // pages being hidden in single panel. |
| 292 | result.add(pairId) |
| 293 | } |
| 294 | return result |
| 295 | } |
Sebastian Franco | 50b74c3 | 2023-11-16 11:36:49 -0600 | [diff] [blame] | 296 | |
| 297 | override fun bindSmartspaceWidget() { |
| 298 | val cl: CellLayout? = |
| 299 | launcher.workspace.getScreenWithId(WorkspaceLayoutManager.FIRST_SCREEN_ID) |
| 300 | val spanX = InvariantDeviceProfile.INSTANCE.get(launcher).numSearchContainerColumns |
| 301 | |
| 302 | if (cl?.isRegionVacant(0, 0, spanX, 1) != true) { |
| 303 | return |
| 304 | } |
| 305 | |
| 306 | val widgetsListBaseEntry: WidgetsListBaseEntry = |
Shamali P | 6661404 | 2024-07-19 21:29:39 +0000 | [diff] [blame^] | 307 | launcher.widgetPickerDataProvider.get().allWidgets.firstOrNull { |
| 308 | item: WidgetsListBaseEntry -> |
Sebastian Franco | 50b74c3 | 2023-11-16 11:36:49 -0600 | [diff] [blame] | 309 | item.mPkgItem.packageName == BuildConfig.APPLICATION_ID |
Sihua Ma | a44f4ac | 2024-06-03 18:35:39 +0000 | [diff] [blame] | 310 | } ?: return |
Sebastian Franco | 50b74c3 | 2023-11-16 11:36:49 -0600 | [diff] [blame] | 311 | |
| 312 | val info = |
| 313 | PendingAddWidgetInfo( |
| 314 | widgetsListBaseEntry.mWidgets[0].widgetInfo, |
| 315 | LauncherSettings.Favorites.CONTAINER_DESKTOP |
| 316 | ) |
| 317 | launcher.addPendingItem( |
| 318 | info, |
| 319 | info.container, |
| 320 | WorkspaceLayoutManager.FIRST_SCREEN_ID, |
| 321 | intArrayOf(0, 0), |
| 322 | info.spanX, |
| 323 | info.spanY |
| 324 | ) |
| 325 | } |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 326 | |
Sebastian Franco | 6196b90 | 2023-12-06 11:39:52 -0600 | [diff] [blame] | 327 | override fun bindScreens(orderedScreenIds: LIntArray) { |
Shamali P | ef4b102 | 2024-02-19 18:05:27 +0000 | [diff] [blame] | 328 | launcher.workspace.pageIndicator.setPauseScroll( |
| 329 | /*pause=*/ true, |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 330 | launcher.deviceProfile.isTwoPanels |
| 331 | ) |
| 332 | val firstScreenPosition = 0 |
| 333 | if ( |
Sihua Ma | a44f4ac | 2024-06-03 18:35:39 +0000 | [diff] [blame] | 334 | (isFirstPagePinnedItemEnabled && !SHOULD_SHOW_FIRST_PAGE_WIDGET) && |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 335 | orderedScreenIds.indexOf(FIRST_SCREEN_ID) != firstScreenPosition |
| 336 | ) { |
| 337 | orderedScreenIds.removeValue(FIRST_SCREEN_ID) |
| 338 | orderedScreenIds.add(firstScreenPosition, FIRST_SCREEN_ID) |
| 339 | } else if ( |
Sihua Ma | a44f4ac | 2024-06-03 18:35:39 +0000 | [diff] [blame] | 340 | (!isFirstPagePinnedItemEnabled || SHOULD_SHOW_FIRST_PAGE_WIDGET) && |
| 341 | orderedScreenIds.isEmpty |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 342 | ) { |
| 343 | // If there are no screens, we need to have an empty screen |
| 344 | launcher.workspace.addExtraEmptyScreens() |
| 345 | } |
| 346 | bindAddScreens(orderedScreenIds) |
| 347 | |
| 348 | // After we have added all the screens, if the wallpaper was locked to the default state, |
| 349 | // then notify to indicate that it can be released and a proper wallpaper offset can be |
| 350 | // computed before the next layout |
| 351 | launcher.workspace.unlockWallpaperFromDefaultPageOnNextLayout() |
| 352 | } |
| 353 | |
| 354 | override fun bindAppsAdded( |
Sebastian Franco | 6196b90 | 2023-12-06 11:39:52 -0600 | [diff] [blame] | 355 | newScreens: LIntArray?, |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 356 | addNotAnimated: java.util.ArrayList<ItemInfo?>?, |
| 357 | addAnimated: java.util.ArrayList<ItemInfo?>? |
| 358 | ) { |
| 359 | // Add the new screens |
| 360 | if (newScreens != null) { |
| 361 | // newScreens can contain an empty right panel that is already bound, but not known |
| 362 | // by BgDataModel. |
| 363 | newScreens.removeAllValues(launcher.workspace.mScreenOrder) |
| 364 | bindAddScreens(newScreens) |
| 365 | } |
| 366 | |
| 367 | // We add the items without animation on non-visible pages, and with |
| 368 | // animations on the new page (which we will try and snap to). |
| 369 | if (!addNotAnimated.isNullOrEmpty()) { |
| 370 | launcher.bindItems(addNotAnimated, false) |
| 371 | } |
| 372 | if (!addAnimated.isNullOrEmpty()) { |
| 373 | launcher.bindItems(addAnimated, true) |
| 374 | } |
| 375 | |
| 376 | // Remove the extra empty screen |
| 377 | launcher.workspace.removeExtraEmptyScreen(false) |
| 378 | } |
| 379 | |
Sebastian Franco | 6196b90 | 2023-12-06 11:39:52 -0600 | [diff] [blame] | 380 | private fun bindAddScreens(orderedScreenIdsArg: LIntArray) { |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 381 | var orderedScreenIds = orderedScreenIdsArg |
| 382 | if (launcher.deviceProfile.isTwoPanels) { |
| 383 | if (FeatureFlags.FOLDABLE_SINGLE_PAGE.get()) { |
| 384 | orderedScreenIds = filterTwoPanelScreenIds(orderedScreenIds) |
| 385 | } else { |
| 386 | // Some empty pages might have been removed while the phone was in a single panel |
| 387 | // mode, so we want to add those empty pages back. |
Sebastian Franco | 0b461ef | 2023-12-08 12:20:07 -0600 | [diff] [blame] | 388 | val screenIds = LIntSet.wrap(orderedScreenIds) |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 389 | orderedScreenIds.forEach { screenId: Int -> |
| 390 | screenIds.add(launcher.workspace.getScreenPair(screenId)) |
| 391 | } |
| 392 | orderedScreenIds = screenIds.array |
| 393 | } |
| 394 | } |
| 395 | orderedScreenIds |
| 396 | .filterNot { screenId -> |
Sihua Ma | a44f4ac | 2024-06-03 18:35:39 +0000 | [diff] [blame] | 397 | isFirstPagePinnedItemEnabled && |
fbaron | 9d08e0f | 2024-04-24 11:26:01 -0700 | [diff] [blame] | 398 | !SHOULD_SHOW_FIRST_PAGE_WIDGET && |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 399 | screenId == WorkspaceLayoutManager.FIRST_SCREEN_ID |
| 400 | } |
| 401 | .forEach { screenId -> |
| 402 | launcher.workspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId) |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Remove odd number because they are already included when isTwoPanels and add the pair screen |
| 408 | * if not present. |
| 409 | */ |
Sebastian Franco | 6196b90 | 2023-12-06 11:39:52 -0600 | [diff] [blame] | 410 | private fun filterTwoPanelScreenIds(orderedScreenIds: LIntArray): LIntArray { |
Sebastian Franco | 0b461ef | 2023-12-08 12:20:07 -0600 | [diff] [blame] | 411 | val screenIds = LIntSet.wrap(orderedScreenIds) |
Sebastian Franco | 6dda7c7 | 2023-11-17 18:03:00 -0600 | [diff] [blame] | 412 | orderedScreenIds |
| 413 | .filter { screenId -> screenId % 2 == 1 } |
| 414 | .forEach { screenId -> |
| 415 | screenIds.remove(screenId) |
| 416 | // In case the pair is not added, add it |
| 417 | if (!launcher.workspace.containsScreenId(screenId - 1)) { |
| 418 | screenIds.add(screenId - 1) |
| 419 | } |
| 420 | } |
| 421 | return screenIds.array |
| 422 | } |
| 423 | |
| 424 | override fun setIsFirstPagePinnedItemEnabled(isFirstPagePinnedItemEnabled: Boolean) { |
| 425 | this.isFirstPagePinnedItemEnabled = isFirstPagePinnedItemEnabled |
| 426 | launcher.workspace.bindAndInitFirstWorkspaceScreen() |
| 427 | } |
| 428 | |
| 429 | override fun bindStringCache(cache: StringCache) { |
| 430 | stringCache = cache |
| 431 | launcher.appsView.updateWorkUI() |
| 432 | } |
| 433 | |
| 434 | fun getIsFirstPagePinnedItemEnabled(): Boolean = isFirstPagePinnedItemEnabled |
Sunny Goyal | 72a7466 | 2024-02-14 12:32:59 -0800 | [diff] [blame] | 435 | |
| 436 | override fun getItemInflater() = launcher.itemInflater |
Sebastian Franco | bd7919c | 2023-09-19 10:55:37 -0700 | [diff] [blame] | 437 | } |