1/**
2 * Copyright (c) 2022 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 Ability from '@ohos.app.ability.UIAbility'
17import Window from '@ohos.window'
18import WorkFactory, { WorkerType } from '../workers/WorkFactory';
19import { HiLog } from '../../../../../common/src/main/ets/util/HiLog';
20import Want from '@ohos.app.ability.Want';
21import SimManager from '../feature/sim/SimManager';
22import { missedCallManager } from '../feature/missedCall/MissedCallManager';
23import PresenterManager from '../presenter/PresenterManager';
24
25const TAG = 'MainAbility ';
26
27export default class MainAbility extends Ability {
28    storage: LocalStorage;
29    simManager: SimManager;
30    mDataWorker = WorkFactory.getWorker(WorkerType.DataWorker);
31
32    updateBreakpoint(windowWidth: number) {
33        let windowWidthVp: number = px2vp(windowWidth);
34        let breakpoint: string;
35        if (windowWidthVp < 520) {
36            breakpoint = 'sm';
37        } else if (windowWidthVp < 840) {
38            breakpoint = 'md';
39        } else {
40            breakpoint = 'lg';
41        }
42        this.storage.setOrCreate('breakpoint', breakpoint);
43    }
44
45    onRequest(want: Want, isOnCreate: boolean) {
46        if (!want || !want.parameters) {
47            return;
48        }
49        const data: any = want.parameters['missedCallData'];
50        const action = want.parameters['action'];
51        HiLog.i(TAG, `onRequest action: ${action}`);
52        if (action != undefined && data != undefined) {
53            missedCallManager.requestMissedCallAction(action, data);
54        }
55    }
56
57    onCreate(want, launchParam) {
58        HiLog.i(TAG, 'Application onCreate start');
59        globalThis.isFromOnCreate = true;
60        globalThis.context = this.context;
61        globalThis.abilityWant = want;
62        this.storage = new LocalStorage()
63        this.onRequest(want, true);
64        this.simManager = new SimManager();
65        globalThis.DataWorker = this.mDataWorker;
66        globalThis.presenterManager = new PresenterManager(this.context, this.mDataWorker);
67        globalThis.presenterManager.onCreate(want);
68    }
69
70    onNewWant(want, launchParam) {
71        HiLog.i(TAG, 'Application onNewWant');
72        globalThis.isFromOnCreate = false;
73        globalThis.abilityWant = want;
74        this.onRequest(want, false);
75        globalThis.presenterManager.onNewWant(want);
76    }
77
78    onDestroy() {
79        HiLog.i(TAG, 'Ability onDestroy');
80        globalThis.presenterManager.onDestroy();
81        this.mDataWorker.close();
82    }
83
84    onWindowStageCreate(windowStage: Window.WindowStage) {
85        // Main window is created, set main page for this ability
86        HiLog.i(TAG, 'Ability onWindowStageCreate');
87
88        Window.getTopWindow(this.context).then((windowObj) => {
89            windowObj.getProperties().then((windowProperties) => {
90                this.updateBreakpoint(windowProperties.windowRect.width);
91            })
92            windowObj.on('windowSizeChange', (data) => {
93                this.updateBreakpoint(data.width);
94            });
95        })
96
97        windowStage.loadContent(globalThis.presenterManager ? globalThis.presenterManager.mainUrl : 'pages/index', this.storage, (err, data) => {
98            if (err.code) {
99                HiLog.e(TAG, 'Failed to load the content. Cause: ' + JSON.stringify(err) ?? '');
100                return;
101            }
102            HiLog.i(TAG, 'Succeeded in loading the content. Data: ' + JSON.stringify(data) ?? '');
103        });
104    }
105
106    onWindowStageDestroy() {
107        // Main window is destroyed, release UI related resources
108        HiLog.i(TAG, 'Ability onWindowStageDestroy');
109    }
110
111    onForeground() {
112        // Ability has brought to foreground
113        HiLog.i(TAG, 'Ability onForeground');
114        this.simManager.init();
115    }
116
117    onBackground() {
118        // Ability has back to background
119        HiLog.i(TAG, 'Ability onBackground');
120        this.simManager.recycle();
121    }
122}