1 /*
2 * Copyright (C) 2021 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 "missed_call_notification.h"
17
18 #include "notification_normal_content.h"
19 #include "notification_helper.h"
20 #include "notification_content.h"
21 #include "notification_request.h"
22 #include "common_event_support.h"
23 #include "common_event_manager.h"
24 #include "common_event.h"
25 #include "want.h"
26
27 #include "call_manager_errors.h"
28 #include "telephony_log_wrapper.h"
29
30 namespace OHOS {
31 namespace Telephony {
MissedCallNotification()32 MissedCallNotification::MissedCallNotification() : isIncomingCallMissed_(true), incomingCallNumber_("") {}
33
NewCallCreated(sptr<CallBase> & callObjectPtr)34 void MissedCallNotification::NewCallCreated(sptr<CallBase> &callObjectPtr)
35 {
36 if (callObjectPtr != nullptr && callObjectPtr->GetTelCallState() == TelCallState::CALL_STATUS_INCOMING &&
37 !callObjectPtr->GetAccountNumber().empty()) {
38 incomingCallNumber_ = callObjectPtr->GetAccountNumber();
39 } else {
40 incomingCallNumber_ = "";
41 }
42 isIncomingCallMissed_ = true;
43 }
44
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)45 void MissedCallNotification::CallStateUpdated(
46 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
47 {
48 if (callObjectPtr != nullptr && nextState == TelCallState::CALL_STATUS_DISCONNECTED &&
49 callObjectPtr->GetAccountNumber() == incomingCallNumber_ && isIncomingCallMissed_) {
50 PublishMissedCallEvent(callObjectPtr);
51 PublishMissedCallNotification(callObjectPtr);
52 }
53 }
54
PublishMissedCallEvent(sptr<CallBase> & callObjectPtr)55 void MissedCallNotification::PublishMissedCallEvent(sptr<CallBase> &callObjectPtr)
56 {
57 #ifdef ABILITY_STATE_NOTIFICATION_SUPPORT
58 AAFwk::Want want;
59 want.SetParam("callId", callObjectPtr->GetCallID());
60 want.SetParam("phoneNumber", callObjectPtr->GetAccountNumber());
61 want.SetParam("notificationId", INCOMING_CALL_MISSED_ID);
62 #ifdef ABILITY_NOTIFICATION_SUPPORT
63 want.SetAction(CommonEventSupport::COMMON_EVENT_INCOMING_CALL_MISSED);
64 #endif
65 #ifndef ABILITY_NOTIFICATION_SUPPORT
66 want.SetAction(COMMON_EVENT_INCOMING_CALL_MISSED);
67 #endif
68 EventFwk::CommonEventData data;
69 data.SetWant(want);
70 data.SetCode(INCOMING_CALL_MISSED_CODE);
71 data.SetData(callObjectPtr->GetAccountNumber());
72 EventFwk::CommonEventPublishInfo publishInfo;
73 publishInfo.SetOrdered(true);
74 bool result = EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
75 TELEPHONY_LOGI("publish missed call event result : %{public}d", result);
76 #endif
77 }
78
PublishMissedCallNotification(sptr<CallBase> & callObjectPtr)79 void MissedCallNotification::PublishMissedCallNotification(sptr<CallBase> &callObjectPtr)
80 {
81 std::shared_ptr<Notification::NotificationNormalContent> normalContent =
82 std::make_shared<Notification::NotificationNormalContent>();
83 if (normalContent == nullptr) {
84 TELEPHONY_LOGE("notification normal content nullptr");
85 return;
86 }
87 normalContent->SetTitle(INCOMING_CALL_MISSED_TITLE);
88 normalContent->SetText(callObjectPtr->GetAccountNumber());
89 std::shared_ptr<Notification::NotificationContent> content =
90 std::make_shared<Notification::NotificationContent>(normalContent);
91 if (content == nullptr) {
92 TELEPHONY_LOGE("notification content nullptr");
93 return;
94 }
95 Notification::NotificationRequest request;
96 request.SetContent(content);
97 request.SetNotificationId(INCOMING_CALL_MISSED_ID);
98 int32_t result = Notification::NotificationHelper::PublishNotification(request);
99 TELEPHONY_LOGI("publish missed call notification result : %{public}d", result);
100 }
101
CancelMissedCallsNotification(int32_t id)102 int32_t MissedCallNotification::CancelMissedCallsNotification(int32_t id)
103 {
104 #ifdef ABILITY_NOTIFICATION_SUPPORT
105 return NotificationHelper::CancelNotification(id);
106 #endif
107 return TELEPHONY_SUCCESS;
108 }
109
IncomingCallActivated(sptr<CallBase> & callObjectPtr)110 void MissedCallNotification::IncomingCallActivated(sptr<CallBase> &callObjectPtr)
111 {
112 if (callObjectPtr != nullptr && callObjectPtr->GetAccountNumber() == incomingCallNumber_) {
113 isIncomingCallMissed_ = false;
114 }
115 }
116
IncomingCallHungUp(sptr<CallBase> & callObjectPtr,bool isSendSms,std::string content)117 void MissedCallNotification::IncomingCallHungUp(sptr<CallBase> &callObjectPtr, bool isSendSms, std::string content)
118 {
119 if (callObjectPtr != nullptr && callObjectPtr->GetAccountNumber() == incomingCallNumber_) {
120 isIncomingCallMissed_ = false;
121 }
122 }
123
CallDestroyed(int32_t cause)124 void MissedCallNotification::CallDestroyed(int32_t cause) {}
125 } // namespace Telephony
126 } // namespace OHOS