• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2021-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 SettingsDataConfig from '../Utils/SettingsDataConfig';
17import SettingsDBHelper from '../Utils/SettingsDBHelper';
18import StaticSubscriberExtensionAbility from '@ohos.application.StaticSubscriberExtensionAbility';
19import { Log } from '../Utils/Log';
20import commonEventManager from '@ohos.commonEventManager';
21
22const CURRENT_USER_TABLE_DROP: string = `DROP TABLE IF EXISTS ${SettingsDataConfig.USER_TABLE_NAME}_`
23const TAG: string = 'UserChangeStaticSubscriber : '
24interface IContent {
25  settings: Array<Map<string,string>> ;
26}
27
28export default class UserChangeStaticSubscriber extends StaticSubscriberExtensionAbility {
29  async onReceiveEvent(event: commonEventManager.CommonEventData) {
30    if (!event) {
31      return
32    }
33    Log.info(TAG + 'onReceiveEvent, event' + JSON.stringify(event))
34    let rdb = await SettingsDBHelper.getInstance().getRdbStore();
35    switch (event.event) {
36      case commonEventManager.Support.COMMON_EVENT_USER_ADDED:
37        // 创建对应用户的数据表
38        await rdb?.executeSql(SettingsDBHelper.CURRENT_USER_TABLE_CREATE_PREFIX + event.code + SettingsDBHelper.TABLE_CREATE_SUFFIX, []);
39        // 加载用户数据表的默认值
40        try {
41          let content = await SettingsDBHelper.getInstance().readDefaultFile() as IContent;
42          if (!content) {
43            Log.error("readDefaultFile is failed!");
44            return
45          }
46          for (let index = 0; index < content.settings.length; index++) {
47            if (content.settings[index]["userConfig"] && event.code) {
48              await SettingsDBHelper.getInstance().loadUserSettings(content.settings[index]["name"], content.settings[index]["value"], event.code)
49              Log.info("content.settings[index]"+content.settings[index]["value"]);
50            }
51          }
52        } catch (err) {
53          Log.error("loadDefaultSettingsData failed! err = " + err);
54        }
55        break
56      case commonEventManager.Support.COMMON_EVENT_USER_REMOVED:
57        // 删除对应用户的数据表
58        rdb?.executeSql(CURRENT_USER_TABLE_DROP + event.code, []);
59        break
60      default:
61        break
62    }
63  }
64}