1 /*
2  * Copyright (c) 2021-2023 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 "notification_local_live_view_subscriber.h"
17 
18 #include "hitrace_meter_adapter.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 
22 namespace OHOS {
23 namespace Notification {
NotificationLocalLiveViewSubscriber()24 NotificationLocalLiveViewSubscriber::NotificationLocalLiveViewSubscriber()
25 {
26     impl_ = new (std::nothrow) SubscriberLocalLiveViewImpl(*this);
27 };
28 
~NotificationLocalLiveViewSubscriber()29 NotificationLocalLiveViewSubscriber::~NotificationLocalLiveViewSubscriber()
30 {}
31 
GetImpl() const32 const sptr<NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl> NotificationLocalLiveViewSubscriber::GetImpl() const
33 {
34     return impl_;
35 }
36 
SubscriberLocalLiveViewImpl(NotificationLocalLiveViewSubscriber & subscriber)37 NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::SubscriberLocalLiveViewImpl(
38     NotificationLocalLiveViewSubscriber &subscriber) : subscriber_(subscriber)
39 {
40     recipient_ = new (std::nothrow) DeathRecipient(*this);
41 };
42 
OnConnected()43 void NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::OnConnected()
44 {
45     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
46     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
47     if (proxy != nullptr) {
48         proxy->AsObject()->AddDeathRecipient(recipient_);
49         ANS_LOGD("%s, Add death recipient.", __func__);
50     }
51     subscriber_.OnConnected();
52 }
53 
OnDisconnected()54 void NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::OnDisconnected()
55 {
56     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
57     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
58     if (proxy != nullptr) {
59         proxy->AsObject()->RemoveDeathRecipient(recipient_);
60         ANS_LOGD("%s, Remove death recipient.", __func__);
61     }
62     subscriber_.OnDisconnected();
63 }
64 
OnResponse(int32_t notificationId,sptr<NotificationButtonOption> buttonOption)65 void NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::OnResponse(int32_t notificationId,
66     sptr<NotificationButtonOption> buttonOption)
67 {
68     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
69     subscriber_.OnResponse(notificationId, buttonOption);
70 }
71 
GetAnsManagerProxy()72 sptr<AnsManagerInterface> NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::GetAnsManagerProxy()
73 {
74     sptr<ISystemAbilityManager> systemAbilityManager =
75         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76     if (!systemAbilityManager) {
77         return nullptr;
78     }
79 
80     sptr<IRemoteObject> remoteObject =
81         systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
82     if (!remoteObject) {
83         return nullptr;
84     }
85 
86     sptr<AnsManagerInterface> proxy = iface_cast<AnsManagerInterface>(remoteObject);
87     if ((proxy == nullptr) || (proxy->AsObject() == nullptr)) {
88         return nullptr;
89     }
90 
91     return proxy;
92 }
93 
DeathRecipient(SubscriberLocalLiveViewImpl & subscriberImpl)94 NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::DeathRecipient::DeathRecipient(
95     SubscriberLocalLiveViewImpl &subscriberImpl) : subscriberImpl_(subscriberImpl) {};
96 
~DeathRecipient()97 NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::DeathRecipient::~DeathRecipient() {};
98 
OnRemoteDied(const wptr<IRemoteObject> & object)99 void NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::DeathRecipient::OnRemoteDied(
100     const wptr<IRemoteObject> &object)
101 {
102     subscriberImpl_.subscriber_.OnDied();
103 }
104 }  // namespace Notification
105 }  // namespace OHOS
106