• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2022 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 GlobalThisHelper from '../utils/GlobalThisHelper';
26import Constants from '../utils/Constants';
27
28const TAG = "IncomingCom";
29const SMS_REJECTION = `${ImagePathConst.BASE_URL}ic_public_message.svg`;
30const SMS_REMINDER = `${ImagePathConst.BASE_URL}remindDisabled.png`;
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 callData: any;
55  private btnList = [];
56  private msgList = [];
57  mCallServiceProxy: CallServiceProxy = null;
58  msgDialogController: CustomDialogController = new CustomDialogController
59  ({
60    builder: InComDialog({
61      cancel: () => {
62      },
63      confirm: (item) => {
64        this.msgItemClick(item);
65      },
66      cancelHandle: () => {
67      },
68      // @ts-ignore
69      List: this.msgList
70    }),
71    cancel: () => {
72    },
73    customStyle: true
74  });
75
76  aboutToAppear() {
77    this.mCallServiceProxy = CallServiceProxy.getInstance();
78    this.getMsgList();
79    this.getBtnList();
80  }
81
82  /**
83   * send Message   slotId : card slot, destinationHost : accountNumber ,content: content
84   *
85   * @param obj
86   */
87  private sendMessage(obj) {
88    let slotId = this.callData.accountId
89    let destinationHost = this.callData.accountNumber
90    let contactName = this.callData.contactName
91    GlobalThisHelper.get<any>(Constants.GLOBALTHIS_CONTEXT)?.resourceManager.getString(obj.msg.id, (err, typeName) => {
92      LogUtils.i(TAG, "sendMessage");
93      sms.sendMessage({
94        slotId: slotId,
95        destinationHost: destinationHost,
96        content: typeName,
97        sendCallback: (err, sendResult) => {
98          if (err) {
99            return;
100          }
101          if (sendResult.result === SEND_SMS_SUCCESS) {
102            GlobalThisHelper.get<any>(Constants.GLOBALTHIS_CONTEXT)?.resourceManager.getString($r("app.string.SMS_Sent")
103              .id, (err, typeName) => {
104              if (err) {
105                return;
106              }
107              prompt.showToast({
108                message: contactName ? typeName + `${contactName}` : typeName + `${destinationHost}`,
109                duration: 2000,
110              });
111            })
112          } else {
113            GlobalThisHelper.get<any>(Constants.GLOBALTHIS_CONTEXT)?.resourceManager.getString($r("app.string.message_Failed").id, (err, typeName) => {
114              if (err) {
115                return;
116              }
117              prompt.showToast({
118                message: typeName,
119                duration: 2000,
120              });
121            })
122          }
123        }
124      });
125    })
126  }
127
128  private getMsgList() {
129    this.msgList = [
130      {
131        id: 1,
132        msg: $r("app.string.backLater")
133      },
134      {
135        id: 2,
136        msg: $r("app.string.answerThePhone")
137      },
138      {
139        id: 3,
140        msg: $r("app.string.contactMeLater")
141      },
142      {
143        id: 4,
144        msg: $r("app.string.beThereSoon")
145      },
146    ];
147  }
148
149  private getBtnList() {
150    this.btnList = [
151      {
152        type: 'msg',
153        iconText: $r("app.string.sms"),
154        iconDefaultUrl: SMS_REJECTION,
155        iconDisableUrl: SMS_REJECTION,
156        isDisable: false
157      },
158
159    ];
160  }
161
162  /**
163   * Call rejection interface
164   *
165   * @param {string} msg - Send SMS content
166   */
167  private msgItemClick(obj) {
168    LogUtils.i(TAG, "msgItemClick rejectCall");
169    this.mCallServiceProxy.rejectCall(this.callData.callId, obj.msg);
170    this.sendMessage(obj);
171  }
172
173  /**
174   * Handling interface call hangup and rejection
175   */
176  private async onReject() {
177    const {callId, callState} = this.callData;
178    if (callState !== CallStateConst.CALL_STATUS_WAITING) {
179      this.mCallServiceProxy.rejectCall(callId);
180      LogUtils.i(TAG, "onReject this.mCallServiceProxy.rejectCall :");
181    } else {
182      this.mCallServiceProxy.hangUpCall(callId);
183      LogUtils.i(TAG, "onReject this.mCallServiceProxy.hangUpCall :");
184    }
185  }
186
187  /**
188   * Enable the SMS reply pop-up
189   */
190  private btnClick(type) {
191    LogUtils.i(TAG, "btnClick : %s" + JSON.stringify(type));
192    if (type === 'msg') {
193      this.msgDialogController.open();
194    }
195  }
196
197  /**
198   * Answer the phone interface
199   */
200  private onAnswer() {
201    LogUtils.i(TAG, "onAnswer :");
202    this.mCallServiceProxy.acceptCall(this.callData.callId);
203  }
204
205  build() {
206    GridRow({ columns: { sm: 4, md: 8, lg: 12 }, gutter: 24 }) {
207      GridCol({ span: { sm: 4, md: 6, lg: 6 }, offset: { sm: 0, md: 1, lg: 3 } }) {
208        Column() {
209          Row() {
210            ForEach(this.btnList, (item) => {
211              Column() {
212                FuncBtn({
213                  btnType: item.type,
214                  iconDisableUrl: item.iconDefaultUrl,
215                  iconDefaultUrl: item.iconDefaultUrl,
216                  iconText: item.iconText,
217                  isDisable: item.isDisable,
218                  btnClick: (type) => {
219                    this.btnClick(type)
220                  }
221                })
222              }
223            })
224          }
225          .height(51)
226
227          Row() {
228            Column() {
229              Image($r("app.media.ic_public_ring_off"))
230                .width(56)
231                .height(56)
232                .onClick(() => {
233                  this.onReject()
234                })
235            }.layoutWeight(1)
236
237            Column() {
238              Image($r("app.media.ic_public_answer"))
239                .width(56)
240                .height(56)
241                .onClick(() => {
242                  this.onAnswer()
243                })
244            }.layoutWeight(1)
245          }
246          .margin({ top: 32 })
247        }
248      }
249    }
250    .width('100%')
251    .margin({ left: 24, right: 24 })
252  }
253}
254