• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 type AbilityConstant from '@ohos.app.ability.AbilityConstant';
17import formBindingData from '@ohos.app.form.formBindingData';
18import formProvider from '@ohos.app.form.formProvider';
19import hilog from '@ohos.hilog';
20import type rpc from '@ohos.rpc';
21import UIAbility from '@ohos.app.ability.UIAbility';
22import type Want from '@ohos.app.ability.Want';
23import type window from '@ohos.window';
24
25const TAG: string = 'WidgetCalleeEntryAbility';
26const DOMAIN_NUMBER: number = 0xFF00;
27const MSG_SEND_METHOD: string = 'funA';
28const CONST_NUMBER_1: number = 1;
29
30class MyParcelable implements rpc.Parcelable {
31  num: number;
32  str: string;
33
34  constructor(num: number, str: string) {
35    this.num = num;
36    this.str = str;
37  };
38
39  marshalling(messageSequence: rpc.MessageSequence): boolean {
40    messageSequence.writeInt(this.num);
41    messageSequence.writeString(this.str);
42    return true;
43  };
44
45  unmarshalling(messageSequence: rpc.MessageSequence): boolean {
46    this.num = messageSequence.readInt();
47    this.str = messageSequence.readString();
48    return true;
49  };
50}
51
52// 在收到call事件后会触发callee监听的方法
53let funACall = (data: rpc.MessageSequence): MyParcelable => {
54  // 获取call事件中传递的所有参数
55  let params: Record<string, string> = JSON.parse(data.readString());
56  if (params.formId !== undefined) {
57    let curFormId: string = params.formId;
58    let message: string = params.calleeDetail;
59    hilog.info(DOMAIN_NUMBER, TAG, `UpdateForm formId: ${curFormId}, message: ${message}`);
60    let formData: Record<string, string> = {
61      calleeDetail: message
62    };
63    let formMsg: formBindingData.FormBindingData = formBindingData.createFormBindingData(formData);
64    formProvider.updateForm(curFormId, formMsg).then((data) => {
65      hilog.info(DOMAIN_NUMBER, TAG, `updateForm success. ${JSON.stringify(data)}`);
66    }).catch((error) => {
67      hilog.error(DOMAIN_NUMBER, TAG, `updateForm failed: ${JSON.stringify(error)}`);
68    });
69  }
70  return new MyParcelable(CONST_NUMBER_1, 'aaa');
71};
72
73export default class WidgetCalleeEntryAbility extends UIAbility {
74  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
75    try {
76      // 监听call事件所需的方法
77      this.callee.on(MSG_SEND_METHOD, funACall);
78    } catch (error) {
79      hilog.error(DOMAIN_NUMBER, TAG, `${MSG_SEND_METHOD} register failed with error ${JSON.stringify(error)}`);
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}