1/** 2 * Copyright (c) 2021-2024 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 */ 15import StaticSubscriberExtensionAbility from '@ohos.application.StaticSubscriberExtensionAbility'; 16import commonEventManager from '@ohos.commonEventManager'; 17import { Log, CommonEventData} from '../Utils/Log'; 18import SettingsDataConfig from '../Utils/SettingsDataConfig'; 19import SettingsDBHelper from '../Utils/SettingsDBHelper'; 20import { IContent, TableType } from '../common/Common'; 21import { GlobalContext } from '../Utils/GlobalContext'; 22 23const CURRENT_USER_TABLE_DROP: string = `DROP TABLE IF EXISTS ${SettingsDataConfig.USER_TABLE_NAME}_`; 24const CURRENT_SECURE_USER_TABLE_DROP: string = `DROP TABLE IF EXISTS ${SettingsDataConfig.SECURE_TABLE_NAME}_`; 25const TAG: string = 'UserChangeStaticSubscriber : ' 26 27export default class UserChangeStaticSubscriber extends StaticSubscriberExtensionAbility { 28 private init() { 29 Log.info('UserChangeStaticSubscriber start') 30 GlobalContext.getContext().setObject('abilityContext', this.context); 31 this.context.area = SettingsDBHelper.getInstance().getArea(); 32 } 33 async onReceiveEvent(event: CommonEventData) { 34 if (!event || !event.code) { 35 Log.error('invalid parameters') 36 return; 37 } 38 Log.info(`onReceiveEvent, event: ${event.event}, userId: ${event.code}`); 39 40 this.init(); 41 let rdb = await SettingsDBHelper.getInstance().getRdbStore(); 42 switch (event.event) { 43 case commonEventManager.Support.COMMON_EVENT_USER_ADDED: 44 // 创建对应用户的数据表 45 await rdb?.executeSql(SettingsDBHelper.CURRENT_USER_TABLE_CREATE_PREFIX + event.code + 46 SettingsDBHelper.TABLE_CREATE_SUFFIX, []); 47 await rdb?.executeSql(SettingsDBHelper.CURRENT_SECURE_TABLE_CREATE_PREFIX + event.code + 48 SettingsDBHelper.TABLE_CREATE_SUFFIX, []); 49 Log.info('create settings data table success!') 50 // 加载用户数据表的默认值 51 try { 52 let content = await SettingsDBHelper.getInstance().readDefaultFile() as IContent; 53 if (!content) { 54 Log.error('readDefaultFile is failed!'); 55 return 56 } 57 // 初始化用户表数据 58 await SettingsDBHelper.getInstance().initialInsert(SettingsDataConfig.USER_TABLE_NAME + '_' + event.code); 59 await SettingsDBHelper.getInstance().initialInsert(SettingsDataConfig.SECURE_TABLE_NAME + '_' + event.code); 60 // 用户数据表包含USER、USER_SECURE 61 await SettingsDBHelper.getInstance().loadTableData({ settings: content.settings, 62 user: content.user, 63 userSecure: content.userSecure }, TableType.USER, event.code as number); 64 await SettingsDBHelper.getInstance().loadTableData({ settings: content.settings, 65 user: content.user, 66 userSecure: content.userSecure }, TableType.USER_SECURE, event.code as number); 67 } catch (err) { 68 Log.error('loadDefaultSettingsData failed! err = ' + err); 69 } 70 break 71 case commonEventManager.Support.COMMON_EVENT_USER_REMOVED: 72 // 删除对应用户数据表 73 rdb?.executeSql(CURRENT_USER_TABLE_DROP + event.code, []); 74 rdb?.executeSql(CURRENT_SECURE_USER_TABLE_DROP + event.code, []); 75 break 76 default: 77 break 78 } 79 } 80}