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
16/**
17 * @file: Voice call API interface call
18 */
19
20/**
21 * The code here is the voice call interface and the sim card interface,
22 * as well as the interface piling test simulation,
23 * which is convenient for development, so I will leave it for now.
24 */
25
26import commonEvent from '@ohos.commonEvent';
27import call from '@ohos.telephony.call';
28import LogUtils from '../common/utils/LogUtils';
29
30const TAG = 'CallServiceProxy';
31const prefixLog = 'callUI app:@ohos.telephony.call:';
32
33/**
34 * dial call
35 * @param { string } phoneNumber - phone number
36 * @param { number } accountId - account id
37 * @param { number } videoState - video state
38 * @param { number } dialScene - dial scene
39 * @return { Object } promise object
40 */
41export default class CallServiceProxy {
42  private static sCallServiceProxy: CallServiceProxy;
43
44  public static getInstance(): CallServiceProxy {
45    if (!CallServiceProxy.sCallServiceProxy) {
46      CallServiceProxy.sCallServiceProxy = new CallServiceProxy();
47    }
48    return CallServiceProxy.sCallServiceProxy;
49  }
50
51  /**
52   * Make a phone call
53   */
54  public dialCall(phoneNumber, accountId = 0, videoState = 0, dialScene = 0) {
55    LogUtils.i(TAG, 'dialCall phoneNumber :');
56    return call.dial(phoneNumber, {
57      accountId,
58      videoState,
59      dialScene
60    });
61  }
62
63  /**
64   * Stops the ringtone.
65   */
66  public muteRinger() {
67    LogUtils.i(TAG, 'muteRinger')
68    call.muteRinger((err) => {
69      LogUtils.e(TAG, `muteRinger callback: err->${JSON.stringify(err)}`);
70    });
71  }
72
73  /**
74   * accept call
75   *
76   * @param { number } callId - call id
77   */
78  public acceptCall = function (callId) {
79    call.answerCall(callId).then((res) => {
80      LogUtils.i(TAG, prefixLog + 'call.answerCall : %s' + JSON.stringify(callId))
81    }).catch((err) => {
82      LogUtils.i(TAG, prefixLog + 'call.answerCall catch : %s' + JSON.stringify(err))
83    });
84  };
85
86  /**
87   * reject call
88   *
89   * @param { number } callId - call id
90   *
91   * @param { boolean } isSendSms - is send sms
92   *
93   * @param { string } msg - message string
94   */
95  public rejectCall = function (callId, isSendSms = false, msg = '') {
96    const rejectCallPromise = isSendSms ? call.rejectCall(callId, {
97      messageContent: msg
98    }) : call.rejectCall(callId);
99    rejectCallPromise.then((res) => {
100      LogUtils.i(TAG, prefixLog + 'then:rejectCall');
101    })
102      .catch((err) => {
103        LogUtils.i(TAG, prefixLog + 'catch:rejectCall : %s' + JSON.stringify(err));
104      });
105  };
106
107  /**
108   * hang up Call
109   *
110   * @param { number } callId - call id
111   *
112   * @return { Object } promise object
113   */
114  public hangUpCall = (callId) => new Promise((resolve, reject) => {
115    call.hangUpCall(callId).then((res) => {
116      resolve(res);
117      LogUtils.i(TAG, prefixLog + 'then:hangUpCall : %s' + JSON.stringify(callId))
118    }).catch((err) => {
119      reject(err);
120      LogUtils.i(TAG, prefixLog + 'catch:hangUpCall : %s' + JSON.stringify(err))
121    });
122  });
123
124  /**
125   * hold call
126   *
127   * @param { number } callId - call id
128   *
129   * @return { Object } promise object
130   */
131  public holdCall = (callId) => new Promise((resolve, reject) => {
132    call.holdCall(callId).then((res) => {
133      resolve(res);
134      LogUtils.i(TAG, prefixLog + 'then:holdCall : %s' + JSON.stringify(callId));
135    })
136      .catch((err) => {
137        reject(err);
138        LogUtils.i(TAG, prefixLog + 'catch:holdCall : %s' + JSON.stringify(err));
139      });
140  });
141
142  /**
143   * un hold call
144   *
145   * @param { number } callId - call id
146   *
147   * @return { Object } promise object
148   */
149  public unHoldCall = (callId) => new Promise((resolve, reject) => {
150    call.unHoldCall(callId).then((res) => {
151      resolve(res);
152      LogUtils.i(TAG, prefixLog + 'then:unholdCall : %s' + JSON.stringify(callId));
153    })
154      .catch((err) => {
155        reject(err);
156        LogUtils.i(TAG, prefixLog + 'catch:unHoldCall : %s' + JSON.stringify(err));
157      });
158  });
159
160  /**
161   * set call mute
162   */
163  public setMuted() {
164    call.setMuted().then((res) => {
165      LogUtils.i(TAG, prefixLog + 'then:setMute');
166    }).catch((err) => {
167      LogUtils.i(TAG, prefixLog + 'catch:setMute : %s' + JSON.stringify(err));
168    });
169  };
170
171  /**
172   * cancel call mute
173   */
174  public cancelMuted() {
175    call.cancelMuted().then((res) => {
176      LogUtils.i(TAG, prefixLog + 'then:cancelMuted');
177    }).catch((err) => {
178      LogUtils.i(TAG, prefixLog + 'catch:cancelMuted : %s' + JSON.stringify(err))
179    });
180  };
181
182  /**
183   * switch call
184   *
185   * @param { number } callId - call id
186   *
187   * @return { Object } promise object
188   */
189  public switchCall = (callId) => new Promise((resolve, reject) => {
190    call.switchCall(callId).then((res) => {
191      resolve(res);
192      LogUtils.i(TAG, prefixLog + 'then:switchCall : %s' + JSON.stringify(callId))
193    })
194      .catch((err) => {
195        reject(err);
196        LogUtils.i(TAG, prefixLog + 'catch:switchCall : %s' + JSON.stringify(err))
197      });
198  });
199
200  /**
201   * register call state callback
202   *
203   * @param { Function } callBack - inject an Function
204   */
205  public registerCallStateCallback(callBack) {
206    call.on('callDetailsChange', (data) => {
207      if (!data) {
208        LogUtils.i(TAG, prefixLog + 'call.on registerCallStateCallback')
209        return;
210      }
211      LogUtils.i(TAG, prefixLog + 'call.on registerCallStateCallback callState')
212      callBack(data);
213    });
214  }
215
216  /**
217   * onRegister call state callback
218   */
219  public unRegisterCallStateCallback() {
220    call.off('callDetailsChange', (data) => {
221      if (!data) {
222        LogUtils.i(TAG, prefixLog + 'call.off unRegisterCallStateCallback')
223        return;
224      }
225      LogUtils.i(TAG, prefixLog + 'call.off unRegisterCallStateCallback')
226    });
227  }
228
229  /**
230   * register call event callback
231   */
232  public registerCallEventCallback() {
233    call.on('callEventChange', (data) => {
234      if (!data) {
235        LogUtils.i(TAG, prefixLog + 'call.on callEventChange')
236      } else {
237        LogUtils.i(TAG, prefixLog + 'call.on callEventChange')
238      }
239    });
240  }
241
242  /**
243   * unRegister call event callback
244   */
245  public unRegisterCallEventCallback() {
246    call.off('callEventChange', (data) => {
247      if (!data) {
248        LogUtils.i(TAG, prefixLog + 'call.off unRegisterCallEventCallback : %s')
249      } else {
250        LogUtils.i(TAG, prefixLog + 'call.off unRegisterCallEventCallback : %s')
251      }
252    });
253  }
254
255  /**
256   * start DTMF
257   *
258   * @param { number } callId - call id
259   *
260   * @param { string } str - str char
261   */
262  public startDTMF = (callId = 1, str = '1') => {
263    call.startDTMF(callId, str).then((res) => {
264      LogUtils.i(TAG, prefixLog + 'then:startDtmf : %s' + JSON.stringify(callId) + JSON.stringify(str))
265    }).catch((err) => {
266      LogUtils.i(TAG, prefixLog + 'catch:startDtmf : %s' + JSON.stringify(err))
267    });
268  };
269
270  /**
271   * stop DTMF
272   *
273   * @param { number } callId - call id
274   */
275  public stopDTMF = (callId = 1) => {
276    call.stopDTMF(callId).then((res) => {
277      LogUtils.i(TAG, prefixLog + 'then:stopDtmf : %s' + JSON.stringify(callId))
278    }).catch((err) => {
279      LogUtils.i(TAG, prefixLog + 'then:startDtmf : %s' + JSON.stringify(err))
280    });
281  };
282
283  /**
284   * combine conference
285   *
286   * @param { number } callId - call id
287   */
288  public combineConference = (callId) => {
289    call.combineConference(callId).then((res) => {
290      LogUtils.i(TAG, prefixLog + 'then:combineConference : %s' + JSON.stringify(callId))
291    }).catch((err) => {
292      LogUtils.i(TAG, prefixLog + 'catch:combineConference : %s' + JSON.stringify(err))
293    });
294  };
295
296  public publish(data) {
297    LogUtils.i(TAG, prefixLog + 'callui.event.callEvent publish')
298    commonEvent.publish('callui.event.callEvent', {
299      bundleName: 'com.ohos.callui',
300      isOrdered: false,
301      subscriberPermissions: ['ohos.permission.GET_TELEPHONY_STATE'],
302      data: JSON.stringify(data)
303    }, (res) => {
304      LogUtils.i(TAG, prefixLog + 'callui.event.callEvent success')
305    });
306    LogUtils.i(TAG, prefixLog + 'callui.event.callEvent publish end')
307  }
308}
309
310