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