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 */ 15import common from '@ohos.app.ability.common'; 16import commonEvent from '@ohos.commonEventManager'; 17import consts from '../module/Consts'; 18import dataPreferences from '@ohos.data.preferences'; 19import Logger from '../module/Logger'; 20import router from '@ohos.router'; 21 22export default class LaunchFeature { 23 private innerContext: common.UIAbilityContext = null; 24 private pref: dataPreferences.Preferences = null; 25 private subscriber = null; 26 private subscriberLow = null; 27 private currentRecordTimes: number = 0; 28 29 constructor(abilityContext: common.UIAbilityContext) { 30 this.innerContext = abilityContext; 31 } 32 33 async init(): Promise<void> { 34 await dataPreferences.getPreferences(this.innerContext, consts.DATA_BASE_NAME).then((pref) => { 35 this.pref = pref; 36 }); 37 await this.pref.get(consts.DATA_BASE_KEY_WIFI_POWER_STATE, 0).then((value: number) => { 38 globalThis.settings.set(commonEvent.Support.COMMON_EVENT_WIFI_POWER_STATE, value); 39 }); 40 await this.pref.get(consts.DATA_BASE_KEY_SCREEN_OFF, 0).then((value1: number) => { 41 globalThis.settings.set(commonEvent.Support.COMMON_EVENT_SCREEN_OFF, value1); 42 }); 43 await this.pref.get(consts.DATA_BASE_KEY_SCREEN_ON, 0).then((value2: number) => { 44 globalThis.settings.set(commonEvent.Support.COMMON_EVENT_SCREEN_ON, value2); 45 }); 46 await this.pref.get(consts.DATA_BASE_KEY_SCREEN_LOCKED, 0).then((value3: number) => { 47 globalThis.settings.set(commonEvent.Support.COMMON_EVENT_SCREEN_LOCKED, value3); 48 }); 49 await this.pref.get(consts.DATA_BASE_KEY_SCREEN_UNLOCKED, 0).then((value4: number) => { 50 globalThis.settings.set(commonEvent.Support.COMMON_EVENT_SCREEN_UNLOCKED, value4); 51 }); 52 } 53 54 private insertRecord = (event, value) => { 55 value.push(event.parameters[consts.DATA_BASE_KEY_START_TIME]); 56 // refresh database 57 this.pref.put(consts.DATA_BASE_KEY_TOTAL_TIMES, value).then(() => { 58 let detail: Array<string> = []; 59 detail.push(event.parameters["startTime"]); 60 detail.push(event.parameters["endTime"]); 61 detail.push(event.parameters["totalTime"]); 62 detail.push(event.parameters["totalEvents"]); 63 this.pref.put(event.parameters[consts.DATA_BASE_KEY_START_TIME], detail).then(() => { 64 this.pref.flush() 65 }) 66 }); 67 } 68 69 private callbackFunc = (error, event) => { 70 this.pref.has(consts.DATA_BASE_KEY_TOTAL_TIMES, (err, ret) => { 71 if (ret) { 72 this.pref.get(consts.DATA_BASE_KEY_TOTAL_TIMES, []).then((value) => { 73 this.insertRecord(event, value); 74 }); 75 } else { 76 let value: Array<string> = []; 77 this.insertRecord(event, value); 78 } 79 if (this.currentRecordTimes >= consts.MAX_RECORD_NUM) { 80 this.subscriber.finishCommonEvent(); 81 return; 82 } 83 this.subscriber.abortCommonEvent(); 84 this.subscriber.finishCommonEvent(); 85 this.currentRecordTimes++; 86 }) 87 } 88 89 private callbackLowFunc = (error, event) => { 90 this.currentRecordTimes = 1; 91 this.pref.get(consts.DATA_BASE_KEY_TOTAL_TIMES, []).then((value: Array<string>) => { 92 for (let i = 0; i < consts.MAX_RECORD_NUM; i++) { 93 this.pref.delete(value[i]).then(() => { 94 this.pref.flush(); 95 this.subscriberLow.finishCommonEvent(); 96 }) 97 } 98 let records = value.slice(consts.MAX_RECORD_NUM, consts.MAX_RECORD_NUM + 1); 99 this.pref.put(consts.DATA_BASE_KEY_TOTAL_TIMES, records); 100 this.pref.flush(); 101 }) 102 } 103 104 jumpToStart = () => { 105 // subscribe 106 if (this.subscriber == null) { 107 let highSubscriberInfo = { 108 events: [ 109 consts.COMMON_EVENT_FINISH_MEDITATION // unordered self defined event 110 ], 111 priority: 2 // 2 indicates high priority subscriber 112 }; 113 commonEvent.createSubscriber(highSubscriberInfo, (err, subscriber) => { 114 this.subscriber = subscriber 115 if (subscriber != null) { 116 commonEvent.subscribe(subscriber, this.callbackFunc) 117 } 118 }); 119 } 120 // subscribe 121 if (this.subscriberLow == null) { 122 let lowSubscriberInfo = { 123 events: [ 124 consts.COMMON_EVENT_FINISH_MEDITATION // unordered self defined event 125 ], 126 priority: 1 // 1 indicates low priority subscriber 127 }; 128 commonEvent.createSubscriber(lowSubscriberInfo, (err, subscriber) => { 129 this.subscriberLow = subscriber 130 if (subscriber != null) { 131 commonEvent.subscribe(subscriber, this.callbackLowFunc) 132 } 133 }); 134 } 135 let want = { 136 bundleName: 'com.samples.customcommonevent', 137 abilityName: 'MainAbility', 138 }; 139 this.innerContext.startAbility(want); 140 } 141 142 jumpToHistory = () => { 143 Logger.info("ready to jump to history page"); 144 router.pushUrl({ 145 url: 'pages/History', 146 params: {} 147 }); 148 } 149 150 jumpToSetting = () => { 151 Logger.info("ready to jump to setting page"); 152 router.pushUrl({ 153 url: 'pages/Setting', 154 params: {} 155 }); 156 } 157 158 jumpToAbout = () => { 159 Logger.info("ready to jump to about page"); 160 router.pushUrl({ 161 url: 'pages/About', 162 params: {} 163 }); 164 } 165}