• 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
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   * accept call
65   *
66   * @param { number } callId - call id
67   */
68  public acceptCall = function (callId) {
69    call.answerCall(callId).then((res) => {
70      LogUtils.i(TAG, prefixLog + "call.answerCall : %s" + JSON.stringify(callId))
71    }).catch((err) => {
72      LogUtils.i(TAG, prefixLog + "call.answerCall catch : %s" + JSON.stringify(err))
73    });
74  };
75
76  /**
77   * reject call
78   *
79   * @param { number } callId - call id
80   *
81   * @param { boolean } isSendSms - is send sms
82   *
83   * @param { string } msg - message string
84   */
85  public rejectCall = function (callId, isSendSms = false, msg = '') {
86    const rejectCallPromise = isSendSms ? call.rejectCall(callId, {
87      messageContent: msg
88    }) : call.rejectCall(callId);
89    rejectCallPromise.then((res) => {
90      LogUtils.i(TAG, prefixLog + "then:rejectCall")
91    })
92      .catch((err) => {
93        LogUtils.i(TAG, prefixLog + "catch:rejectCall : %s" + JSON.stringify(err))
94      });
95  };
96
97  /**
98   * hang up Call
99   *
100   * @param { number } callId - call id
101   *
102   * @return { Object } promise object
103   */
104  public hangUpCall = (callId) => new Promise((resolve, reject) => {
105    call.hangUpCall(callId).then((res) => {
106      resolve(res);
107      LogUtils.i(TAG, prefixLog + "then:hangUpCall : %s" + JSON.stringify(callId))
108    }).catch((err) => {
109      reject(err);
110      LogUtils.i(TAG, prefixLog + "catch:hangUpCall : %s" + JSON.stringify(err))
111    });
112  });
113
114  /**
115   * hold call
116   *
117   * @param { number } callId - call id
118   *
119   * @return { Object } promise object
120   */
121  public holdCall = (callId) => new Promise((resolve, reject) => {
122    call.holdCall(callId).then((res) => {
123      resolve(res);
124      LogUtils.i(TAG, prefixLog + "then:holdCall : %s" + JSON.stringify(callId))
125    })
126      .catch((err) => {
127        reject(err);
128        LogUtils.i(TAG, prefixLog + "catch:holdCall : %s" + JSON.stringify(err))
129      });
130  });
131
132  /**
133   * un hold call
134   *
135   * @param { number } callId - call id
136   *
137   * @return { Object } promise object
138   */
139  public unHoldCall = (callId) => new Promise((resolve, reject) => {
140    call.unHoldCall(callId).then((res) => {
141      resolve(res);
142      LogUtils.i(TAG, prefixLog + "then:unholdCall : %s" + JSON.stringify(callId))
143    })
144      .catch((err) => {
145        reject(err);
146        LogUtils.i(TAG, prefixLog + "catch:unHoldCall : %s" + JSON.stringify(err))
147      });
148  });
149
150  /**
151   * set call mute
152   */
153  public setMuted() {
154    call.setMuted().then((res) => {
155      LogUtils.i(TAG, prefixLog + "then:setMute")
156    }).catch((err) => {
157      LogUtils.i(TAG, prefixLog + "catch:setMute : %s" + JSON.stringify(err))
158    });
159  };
160
161  /**
162   * cancel call mute
163   */
164  public cancelMuted() {
165    call.cancelMuted().then((res) => {
166      LogUtils.i(TAG, prefixLog + "then:cancelMuted")
167    }).catch((err) => {
168      LogUtils.i(TAG, prefixLog + "catch:cancelMuted : %s" + JSON.stringify(err))
169    });
170  };
171
172  /**
173   * switch call
174   *
175   * @param { number } callId - call id
176   *
177   * @return { Object } promise object
178   */
179  public switchCall = (callId) => new Promise((resolve, reject) => {
180    call.switchCall(callId).then((res) => {
181      resolve(res);
182      LogUtils.i(TAG, prefixLog + "then:switchCall : %s" + JSON.stringify(callId))
183    })
184      .catch((err) => {
185        reject(err);
186        LogUtils.i(TAG, prefixLog + "catch:switchCall : %s" + JSON.stringify(err))
187      });
188  });
189
190  /**
191   * register call state callback
192   *
193   * @param { Function } callBack - inject an Function
194   */
195  public registerCallStateCallback(callBack) {
196    call.on('callDetailsChange', (data) => {
197      if (!data) {
198        LogUtils.i(TAG, prefixLog + "call.on registerCallStateCallback")
199        return;
200      }
201      LogUtils.i(TAG, prefixLog + "call.on registerCallStateCallback callState")
202      callBack(data);
203    });
204  }
205
206  /**
207   * onRegister call state callback
208   */
209  public unRegisterCallStateCallback() {
210    call.off('callDetailsChange', (data) => {
211      if (!data) {
212        LogUtils.i(TAG, prefixLog + "call.off unRegisterCallStateCallback")
213        return;
214      }
215      LogUtils.i(TAG, prefixLog + "call.off unRegisterCallStateCallback")
216    });
217  }
218
219  /**
220   * register call event callback
221   */
222  public registerCallEventCallback() {
223    call.on('callEventChange', (data) => {
224      if (!data) {
225        LogUtils.i(TAG, prefixLog + "call.on callEventChange")
226      } else {
227        LogUtils.i(TAG, prefixLog + "call.on callEventChange")
228      }
229    });
230  }
231
232  /**
233   * unRegister call event callback
234   */
235  public unRegisterCallEventCallback() {
236    call.off('callEventChange', (data) => {
237      if (!data) {
238        LogUtils.i(TAG, prefixLog + "call.off unRegisterCallEventCallback : %s")
239      } else {
240        LogUtils.i(TAG, prefixLog + "call.off unRegisterCallEventCallback : %s")
241      }
242    });
243  }
244
245  /**
246   * start DTMF
247   *
248   * @param { number } callId - call id
249   *
250   * @param { string } str - str char
251   */
252  public startDTMF = (callId, str) => {
253    call.startDTMF(callId, str).then((res) => {
254      LogUtils.i(TAG, prefixLog + "then:startDtmf : %s" + JSON.stringify(callId) + JSON.stringify(str))
255    }).catch((err) => {
256      LogUtils.i(TAG, prefixLog + "catch:startDtmf : %s" + JSON.stringify(err))
257    });
258  };
259
260  /**
261   * stop DTMF
262   *
263   * @param { number } callId - call id
264   */
265  public stopDTMF = (callId) => {
266    call.stopDTMF(callId).then((res) => {
267      LogUtils.i(TAG, prefixLog + "then:stopDtmf : %s" + JSON.stringify(callId))
268    }).catch((err) => {
269      LogUtils.i(TAG, prefixLog + "then:startDtmf : %s" + JSON.stringify(err))
270    });
271  };
272
273  /**
274   * combine conference
275   *
276   * @param { number } callId - call id
277   */
278  public combineConference = (callId) => {
279    call.combineConference(callId).then((res) => {
280      LogUtils.i(TAG, prefixLog + "then:combineConference : %s" + JSON.stringify(callId))
281    }).catch((err) => {
282      LogUtils.i(TAG, prefixLog + "catch:combineConference : %s" + JSON.stringify(err))
283    });
284  };
285
286  public publish(data) {
287    LogUtils.i(TAG, prefixLog + "callui.event.callEvent publish")
288    commonEvent.publish('callui.event.callEvent', {
289      bundleName: 'com.ohos.callui',
290      isOrdered: false,
291      subscriberPermissions: ["ohos.permission.GET_TELEPHONY_STATE"],
292      data: JSON.stringify(data)
293    }, (res) => {
294      LogUtils.i(TAG, prefixLog + "callui.event.callEvent success")
295    });
296    LogUtils.i(TAG, prefixLog + "callui.event.callEvent publish end")
297  }
298}
299
300