• 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 { hilog } from '@kit.PerformanceAnalysisKit';
16import { BusinessError, commonEventManager } from '@kit.BasicServicesKit';
17import { abilityDelegatorRegistry } from '@kit.TestKit';
18
19@Entry
20@Component
21struct HostInstance {
22  aboutToAppear() {
23    hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility SameIst aboutToAppear');
24
25  }
26
27  @State message: string = 'ni'
28  @State flag: boolean = false
29  @State hostSpecified: string = ''
30  private want1: Want = {
31    bundleName: 'com.example.act_embeddeduiextension',
32    abilityName: 'EmbeddedUIExtAbilityInstance',
33    parameters: {}
34  }
35  private want: Want = {
36    bundleName: 'com.example.act_embeddeduiextension',
37    abilityName: 'EntryEmbeddedUIExtAbility4',
38    parameters: {
39      'ohos.extension.processMode.hostSpecified': `${this.hostSpecified}`
40    }
41  }
42
43  build() {
44    Row() {
45      Column() {
46        Text(this.message).fontSize(30)
47        EmbeddedComponent(this.want1, EmbeddedType.EMBEDDED_UI_EXTENSION)
48          .onTerminated((info: TerminationInfo) => {
49            this.message = 'Terminarion: code = ' + info.code + ', want = ' + JSON.stringify(info.want);
50          })
51          .onError((error: BusinessError) => {
52          })
53        Button()
54          .onClick(() => {
55            this.flag = true
56            let abilityDelegator: abilityDelegatorRegistry.AbilityDelegator;
57            let cmd = 'aa dump -r';
58            abilityDelegator = abilityDelegatorRegistry.getAbilityDelegator();
59            abilityDelegator.executeShellCommand(cmd, (error: BusinessError, data) => {
60              if (error) {
61                hilog.info(0x0000, this.message, '%{public}s',
62                  `executeShellCommand fail, error: ${JSON.stringify(error)}`);
63              } else {
64                hilog.info(0x0000, this.message, '%{public}s',
65                  `executeShellCommand fail, executeShellCommand success, data: ${JSON.stringify(data)}`);
66                let result = JSON.stringify(data)
67                const regex = /com\.example\.act_embeddeduiextension:EmbeddedUIExtAbilityInstance:\d+/;
68                const match = result.match(regex);
69                if (match) {
70                  console.log('Matched string:', match[0]);
71                  this.hostSpecified = match[0]
72                  console.log('sss', typeof match[0])
73                } else {
74                  console.log('No match found');
75                }
76              }
77            });
78          })
79          .id('EmbeddedUIExtension_4600')
80        if (this.flag) {
81          EmbeddedComponent(this.want, EmbeddedType.EMBEDDED_UI_EXTENSION)
82            .onTerminated((info: TerminationInfo) => {
83              this.message = 'Terminarion: code = ' + info.code + ', want = ' + JSON.stringify(info.want);
84            })
85            .onError((error: BusinessError) => {
86              let commonEventPublishData: commonEventManager.CommonEventPublishData = {
87                parameters: {
88                  data: error.code
89                }
90              }
91              commonEventManager.publish('Start_Fail', commonEventPublishData, async (error) => {
92                hilog.info(0x0000, 'testTag1', '%{public}s', `HostInstancesdas publish: ${JSON.stringify(error)}`);
93              })
94              this.message = 'Error: code = ' + error.code;
95            })
96        }
97      }
98      .width('100%')
99      .height('100%')
100      .justifyContent(FlexAlign.Center)
101    }
102    .alignItems(VerticalAlign.Center)
103    .width('100%')
104    .height('100%')
105  }
106}