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