1315b9658Sopenharmony_ci/**
2315b9658Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3315b9658Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4315b9658Sopenharmony_ci * you may not use this file except in compliance with the License.
5315b9658Sopenharmony_ci * You may obtain a copy of the License at
6315b9658Sopenharmony_ci *
7315b9658Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8315b9658Sopenharmony_ci *
9315b9658Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10315b9658Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11315b9658Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12315b9658Sopenharmony_ci * See the License for the specific language governing permissions and
13315b9658Sopenharmony_ci * limitations under the License.
14315b9658Sopenharmony_ci */
15315b9658Sopenharmony_ciimport StaticSubscriberExtensionAbility from '@ohos.application.StaticSubscriberExtensionAbility';
16315b9658Sopenharmony_ciimport commonEventManager from '@ohos.commonEventManager';
17315b9658Sopenharmony_ciimport { Log, CommonEventData} from '../Utils/Log';
18315b9658Sopenharmony_ciimport SettingsDataConfig from '../Utils/SettingsDataConfig';
19315b9658Sopenharmony_ciimport SettingsDBHelper from '../Utils/SettingsDBHelper';
20315b9658Sopenharmony_ciimport { IContent, TableType } from '../common/Common';
21315b9658Sopenharmony_ciimport { GlobalContext } from '../Utils/GlobalContext';
22315b9658Sopenharmony_ci
23315b9658Sopenharmony_ciconst CURRENT_USER_TABLE_DROP: string = `DROP TABLE IF EXISTS ${SettingsDataConfig.USER_TABLE_NAME}_`;
24315b9658Sopenharmony_ciconst CURRENT_SECURE_USER_TABLE_DROP: string = `DROP TABLE IF EXISTS ${SettingsDataConfig.SECURE_TABLE_NAME}_`;
25315b9658Sopenharmony_ciconst TAG: string = 'UserChangeStaticSubscriber : '
26315b9658Sopenharmony_ci
27315b9658Sopenharmony_ciexport default class UserChangeStaticSubscriber extends StaticSubscriberExtensionAbility {
28315b9658Sopenharmony_ci  private init() {
29315b9658Sopenharmony_ci    Log.info('UserChangeStaticSubscriber start')
30315b9658Sopenharmony_ci    GlobalContext.getContext().setObject('abilityContext', this.context);
31315b9658Sopenharmony_ci    this.context.area = SettingsDBHelper.getInstance().getArea();
32315b9658Sopenharmony_ci  }
33315b9658Sopenharmony_ci  async onReceiveEvent(event: CommonEventData) {
34315b9658Sopenharmony_ci    if (!event || !event.code) {
35315b9658Sopenharmony_ci      Log.error('invalid parameters')
36315b9658Sopenharmony_ci      return;
37315b9658Sopenharmony_ci    }
38315b9658Sopenharmony_ci    Log.info(`onReceiveEvent, event: ${event.event}, userId: ${event.code}`);
39315b9658Sopenharmony_ci
40315b9658Sopenharmony_ci    this.init();
41315b9658Sopenharmony_ci    let rdb = await SettingsDBHelper.getInstance().getRdbStore();
42315b9658Sopenharmony_ci    switch (event.event) {
43315b9658Sopenharmony_ci      case commonEventManager.Support.COMMON_EVENT_USER_ADDED:
44315b9658Sopenharmony_ci        // 创建对应用户的数据表
45315b9658Sopenharmony_ci        await rdb?.executeSql(SettingsDBHelper.CURRENT_USER_TABLE_CREATE_PREFIX + event.code +
46315b9658Sopenharmony_ci        SettingsDBHelper.TABLE_CREATE_SUFFIX, []);
47315b9658Sopenharmony_ci        await rdb?.executeSql(SettingsDBHelper.CURRENT_SECURE_TABLE_CREATE_PREFIX + event.code +
48315b9658Sopenharmony_ci        SettingsDBHelper.TABLE_CREATE_SUFFIX, []);
49315b9658Sopenharmony_ci        Log.info('create settings data table success!')
50315b9658Sopenharmony_ci        // 加载用户数据表的默认值
51315b9658Sopenharmony_ci        try {
52315b9658Sopenharmony_ci          let content = await SettingsDBHelper.getInstance().readDefaultFile() as IContent;
53315b9658Sopenharmony_ci          if (!content) {
54315b9658Sopenharmony_ci            Log.error('readDefaultFile is failed!');
55315b9658Sopenharmony_ci            return
56315b9658Sopenharmony_ci          }
57315b9658Sopenharmony_ci          // 初始化用户表数据
58315b9658Sopenharmony_ci          await SettingsDBHelper.getInstance().initialInsert(SettingsDataConfig.USER_TABLE_NAME + '_' + event.code);
59315b9658Sopenharmony_ci          await SettingsDBHelper.getInstance().initialInsert(SettingsDataConfig.SECURE_TABLE_NAME + '_' + event.code);
60315b9658Sopenharmony_ci          // 用户数据表包含USER、USER_SECURE
61315b9658Sopenharmony_ci          await SettingsDBHelper.getInstance().loadTableData({ settings: content.settings,
62315b9658Sopenharmony_ci            user: content.user,
63315b9658Sopenharmony_ci            userSecure: content.userSecure }, TableType.USER, event.code as number);
64315b9658Sopenharmony_ci          await SettingsDBHelper.getInstance().loadTableData({ settings: content.settings,
65315b9658Sopenharmony_ci            user: content.user,
66315b9658Sopenharmony_ci            userSecure: content.userSecure }, TableType.USER_SECURE, event.code as number);
67315b9658Sopenharmony_ci        } catch (err) {
68315b9658Sopenharmony_ci          Log.error('loadDefaultSettingsData failed! err = ' + err);
69315b9658Sopenharmony_ci        }
70315b9658Sopenharmony_ci        break
71315b9658Sopenharmony_ci      case commonEventManager.Support.COMMON_EVENT_USER_REMOVED:
72315b9658Sopenharmony_ci        // 删除对应用户数据表
73315b9658Sopenharmony_ci        rdb?.executeSql(CURRENT_USER_TABLE_DROP + event.code, []);
74315b9658Sopenharmony_ci        rdb?.executeSql(CURRENT_SECURE_USER_TABLE_DROP + event.code, []);
75315b9658Sopenharmony_ci        break
76315b9658Sopenharmony_ci      default:
77315b9658Sopenharmony_ci        break
78315b9658Sopenharmony_ci    }
79315b9658Sopenharmony_ci  }
80315b9658Sopenharmony_ci}