• 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 UIAbility from '@ohos.app.ability.UIAbility';
18import type Want from '@ohos.app.ability.Want';
19import hilog from '@ohos.hilog';
20import type rpc from '@ohos.rpc';
21import type window from '@ohos.window';
22import type { Caller } from '@ohos.app.ability.UIAbility';
23
24const TAG: string = '[CalleeAbility]';
25const MSG_SEND_METHOD: string = 'CallSendMsg';
26const DOMAIN_NUMBER: number = 0xFF00;
27
28class MyParcelable {
29  num: number = 0;
30  str: string = '';
31
32  constructor(num: number, string: string) {
33    this.num = num;
34    this.str = string;
35  };
36
37  mySequenceable(num, string): void {
38    this.num = num;
39    this.str = string;
40  };
41
42  marshalling(messageSequence: rpc.MessageSequence): boolean {
43    messageSequence.writeInt(this.num);
44    messageSequence.writeString(this.str);
45    return true;
46  };
47
48  unmarshalling(messageSequence: rpc.MessageSequence): boolean {
49    this.num = messageSequence.readInt();
50    this.str = messageSequence.readString();
51    return true;
52  };
53};
54
55function sendMsgCallback(data: rpc.MessageSequence): rpc.Parcelable {
56  hilog.info(DOMAIN_NUMBER, TAG, '%{public}s', 'CalleeSortFunc called');
57
58  // 获取Caller发送的序列化数据
59  let receivedData: MyParcelable = new MyParcelable(0, '');
60  data.readParcelable(receivedData);
61  hilog.info(DOMAIN_NUMBER, TAG, '%{public}s', `receiveData[${receivedData.num}, ${receivedData.str}]`);
62  let num: number = receivedData.num;
63
64  // 作相应处理
65  // 返回序列化数据result给Caller
66  return new MyParcelable(num + 1, `send ${receivedData.str} succeed`) as rpc.Parcelable;
67};
68
69export default class CalleeAbility extends UIAbility {
70  caller: Caller | undefined;
71
72  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
73    try {
74      this.callee.on(MSG_SEND_METHOD, sendMsgCallback);
75    } catch (error) {
76      hilog.error(DOMAIN_NUMBER, TAG, '%{public}s', `Failed to register. Error is ${error}`);
77    };
78  };
79
80  async onButtonCall(): Promise<void> {
81    try {
82      let msg: MyParcelable = new MyParcelable(1, 'origin_Msg');
83      if (this.caller) {
84        await this.caller.call(MSG_SEND_METHOD, msg);
85      }
86    } catch (error) {
87      hilog.info(DOMAIN_NUMBER, TAG, `caller call failed with ${error}`);
88    };
89  };
90
91  async onButtonCallWithResult(originMsg: string, backMsg: string): Promise<void> {
92    try {
93      let msg: MyParcelable = new MyParcelable(1, originMsg);
94      if (this.caller) {
95        const data = await this.caller.callWithResult(MSG_SEND_METHOD, msg);
96        hilog.info(DOMAIN_NUMBER, TAG, 'caller callWithResult succeed');
97        let result: MyParcelable = new MyParcelable(0, '');
98        data.readParcelable(result);
99        backMsg = result.str;
100        hilog.info(DOMAIN_NUMBER, TAG, `caller result is [${result.num}, ${result.str}]`);
101      }
102    } catch (error) {
103      hilog.info(DOMAIN_NUMBER, TAG, `caller callWithResult failed with ${error}`);
104    };
105  };
106
107  releaseCall(): void {
108    try {
109      if (this.caller) {
110        this.caller.release();
111        this.caller = undefined;
112      }
113      hilog.info(DOMAIN_NUMBER, TAG, 'caller release succeed');
114    } catch (error) {
115      hilog.info(DOMAIN_NUMBER, TAG, `caller release failed with ${error}`);
116    };
117  };
118
119  onWindowStageCreate(windowStage: window.WindowStage): void {
120    // 设置UI加载
121    windowStage.loadContent('pages/Page_CalleeAbility', (err, data) => {
122    });
123  };
124
125  onDestroy(): void {
126    try {
127      this.callee.off(MSG_SEND_METHOD);
128      hilog.info(DOMAIN_NUMBER, TAG, '%{public}s', 'Callee OnDestroy');
129      this.releaseCall();
130    } catch (error) {
131      hilog.error(DOMAIN_NUMBER, TAG, '%{public}s', `Failed to register. Error is ${error}`);
132    };
133  };
134};
135