• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2025 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
16// [Start widget_callee_entry_ability]
17import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
18import { window } from '@kit.ArkUI';
19import { BusinessError } from '@kit.BasicServicesKit';
20import { formBindingData, formProvider } from '@kit.FormKit';
21import { rpc } from '@kit.IPCKit';
22import { hilog } from '@kit.PerformanceAnalysisKit';
23
24const TAG: string = 'WidgetCalleeEntryAbility';
25const DOMAIN_NUMBER: number = 0xFF00;
26const MSG_SEND_METHOD: string = 'funA';
27const CONST_NUMBER_1: number = 1;
28
29class MyParcelable implements rpc.Parcelable {
30  num: number;
31  str: string;
32
33  constructor(num: number, str: string) {
34    this.num = num;
35    this.str = str;
36  };
37
38  marshalling(messageSequence: rpc.MessageSequence): boolean {
39    messageSequence.writeInt(this.num);
40    messageSequence.writeString(this.str);
41    return true;
42  };
43
44  unmarshalling(messageSequence: rpc.MessageSequence): boolean {
45    this.num = messageSequence.readInt();
46    this.str = messageSequence.readString();
47    return true;
48  };
49}
50
51// 在收到call事件后会触发callee监听的方法
52let funACall = (data: rpc.MessageSequence): MyParcelable => {
53  // 获取call事件中传递的所有参数
54  let params: Record<string, string> = JSON.parse(data.readString());
55  if (params.formId !== undefined) {
56    let curFormId: string = params.formId;
57    let message: string = params.calleeDetail;
58    hilog.info(DOMAIN_NUMBER, TAG, `UpdateForm formId: ${curFormId}, message: ${message}`);
59    let formData: Record<string, string> = {
60      'calleeDetail': message
61    };
62    let formMsg: formBindingData.FormBindingData = formBindingData.createFormBindingData(formData);
63    formProvider.updateForm(curFormId, formMsg).then((data) => {
64      hilog.info(DOMAIN_NUMBER, TAG, `updateForm success. ${JSON.stringify(data)}`);
65    }).catch((error: BusinessError) => {
66      hilog.error(DOMAIN_NUMBER, TAG, `updateForm failed: ${JSON.stringify(error)}`);
67    });
68  }
69  return new MyParcelable(CONST_NUMBER_1, 'aaa');
70};
71
72export default class WidgetCalleeEntryAbility extends UIAbility {
73  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
74    try {
75      // 监听call事件所需的方法
76      this.callee.on(MSG_SEND_METHOD, funACall);
77    } catch (error) {
78      hilog.error(DOMAIN_NUMBER, TAG, `${MSG_SEND_METHOD} register failed with error ${JSON.stringify(error)}`);
79    }
80    ;
81  }
82
83  onWindowStageCreate(windowStage: window.WindowStage): void {
84    // Main window is created, set main page for this ability
85    hilog.info(DOMAIN_NUMBER, TAG, '%{public}s', 'Ability onWindowStageCreate');
86
87    windowStage.loadContent('pages/Index', (err, data) => {
88      if (err.code) {
89        hilog.error(DOMAIN_NUMBER, TAG, 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
90        return;
91      }
92      hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
93    });
94  }
95}
96// [End widget_callee_entry_ability]