• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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->GetCallType() == CallType::TYPE_VOIP) {
38         return;
39     }
40     if (callObjectPtr != nullptr && callObjectPtr->GetTelCallState() == TelCallState::CALL_STATUS_INCOMING &&
41         !callObjectPtr->GetAccountNumber().empty()) {
42         incomingCallNumber_ = callObjectPtr->GetAccountNumber();
43     } else {
44         incomingCallNumber_ = "";
45     }
46     isIncomingCallMissed_ = true;
47 }
48 
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)49 void MissedCallNotification::CallStateUpdated(
50     sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
51 {
52     if (callObjectPtr != nullptr && callObjectPtr->GetCallType() == CallType::TYPE_VOIP) {
53         TELEPHONY_LOGI("Voip call should not save  missed notification");
54         return;
55     }
56     if (callObjectPtr != nullptr && nextState == TelCallState::CALL_STATUS_DISCONNECTED &&
57         callObjectPtr->GetAccountNumber() == incomingCallNumber_ && isIncomingCallMissed_) {
58         PublishMissedCallEvent(callObjectPtr);
59         PublishMissedCallNotification(callObjectPtr);
60     }
61 }
62 
PublishMissedCallEvent(sptr<CallBase> & callObjectPtr)63 void MissedCallNotification::PublishMissedCallEvent(sptr<CallBase> &callObjectPtr)
64 {
65     AAFwk::Want want;
66     want.SetParam("callId", callObjectPtr->GetCallID());
67     want.SetParam("notificationId", INCOMING_CALL_MISSED_ID);
68     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_INCOMING_CALL_MISSED);
69     EventFwk::CommonEventData data;
70     data.SetWant(want);
71     data.SetCode(INCOMING_CALL_MISSED_CODE);
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 
77     AAFwk::Want wantWithNumber = want;
78     wantWithNumber.SetParam("phoneNumber", callObjectPtr->GetAccountNumber());
79     EventFwk::CommonEventData dataWithNumber;
80     dataWithNumber.SetWant(wantWithNumber);
81     dataWithNumber.SetCode(INCOMING_CALL_MISSED_CODE);
82     dataWithNumber.SetData(callObjectPtr->GetAccountNumber());
83     std::vector<std::string> callPermissions;
84     callPermissions.emplace_back(Permission::GET_TELEPHONY_STATE);
85     publishInfo.SetSubscriberPermissions(callPermissions);
86     bool resultWithNumber = EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
87     TELEPHONY_LOGI("publish missed call event with number result : %{public}d", resultWithNumber);
88 }
89 
PublishMissedCallNotification(sptr<CallBase> & callObjectPtr)90 void MissedCallNotification::PublishMissedCallNotification(sptr<CallBase> &callObjectPtr)
91 {
92     std::shared_ptr<Notification::NotificationNormalContent> normalContent =
93         std::make_shared<Notification::NotificationNormalContent>();
94     if (normalContent == nullptr) {
95         TELEPHONY_LOGE("notification normal content nullptr");
96         return;
97     }
98     normalContent->SetTitle(INCOMING_CALL_MISSED_TITLE);
99     normalContent->SetText(callObjectPtr->GetAccountNumber());
100     std::shared_ptr<Notification::NotificationContent> content =
101         std::make_shared<Notification::NotificationContent>(normalContent);
102     if (content == nullptr) {
103         TELEPHONY_LOGE("notification content nullptr");
104         return;
105     }
106     Notification::NotificationRequest request;
107     request.SetContent(content);
108     request.SetNotificationId(INCOMING_CALL_MISSED_ID);
109     int32_t result = Notification::NotificationHelper::PublishNotification(request);
110     TELEPHONY_LOGI("publish missed call notification result : %{public}d", result);
111 }
112 
CancelMissedCallsNotification(int32_t id)113 int32_t MissedCallNotification::CancelMissedCallsNotification(int32_t id)
114 {
115 #ifdef ABILITY_NOTIFICATION_SUPPORT
116     return NotificationHelper::CancelNotification(id);
117 #endif
118     return TELEPHONY_SUCCESS;
119 }
120 
NotifyUnReadMissedCall(std::map<std::string,int32_t> & phoneNumAndUnreadCountMap)121 int32_t MissedCallNotification::NotifyUnReadMissedCall(std::map<std::string, int32_t> &phoneNumAndUnreadCountMap)
122 {
123     AAFwk::Want want;
124     want.SetParam("notificationId", INCOMING_CALL_MISSED_ID);
125     std::vector<std::string> phoneNumberList;
126     std::vector<int32_t> countList;
127     for (auto it = phoneNumAndUnreadCountMap.begin(); it != phoneNumAndUnreadCountMap.end(); it++) {
128         phoneNumberList.push_back(it->first);
129         countList.push_back(it->second);
130     }
131     want.SetParam("phoneNumberList", phoneNumberList);
132     want.SetParam("countList", countList);
133     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_INCOMING_CALL_MISSED);
134     EventFwk::CommonEventData data;
135     data.SetWant(want);
136     EventFwk::CommonEventPublishInfo publishInfo;
137     std::vector<std::string> callPermissions;
138     callPermissions.emplace_back(Permission::GET_TELEPHONY_STATE);
139     publishInfo.SetSubscriberPermissions(callPermissions);
140     bool resultWithNumber = EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
141     TELEPHONY_LOGI("publish unread missed call event with number result : %{public}d", resultWithNumber);
142     if (!resultWithNumber) {
143         TELEPHONY_LOGE("publish unread missed call event with number error");
144         return TELEPHONY_ERR_PUBLISH_BROADCAST_FAIL;
145     }
146     return TELEPHONY_ERR_SUCCESS;
147 }
148 
IncomingCallActivated(sptr<CallBase> & callObjectPtr)149 void MissedCallNotification::IncomingCallActivated(sptr<CallBase> &callObjectPtr)
150 {
151     if (callObjectPtr != nullptr && callObjectPtr->GetAccountNumber() == incomingCallNumber_) {
152         isIncomingCallMissed_ = false;
153     }
154 }
155 
IncomingCallHungUp(sptr<CallBase> & callObjectPtr,bool isSendSms,std::string content)156 void MissedCallNotification::IncomingCallHungUp(sptr<CallBase> &callObjectPtr, bool isSendSms, std::string content)
157 {
158     if (callObjectPtr != nullptr && callObjectPtr->GetAccountNumber() == incomingCallNumber_) {
159         isIncomingCallMissed_ = false;
160     }
161 }
162 
CallDestroyed(const DisconnectedDetails & details)163 void MissedCallNotification::CallDestroyed(const DisconnectedDetails &details) {}
164 } // namespace Telephony
165 } // namespace OHOS
166