• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 Logger from '../utils/Logger';
16import Caller from '../bean/Caller';
17import Constants from '../Constants';
18import commonEvent from '@ohos.commonEventManager';
19import Want from '@ohos.app.ability.Want';
20
21const TAG = 'OptionModel';
22
23const callData: Array<Caller> = [{
24  index: 0x01,
25  name: $r('app.string.name1'),
26  head: $r('app.string.head1')
27}, {
28  index: 0x02,
29  name: $r('app.string.name2'),
30  head: $r('app.string.head2')
31}];
32
33// 配合测试,默认打开被呼叫页
34const DEFAULT_EVENT = Constants.EVENT_UI_ANSWER;
35
36export declare interface OptCallback {
37  //对端接听
38  onRemoteAnswer();
39
40  //对端挂断/拒绝
41  onRemoteHangUp();
42}
43
44
45class OptionModel {
46  private mSubscriber: commonEvent.CommonEventSubscriber | null = null;
47  private mCallback: OptCallback | null = null;
48  private caller: Caller = callData[0]; // 配合测试,默认用户
49
50  getCaller(): Caller {
51    return this.caller;
52  }
53
54  public readWant(want: Want): number {
55    if (!want.parameters) {
56      return DEFAULT_EVENT;
57    }
58    let event = want.parameters[Constants.START_ABILITY_EVENT_KEY];
59    let data: number = want.parameters[Constants.START_ABILITY_DATA_KEY] as number;
60    if ((!event) || !data) {
61      Logger.log(TAG, `readWant param empty`);
62      return DEFAULT_EVENT;
63    }
64    this.caller = callData[data-1];
65    if (event === Constants.START_ABILITY_EVENT_CALL) {
66      return Constants.EVENT_UI_CALL;
67    } else if (event === Constants.START_ABILITY_EVENT_ANSWER) {
68      return Constants.EVENT_UI_ANSWER;
69    }
70    return DEFAULT_EVENT;
71  }
72
73  public async subscriber(): Promise<void> {
74
75    this.mSubscriber = await commonEvent.createSubscriber({
76      events: [
77        Constants.COMM_EVENT_ANSWER_FROM_HOST,
78        Constants.COMM_EVENT_HANGUP_FROM_HOST,
79      ]
80    });
81
82    commonEvent.subscribe(this.mSubscriber, (err, data) => {
83      if (err) {
84        Logger.error(TAG, `subscribe common event failed ${JSON.stringify(err)}`);
85        return;
86      }
87      this.onRemoteEvent(data.event);
88    });
89  }
90
91  // 注册监听
92  public registerCallback(callback: OptCallback): void {
93    this.mCallback = callback;
94  }
95
96  private onRemoteEvent(event: string) {
97    Logger.log(TAG,` onRemoteEvent ${event}`);
98    if (this.mCallback == null) {
99      return;
100    }
101    switch (event) {
102      case Constants.COMM_EVENT_ANSWER_FROM_HOST: //接听
103        this.mCallback.onRemoteAnswer();
104        break;
105      case Constants.COMM_EVENT_HANGUP_FROM_HOST: //挂断 / 拒接
106        this.mCallback.onRemoteHangUp();
107        break;
108    }
109  }
110
111  // 发送事件
112  public async answer(): Promise<void> {
113    Logger.info(TAG,`answer`);
114    commonEvent.publish(Constants.COMM_EVENT_ANSWER_FROM_VOICE, (err) => {
115      if (err) {
116        Logger.error(TAG,`publish common event failed ${JSON.stringify(err)}`);
117        return;
118      }
119    });
120  }
121
122  public async hangUp(): Promise<void> {
123    Logger.info(TAG,`hangUp`);
124    commonEvent.publish(Constants.COMM_EVENT_HANGUP_FROM_VOICE, (err) => {
125      if (err) {
126        Logger.error(TAG,`publish common event failed ${JSON.stringify(err)}`);
127        return;
128      }
129    });
130  }
131
132  //销毁
133  public destroy(): void {
134    try {
135      if (this.mSubscriber) {
136        commonEvent.unsubscribe(this.mSubscriber);
137      }
138    } catch (err) {
139      Logger.error(TAG,`destroy common event failed ${JSON.stringify(err)}`);
140    }
141    this.mCallback = null;
142  }
143}
144
145let mOptionModel = new OptionModel();
146
147export default mOptionModel as OptionModel;