• 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 FuncBtn from './FuncBtn';
17import InComDialog from './InComDialog';
18import ImagePathConst from '../constant/ImagePathConst';
19import CallStateConst from '../constant/CallStateConst';
20import CallServiceProxy from '../../model/CallServiceProxy';
21import sms from '@ohos.telephony.sms';
22import resourceManager from '@ohos.resourceManager';
23import prompt from '@system.prompt';
24import LogUtils from '../utils/LogUtils';
25import DefaultCallData from '../struct/TypeUtils';
26import CallListStruct from '../struct/CallListStruct'
27import VibrationAndProximityUtils from '../utils/VibrationAndProximityUtils';
28
29const TAG = 'IncomingCom';
30const SMS_REJECTION = `${ImagePathConst.BASE_URL}ic_public_message.svg`;
31
32/**
33 * SMS sent successfully
34 */
35const SEND_SMS_SUCCESS = 0;
36
37/**
38 * Failed to send SMS for unknown reason
39 */
40const SEND_SMS_FAILURE_UNKNOWN = 1;
41
42/**
43 * The wireless module for sending text messages is turned off
44 */
45const SEND_SMS_FAILURE_RADIO_OFF = 2;
46
47/**
48 * The network sending the text message is unavailable
49 */
50const SEND_SMS_FAILURE_SERVICE_UNAVAILABLE = 3;
51
52@Component
53export default struct IncomingCom {
54  @Link callList: Array<CallListStruct>;
55  @Link callData: DefaultCallData;
56  @Link hangup: boolean;
57  private btnList = [];
58  private msgList = [];
59  mCallServiceProxy: CallServiceProxy = null;
60  msgDialogController: CustomDialogController = new CustomDialogController ({
61    builder: InComDialog({
62      cancel: () => {},
63      confirm: (item) => {
64        this.msgItemClick(item);
65      },
66      cancelHandle: () => {},
67      // @ts-ignore
68      list: this.msgList
69    }),
70    cancel: () => {},
71    customStyle: true
72  });
73
74  aboutToAppear() {
75    this.mCallServiceProxy = CallServiceProxy.getInstance();
76    this.getMsgList();
77    this.getBtnList();
78  }
79
80  aboutToDisappear() {
81    this.msgDialogController = null;
82  }
83
84  /**
85   * send Message   slotId : card slot, destinationHost : accountNumber ,content: content
86   *
87   * @param obj
88   */
89  private sendMessage(obj) {
90    let slotId = this.callData.accountId;
91    let destinationHost = this.callData.accountNumber
92    let contactName = this.callData.contactName
93    globalThis.calluiAbilityContext?.resourceManager.getString(obj.msg.id, (err, typeName) => {
94      LogUtils.i(TAG, 'sendMessage');
95      sms.sendMessage({
96        slotId: slotId,
97        destinationHost: destinationHost,
98        content: typeName,
99        sendCallback: (err, sendResult) => {
100          if (err) {
101            return;
102          }
103          if (sendResult.result === SEND_SMS_SUCCESS) {
104            globalThis.calluiAbilityContext?.resourceManager.getString($r('app.string.SMS_Sent')
105              .id, (err, typeName) => {
106              if (err) {
107                return;
108              }
109              prompt.showToast({
110                message: contactName ? typeName + `${contactName}` : typeName + `${destinationHost}`,
111                duration: 2000,
112              });
113            })
114          } else {
115            globalThis.calluiAbilityContext?.resourceManager.getString($r('app.string.message_Failed')
116              .id, (err, typeName) => {
117              if (err) {
118                return;
119              }
120              prompt.showToast({
121                message: typeName,
122                duration: 2000,
123              });
124            })
125          }
126        }
127      });
128    })
129  }
130
131  private getMsgList() {
132    this.msgList = [
133      {
134        id: 1,
135        msg: $r('app.string.backLater')
136      },
137      {
138        id: 2,
139        msg: $r('app.string.answerThePhone')
140      },
141      {
142        id: 3,
143        msg: $r('app.string.contactMeLater')
144      },
145      {
146        id: 4,
147        msg: $r('app.string.beThereSoon')
148      },
149    ];
150  }
151
152  private getBtnList() {
153    this.btnList = [
154      {
155        type: 'msg',
156        iconText: $r('app.string.sms'),
157        iconDefaultUrl: SMS_REJECTION,
158        iconDisableUrl: SMS_REJECTION,
159        isDisable: false
160      },
161    ];
162  }
163
164  /**
165   * Call rejection interface
166   *
167   * @param {string} msg - Send SMS content
168   */
169  private msgItemClick(obj) {
170    LogUtils.i(TAG, 'msgItemClick rejectCall');
171    this.mCallServiceProxy.rejectCall(this.callData.callId, obj.msg);
172    this.sendMessage(obj);
173  }
174
175  /**
176   * Handling interface call hangup and rejection
177   */
178  private async onReject() {
179    if (this.callList.length > 1) {
180      const incomingData = this.callList.find((v) => v.callState === CallStateConst.callStateObj.CALL_STATUS_WAITING ||
181        v.callState === CallStateConst.callStateObj.CALL_STATUS_INCOMING);
182      Object.assign(this.callData, {
183        ...incomingData
184      });
185    }
186    const {callId, callState} = this.callData;
187    if (callState !== CallStateConst.CALL_STATUS_WAITING) {
188      this.mCallServiceProxy.rejectCall(callId);
189      LogUtils.i(TAG, 'onReject this.mCallServiceProxy.rejectCall :');
190    } else {
191      this.mCallServiceProxy.hangUpCall(callId);
192      LogUtils.i(TAG, 'onReject this.mCallServiceProxy.hangUpCall :');
193    }
194    if (this.callList.length === 1) {
195      this.hangup = true;
196      globalThis.calluiAbilityContext?.terminateSelf().then((data) => {
197        LogUtils.i(TAG, 'onReject terminateSelfCallBack');
198      });
199    }
200  }
201
202  /**
203   * Enable the SMS reply pop-up
204   */
205  private btnClick(type) {
206    LogUtils.i(TAG, 'btnClick : %s' + JSON.stringify(type));
207    if (type === 'msg') {
208      this.msgDialogController.open();
209    }
210  }
211
212  /**
213   * Answer the phone interface
214   */
215  private onAnswer() {
216    LogUtils.i(TAG, 'onAnswer :');
217    const incomingData = this.callList.find((v) => v.callState === CallStateConst.callStateObj.CALL_STATUS_WAITING ||
218      v.callState === CallStateConst.callStateObj.CALL_STATUS_INCOMING);
219    if (incomingData !== undefined) {
220      this.mCallServiceProxy.acceptCall(incomingData.callId);
221    } else {
222      this.mCallServiceProxy.acceptCall(this.callData.callId);
223    }
224    VibrationAndProximityUtils.suspendScreen();
225    VibrationAndProximityUtils.stopVibration();
226  }
227
228  build() {
229    GridRow({ columns: { sm: 4, md: 8, lg: 12 }, gutter: 24 }) {
230      GridCol({ span: { sm: 4, md: 6, lg: 6 }, offset: { sm: 0, md: 1, lg: 3 } }) {
231        Column() {
232          Row() {
233            ForEach(this.btnList, (item) => {
234              Column() {
235                FuncBtn({
236                  btnType: item.type,
237                  iconDisableUrl: item.iconDefaultUrl,
238                  iconDefaultUrl: item.iconDefaultUrl,
239                  iconText: item.iconText,
240                  isDisable: item.isDisable,
241                  btnClick: (type) => {
242                    this.btnClick(type)
243                  }
244                })
245              }
246            })
247          }
248          .height(51)
249
250          Row() {
251            Column() {
252              Image($r('app.media.ic_public_ring_off'))
253                .width(56)
254                .height(56)
255                .onClick(() => {
256                  this.onReject()
257                })
258            }.layoutWeight(1)
259
260            Column() {
261              Image($r('app.media.ic_public_answer'))
262                .width(56)
263                .height(56)
264                .onClick(() => {
265                  this.onAnswer()
266                })
267            }.layoutWeight(1)
268          }
269          .margin({ top: 32 })
270        }
271      }
272    }
273    .width('100%')
274    .margin({ left: 24, right: 24 })
275  }
276}
277