• 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 { contactDataShareUtil, logger } from '@ohos/common'
19
20const TAG: string = 'MainAbility'
21
22export default class MainAbility extends Ability {
23  onCreate(want, launchParam) {
24    logger.info(TAG, 'onCreate')
25    globalThis.context = this.context
26  }
27
28  onDestroy() {
29    logger.info(TAG, 'onDestroy')
30  }
31
32  onWindowStageCreate(windowStage: Window.WindowStage) {
33    // Main window is created, set main page for this ability
34    logger.info(TAG, 'onWindowStageCreate')
35    this.loadContent(windowStage)
36  }
37
38  loadContent = async (windowStage: Window.WindowStage) => {
39    await contactDataShareUtil.createDataShareHelper(this.context)
40    windowStage.loadContent('pages/Home', (err, data) => {
41      if (err.code) {
42        logger.info(TAG, 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '')
43        return;
44      }
45      logger.info(TAG, 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '')
46    });
47  }
48
49  onWindowStageDestroy() {
50    // Main window is destroyed, release UI related resources
51    logger.info(TAG, 'onWindowStageDestroy')
52  }
53
54  onForeground() {
55    // Ability has brought to foreground
56    logger.info(TAG, 'onForeground')
57  }
58
59  onBackground() {
60    // Ability has back to background
61    logger.info(TAG, 'onBackground')
62  }
63}
64