1/*
2 * Copyright (c) 2024 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 */
15const call = requireInternal('telephony.call');
16const ARGUMENTS_LEN_TWO = 2;
17const ARGUMENTS_LEN_ONE = 1;
18async function makeCallFunc(...args) {
19    if ((arguments.length === ARGUMENTS_LEN_TWO && typeof arguments[1] === 'function') ||
20        (arguments.length === ARGUMENTS_LEN_ONE)) {
21        try {
22            let context = getContext(this);
23            await startAbility(arguments, context);
24            if (arguments.length === ARGUMENTS_LEN_TWO && typeof arguments[1] === 'function') {
25                return arguments[1](undefined, undefined);
26            }
27            return new Promise((resolve, reject) => {
28                resolve();
29            });
30        } catch (error) {
31            console.log("[call] makeCall error: " + error);
32            if (arguments.length === ARGUMENTS_LEN_TWO && typeof arguments[1] === 'function') {
33                return arguments[1](error, undefined);
34            }
35            return new Promise((resolve, reject) => {
36                reject(error);
37            });
38        }
39    } else if (arguments.length === ARGUMENTS_LEN_TWO && typeof arguments[1] === 'string') {
40        try {
41            let context = arguments[0];
42            await startAbility(arguments, context);
43            return new Promise((resolve, reject) => {
44                resolve();
45            });
46        } catch (error) {
47            console.log("[call] makeCall error: " + error);
48            return new Promise((resolve, reject) => {
49                reject(error);
50            });
51        }
52    } else {
53        console.log('[call] makeCall callback invalid');
54        throw Error('invalid callback');
55    }
56
57}
58
59async function startAbility(args, context) {
60    let config = {
61        parameters: {
62            'phoneNumber': '',
63            'pageFlag': 'page_flag_edit_before_calling'
64        },
65        bundleName: 'com.ohos.contacts',
66        abilityName: 'com.ohos.contacts.MainAbility'
67    };
68    if (args.length > 0 && typeof args[0] === 'string') {
69        config.parameters.phoneNumber = args[0];
70    } else if (args.length > 1 && typeof args[1] === 'string') {
71        config.parameters.phoneNumber = args[1];
72    }
73    if (context) {
74        await context.startAbility(config);
75    } else {
76        call.makeCall(config.parameters.phoneNumber);
77    }
78}
79
80export default {
81    makeCall: makeCallFunc,
82    dialCall: call.dialCall,
83    muteRinger: call.muteRinger,
84    answerCall: call.answerCall,
85    hangUpCall: call.hangUpCall,
86    hangup: call.hangup,
87    rejectCall: call.rejectCall,
88    reject: call.reject,
89    holdCall: call.holdCall,
90    unHoldCall: call.unHoldCall,
91    switchCall: call.switchCall,
92    setCallPreferenceMode: call.setCallPreferenceMode,
93    combineConference: call.combineConference,
94    kickOutFromConference: call.kickOutFromConference,
95    getMainCallId: call.getMainCallId,
96    getSubCallIdList: call.getSubCallIdList,
97    getCallIdListForConference: call.getCallIdListForConference,
98    getCallWaitingStatus: call.getCallWaitingStatus,
99    setCallWaiting: call.setCallWaiting,
100    startDTMF: call.startDTMF,
101    stopDTMF: call.stopDTMF,
102    postDialProceed: call.postDialProceed,
103    isInEmergencyCall: call.isInEmergencyCall,
104    on: call.on,
105    off: call.off,
106    isNewCallAllowed: call.isNewCallAllowed,
107    separateConference: call.separateConference,
108    getCallRestrictionStatus: call.getCallRestrictionStatus,
109    setCallRestriction: call.setCallRestriction,
110    setCallRestrictionPassword: call.setCallRestrictionPassword,
111    getCallTransferInfo: call.getCallTransferInfo,
112    setCallTransfer: call.setCallTransfer,
113    isRinging: call.isRinging,
114    setMuted: call.setMuted,
115    cancelMuted: call.cancelMuted,
116    setAudioDevice: call.setAudioDevice,
117    joinConference: call.joinConference,
118    updateImsCallMode: call.updateImsCallMode,
119    enableImsSwitch: call.enableImsSwitch,
120    disableImsSwitch: call.disableImsSwitch,
121    isImsSwitchEnabled: call.isImsSwitchEnabled,
122    isImsSwitchEnabledSync: call.isImsSwitchEnabledSync,
123    closeUnfinishedUssd: call.closeUnfinishedUssd,
124    setVoNRState: call.setVoNRState,
125    getVoNRState: call.getVoNRState,
126    canSetCallTransferTime: call.canSetCallTransferTime,
127    inputDialerSpecialCode: call.inputDialerSpecialCode,
128    reportOttCallDetailsInfo: call.reportOttCallDetailsInfo,
129    reportOttCallEventInfo: call.reportOttCallEventInfo,
130    removeMissedIncomingCallNotification: call.removeMissedIncomingCallNotification,
131    setVoIPCallState: call.setVoIPCallState,
132    sendCallUiEvent: call.sendCallUiEvent,
133    DialOptions: call.DialOptions,
134    DialCallOptions: call.DialCallOptions,
135    ImsCallMode: call.ImsCallMode,
136    VoNRState: call.VoNRState,
137    AudioDevice: call.AudioDevice,
138    AudioDeviceType: call.AudioDeviceType,
139    AudioDeviceCallbackInfo: call.AudioDeviceCallbackInfo,
140    CallRestrictionType: call.CallRestrictionType,
141    CallTransferInfo: call.CallTransferInfo,
142    CallTransferType: call.CallTransferType,
143    CallTransferSettingType: call.CallTransferSettingType,
144    CallAttributeOptions: call.CallAttributeOptions,
145    VoipCallAttribute: call.VoipCallAttribute,
146    ConferenceState: call.ConferenceState,
147    CallType: call.CallType,
148    VideoStateType: call.VideoStateType,
149    DetailedCallState: call.DetailedCallState,
150    CallRestrictionInfo: call.CallRestrictionInfo,
151    CallRestrictionMode: call.CallRestrictionMode,
152    CallEventOptions: call.CallEventOptions,
153    CallAbilityEventId: call.CallAbilityEventId,
154    DialScene: call.DialScene,
155    DialType: call.DialType,
156    RejectMessageOptions: call.RejectMessageOptions,
157    CallTransferResult: call.CallTransferResult,
158    CallWaitingStatus: call.CallWaitingStatus,
159    RestrictionStatus: call.RestrictionStatus,
160    TransferStatus: call.TransferStatus,
161    DisconnectedDetails: call.DisconnectedDetails,
162    DisconnectedReason: call.DisconnectedReason,
163    MmiCodeResults: call.MmiCodeResults,
164    MmiCodeResult: call.MmiCodeResult,
165    answer: call.answer,
166    cancelCallUpgrade: call.cancelCallUpgrade,
167    controlCamera: call.controlCamera,
168    setPreviewSurface: call.setPreviewSurface,
169    setCameraZoom: call.setCameraZoom,
170    setDisplaySurface: call.setDisplaySurface,
171    setDeviceDirection: call.setDeviceDirection,
172    VideoRequestResultType: call.VideoRequestResultType,
173    DeviceDirection: call.DeviceDirection,
174    CallSessionEventId: call.CallSessionEventId,
175    ImsCallModeInfo: call.ImsCallModeInfo,
176    CallSessionEvent: call.CallSessionEvent,
177    PeerDimensionsDetail: call.PeerDimensionsDetail,
178    CameraCapabilities: call.CameraCapabilities,
179    NumberMarkInfo: call.NumberMarkInfo,
180    MarkType: call.MarkType,
181    dial: call.dial,
182    hasCall: call.hasCall,
183    hasCallSync: call.hasCallSync,
184    getCallState: call.getCallState,
185    getCallStateSync: call.getCallStateSync,
186    hasVoiceCapability: call.hasVoiceCapability,
187    isEmergencyPhoneNumber: call.isEmergencyPhoneNumber,
188    formatPhoneNumber: call.formatPhoneNumber,
189    formatPhoneNumberToE164: call.formatPhoneNumberToE164,
190    CallState: call.CallState,
191    EmergencyNumberOptions: call.EmergencyNumberOptions,
192    NumberFormatOptions: call.NumberFormatOptions,
193    startRTT: call.startRTT,
194    stopRTT: call.stopRTT
195};