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() {}
35
NewCallCreated(sptr<CallBase> & callObjectPtr)36 void MissedCallNotification::NewCallCreated(sptr<CallBase> &callObjectPtr) {}
37
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)38 void MissedCallNotification::CallStateUpdated(
39 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
40 {
41 if (callObjectPtr == nullptr) {
42 return;
43 }
44 if (callObjectPtr->GetCallType() == CallType::TYPE_VOIP) {
45 TELEPHONY_LOGI("Voip call should not save missed notification");
46 return;
47 }
48 CallAnswerType answerType = callObjectPtr->GetAnswerType();
49 if (nextState == TelCallState::CALL_STATUS_DISCONNECTED &&
50 callObjectPtr->GetCallDirection() == CallDirection::CALL_DIRECTION_IN &&
51 ((answerType == CallAnswerType::CALL_ANSWER_MISSED) ||
52 (answerType == CallAnswerType::CALL_ANSWER_ACTIVED && callObjectPtr->IsAiAutoAnswer()))) {
53 int32_t userId = 0;
54 bool isUserUnlocked = false;
55 AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
56 AccountSA::OsAccountManager::IsOsAccountVerified(userId, isUserUnlocked);
57 TELEPHONY_LOGI("isUserUnlocked: %{public}d", isUserUnlocked);
58 if (isUserUnlocked) {
59 PublishMissedCallEvent(callObjectPtr);
60 }
61 }
62 }
63
PublishMissedCallEvent(sptr<CallBase> & callObjectPtr)64 void MissedCallNotification::PublishMissedCallEvent(sptr<CallBase> &callObjectPtr)
65 {
66 AAFwk::Want want;
67 want.SetParam("callId", callObjectPtr->GetCallID());
68 want.SetParam("notificationId", INCOMING_CALL_MISSED_ID);
69 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_INCOMING_CALL_MISSED);
70 EventFwk::CommonEventData data;
71 data.SetWant(want);
72 data.SetCode(INCOMING_CALL_MISSED_CODE);
73 EventFwk::CommonEventPublishInfo publishInfo;
74 publishInfo.SetOrdered(true);
75 bool result = EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
76 TELEPHONY_LOGW("publish missed call event result : %{public}d", result);
77
78 AAFwk::Want wantWithNumber = want;
79 wantWithNumber.SetParam("phoneNumber", callObjectPtr->GetAccountNumber());
80 EventFwk::CommonEventData dataWithNumber;
81 dataWithNumber.SetWant(wantWithNumber);
82 dataWithNumber.SetCode(INCOMING_CALL_MISSED_CODE);
83 dataWithNumber.SetData(callObjectPtr->GetAccountNumber());
84 std::vector<std::string> callPermissions;
85 callPermissions.emplace_back(Permission::GET_TELEPHONY_STATE);
86 publishInfo.SetSubscriberPermissions(callPermissions);
87 bool resultWithNumber = EventFwk::CommonEventManager::PublishCommonEvent(dataWithNumber, publishInfo, nullptr);
88 TELEPHONY_LOGW("publish missed call event with number result : %{public}d", resultWithNumber);
89 }
90
PublishBlockedCallEvent(sptr<CallBase> & callObjectPtr)91 void MissedCallNotification::PublishBlockedCallEvent(sptr<CallBase> &callObjectPtr)
92 {
93 AAFwk::Want want;
94 want.SetParam("callId", callObjectPtr->GetCallID());
95 want.SetParam("notificationId", INCOMING_CALL_MISSED_ID);
96 want.SetParam("phoneNumber", callObjectPtr->GetAccountNumber());
97 want.SetParam("isBlocked", true);
98 std::string detectDetails = callObjectPtr->GetDetectDetails();
99 want.SetParam("detectDetails", detectDetails);
100 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_INCOMING_CALL_MISSED);
101 EventFwk::CommonEventData data;
102 data.SetWant(want);
103 data.SetCode(INCOMING_CALL_MISSED_CODE);
104 EventFwk::CommonEventPublishInfo publishInfo;
105 publishInfo.SetOrdered(true);
106 std::vector<std::string> callPermissions;
107 callPermissions.emplace_back(Permission::GET_TELEPHONY_STATE);
108 publishInfo.SetSubscriberPermissions(callPermissions);
109 bool result = EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
110 TELEPHONY_LOGW("publish blocked call event result: %{public}d, detectDetails length: %{public}zu",
111 result, detectDetails.length());
112 }
113
NotifyUnReadMissedCall(std::map<std::string,int32_t> & phoneNumAndUnreadCountMap)114 int32_t MissedCallNotification::NotifyUnReadMissedCall(std::map<std::string, int32_t> &phoneNumAndUnreadCountMap)
115 {
116 AAFwk::Want want;
117 want.SetParam("notificationId", INCOMING_CALL_MISSED_ID);
118 std::vector<std::string> phoneNumberList;
119 std::vector<int32_t> countList;
120 for (auto it = phoneNumAndUnreadCountMap.begin(); it != phoneNumAndUnreadCountMap.end(); it++) {
121 phoneNumberList.push_back(it->first);
122 countList.push_back(it->second);
123 }
124 want.SetParam("phoneNumberList", phoneNumberList);
125 want.SetParam("countList", countList);
126 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_INCOMING_CALL_MISSED);
127 EventFwk::CommonEventData data;
128 data.SetWant(want);
129 EventFwk::CommonEventPublishInfo publishInfo;
130 std::vector<std::string> callPermissions;
131 callPermissions.emplace_back(Permission::GET_TELEPHONY_STATE);
132 publishInfo.SetSubscriberPermissions(callPermissions);
133 bool resultWithNumber = EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
134 TELEPHONY_LOGI("publish unread missed call event with number result : %{public}d", resultWithNumber);
135 if (!resultWithNumber) {
136 TELEPHONY_LOGE("publish unread missed call event with number error");
137 return TELEPHONY_ERR_PUBLISH_BROADCAST_FAIL;
138 }
139 return TELEPHONY_ERR_SUCCESS;
140 }
141
IncomingCallActivated(sptr<CallBase> & callObjectPtr)142 void MissedCallNotification::IncomingCallActivated(sptr<CallBase> &callObjectPtr) {}
143
IncomingCallHungUp(sptr<CallBase> & callObjectPtr,bool isSendSms,std::string content)144 void MissedCallNotification::IncomingCallHungUp(sptr<CallBase> &callObjectPtr,
145 bool isSendSms, std::string content) {}
146
CallDestroyed(const DisconnectedDetails & details)147 void MissedCallNotification::CallDestroyed(const DisconnectedDetails &details) {}
148 } // namespace Telephony
149 } // namespace OHOS
150