1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.launcher3.model.gridmigration
18 
19 import android.content.ContentValues
20 import android.database.sqlite.SQLiteDatabase
21 import android.graphics.Point
22 import com.android.launcher3.LauncherSettings.Favorites
23 import com.android.launcher3.celllayout.board.CellLayoutBoard
24 
25 class MockSet(override val size: Int) : Set<String> {
containsnull26     override fun contains(element: String): Boolean = true
27     override fun containsAll(elements: Collection<String>): Boolean = true
28     override fun isEmpty(): Boolean = false
29     override fun iterator(): Iterator<String> = listOf<String>().iterator()
30 }
31 
32 fun itemListToBoard(itemsArg: List<WorkspaceItem>, boardSize: Point): List<CellLayoutBoard> {
33     val items = itemsArg.filter { it.container != Favorites.CONTAINER_HOTSEAT }
34     val boardList =
35         List(items.maxOf { it.screenId + 1 }) { CellLayoutBoard(boardSize.x, boardSize.y) }
36     items.forEach {
37         when (it.type) {
38             Favorites.ITEM_TYPE_FOLDER,
39             Favorites.ITEM_TYPE_APP_PAIR -> throw Exception("Not implemented")
40             Favorites.ITEM_TYPE_APPWIDGET ->
41                 boardList[it.screenId].addWidget(it.x, it.y, it.spanX, it.spanY)
42             Favorites.ITEM_TYPE_APPLICATION -> boardList[it.screenId].addIcon(it.x, it.y)
43         }
44     }
45     return boardList
46 }
47 
insertIntoDbnull48 fun insertIntoDb(tableName: String, entry: WorkspaceItem, db: SQLiteDatabase) {
49     val values = ContentValues()
50     values.put(Favorites.SCREEN, entry.screenId)
51     values.put(Favorites.CELLX, entry.x)
52     values.put(Favorites.CELLY, entry.y)
53     values.put(Favorites.SPANX, entry.spanX)
54     values.put(Favorites.SPANY, entry.spanY)
55     values.put(Favorites.TITLE, entry.title)
56     values.put(Favorites.INTENT, entry.intent)
57     values.put(Favorites.APPWIDGET_PROVIDER, entry.appWidgetProvider)
58     values.put(Favorites.APPWIDGET_ID, entry.appWidgetId)
59     values.put(Favorites.CONTAINER, entry.container)
60     values.put(Favorites.ITEM_TYPE, entry.type)
61     values.put(Favorites._ID, entry.id)
62     db.insert(tableName, null, values)
63 }
64 
readDbnull65 fun readDb(tableName: String, db: SQLiteDatabase): List<WorkspaceItem> {
66     val result = mutableListOf<WorkspaceItem>()
67     val cursor = db.query(tableName, null, null, null, null, null, null)
68     val indexCellX: Int = cursor.getColumnIndexOrThrow(Favorites.CELLX)
69     val indexCellY: Int = cursor.getColumnIndexOrThrow(Favorites.CELLY)
70     val indexSpanX: Int = cursor.getColumnIndexOrThrow(Favorites.SPANX)
71     val indexSpanY: Int = cursor.getColumnIndexOrThrow(Favorites.SPANY)
72     val indexId: Int = cursor.getColumnIndexOrThrow(Favorites._ID)
73     val indexScreen: Int = cursor.getColumnIndexOrThrow(Favorites.SCREEN)
74     val indexTitle: Int = cursor.getColumnIndexOrThrow(Favorites.TITLE)
75     val indexAppWidgetId: Int = cursor.getColumnIndexOrThrow(Favorites.APPWIDGET_ID)
76     val indexWidgetProvider: Int = cursor.getColumnIndexOrThrow(Favorites.APPWIDGET_PROVIDER)
77     val indexIntent: Int = cursor.getColumnIndexOrThrow(Favorites.INTENT)
78     val indexItemType: Int = cursor.getColumnIndexOrThrow(Favorites.ITEM_TYPE)
79     val container: Int = cursor.getColumnIndexOrThrow(Favorites.CONTAINER)
80     while (cursor.moveToNext()) {
81         result.add(
82             WorkspaceItem(
83                 x = cursor.getInt(indexCellX),
84                 y = cursor.getInt(indexCellY),
85                 spanX = cursor.getInt(indexSpanX),
86                 spanY = cursor.getInt(indexSpanY),
87                 id = cursor.getInt(indexId),
88                 screenId = cursor.getInt(indexScreen),
89                 title = cursor.getString(indexTitle),
90                 appWidgetId = cursor.getInt(indexAppWidgetId),
91                 appWidgetProvider = cursor.getString(indexWidgetProvider),
92                 intent = cursor.getString(indexIntent),
93                 type = cursor.getInt(indexItemType),
94                 container = cursor.getInt(container)
95             )
96         )
97     }
98     return result
99 }
100 
101 data class WorkspaceItem(
102     val x: Int,
103     val y: Int,
104     val spanX: Int,
105     val spanY: Int,
106     val id: Int,
107     val screenId: Int,
108     val title: String,
109     val appWidgetId: Int,
110     val appWidgetProvider: String,
111     val intent: String,
112     val type: Int,
113     val container: Int,
114 )
115