1/* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import dataRdb from '@ohos.data.relationalStore' 17import { CheckEmptyUtils } from '../utils/CheckEmptyUtils' 18import { CommonConstants } from '../constants/CommonConstants' 19import { GridLayoutItemInfo } from '../bean/GridLayoutItemInfo' 20import { GridLayoutInfoColumns } from '../bean/GridLayoutInfoColumns' 21import { Logger } from '../utils/Logger' 22 23export const TABLE_NAME: string = 'launcher' 24 25export const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS launcher ' + 26'(id INTEGER PRIMARY KEY AUTOINCREMENT, ' + 27'app_name TEXT, ' + 28'appIcon_id INTEGER, ' + 29'container INTEGER, ' + 30'type_id INTEGER, ' + 31'card_id INTEGER, ' + 32'card_name TEXT, ' + 33'badge_number INTEGER, ' + 34'module_name TEXT, ' + 35'bundle_name TEXT, ' + 36'ability_name TEXT, ' + 37'area TEXT, ' + 38'page INTEGER, ' + 39'column INTEGER, ' + 40'row INTEGER)' 41 42export const STORE_CONFIG = { name: 'launcher.db', securityLevel: dataRdb.SecurityLevel.S1 } 43const TAG: string = 'RdbModel' 44class RdbManagerModel { 45 private mRdbStore: dataRdb.RdbStore = undefined 46 47 constructor() { 48 } 49 50 /** 51 * initRdbConfig 52 * 53 * @param context 54 */ 55 async initRdbConfig(context): Promise<void> { 56 Logger.info(TAG, 'initRdbConfig start') 57 if (this.mRdbStore === undefined) { 58 this.mRdbStore = await dataRdb.getRdbStore(context, STORE_CONFIG); 59 await this.mRdbStore.executeSql(SQL_CREATE_TABLE); 60 Logger.info(TAG, 'create table end'); 61 } 62 } 63 64 /** 65 * deleteTable 66 * 67 * @param tableName 68 */ 69 async deleteTable(tableName: string): Promise<void> { 70 Logger.info(TAG, 'deleteTable start') 71 try { 72 let detelSql = `DELETE FROM ${tableName};` 73 let detelSequenceSql = `UPDATE sqlite_sequence SET seq=0 WHERE name = '${tableName}';` 74 await this.mRdbStore.executeSql(detelSql) 75 await this.mRdbStore.executeSql(detelSequenceSql) 76 Logger.debug(TAG, 'deleteTable end') 77 } catch (e) { 78 Logger.error(TAG, `deleteTable err: ${e}`) 79 } 80 } 81 82 /** 83 * insertData 84 * 85 * @param layoutInfo 86 */ 87 async insertData(layoutInfo: any) { 88 Logger.info(TAG, 'insertGridLayoutInfo start'); 89 let result: boolean = true 90 if (CheckEmptyUtils.isEmpty(layoutInfo)) { 91 Logger.error(TAG, 'insertGridLayoutInfo gridlayoutinfo is empty') 92 result = false 93 return result 94 } 95 try { 96 // delete gridlayoutinfo table 97 await this.deleteTable(TABLE_NAME) 98 // insert into gridlayoutinfo 99 for (let i in layoutInfo) { 100 let layout = layoutInfo[i] 101 for (let j in layout) { 102 let element = layout[j] 103 Logger.info(TAG, `insertGridLayoutInfo i= ${i}`) 104 let item = {} 105 if (element.typeId === CommonConstants.TYPE_APP) { 106 item = { 107 'app_name': element.appName, 108 'bundle_name': element.bundleName, 109 'module_name': element.modelName, 110 'ability_name': element.abilityName, 111 'appIcon_id': element.appIconId, 112 'type_id': element.typeId, 113 'area': element.area[0] + ',' + element.area[1], 114 'page': element.page, 115 'column': element.column, 116 'row': element.row, 117 'container': -100 118 } 119 let ret = await this.mRdbStore.insert(TABLE_NAME, item) 120 Logger.debug(TAG, `insertGridLayoutInfo type is app ${i} ret: ${ret}`) 121 } else if (element.typeId === CommonConstants.TYPE_CARD) { 122 item = { 123 'app_name': element.appName, 124 'bundle_name': element.bundleName, 125 'module_name': element.modelName, 126 'ability_name': element.abilityName, 127 'card_id': element.cardId, 128 'card_name': element.cardName, 129 'type_id': element.typeId, 130 'area': element.area[0] + ',' + element.area[1], 131 'page': element.page, 132 'column': element.column, 133 'row': element.row, 134 'container': -100 135 } 136 let ret = await this.mRdbStore.insert(TABLE_NAME, item) 137 Logger.debug(TAG, `insertGridLayoutInfo type is card ${i} ret: ${ret}`) 138 } 139 } 140 } 141 } catch (e) { 142 Logger.error(TAG, `insertGridLayoutInfo error: ${e}`) 143 } 144 return result 145 } 146 147 async queryLayoutInfo() { 148 Logger.info(TAG, 'queryLayoutInfo start') 149 const resultList: GridLayoutItemInfo[] = [] 150 const predicates = new dataRdb.RdbPredicates(TABLE_NAME) 151 predicates.equalTo(GridLayoutInfoColumns.CONTAINER, -100) 152 .and().orderByAsc('page').and().orderByAsc('row').and().orderByAsc('column') 153 let resultSet = await this.mRdbStore.query(predicates) 154 Logger.info(TAG, `queryLayoutInfo query,count=${resultSet.rowCount}`) 155 let isLast = resultSet.goToFirstRow() 156 while (isLast) { 157 const layoutInfo: GridLayoutItemInfo = GridLayoutItemInfo.fromResultSet(resultSet) 158 resultList.push(layoutInfo) 159 isLast = resultSet.goToNextRow() 160 } 161 resultSet.close() 162 resultSet = null 163 return resultList 164 } 165 166 async insertItem(item: GridLayoutItemInfo) { 167 if (CheckEmptyUtils.isEmpty(item)) { 168 return 169 } 170 let element = { 171 'app_name': item.appName, 172 'module_name': item.moduleName, 173 'bundle_name': item.bundleName, 174 'ability_name': item.abilityName, 175 'appIcon_id': item.appIconId, 176 'card_id': item.cardId, 177 'card_name': item.cardName, 178 'type_id': item.typeId, 179 'area': item.area[0] + ',' + item.area[1], 180 'page': item.page, 181 'column': item.column, 182 'row': item.row, 183 'container': -100 184 } 185 let ret = await this.mRdbStore.insert(TABLE_NAME, element) 186 Logger.debug(TAG, `insertGridLayoutInfo ret: ${ret}`) 187 } 188 189 async deleteItemByPosition(page: number, row: number, column: number) { 190 const predicates = new dataRdb.RdbPredicates(TABLE_NAME); 191 predicates.equalTo('page', page) 192 .and().equalTo('row', row) 193 .and().equalTo('column', column); 194 let query = await this.mRdbStore.query(predicates); 195 if (query.rowCount > 0) { 196 let ret = await this.mRdbStore.delete(predicates); 197 Logger.debug(TAG, `deleteItem ret: ${ret}`); 198 } 199 } 200} 201 202export let RdbManager = new RdbManagerModel()