• 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 hilog from '@ohos.hilog';
17import Ability from '@ohos.application.Ability'
18import Window from '@ohos.window'
19
20const PERMISSIONS: Array<string> = [
21  'ohos.permission.READ_MEDIA',
22  'ohos.permission.WRITE_MEDIA',
23  'ohos.permission.CAPTURE_SCREEN']
24
25export default class MainAbility extends Ability {
26  onCreate(want, launchParam) {
27    hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
28    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
29    hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? '');
30    hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? '');
31    this.context.requestPermissionsFromUser(PERMISSIONS)
32  }
33
34  onDestroy() {
35    hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
36    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
37  }
38
39  onWindowStageCreate(windowStage: Window.WindowStage) {
40    // Main window is created, set main page for this ability
41    hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
42    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
43
44    windowStage.loadContent('pages/homePage', (err, data) => {
45      if (err.code) {
46        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.ERROR);
47        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
48        return;
49      }
50      hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
51      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
52    });
53  }
54
55  onWindowStageDestroy() {
56    // Main window is destroyed, release UI related resources
57    hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
58    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
59  }
60
61  onForeground() {
62    // Ability has brought to foreground
63    hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
64    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
65  }
66
67  onBackground() {
68    // Ability has back to background
69    hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
70    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
71  }
72}
73