• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (GetAnsManagerProxy()) {
47         proxy_->AsObject()->AddDeathRecipient(recipient_);
48         ANS_LOGD("%s, Add death recipient.", __func__);
49     }
50     subscriber_.OnConnected();
51 }
52 
OnDisconnected()53 void NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::OnDisconnected()
54 {
55     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
56     if (GetAnsManagerProxy()) {
57         proxy_->AsObject()->RemoveDeathRecipient(recipient_);
58         ANS_LOGD("%s, Remove death recipient.", __func__);
59     }
60     subscriber_.OnDisconnected();
61 }
62 
OnResponse(int32_t notificationId,sptr<NotificationButtonOption> buttonOption)63 void NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::OnResponse(int32_t notificationId,
64     sptr<NotificationButtonOption> buttonOption)
65 {
66     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
67     subscriber_.OnResponse(notificationId, buttonOption);
68 }
69 
GetAnsManagerProxy()70 bool NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::GetAnsManagerProxy()
71 {
72     if (proxy_ == nullptr) {
73         std::lock_guard<std::mutex> lock(mutex_);
74         if (proxy_ == nullptr) {
75             sptr<ISystemAbilityManager> systemAbilityManager =
76                 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
77             if (!systemAbilityManager) {
78                 return false;
79             }
80 
81             sptr<IRemoteObject> remoteObject =
82                 systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
83             if (!remoteObject) {
84                 return false;
85             }
86 
87             proxy_ = iface_cast<AnsManagerInterface>(remoteObject);
88             if ((proxy_ == nullptr) || (proxy_->AsObject() == nullptr)) {
89                 return false;
90             }
91         }
92     }
93 
94     return true;
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_.proxy_ = nullptr;
106     subscriberImpl_.subscriber_.OnDied();
107 }
108 }  // namespace Notification
109 }  // namespace OHOS
110