1 /*
2 * Copyright (C) 2025-2025 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 #include "ohos.telephony.call.proj.hpp"
17 #include "ohos.telephony.call.impl.hpp"
18 #include "taihe/runtime.hpp"
19 #include "stdexcept"
20
21 #include "call_manager_client.h"
22 #include "telephony_errors.h"
23 #include "telephony_log_wrapper.h"
24 #include <string_ex.h>
25
26 using namespace taihe;
27 using namespace OHOS::Telephony;
28 namespace {
29 // To be implemented.
30
MakeCallSync(::taihe::string_view phoneNumber)31 void MakeCallSync(::taihe::string_view phoneNumber)
32 {
33 auto errCode = OHOS::DelayedSingleton<CallManagerClient>::GetInstance()->MakeCall(std::string(phoneNumber));
34 if (errCode != TELEPHONY_ERR_SUCCESS) {
35 set_business_error(errCode, "makeCall return error");
36 }
37 return;
38 }
39
MakeCallSync2(uintptr_t context,::taihe::string_view phoneNumber)40 void MakeCallSync2(uintptr_t context, ::taihe::string_view phoneNumber)
41 {
42 auto errCode = OHOS::DelayedSingleton<CallManagerClient>::GetInstance()->MakeCall(std::string(phoneNumber));
43 if (errCode != TELEPHONY_ERR_SUCCESS) {
44 set_business_error(errCode, "makeCall2 return error");
45 }
46 return;
47 }
48
HasVoiceCapability()49 bool HasVoiceCapability()
50 {
51 auto ret = OHOS::DelayedSingleton<CallManagerClient>::GetInstance()->HasVoiceCapability();
52 return static_cast<ani_boolean>(ret);
53 }
54
FormatPhoneNumberSync(::taihe::string_view phoneNumber,::ohos::telephony::call::NumberFormatOptions const & options)55 void FormatPhoneNumberSync(::taihe::string_view phoneNumber,
56 ::ohos::telephony::call::NumberFormatOptions const& options)
57 {
58 std::u16string phoneNum = OHOS::Str8ToStr16(std::string(phoneNumber));
59 std::u16string countryCode = OHOS::Str8ToStr16(std::string(options.countryCode));
60 std::u16string formatNum = OHOS::Str8ToStr16("");
61 auto errCode = OHOS::DelayedSingleton<CallManagerClient>::GetInstance()->FormatPhoneNumber(
62 phoneNum, countryCode, formatNum);
63 if (errCode != TELEPHONY_ERR_SUCCESS) {
64 set_business_error(errCode, "formatPhoneNumber return error");
65 }
66 return;
67 }
68
FormatPhoneNumberSync2(::taihe::string_view phoneNumber)69 void FormatPhoneNumberSync2(::taihe::string_view phoneNumber)
70 {
71 std::u16string phoneNum = OHOS::Str8ToStr16(std::string(phoneNumber));
72 std::u16string countryCode = OHOS::Str8ToStr16("");
73 std::u16string formatNum = OHOS::Str8ToStr16("");
74 auto errCode = OHOS::DelayedSingleton<CallManagerClient>::GetInstance()->FormatPhoneNumber(
75 phoneNum, countryCode, formatNum);
76 if (errCode != TELEPHONY_ERR_SUCCESS) {
77 set_business_error(errCode, "formatPhoneNumber2 return error");
78 }
79 return;
80 }
81
GetCallStateSync()82 ::ohos::telephony::call::CallState GetCallStateSync()
83 {
84 int32_t callState = static_cast<int32_t>(CallStateToApp::CALL_STATE_UNKNOWN);
85 callState = OHOS::DelayedSingleton<CallManagerClient>::GetInstance()->GetCallState();
86 return static_cast<::ohos::telephony::call::CallState::key_t>(callState);
87 }
88
HasCallSync()89 bool HasCallSync()
90 {
91 auto ret = OHOS::DelayedSingleton<CallManagerClient>::GetInstance()->HasCall();
92 return static_cast<ani_boolean>(ret);
93 }
94 } //namespace
95
96 // Since these macros are auto-generate, lint will cause false positive.
97 // NOLINTBEGIN
98 TH_EXPORT_CPP_API_MakeCallSync(MakeCallSync);
99 TH_EXPORT_CPP_API_MakeCallSync2(MakeCallSync2);
100 TH_EXPORT_CPP_API_HasVoiceCapability(HasVoiceCapability);
101 TH_EXPORT_CPP_API_FormatPhoneNumberSync(FormatPhoneNumberSync);
102 TH_EXPORT_CPP_API_FormatPhoneNumberSync2(FormatPhoneNumberSync2);
103 TH_EXPORT_CPP_API_GetCallStateSync(GetCallStateSync);
104 TH_EXPORT_CPP_API_HasCallSync(HasCallSync);
105 // NOLINTEND