• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "ans_trace_wrapper.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 ErrCode NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::OnConnected()
44 {
45     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
46     sptr<IAnsManager> proxy = GetAnsManagerProxy();
47     if (proxy != nullptr) {
48         proxy->AsObject()->AddDeathRecipient(recipient_);
49         ANS_LOGD("Add death recipient");
50     }
51     subscriber_.OnConnected();
52     return ERR_OK;
53 }
54 
OnDisconnected()55 ErrCode NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::OnDisconnected()
56 {
57     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
58     sptr<IAnsManager> proxy = GetAnsManagerProxy();
59     if (proxy != nullptr) {
60         proxy->AsObject()->RemoveDeathRecipient(recipient_);
61         ANS_LOGD("Remove death recipient");
62     }
63     subscriber_.OnDisconnected();
64     return ERR_OK;
65 }
66 
OnResponse(int32_t notificationId,const sptr<NotificationButtonOption> & buttonOption)67 ErrCode NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::OnResponse(int32_t notificationId,
68     const sptr<NotificationButtonOption> &buttonOption)
69 {
70     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
71     subscriber_.OnResponse(notificationId, buttonOption);
72     return ERR_OK;
73 }
74 
GetAnsManagerProxy()75 sptr<IAnsManager> NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::GetAnsManagerProxy()
76 {
77     sptr<ISystemAbilityManager> systemAbilityManager =
78         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
79     if (!systemAbilityManager) {
80         return nullptr;
81     }
82 
83     sptr<IRemoteObject> remoteObject =
84         systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
85     if (!remoteObject) {
86         return nullptr;
87     }
88 
89     sptr<IAnsManager> proxy = iface_cast<IAnsManager>(remoteObject);
90     if ((proxy == nullptr) || (proxy->AsObject() == nullptr)) {
91         return nullptr;
92     }
93 
94     return proxy;
95 }
96 
DeathRecipient(SubscriberLocalLiveViewImpl & subscriberImpl)97 NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::DeathRecipient::DeathRecipient(
98     SubscriberLocalLiveViewImpl &subscriberImpl) : subscriberImpl_(subscriberImpl) {};
99 
~DeathRecipient()100 NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::DeathRecipient::~DeathRecipient() {};
101 
OnRemoteDied(const wptr<IRemoteObject> & object)102 void NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::DeathRecipient::OnRemoteDied(
103     const wptr<IRemoteObject> &object)
104 {
105     subscriberImpl_.subscriber_.OnDied();
106 }
107 }  // namespace Notification
108 }  // namespace OHOS
109