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 AppStorage.setOrCreate('context', this.context); 32 Logger.info(`LauncherAbility onCreate, settings.size = ${globalThis.settings.size}`) 33 } 34 35 async onDestroy() { 36 Logger.info("LauncherAbility onDestroy") 37 globalThis.settings.forEach((value: number, key: string) => { 38 if (value == consts.ENABLE_STATE_ONCE) { 39 globalThis.settings.set(key, consts.ENABLE_STATE_NEVER); 40 } 41 }); 42 let thisPref = null; 43 await dataPreferences.getPreferences(this.context, consts.DATA_BASE_NAME).then((pref) => { 44 thisPref = pref; 45 }); 46 for (let element of surveillanceEventsManager.surveillanceEvents) { 47 await thisPref.put(element, globalThis.settings.get(element)); 48 }; 49 await thisPref.flush(); 50 let options = { 51 isSticky: true, 52 parameters: surveillanceEventsManager.getSurveillanceEventStates() 53 }; 54 commonEvent.publish(consts.COMMON_EVENT_SETTING_UPDATE, options, () => { 55 Logger.info("success to publish once enable event"); 56 }); 57 } 58 59 onWindowStageCreate(windowStage) { 60 // Main window is created, set main page for this ability 61 windowStage.loadContent("pages/Launch", (err, data) => { 62 if (err.code) { 63 Logger.error('Failed to load the content. Cause:' + JSON.stringify(err)); 64 return; 65 } 66 Logger.info('Succeeded in loading the content. Data: ' + JSON.stringify(data)); 67 }); 68 } 69 70 onWindowStageDestroy() { 71 // Main window is destroyed, release UI related resources 72 Logger.info("LauncherAbility onWindowStageDestroy"); 73 } 74 75 onForeground() { 76 // Ability has brought to foreground 77 Logger.info("LauncherAbility onForeground"); 78 } 79 80 onBackground() { 81 // Ability has back to background 82 Logger.info("LauncherAbility onBackground"); 83 } 84} 85