1/* 2 * Copyright (c) 2024 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 AbilityConstant from '@ohos.app.ability.AbilityConstant'; 16import hilog from '@ohos.hilog'; 17import UIAbility from '@ohos.app.ability.UIAbility'; 18import Want from '@ohos.app.ability.Want'; 19import window from '@ohos.window'; 20import Logger from '../utils/Logger'; 21 22const TAG = '[testTag_AsyncAbility]: '; 23 24export default class AsyncAbility extends UIAbility { 25 contentStorage?: LocalStorage; 26 27 handleContinueParam(want: Want, launchParam: AbilityConstant.LaunchParam) { 28 // 迁移应用启动时(onCreate)&应用热启动时(onNewWant):恢复保存的迁移数据 29 if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) { 30 // 从want中恢复迁移数据 31 let testDate = want?.parameters?.testDate; 32 33 AppStorage.setOrCreate<string>('testDate', testDate as string); 34 35 //显式调用页面恢复 36 this.contentStorage = new LocalStorage(); 37 Logger.info(TAG, 'handleContinueParam() : ready to restore'); 38 this.context.restoreWindowStage(this.contentStorage); 39 } 40 } 41 42 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 43 Logger.info(TAG, 'onCreate : Ability onCreate'); 44 45 // 调用原因为迁移时,设置状态为可迁移,应对冷启动情况(保证迁移连续性) 46 if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) { 47 this.context.setMissionContinueState(AbilityConstant.ContinueState.ACTIVE, (result) => { 48 Logger.info(TAG, `setMissionContinueState ACTIVE result: ${JSON.stringify(result)}`); 49 }); 50 } 51 52 // 迁移应用启动时恢复保存的迁移数据 53 this.handleContinueParam(want, launchParam); 54 } 55 56 async setAsyncWant(wantParam: Record<string, Object>) { 57 Logger.info(TAG, 'setAsyncWant : onContinue setWant start'); 58 59 let testDate: string = AppStorage.get<string>('testDate') as string; 60 wantParam['testDate'] = testDate; 61 Logger.info(TAG, `onContinue testDate ${testDate}`); 62 63 Logger.info(TAG, 'setAsyncWant : onContinue setWant end'); 64 } 65 66 onContinue(wantParam: Record<string, Object>): Promise<AbilityConstant.OnContinueResult> { 67 Logger.info(TAG, 'onContinue : wantParam.version,wantParam.targetDevice : ' + wantParam.version + wantParam.targetDevice); 68 return this.setAsyncWant(wantParam).then(() => { 69 return AbilityConstant.OnContinueResult.AGREE; 70 }); 71 } 72 73 onNewWant(want: Want, launchParams: AbilityConstant.LaunchParam) { 74 Logger.info(TAG, 'Ability onNewWant'); 75 Logger.info(TAG, 'onNewWant, want:' + want.abilityName); 76 Logger.info(TAG, 'onNewWant, launchParams:' + JSON.stringify(launchParams)); 77 Logger.info(TAG, `onNewWant, want: ${want.abilityName}`); 78 Logger.info(TAG, `onNewWant, launchParam: ${JSON.stringify(launchParams)}`); 79 80 // 调用原因为迁移时,设置状态为可迁移,应对热启动情况(保证迁移连续性) 81 if (launchParams.launchReason == AbilityConstant.LaunchReason.CONTINUATION) { 82 this.context.setMissionContinueState(AbilityConstant.ContinueState.ACTIVE, (result) => { 83 Logger.info(`setMissionContinueState ACTIVE result: ${JSON.stringify(result)}`); 84 }); 85 } 86 87 //应用热启动时:恢复保存的迁移数据 88 this.handleContinueParam(want, launchParams); 89 Logger.info(TAG, 'onNewWant end') 90 } 91 92 onDestroy(): void { 93 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 94 } 95 96 onWindowStageCreate(windowStage: window.WindowStage): void { 97 // Main window is created, set main page for this ability 98 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 99 100 windowStage.loadContent('pages/Page_AsyncAbility', (err, data) => { 101 if (err.code) { 102 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 103 return; 104 } 105 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 106 }); 107 } 108 109 onWindowStageRestore(windowStage: window.WindowStage) { 110 Logger.info(TAG, 'onWindowStageRestore, pageStack_isON: ' + AppStorage.get<boolean>('pageStack_isON')); 111 if (AppStorage.get<boolean>('pageStack_isON') == false) { 112 Logger.info(TAG, 'onWindowStageRestore : ' + 'no pageStack'); 113 windowStage.loadContent('pages/Page_AsyncAbility', (err, data) => { 114 if (err.code) { 115 Logger.error(TAG, `Failed to load the content. Cause: ${JSON.stringify(err)}`); 116 return; 117 } 118 Logger.info(TAG, `Succeeded in loading the content. Data: ${JSON.stringify(data)}`); 119 }); 120 } else { 121 Logger.info(TAG, 'onWindowStageRestore : have pageStack'); 122 } 123 } 124 125 126 onWindowStageDestroy(): void { 127 // Main window is destroyed, release UI related resources 128 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 129 } 130 131 onForeground(): void { 132 // Ability has brought to foreground 133 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 134 } 135 136 onBackground(): void { 137 // Ability has back to background 138 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 139 } 140} 141