<lambda>null1 package com.android.launcher3.util
2 
3 import android.content.ContentValues
4 import com.android.launcher3.Flags
5 import com.android.launcher3.LauncherModel
6 import com.android.launcher3.LauncherSettings.Favorites
7 import com.android.launcher3.LauncherSettings.Favorites.APPWIDGET_ID
8 import com.android.launcher3.LauncherSettings.Favorites.APPWIDGET_PROVIDER
9 import com.android.launcher3.LauncherSettings.Favorites.APPWIDGET_SOURCE
10 import com.android.launcher3.LauncherSettings.Favorites.CELLX
11 import com.android.launcher3.LauncherSettings.Favorites.CELLY
12 import com.android.launcher3.LauncherSettings.Favorites.CONTAINER
13 import com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP
14 import com.android.launcher3.LauncherSettings.Favorites.INTENT
15 import com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE
16 import com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
17 import com.android.launcher3.LauncherSettings.Favorites.PROFILE_ID
18 import com.android.launcher3.LauncherSettings.Favorites.RESTORED
19 import com.android.launcher3.LauncherSettings.Favorites.SCREEN
20 import com.android.launcher3.LauncherSettings.Favorites.SPANX
21 import com.android.launcher3.LauncherSettings.Favorites.SPANY
22 import com.android.launcher3.LauncherSettings.Favorites.TITLE
23 import com.android.launcher3.LauncherSettings.Favorites._ID
24 import com.android.launcher3.model.BgDataModel
25 import com.android.launcher3.model.ModelDbController
26 
27 object ModelTestExtensions {
28     /** Clears and reloads Launcher db to cleanup the workspace */
29     fun LauncherModel.clearModelDb() {
30         // Load the model once so that there is no pending migration:
31         loadModelSync()
32         TestUtil.runOnExecutorSync(Executors.MODEL_EXECUTOR) {
33             modelDbController.run {
34                 if (Flags.gridMigrationRefactor()) attemptMigrateDb(null /* restoreEventLogger */)
35                 else tryMigrateDB(null /* restoreEventLogger */)
36                 createEmptyDB()
37                 clearEmptyDbFlag()
38             }
39         }
40         // Reload model
41         TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) { forceReload() }
42         loadModelSync()
43     }
44 
45     /** Loads the model in memory synchronously */
46     fun LauncherModel.loadModelSync() {
47         val mockCb: BgDataModel.Callbacks = object : BgDataModel.Callbacks {}
48         TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) { addCallbacksAndLoad(mockCb) }
49         TestUtil.runOnExecutorSync(Executors.MODEL_EXECUTOR) {}
50         TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) {}
51         TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) { removeCallbacks(mockCb) }
52     }
53 
54     /** Adds and commits a new item to Launcher.db */
55     fun LauncherModel.addItem(
56         title: String = "LauncherTestApp",
57         intent: String =
58             "#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;component=com.google.android.apps.nexuslauncher.tests/com.android.launcher3.testcomponent.BaseTestingActivity;launchFlags=0x10200000;end",
59         type: Int = ITEM_TYPE_APPLICATION,
60         restoreFlags: Int = 0,
61         screen: Int = 0,
62         container: Int = CONTAINER_DESKTOP,
63         x: Int,
64         y: Int,
65         spanX: Int = 1,
66         spanY: Int = 1,
67         id: Int = 0,
68         profileId: Int = 0,
69         tableName: String = Favorites.TABLE_NAME,
70         appWidgetId: Int = -1,
71         appWidgetSource: Int = -1,
72         appWidgetProvider: String? = null,
73     ) {
74         loadModelSync()
75         TestUtil.runOnExecutorSync(Executors.MODEL_EXECUTOR) {
76             val controller: ModelDbController = modelDbController
77             controller.attemptMigrateDb(null /* restoreEventLogger */)
78             modelDbController.newTransaction().use { transaction ->
79                 val values =
80                     ContentValues().apply {
81                         values[_ID] = id
82                         values[TITLE] = title
83                         values[PROFILE_ID] = profileId
84                         values[CONTAINER] = container
85                         values[SCREEN] = screen
86                         values[CELLX] = x
87                         values[CELLY] = y
88                         values[SPANX] = spanX
89                         values[SPANY] = spanY
90                         values[ITEM_TYPE] = type
91                         values[RESTORED] = restoreFlags
92                         values[INTENT] = intent
93                         values[APPWIDGET_ID] = appWidgetId
94                         values[APPWIDGET_SOURCE] = appWidgetSource
95                         values[APPWIDGET_PROVIDER] = appWidgetProvider
96                     }
97                 // Migrate any previous data so that the DB state is correct
98                 controller.insert(tableName, values)
99                 transaction.commit()
100             }
101         }
102     }
103 }
104