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 */ 15 16import appRecovery from '@ohos.app.ability.appRecovery'; 17import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 18import UIAbility from '@ohos.app.ability.UIAbility'; 19import Want from '@ohos.app.ability.Want'; 20import Window from '@ohos.window'; 21 22import Logger from '../common/Logger'; 23import { CurActiveAbility } from '../common/CurActiveAbility'; 24 25const TAG: string = 'EntryAbility'; 26 27export default class EntryAbility extends UIAbility { 28 isRecoverLaunch: boolean = false; 29 storage: LocalStorage = new LocalStorage(); 30 31 // Read previous saved status from want if we are launched by appRecovery 32 restoreLocalStorage(want: Want): void { 33 Logger.info(TAG, "RestoreLocalStorage String:${want.parameters['Page1Str']} Counter:${want.parameters['Page2Counter']}"); 34 if (want.parameters !== undefined) { 35 this.storage.setOrCreate<string>('FaultTriggerPageString', want.parameters['FaultTriggerPageString'] as string); 36 this.storage.setOrCreate<number>('FaultTriggerPageCounter', want.parameters['FaultTriggerPageCounter'] as number); 37 } 38 39 } 40 41 onCreate(want: Want, launchParam:AbilityConstant.LaunchParam): void { 42 Logger.info(TAG, 'onCreate Want:${JSON.stringify(want)} launchParam: ${JSON.stringify(launchParam)}'); 43 this.isRecoverLaunch = (launchParam.launchReason == AbilityConstant.LaunchReason.APP_RECOVERY); 44 this.storage.setOrCreate<string>('CurrentAbilityName', 'EntryAbility'); 45 this.storage.setOrCreate<string>('launchReason', launchParam.launchReason.toString()); 46 this.storage.setOrCreate<string>('lastExitReason', launchParam.lastExitReason.toString()); 47 if (this.isRecoverLaunch) { 48 // savedState is in wantParams 49 this.restoreLocalStorage(want); 50 this.context.restoreWindowStage(this.storage); 51 } 52 } 53 54 onDestroy(): void { 55 Logger.info(TAG, 'onDestroy'); 56 } 57 58 onWindowStageCreate(windowStage: Window.WindowStage): void { 59 Logger.info(TAG, 'onWindowStageCreate'); 60 // Share storage between pages and current ability 61 windowStage.loadContent('pages/Index', this.storage, (err, data): void => { 62 if (err.code) { 63 Logger.info(TAG, 'Failed to load the content. Cause:${JSON.stringify(err)}'); 64 return; 65 } 66 }); 67 } 68 69 onWindowStageDestroy(): void { 70 Logger.info(TAG, 'onWindowStageDestroy'); 71 } 72 73 onForeground(): void { 74 Logger.info(TAG, 'onForeground'); 75 CurActiveAbility.GetInstance().SetGlobalAbility(this); 76 } 77 78 onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>): AbilityConstant.OnSaveResult { 79 Logger.info(TAG, 'onDestroy'); 80 let string = this.storage.get<string>('FaultTriggerPageString'); 81 let counter = this.storage.get<number>('FaultTriggerPageCounter'); 82 wantParam['FaultTriggerPageString'] = string ?? 'Empty'; 83 wantParam['FaultTriggerPageCounter'] = counter as number; 84 Logger.info(TAG, 'SavedText:${string} SavedCounter:${counter}'); 85 return AbilityConstant.OnSaveResult.ALL_AGREE; 86 } 87 88 onBackground(): void { 89 Logger.info(TAG, 'onBackground'); 90 // save state when background 91 appRecovery.saveAppState(this.context); 92 } 93} 94