18779efd5Sopenharmony_ci/**
28779efd5Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
38779efd5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48779efd5Sopenharmony_ci * you may not use this file except in compliance with the License.
58779efd5Sopenharmony_ci * You may obtain a copy of the License at
68779efd5Sopenharmony_ci *
78779efd5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88779efd5Sopenharmony_ci *
98779efd5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108779efd5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118779efd5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128779efd5Sopenharmony_ci * See the License for the specific language governing permissions and
138779efd5Sopenharmony_ci * limitations under the License.
148779efd5Sopenharmony_ci */
158779efd5Sopenharmony_ci
168779efd5Sopenharmony_ciimport Ability from '@ohos.app.ability.UIAbility'
178779efd5Sopenharmony_ciimport Window from '@ohos.window'
188779efd5Sopenharmony_ciimport WorkFactory, { WorkerType } from '../workers/WorkFactory';
198779efd5Sopenharmony_ciimport { HiLog } from '../../../../../common/src/main/ets/util/HiLog';
208779efd5Sopenharmony_ciimport Want from '@ohos.app.ability.Want';
218779efd5Sopenharmony_ciimport SimManager from '../feature/sim/SimManager';
228779efd5Sopenharmony_ciimport { missedCallManager } from '../feature/missedCall/MissedCallManager';
238779efd5Sopenharmony_ciimport PresenterManager from '../presenter/PresenterManager';
248779efd5Sopenharmony_ci
258779efd5Sopenharmony_ciconst TAG = 'MainAbility ';
268779efd5Sopenharmony_ci
278779efd5Sopenharmony_ciexport default class MainAbility extends Ability {
288779efd5Sopenharmony_ci    storage: LocalStorage;
298779efd5Sopenharmony_ci    simManager: SimManager;
308779efd5Sopenharmony_ci    mDataWorker = WorkFactory.getWorker(WorkerType.DataWorker);
318779efd5Sopenharmony_ci
328779efd5Sopenharmony_ci    updateBreakpoint(windowWidth: number) {
338779efd5Sopenharmony_ci        let windowWidthVp: number = px2vp(windowWidth);
348779efd5Sopenharmony_ci        let breakpoint: string;
358779efd5Sopenharmony_ci        if (windowWidthVp < 520) {
368779efd5Sopenharmony_ci            breakpoint = 'sm';
378779efd5Sopenharmony_ci        } else if (windowWidthVp < 840) {
388779efd5Sopenharmony_ci            breakpoint = 'md';
398779efd5Sopenharmony_ci        } else {
408779efd5Sopenharmony_ci            breakpoint = 'lg';
418779efd5Sopenharmony_ci        }
428779efd5Sopenharmony_ci        this.storage.setOrCreate('breakpoint', breakpoint);
438779efd5Sopenharmony_ci    }
448779efd5Sopenharmony_ci
458779efd5Sopenharmony_ci    onRequest(want: Want, isOnCreate: boolean) {
468779efd5Sopenharmony_ci        if (!want || !want.parameters) {
478779efd5Sopenharmony_ci            return;
488779efd5Sopenharmony_ci        }
498779efd5Sopenharmony_ci        const data: any = want.parameters['missedCallData'];
508779efd5Sopenharmony_ci        const action = want.parameters['action'];
518779efd5Sopenharmony_ci        HiLog.i(TAG, `onRequest action: ${action}`);
528779efd5Sopenharmony_ci        if (action != undefined && data != undefined) {
538779efd5Sopenharmony_ci            missedCallManager.requestMissedCallAction(action, data);
548779efd5Sopenharmony_ci        }
558779efd5Sopenharmony_ci    }
568779efd5Sopenharmony_ci
578779efd5Sopenharmony_ci    onCreate(want, launchParam) {
588779efd5Sopenharmony_ci        HiLog.i(TAG, 'Application onCreate start');
598779efd5Sopenharmony_ci        globalThis.isFromOnCreate = true;
608779efd5Sopenharmony_ci        globalThis.context = this.context;
618779efd5Sopenharmony_ci        globalThis.abilityWant = want;
628779efd5Sopenharmony_ci        this.storage = new LocalStorage()
638779efd5Sopenharmony_ci        this.onRequest(want, true);
648779efd5Sopenharmony_ci        this.simManager = new SimManager();
658779efd5Sopenharmony_ci        globalThis.DataWorker = this.mDataWorker;
668779efd5Sopenharmony_ci        globalThis.presenterManager = new PresenterManager(this.context, this.mDataWorker);
678779efd5Sopenharmony_ci        globalThis.presenterManager.onCreate(want);
688779efd5Sopenharmony_ci    }
698779efd5Sopenharmony_ci
708779efd5Sopenharmony_ci    onNewWant(want, launchParam) {
718779efd5Sopenharmony_ci        HiLog.i(TAG, 'Application onNewWant');
728779efd5Sopenharmony_ci        globalThis.isFromOnCreate = false;
738779efd5Sopenharmony_ci        globalThis.abilityWant = want;
748779efd5Sopenharmony_ci        this.onRequest(want, false);
758779efd5Sopenharmony_ci        globalThis.presenterManager.onNewWant(want);
768779efd5Sopenharmony_ci    }
778779efd5Sopenharmony_ci
788779efd5Sopenharmony_ci    onDestroy() {
798779efd5Sopenharmony_ci        HiLog.i(TAG, 'Ability onDestroy');
808779efd5Sopenharmony_ci        globalThis.presenterManager.onDestroy();
818779efd5Sopenharmony_ci        this.mDataWorker.close();
828779efd5Sopenharmony_ci    }
838779efd5Sopenharmony_ci
848779efd5Sopenharmony_ci    onWindowStageCreate(windowStage: Window.WindowStage) {
858779efd5Sopenharmony_ci        // Main window is created, set main page for this ability
868779efd5Sopenharmony_ci        HiLog.i(TAG, 'Ability onWindowStageCreate');
878779efd5Sopenharmony_ci
888779efd5Sopenharmony_ci        Window.getTopWindow(this.context).then((windowObj) => {
898779efd5Sopenharmony_ci            windowObj.getProperties().then((windowProperties) => {
908779efd5Sopenharmony_ci                this.updateBreakpoint(windowProperties.windowRect.width);
918779efd5Sopenharmony_ci            })
928779efd5Sopenharmony_ci            windowObj.on('windowSizeChange', (data) => {
938779efd5Sopenharmony_ci                this.updateBreakpoint(data.width);
948779efd5Sopenharmony_ci            });
958779efd5Sopenharmony_ci        })
968779efd5Sopenharmony_ci
978779efd5Sopenharmony_ci        windowStage.loadContent(globalThis.presenterManager ? globalThis.presenterManager.mainUrl : 'pages/index', this.storage, (err, data) => {
988779efd5Sopenharmony_ci            if (err.code) {
998779efd5Sopenharmony_ci                HiLog.e(TAG, 'Failed to load the content. Cause: ' + JSON.stringify(err) ?? '');
1008779efd5Sopenharmony_ci                return;
1018779efd5Sopenharmony_ci            }
1028779efd5Sopenharmony_ci            HiLog.i(TAG, 'Succeeded in loading the content. Data: ' + JSON.stringify(data) ?? '');
1038779efd5Sopenharmony_ci        });
1048779efd5Sopenharmony_ci    }
1058779efd5Sopenharmony_ci
1068779efd5Sopenharmony_ci    onWindowStageDestroy() {
1078779efd5Sopenharmony_ci        // Main window is destroyed, release UI related resources
1088779efd5Sopenharmony_ci        HiLog.i(TAG, 'Ability onWindowStageDestroy');
1098779efd5Sopenharmony_ci    }
1108779efd5Sopenharmony_ci
1118779efd5Sopenharmony_ci    onForeground() {
1128779efd5Sopenharmony_ci        // Ability has brought to foreground
1138779efd5Sopenharmony_ci        HiLog.i(TAG, 'Ability onForeground');
1148779efd5Sopenharmony_ci        this.simManager.init();
1158779efd5Sopenharmony_ci    }
1168779efd5Sopenharmony_ci
1178779efd5Sopenharmony_ci    onBackground() {
1188779efd5Sopenharmony_ci        // Ability has back to background
1198779efd5Sopenharmony_ci        HiLog.i(TAG, 'Ability onBackground');
1208779efd5Sopenharmony_ci        this.simManager.recycle();
1218779efd5Sopenharmony_ci    }
1228779efd5Sopenharmony_ci}