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 */
15 #include "call_request_event_handler_helper.h"
16
17 #include <thread>
18
19 #include "telephony_errors.h"
20 #include "telephony_log_wrapper.h"
21 namespace OHOS {
22 namespace Telephony {
23
24 const std::string TASK_ID = "handler_restore_dialing_flag";
25 const int32_t DELAY_TIME = 3000;
26
CallRequestEventHandlerHelper()27 CallRequestEventHandlerHelper::CallRequestEventHandlerHelper()
28 {
29 auto runner = AppExecFwk::EventRunner::Create(TASK_ID);
30 if (callRequestEventHandler_ == nullptr) {
31 callRequestEventHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
32 }
33 if (callRequestEventHandler_ == nullptr) {
34 TELEPHONY_LOGE("init call request event handler failed");
35 }
36 }
37
~CallRequestEventHandlerHelper()38 CallRequestEventHandlerHelper::~CallRequestEventHandlerHelper() {}
39
SetDialingCallProcessing()40 int32_t CallRequestEventHandlerHelper::SetDialingCallProcessing()
41 {
42 TELEPHONY_LOGI("start restore dialing flag task");
43 if (IsDialingCallProcessing()) {
44 auto task = [this]() {
45 RestoreDialingFlag(false);
46 };
47 callRequestEventHandler_->RemoveTask(TASK_ID);
48 bool ret = callRequestEventHandler_->PostTask(task, TASK_ID, DELAY_TIME);
49 if (ret) {
50 TELEPHONY_LOGE("restore dialing flag task failed");
51 return TELEPHONY_ERROR;
52 }
53 return TELEPHONY_SUCCESS;
54 }
55 return TELEPHONY_ERROR;
56 }
57
RemoveEventHandlerTask()58 void CallRequestEventHandlerHelper::RemoveEventHandlerTask()
59 {
60 if (callRequestEventHandler_ != nullptr) {
61 callRequestEventHandler_->RemoveTask(TASK_ID);
62 }
63 }
64
RestoreDialingFlag(bool isDialingCallProcessing)65 void CallRequestEventHandlerHelper::RestoreDialingFlag(bool isDialingCallProcessing)
66 {
67 if (isDialingCallProcessing != isDialingCallProcessing_) {
68 isDialingCallProcessing_ = isDialingCallProcessing;
69 }
70 TELEPHONY_LOGI("isDialingCallProcessing = %{public}d.", isDialingCallProcessing_);
71 }
72
IsDialingCallProcessing()73 bool CallRequestEventHandlerHelper::IsDialingCallProcessing()
74 {
75 return isDialingCallProcessing_;
76 }
77
SetPendingMo(bool pendingMo, int32_t callId)78 void CallRequestEventHandlerHelper::SetPendingMo(bool pendingMo, int32_t callId)
79 {
80 pendingMo_ = pendingMo;
81 pendingMoCallId_ = callId;
82 }
83
HasPendingMo(int32_t callId)84 bool CallRequestEventHandlerHelper::HasPendingMo(int32_t callId)
85 {
86 return pendingMo_ && (pendingMoCallId_ == callId);
87 }
88
IsPendingHangup()89 bool CallRequestEventHandlerHelper::IsPendingHangup()
90 {
91 if (pendingHangupCallId_ != -1) {
92 return pendingHangup_;
93 }
94 return false;
95 }
96
GetPendingHangupCallId()97 int32_t CallRequestEventHandlerHelper::GetPendingHangupCallId()
98 {
99 if (pendingHangup_) {
100 return pendingHangupCallId_;
101 }
102 return -1;
103 }
104
SetPendingHangup(bool pendingHangup, int32_t callId)105 void CallRequestEventHandlerHelper::SetPendingHangup(bool pendingHangup, int32_t callId)
106 {
107 pendingHangup_ = pendingHangup;
108 pendingHangupCallId_ = callId;
109 }
110
HasPendingHangup(int32_t callId)111 bool CallRequestEventHandlerHelper::HasPendingHangup(int32_t callId)
112 {
113 return pendingHangup_ && (pendingHangupCallId_ == callId);
114 }
115
116 } // namespace Telephony
117 } // namespace OHOS
118