• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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';
24import dataShare from '@ohos.data.dataShare';
25import settings from '@ohos.settings';
26import DataShareHelper from "@ohos.data.dataShare";
27const SETTING_TIME_FORMAT_URI ='dataShare:///com.phos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=' +settings.date.TIME_FORMAT;
28const TAG = 'MainAbility ';
29
30export default class MainAbility extends Ability {
31    storage: LocalStorage;
32    simManager: SimManager;
33    mDataWorker = WorkFactory.getWorker(WorkerType.DataWorker);
34    settingDataShareHelper: DataShareHelper.DataShareHelper = null;
35
36    updateBreakpoint(windowWidth: number) {
37        let windowWidthVp: number = px2vp(windowWidth);
38        let breakpoint: string;
39        if (windowWidthVp < 520) {
40            breakpoint = 'sm';
41        } else if (windowWidthVp < 840) {
42            breakpoint = 'md';
43        } else {
44            breakpoint = 'lg';
45        }
46        this.storage.setOrCreate('breakpoint', breakpoint);
47    }
48
49    onRequest(want: Want, isOnCreate: boolean) {
50        if (!want || !want.parameters) {
51            return;
52        }
53        const data: any = want.parameters["missedCallData"];
54        const action = want.parameters["action"];
55        HiLog.i(TAG, `onRequest action: ${action}`);
56        if (action != undefined && data != undefined) {
57            missedCallManager.requestMissedCallAction(action, data);
58        }
59    }
60
61    onCreate(want, launchParam) {
62        HiLog.i(TAG, 'Application onCreate start');
63        globalThis.isFromOnCreate = true;
64        globalThis.context = this.context;
65        globalThis.abilityWant = want;
66        this.storage = new LocalStorage()
67        this.onRequest(want, true);
68        this.simManager = new SimManager();
69        globalThis.DataWorker = this.mDataWorker;
70        globalThis.presenterManager = new PresenterManager(this.context, this.mDataWorker);
71        globalThis.presenterManager.onCreate(want);
72        this.subscribeTimeFormatChange();
73    }
74
75    onNewWant(want, launchParam) {
76        HiLog.i(TAG, 'Application onNewWant');
77        globalThis.isFromOnCreate = false;
78        globalThis.abilityWant = want;
79        this.onRequest(want, false);
80        globalThis.presenterManager.onNewWant(want);
81    }
82
83    subscribeTimeFormatChange(): void{
84        HiLog.i(TAG, 'subscribeTimeFormatChange');
85        try {
86            dataShare.createDataShareHelper(globalThis.context, SETTING_TIME_FORMAT_URI).then((data) => {
87                HiLog.i(TAG, 'createDataShareHelper succeed');
88                this.settingDataShareHelper = data;
89                this.settingDataShareHelper.on("dataChange", SETTING_TIME_FORMAT_URI, () => {
90                    let timeFormat = settings.getValueSync(globalThis.context, settings.date.TIME_FORMAT, "24");
91                    HiLog.i(TAG, 'refreshTimeFormat: ' + timeFormat);
92                    AppStorage.setOrCreate("timeFormat", timeFormat);
93                });
94            }).catch((err) => {
95                HiLog.e(TAG, `createDataShareHelper error: code: ${err.code}, message: ${err.message} `);
96            });
97        } catch (err) {
98            HiLog.e(TAG, `createDataShareHelper error: code: ${err.code}, message: ${err.message} `);
99        }
100    }
101
102    onDestroy() {
103        HiLog.i(TAG, 'Ability onDestroy');
104        try {
105            if (this.settingDataShareHelper !== null) {
106                this.settingDataShareHelper.off("dataChange", SETTING_TIME_FORMAT_URI);
107            }
108        } catch (err) {
109            HiLog.e(TAG, `settingDataShareHelper.off error: code: ${err.code}, message: ${err.message} `);
110        }
111        globalThis.presenterManager.onDestroy();
112        this.mDataWorker.close();
113    }
114
115
116    onWindowStageCreate(windowStage: Window.WindowStage) {
117        // Main window is created, set main page for this ability
118        HiLog.i(TAG, 'Ability onWindowStageCreate');
119
120        Window.getTopWindow(this.context).then((windowObj) => {
121            windowObj.getProperties().then((windowProperties) => {
122                this.updateBreakpoint(windowProperties.windowRect.width);
123            })
124            windowObj.on('windowSizeChange', (data) => {
125                this.updateBreakpoint(data.width);
126            });
127        })
128
129        windowStage.loadContent(globalThis.presenterManager ? globalThis.presenterManager.mainUrl : 'pages/index', this.storage, (err, data) => {
130            if (err.code) {
131                HiLog.e(TAG, 'Failed to load the content. Cause: ' + JSON.stringify(err) ?? '');
132                return;
133            }
134            HiLog.i(TAG, 'Succeeded in loading the content. Data: ' + JSON.stringify(data) ?? '');
135        });
136    }
137
138    onWindowStageDestroy() {
139        // Main window is destroyed, release UI related resources
140        HiLog.i(TAG, 'Ability onWindowStageDestroy');
141    }
142
143    onForeground() {
144        // Ability has brought to foreground
145        HiLog.i(TAG, 'Ability onForeground');
146        this.simManager.init();
147    }
148
149    onBackground() {
150        // Ability has back to background
151        HiLog.i(TAG, 'Ability onBackground');
152        this.simManager.recycle();
153    }
154}