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 onRemoteHangUp(); 41} 42 43 44class OptionModel { 45 private mSubscriber; 46 private mCallback: OptCallback; 47 private caller: Caller = callData[0]; // 配合测试,默认用户 48 49 getCaller(): Caller { 50 return this.caller; 51 } 52 53 public readWant(): number { 54 let want: Want = globalThis.abilityWant; 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, code is ${err.code}, message is ${err.message}`); 85 return; 86 } 87 this.onRemoteEvent(data.event); 88 }); 89 } 90 91 92 // 注册监听 93 public registerCallback(callback: OptCallback): void { 94 this.mCallback = callback; 95 } 96 97 private onRemoteEvent(event: string) { 98 Logger.log(`${TAG} onRemoteEvent ${event}`); 99 switch (event) { 100 case Constants.COMM_EVENT_ANSWER_FROM_HOST: //接听 101 this.mCallback.onRemoteAnswer(); 102 break; 103 case Constants.COMM_EVENT_HANGUP_FROM_HOST: //挂断 / 拒接 104 this.mCallback.onRemoteHangUp(); 105 break; 106 } 107 } 108 109 110 // 发送事件 111 public async answer(): Promise<void> { 112 Logger.info(`${TAG} answer`); 113 commonEvent.publish(Constants.COMM_EVENT_ANSWER_FROM_VOICE, (err) => { 114 if (err) { 115 Logger.error(`${TAG}: publish common event failed, code is ${err.code}, message is ${err.message}`); 116 return; 117 } 118 }); 119 } 120 121 public async hangUp(): Promise<void> { 122 Logger.info(`${TAG} hangUp`); 123 commonEvent.publish(Constants.COMM_EVENT_HANGUP_FROM_VOICE, (err) => { 124 if (err) { 125 Logger.error(`${TAG}: publish common event failed, code is ${err.code}, message is ${err.message}`); 126 return; 127 } 128 }); 129 } 130 131 //销毁 132 public destroy(): void { 133 try { 134 commonEvent.unsubscribe(this.mSubscriber); 135 } catch (err) { 136 137 } 138 this.mCallback = null; 139 } 140} 141 142let mOptionModel = new OptionModel(); 143 144export default mOptionModel as OptionModel;