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