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 "incoming_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 {
IncomingCallNotification()32 IncomingCallNotification::IncomingCallNotification() : incomingCallNumber_("") {}
33 
NewCallCreated(sptr<CallBase> & callObjectPtr)34 void IncomingCallNotification::NewCallCreated(sptr<CallBase> &callObjectPtr)
35 {
36     if (callObjectPtr != nullptr && callObjectPtr->GetTelCallState() == TelCallState::CALL_STATUS_INCOMING &&
37         !callObjectPtr->GetAccountNumber().empty()) {
38         incomingCallNumber_ = callObjectPtr->GetAccountNumber();
39         PublishIncomingCallNotification(callObjectPtr);
40     } else {
41         incomingCallNumber_ = "";
42     }
43 }
44 
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)45 void IncomingCallNotification::CallStateUpdated(
46     sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
47 {
48     if (callObjectPtr != nullptr && priorState == TelCallState::CALL_STATUS_INCOMING &&
49         callObjectPtr->GetAccountNumber() == incomingCallNumber_) {
50         CancelIncomingCallNotification();
51     }
52 }
53 
IncomingCallActivated(sptr<CallBase> & callObjectPtr)54 void IncomingCallNotification::IncomingCallActivated(sptr<CallBase> &callObjectPtr)
55 {
56     if (callObjectPtr != nullptr && callObjectPtr->GetAccountNumber() == incomingCallNumber_) {
57         CancelIncomingCallNotification();
58     }
59 }
60 
IncomingCallHungUp(sptr<CallBase> & callObjectPtr,bool isSendSms,std::string content)61 void IncomingCallNotification::IncomingCallHungUp(
62     sptr<CallBase> &callObjectPtr, bool isSendSms, std::string content)
63 {
64     if (callObjectPtr != nullptr && callObjectPtr->GetAccountNumber() == incomingCallNumber_) {
65         CancelIncomingCallNotification();
66     }
67 }
68 
CallDestroyed(const DisconnectedDetails & details)69 void IncomingCallNotification::CallDestroyed(const DisconnectedDetails &details) {}
70 
PublishIncomingCallNotification(sptr<CallBase> & callObjectPtr)71 void IncomingCallNotification::PublishIncomingCallNotification(sptr<CallBase> &callObjectPtr)
72 {
73     // show the incoming call notification while full screen
74     if (!IsFullScreen()) {
75         TELEPHONY_LOGE("not full screen");
76         return;
77     }
78     std::shared_ptr<Notification::NotificationNormalContent> normalContent =
79         std::make_shared<Notification::NotificationNormalContent>();
80     if (normalContent == nullptr) {
81         TELEPHONY_LOGE("notification normal content nullptr");
82         return;
83     }
84     normalContent->SetTitle(INCOMING_CALL_NOTIFICATION_TITLE);
85     normalContent->SetText(callObjectPtr->GetAccountNumber());
86     std::shared_ptr<Notification::NotificationContent> content =
87         std::make_shared<Notification::NotificationContent>(normalContent);
88     if (content == nullptr) {
89         TELEPHONY_LOGE("notification content nullptr");
90         return;
91     }
92     Notification::NotificationRequest request;
93     request.SetContent(content);
94     request.SetNotificationId(INCOMING_CALL_NOTIFICATION_ID);
95     int32_t result = Notification::NotificationHelper::PublishNotification(request);
96     TELEPHONY_LOGI("publish incoming call notification result : %{public}d", result);
97 }
98 
CancelIncomingCallNotification()99 int32_t IncomingCallNotification::CancelIncomingCallNotification()
100 {
101 #ifdef ABILITY_NOTIFICATION_SUPPORT
102     return NotificationHelper::CancelNotification(INCOMING_CALL_NOTIFICATION_ID);
103 #endif
104     return TELEPHONY_SUCCESS;
105 }
106 
IsFullScreen()107 bool IncomingCallNotification::IsFullScreen()
108 {
109 #ifdef ABILITY_DISPLAY_SUPPORT
110     return DisplayManager::IsFullScreen();
111 #endif
112     return false;
113 }
114 } // namespace Telephony
115 } // namespace OHOS