1/* 2 * Copyright (c) 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 hilog from '@ohos.hilog'; 16import Ability from '@ohos.app.ability.UIAbility'; 17import type Window from '@ohos.window'; 18import commonEvent from '@ohos.commonEvent'; 19 20let commonStateArr: number[] = [-1, -1, -1, -1]; 21let commonEventData = { 22 parameters: { 23 commonStateArr: commonStateArr 24 } 25}; 26 27let tag = 'StateChangeTesttag'; 28let delayTime500 = 500; 29let delayTime1000 = 1000; 30let delayTime2000 = 2000; 31let undefineTag = -1; 32let defineTag = 1; 33let arrLength = 4; 34let onForeGroundTAG = undefineTag; 35let zero = 0; 36let one = 1; 37let two = 2; 38let three = 3; 39let applicationStateChangeCallbackFir = { 40 onApplicationForeground() { 41 console.log(tag, 'applicationStateChangeCallbackFir onApplicationForeground'); 42 commonEventData.parameters.commonStateArr[zero] = defineTag; 43 44 setTimeout(() => { 45 console.info('Enter onApplicationForeground publish!'); 46 commonEvent.publish('processState', commonEventData, (err) => { 47 console.info('====>processState publish err: ' + JSON.stringify(err)); 48 }); 49 }, delayTime1000); 50 }, 51 onApplicationBackground() { 52 console.log(tag, 'applicationStateChangeCallbackFir onApplicationBackground'); 53 commonEventData.parameters.commonStateArr[one] = defineTag; 54 55 if (globalThis.want.action === 'NeedBackGroundOff' || globalThis.want.action === 'MultiAppRegister') { 56 console.info('entered needbackgroundoff!'); 57 globalThis.applicationContext.off('applicationStateChange', applicationStateChangeCallbackFir); 58 } 59 } 60}; 61 62let applicationStateChangeCallbackSec = { 63 onApplicationForeground() { 64 console.log(tag, 'applicationStateChangeCallbackSec onApplicationForeground'); 65 commonEventData.parameters.commonStateArr[two] = defineTag; 66 }, 67 onApplicationBackground() { 68 console.log(tag, 'applicationStateChangeCallbackSec onApplicationBackground'); 69 commonEventData.parameters.commonStateArr[three] = defineTag; 70 if (globalThis.want.action === 'doubleNeedBackGroundOff') { 71 setTimeout(() => { 72 globalThis.applicationContext.off('applicationStateChange', applicationStateChangeCallbackSec); 73 }, delayTime500); 74 } 75 else if (globalThis.want.action === 'DoubleRegisterOff') { 76 setTimeout(() => { 77 console.info('entered DoubleRegisterOff'); 78 globalThis.applicationContext.off('applicationStateChange'); 79 }, delayTime500); 80 } 81 } 82}; 83 84export default class EntryAbility extends Ability { 85 onCreate(want, launchParam) { 86 onForeGroundTAG = undefineTag; 87 for (let i = 0; i < arrLength; i++) { 88 commonStateArr[i] = undefineTag; 89 } 90 hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); 91 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 92 hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? ''); 93 hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? ''); 94 globalThis.abilityContext = this.context; 95 globalThis.want = want; 96 globalThis.applicationContext = this.context.getApplicationContext(); 97 if (globalThis.want.action === 'RegisterOnOffOn') { 98 globalThis.applicationContext.on('applicationStateChange', applicationStateChangeCallbackFir); 99 globalThis.applicationContext.off('applicationStateChange', applicationStateChangeCallbackFir); 100 } 101 globalThis.applicationContext.on('applicationStateChange', applicationStateChangeCallbackFir); 102 if (globalThis.want.action === 'doubleRegister' || globalThis.want.action === 'doubleNeedBackGroundOff' || 103 globalThis.want.action === 'DoubleRegisterOff') { 104 console.info('double in action is logic entered!'); 105 globalThis.applicationContext.on('applicationStateChange', applicationStateChangeCallbackSec); 106 } 107 108 } 109 110 onDestroy() { 111 hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); 112 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 113 } 114 115 onWindowStageCreate(windowStage: Window.WindowStage) { 116 // Main window is created, set main page for this ability 117 hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); 118 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 119 120 windowStage.loadContent('pages/Index', (err, data) => { 121 if (err.code) { 122 hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.ERROR); 123 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 124 return; 125 } 126 hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); 127 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 128 }); 129 } 130 131 onWindowStageDestroy() { 132 // Main window is destroyed, release UI related resources 133 hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); 134 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 135 } 136 137 onForeground() { 138 // Ability has brought to foreground 139 hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); 140 hilog.info(0x0000, 'testTag', '%{public}s', 'Abilityone onForeground'); 141 onForeGroundTAG += defineTag; 142 if (onForeGroundTAG === defineTag && (globalThis.want.action === 'NeedBackGroundOff' || globalThis.want.action === 'MultiAppRegister' || 143 globalThis.want.action === 'DoubleRegisterOff')) { 144 setTimeout(() => { 145 commonEvent.publish('processState', commonEventData, (err) => { 146 console.info('====>processState publish err: ' + JSON.stringify(err)); 147 }); 148 }, delayTime2000); 149 150 } 151 } 152 153 onBackground() { 154 // Ability has back to background 155 hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); 156 hilog.info(0x0000, 'testTag', '%{public}s', 'Abilityone onBackground'); 157 } 158} 159