• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 UIAbility from '@ohos.app.ability.UIAbility';
17import commonEvent from '@ohos.commonEventManager';
18import consts from '../module/Consts';
19import dataPreferences from '@ohos.data.preferences';
20import surveillanceEventsManager from '../module/SurveillanceEventsManager';
21import Logger from '../module/Logger';
22
23export default class LauncherAbility extends UIAbility {
24  onCreate(want) {
25    globalThis.abilityWant = want;
26    let settings: Map<string, number> = new Map();
27    surveillanceEventsManager.surveillanceEvents.forEach((element: string) => {
28      settings.set(element, consts.ENABLE_STATE_ALWAYS);
29    });
30    globalThis.settings = settings;
31    Logger.info(`LauncherAbility onCreate, settings.size = ${globalThis.settings.size}`)
32  }
33
34  async onDestroy() {
35    Logger.info("LauncherAbility onDestroy")
36    globalThis.settings.forEach((value: number, key: string) => {
37      if (value == consts.ENABLE_STATE_ONCE) {
38        globalThis.settings.set(key, consts.ENABLE_STATE_NEVER);
39      }
40    });
41    let thisPref = null;
42    await dataPreferences.getPreferences(this.context, consts.DATA_BASE_NAME).then((pref) => {
43      thisPref = pref;
44    });
45    for (let element of surveillanceEventsManager.surveillanceEvents) {
46      await thisPref.put(element, globalThis.settings.get(element));
47    };
48    await thisPref.flush();
49    let options = {
50      isSticky: true,
51      parameters: surveillanceEventsManager.getSurveillanceEventStates()
52    };
53    commonEvent.publish(consts.COMMON_EVENT_SETTING_UPDATE, options, () => {
54      Logger.info("success to publish once enable event");
55    });
56  }
57
58  onWindowStageCreate(windowStage) {
59    // Main window is created, set main page for this ability
60    windowStage.loadContent("pages/Launch", (err, data) => {
61      if (err.code) {
62        Logger.error('Failed to load the content. Cause:' + JSON.stringify(err));
63        return;
64      }
65      Logger.info('Succeeded in loading the content. Data: ' + JSON.stringify(data));
66    });
67  }
68
69  onWindowStageDestroy() {
70    // Main window is destroyed, release UI related resources
71    Logger.info("LauncherAbility onWindowStageDestroy");
72  }
73
74  onForeground() {
75    // Ability has brought to foreground
76    Logger.info("LauncherAbility onForeground");
77  }
78
79  onBackground() {
80    // Ability has back to background
81    Logger.info("LauncherAbility onBackground");
82  }
83}
84