• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2024 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 */
15import { AbilityConstant, UIAbility, Want, common, AbilityLifecycleCallback, wantConstant } from '@kit.AbilityKit';
16import { hilog } from '@kit.PerformanceAnalysisKit';
17import { window } from '@kit.ArkUI';
18import { BusinessError, commonEventManager } from '@kit.BasicServicesKit';
19
20
21export default class EntryAbility extends UIAbility {
22  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
23    console.log('p1test')
24    let funcAbilityWant = want;
25    let info = funcAbilityWant?.parameters?.key;
26    console.log('===111==='+JSON.stringify(want))
27    console.log('===222==='+JSON.stringify(info))
28    console.log(`onCreate, want parameters: ${want.parameters?.developerParameters}`);
29    let commonEventPublishData: commonEventManager.CommonEventPublishData = {
30      parameters: {
31        'callerInfo':want,
32      }
33    }
34    commonEventManager.publish('ability_onCreat', commonEventPublishData, async (error) =>{
35      hilog.info(0x0000, 'testTag1', '%{public}s',
36        `onCreat publish: ${JSON.stringify(error)} data: ${JSON.stringify(commonEventPublishData)}`);
37    })
38
39    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
40  }
41
42  onDestroy(): void {
43    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
44  }
45
46  onWindowStageCreate(windowStage: window.WindowStage): void {
47    // Main window is created, set main page for this ability
48    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
49
50    windowStage.loadContent('pages/Index', (err) => {
51      if (err.code) {
52        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
53        return;
54      }
55      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
56    });
57  }
58
59  onWindowStageDestroy(): void {
60    // Main window is destroyed, release UI related resources
61    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
62  }
63
64  onForeground(): void {
65    let commonEventPublishData: commonEventManager.CommonEventPublishData = {
66      parameters: {
67        date:'111'
68      }
69    }
70    commonEventManager.publish('ability_onForegroundCallBack', commonEventPublishData, async (error) =>{
71      hilog.info(0x0000, 'testTag1', '%{public}s',
72        `onForeground publish: ${JSON.stringify(error)} data: ${JSON.stringify(commonEventPublishData)}`);
73    })
74    let want: Want = {
75      'deviceId': '',
76      'bundleName': '',
77      'abilityName': '',
78      'type': '',
79      'action': '111',
80      'entities':[]
81    }
82    try{
83      this.context.startAbility(want,(err:BusinessError) => {
84        if(err.code){
85          console.log('error====75===:' + JSON.stringify(err));
86        } else {
87          console.log('start ability successfully')
88        }
89      })
90    } catch (err){
91      let code = (err as BusinessError).code;
92      let message = (err as BusinessError).message;
93      console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`);
94    }
95    // Ability has brought to foreground
96    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
97
98  }
99
100  onBackground(): void {
101    // Ability has back to background
102    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
103  }
104
105  onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) {
106    let funcAbilityWant = want;
107    let info = funcAbilityWant?.parameters?.key;
108    console.log('===333==='+JSON.stringify(want))
109    console.log('===444==='+JSON.stringify(info))
110    console.log(`onCreate, want parameters: ${want.parameters?.developerParameters}`);
111    console.log('AbilityLifecycleCallback onNewWant');
112    let commonEventPublishData: commonEventManager.CommonEventPublishData = {
113      parameters: {
114        'callerInfo':want,
115      }
116    }
117    commonEventManager.publish('ability_onNewWant', commonEventPublishData, async (error) =>{
118      hilog.info(0x0000, 'testTag1', '%{public}s',
119        `onNewWant publish: ${JSON.stringify(error)} data: ${JSON.stringify(commonEventPublishData)}`);
120    })
121  }
122}