From 658589c3706b597dafa2e08d09ed685c944c483d Mon Sep 17 00:00:00 2001 From: markYao Date: Sun, 22 Oct 2023 20:10:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E5=AE=9E=E5=86=B5?= =?UTF-8?q?=E7=AA=97frameworks/core=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: markYao --- .../core/include/ans_manager_interface.h | 23 +++ frameworks/core/include/ans_manager_proxy.h | 23 +++ frameworks/core/include/ans_manager_stub.h | 18 +++ frameworks/core/include/ans_notification.h | 29 ++++ ...ans_subscriber_local_live_view_interface.h | 56 ++++++++ .../ans_subscriber_local_live_view_proxy.h | 68 +++++++++ .../ans_subscriber_local_live_view_stub.h | 67 +++++++++ ..._notification_service_ipc_interface_code.h | 3 + frameworks/core/src/ans_manager_proxy.cpp | 94 +++++++++++++ frameworks/core/src/ans_manager_stub.cpp | 79 +++++++++++ frameworks/core/src/ans_notification.cpp | 42 ++++++ .../ans_subscriber_local_live_view_proxy.cpp | 118 ++++++++++++++++ .../ans_subscriber_local_live_view_stub.cpp | 131 ++++++++++++++++++ .../ans_notification_branch_test.cpp | 12 ++ 14 files changed, 763 insertions(+) create mode 100644 frameworks/core/include/ans_subscriber_local_live_view_interface.h create mode 100644 frameworks/core/include/ans_subscriber_local_live_view_proxy.h create mode 100644 frameworks/core/include/ans_subscriber_local_live_view_stub.h create mode 100644 frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp create mode 100644 frameworks/core/src/ans_subscriber_local_live_view_stub.cpp diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index 6159fd145..3a595b2cb 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -20,6 +20,7 @@ #include #include "ans_subscriber_interface.h" +#include "ans_subscriber_local_live_view_interface.h" #include "iremote_broker.h" #include "notification_bundle_option.h" #include "notification_constant.h" @@ -246,6 +247,18 @@ public: */ virtual ErrCode HasNotificationPolicyAccessPermission(bool &granted) = 0; + /** + * @brief Trigger the local activity after the button has been clicked. + * @note Your application must have platform signature to use this method. + * + * @param bundleOption Indicates the bundle name and uid of the application whose notifications has been clicked. + * @param notificationId Indicates the id of the notification. + * @param buttonOption Indicates which button has been clicked. + * @return Returns trigger localLiveView result. + */ + virtual ErrCode TriggerLocalLiveView(const sptr &bundleOption, + const int32_t notificationId, const sptr &buttonOption) = 0; + /** * @brief Delete notification based on key. * @@ -384,6 +397,16 @@ public: virtual ErrCode Subscribe(const sptr &subscriber, const sptr &info) = 0; + /** + * @brief Subscribes system activity notifications. + * + * @param subscriber Indicates the subscriber. + * @param info Indicates the NotificationSubscribeInfo object. + * @return Returns ERR_OK on success, others on failure. + */ + virtual ErrCode SubscribeLocalLiveView(const sptr &subscriber, + const sptr &info) = 0; + /** * @brief Unsubscribes notifications. * diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index c31fc4330..f938d548b 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -235,6 +235,18 @@ public: */ ErrCode HasNotificationPolicyAccessPermission(bool &granted) override; + /** + * @brief Trigger the local activity after the button has been clicked. + * @note Your application must have platform signature to use this method. + * + * @param bundleOption Indicates the bundle name and uid of the application whose notifications has been clicked. + * @param notificationId Indicates the id of the notification. + * @param buttonOption Indicates which button has been clicked. + * @return Returns trigger localLiveView result. + */ + ErrCode TriggerLocalLiveView(const sptr &bundleOption, + const int32_t notificationId, const sptr &buttonOption) override; + /** * @brief Delete notification. * @@ -373,6 +385,17 @@ public: ErrCode Subscribe(const sptr &subscriber, const sptr &info) override; + /** + * @brief Subscribes notifications. + * + * @param subscriber Indicates the subscriber. + * @param info Indicates the NotificationSubscribeInfo object. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode SubscribeLocalLiveView(const sptr &subscriber, + const sptr &info) override; + + /** * @brief Unsubscribes notifications. * diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index e901da196..2b1317a6c 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -20,6 +20,7 @@ #include #include "ans_manager_interface.h" +#include "ans_subscriber_local_live_view_interface.h" #include "distributed_notification_service_ipc_interface_code.h" #include "iremote_stub.h" @@ -250,6 +251,18 @@ public: */ virtual ErrCode HasNotificationPolicyAccessPermission(bool &granted) override; + /** + * @brief Trigger the local activity after the button has been clicked. + * @note Your application must have platform signature to use this method. + * + * @param bundleOption Indicates the bundle name and uid of the application whose notifications has been clicked. + * @param notificationId Indicates the id of the notification. + * @param buttonOption Indicates which button has been clicked. + * @return Returns trigger localLiveView result. + */ + virtual ErrCode TriggerLocalLiveView(const sptr &bundleOption, + const int32_t notificationId, const sptr &buttonOption) override; + /** * @brief Delete notification. * @@ -391,6 +404,9 @@ public: virtual ErrCode Subscribe( const sptr &subscriber, const sptr &info) override; + virtual ErrCode SubscribeLocalLiveView( + const sptr &subscriber, const sptr &info) override; + /** * @brief Unsubscribes notifications. * @@ -760,6 +776,8 @@ private: ErrCode HandleSetBadgeNumber(MessageParcel &data, MessageParcel &reply); ErrCode HandleRegisterPushCallback(MessageParcel &data, MessageParcel &reply); ErrCode HandleUnregisterPushCallback(MessageParcel &data, MessageParcel &reply); + ErrCode HandleSubscribeLocalLiveView(MessageParcel &data, MessageParcel &reply); + ErrCode HandleTriggerLocalLiveView(MessageParcel &data, MessageParcel &reply); template bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &reply, ErrCode &result) diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index b91ea0dec..3ea593bd3 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -21,6 +21,7 @@ #include "ans_manager_death_recipient.h" #include "ans_manager_interface.h" #include "notification_subscriber.h" +#include "notification_local_live_view_subscriber.h" namespace OHOS { namespace Notification { @@ -297,6 +298,22 @@ public: */ ErrCode SubscribeNotification(const NotificationSubscriber &subscriber); + /** + * @brief Subscribes to notifications from all applications. This method can be called only by applications + * with required system permissions. + * @note To subscribe to a notification, inherit the {NotificationSubscriber} class, override its + * callback methods and create a subscriber. The subscriber will be used as a parameter of this method. + * After the notification is published, subscribers that meet the filter criteria can receive the + * notification. To subscribe to notifications published only by specified sources, for example, notifications from + * certain applications, call the {SubscribeNotification(NotificationSubscriber, NotificationSubscribeInfo)} + * method. + * + * @param subscriber Indicates the {NotificationSubscriber} to receive notifications. + * This parameter must be specified. + * @return Returns subscribe notification result. + */ + ErrCode SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber &subscriber); + /** * @brief Subscribes to all notifications based on the filtering criteria. This method can be called only * by applications with required system permissions. @@ -347,6 +364,18 @@ public: */ ErrCode UnSubscribeNotification(NotificationSubscriber &subscriber, NotificationSubscribeInfo subscribeInfo); + /** + * @brief Trigger the local activity after the button has been clicked. + * @note Your application must have platform signature to use this method. + * + * @param bundleOption Indicates the bundle name and uid of the application whose notifications has been clicked. + * @param notificationId Indicates the id of the notification. + * @param buttonOption Indicates which button has been clicked. + * @return Returns trigger localLiveView result. + */ + ErrCode TriggerLocalLiveView(const NotificationBundleOption &bundleOption, + const int32_t notificationId, const NotificationButtonOption &buttonOption); + /** * @brief Removes a specified removable notification of other applications. * @note Your application must have platform signature to use this method. diff --git a/frameworks/core/include/ans_subscriber_local_live_view_interface.h b/frameworks/core/include/ans_subscriber_local_live_view_interface.h new file mode 100644 index 000000000..44cd09767 --- /dev/null +++ b/frameworks/core/include/ans_subscriber_local_live_view_interface.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_LOCAL_LIVE_VIEW_SUBSCRIBER_INTERFACE_H +#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_LOCAL_LIVE_VIEW_SUBSCRIBER_INTERFACE_H + +#include "iremote_broker.h" + +#include "badge_number_callback_data.h" +#include "enabled_notification_callback_data.h" +#include "notification.h" +#include "notification_constant.h" +#include "notification_do_not_disturb_date.h" +#include "notification_request.h" +#include "notification_sorting.h" +#include "notification_sorting_map.h" + +namespace OHOS { +namespace Notification { +class AnsSubscriberLocalLiveViewInterface : public IRemoteBroker { +public: + AnsSubscriberLocalLiveViewInterface() = default; + virtual ~AnsSubscriberLocalLiveViewInterface() override = default; + DISALLOW_COPY_AND_MOVE(AnsSubscriberLocalLiveViewInterface); + + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Notification.AnsSubscriberLocalLiveViewInterface"); + + /** + * @brief The callback function for the subscriber to establish a connection. + */ + virtual void OnConnected() = 0; + + /** + * @brief The callback function for subscriber disconnected. + */ + virtual void OnDisconnected() = 0; + + virtual void OnResponse(int32_t notificationId, sptr buttonOption) = 0; + +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_LOCAL_LIVE_VIEW_SUBSCRIBER_INTERFACE_H diff --git a/frameworks/core/include/ans_subscriber_local_live_view_proxy.h b/frameworks/core/include/ans_subscriber_local_live_view_proxy.h new file mode 100644 index 000000000..36cf7d8c8 --- /dev/null +++ b/frameworks/core/include/ans_subscriber_local_live_view_proxy.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_SYSTEM_ACTIVITY_PROXY_H +#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_SYSTEM_ACTIVITY_PROXY_H + +#include "ans_subscriber_local_live_view_interface.h" +#include "distributed_notification_service_ipc_interface_code.h" +#include "iremote_proxy.h" + +namespace OHOS { +namespace Notification { +class AnsSubscriberLocalLiveViewProxy : public IRemoteProxy { +public: + AnsSubscriberLocalLiveViewProxy() = delete; + explicit AnsSubscriberLocalLiveViewProxy(const sptr &impl); + ~AnsSubscriberLocalLiveViewProxy() override; + DISALLOW_COPY_AND_MOVE(AnsSubscriberLocalLiveViewProxy); + + /** + * @brief The callback function for the subscriber to establish a connection. + */ + void OnConnected() override; + + /** + * @brief The callback function for subscriber disconnected. + */ + void OnDisconnected() override; + + void OnResponse(int32_t notificationId, sptr buttonOption) override; + +private: + ErrCode InnerTransact(NotificationInterfaceCode code, MessageOption &flags, MessageParcel &data, MessageParcel &reply); + static inline BrokerDelegator delegator_; + + template + bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &data) + { + if (!data.WriteInt32(parcelableVector.size())) { + ANS_LOGE("write ParcelableVector size failed"); + return false; + } + + for (auto &parcelable : parcelableVector) { + if (!data.WriteStrongParcelable(parcelable)) { + ANS_LOGE("write ParcelableVector failed"); + return false; + } + } + return true; + } +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_SYSTEM_ACTIVITY_PROXY_H diff --git a/frameworks/core/include/ans_subscriber_local_live_view_stub.h b/frameworks/core/include/ans_subscriber_local_live_view_stub.h new file mode 100644 index 000000000..63e9080ca --- /dev/null +++ b/frameworks/core/include/ans_subscriber_local_live_view_stub.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_LOCAL_LIVE_VIEW_STUB_H +#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_LOCAL_LIVE_VIEW_STUB_H + +#include "ans_subscriber_local_live_view_interface.h" +#include "distributed_notification_service_ipc_interface_code.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace Notification { +class AnsSubscriberLocalLiveViewStub : public IRemoteStub { +public: + AnsSubscriberLocalLiveViewStub(); + ~AnsSubscriberLocalLiveViewStub() override; + DISALLOW_COPY_AND_MOVE(AnsSubscriberLocalLiveViewStub); + + /** + * @brief Handle remote request. + * + * @param data Indicates the input parcel. + * @param reply Indicates the output parcel. + * @param option Indicates the message option. + * @return Returns ERR_OK on success, others on failure. + */ + virtual int32_t OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + + /** + * @brief The callback function for the subscriber to establish a connection. + */ + void OnConnected() override; + + /** + * @brief The callback function for subscriber disconnected. + */ + void OnDisconnected() override; + + void OnResponse(int32_t notificationId, sptr buttonOption) override; + +private: + std::map> interfaces_; + + ErrCode HandleOnConnected(MessageParcel &data, MessageParcel &reply); + ErrCode HandleOnDisconnected(MessageParcel &data, MessageParcel &reply); + ErrCode HandleOnResponse(MessageParcel &data, MessageParcel &reply); + + template + bool ReadParcelableVector(std::vector> &parcelableInfos, MessageParcel &data); +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_LOCAL_LIVE_VIEW_STUB_H diff --git a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h index 959baaa8d..78f24ac5d 100644 --- a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h +++ b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h @@ -117,6 +117,9 @@ namespace Notification { ON_BADGE_CHANGED, // push_callback_interface ON_CHECK_NOTIFICATION, + ON_RESPONSE, + SUBSCRIBE_LOCAL_LIVE_VIEW_NOTIFICATION, + TRIGGER_LOCAL_LIVE_VIEW_NOTIFICATION, }; } } diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index e50a8445f..f2acb74e3 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -18,7 +18,9 @@ #include "ans_const_define.h" #include "ans_inner_errors.h" #include "ans_log_wrapper.h" +#include "ans_subscriber_local_live_view_interface.h" #include "distributed_notification_service_ipc_interface_code.h" +#include "errors.h" #include "message_option.h" #include "message_parcel.h" #include "parcel.h" @@ -739,6 +741,51 @@ ErrCode AnsManagerProxy::HasNotificationPolicyAccessPermission(bool &granted) return result; } +ErrCode AnsManagerProxy::TriggerLocalLiveView(const sptr &bundleOption, + const int32_t notificationId, const sptr &buttonOption) +{ + if (bundleOption == nullptr) { + ANS_LOGE("[TriggerLocalLiveView] fail: bundle is empty."); + return ERR_ANS_INVALID_PARAM; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("[TriggerLocalLiveView] fail:, write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteStrongParcelable(bundleOption)) { + ANS_LOGE("[TriggerLocalLiveView] fail:: write bundle failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(notificationId)) { + ANS_LOGE("[TriggerLocalLiveView] fail: write notificationId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteStrongParcelable(buttonOption)) { + ANS_LOGE("[TriggerLocalLiveView] fail: write label failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::TRIGGER_LOCAL_LIVE_VIEW_NOTIFICATION, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[TriggerLocalLiveView] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[TriggerLocalLiveView] fail: read result error."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + ErrCode AnsManagerProxy::RemoveNotification(const sptr &bundleOption, int32_t notificationId, const std::string &label, int32_t removeReason) { @@ -1358,6 +1405,53 @@ ErrCode AnsManagerProxy::Subscribe(const sptr &subscribe return result; } +ErrCode AnsManagerProxy::SubscribeLocalLiveView(const sptr &subscriber, const sptr &info) +{ + if (subscriber == nullptr) { + ANS_LOGE("[Subscribe] fail: subscriber is empty."); + return ERR_ANS_INVALID_PARAM; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("[Subscribe] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + bool ret = data.WriteRemoteObject(subscriber->AsObject()); + if (!ret) { + ANS_LOGE("[Subscribe] fail: write subscriber failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteBool(info != nullptr)) { + ANS_LOGE("[Subscribe] fail: write isSubcribeInfo failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (info != nullptr) { + if (!data.WriteParcelable(info)) { + ANS_LOGE("[Subscribe] fail: write subcribeInfo failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::SUBSCRIBE_LOCAL_LIVE_VIEW_NOTIFICATION, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[Subscribe] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[Subscribe] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + ErrCode AnsManagerProxy::Unsubscribe( const sptr &subscriber, const sptr &info) { diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index d34a92c41..2c7b5c090 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -17,8 +17,10 @@ #include "ans_const_define.h" #include "ans_inner_errors.h" #include "ans_log_wrapper.h" +#include "ans_subscriber_local_live_view_interface.h" #include "message_option.h" #include "message_parcel.h" +#include "notification_button_option.h" #include "parcel.h" #include "reminder_request_alarm.h" #include "reminder_request_calendar.h" @@ -251,6 +253,10 @@ const std::map bundleOption = data.ReadStrongParcelable(); + if (bundleOption == nullptr) { + ANS_LOGE("[HandleTriggerLocalLiveView] fail: read bundle failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + int32_t notificationId = 0; + if (!data.ReadInt32(notificationId)) { + ANS_LOGE("[HandleTriggerLocalLiveView] fail: read notificationId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + sptr buttonOption = data.ReadStrongParcelable(); + if (buttonOption == nullptr) { + ANS_LOGE("[HandleTriggerLocalLiveView] fail: read button failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + ErrCode result = TriggerLocalLiveView(bundleOption, notificationId, buttonOption); + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleTriggerLocalLiveView] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + return ERR_OK; +} + ErrCode AnsManagerStub::HandleRemoveNotification(MessageParcel &data, MessageParcel &reply) { sptr bundleOption = data.ReadStrongParcelable(); @@ -1103,6 +1137,37 @@ ErrCode AnsManagerStub::HandleSubscribe(MessageParcel &data, MessageParcel &repl return ERR_OK; } +ErrCode AnsManagerStub::HandleSubscribeLocalLiveView(MessageParcel &data, MessageParcel &reply) +{ + sptr subscriber = data.ReadRemoteObject(); + if (subscriber == nullptr) { + ANS_LOGE("[HandleSubscribeLocalLiveView] fail: read subscriber failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + bool subcribeInfo = false; + if (!data.ReadBool(subcribeInfo)) { + ANS_LOGE("[HandleSubscribeLocalLiveView] fail: read isSubcribeInfo failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + sptr info = nullptr; + if (subcribeInfo) { + info = data.ReadParcelable(); + if (info == nullptr) { + ANS_LOGE("[HandleSubscribeLocalLiveView] fail: read info failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + } + + ErrCode result = SubscribeLocalLiveView(iface_cast(subscriber), info); + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleSubscribeLocalLiveView] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + return ERR_OK; +} + ErrCode AnsManagerStub::HandleUnsubscribe(MessageParcel &data, MessageParcel &reply) { sptr subscriber = data.ReadRemoteObject(); @@ -1884,6 +1949,13 @@ ErrCode AnsManagerStub::HasNotificationPolicyAccessPermission(bool &granted) return ERR_INVALID_OPERATION; } +ErrCode AnsManagerStub::TriggerLocalLiveView(const sptr &bundleOption, + const int32_t notificationId, const sptr &buttonOption) +{ + ANS_LOGE("AnsManagerStub::TriggerLocalLiveView called!"); + return ERR_INVALID_OPERATION; +} + ErrCode AnsManagerStub::RemoveNotification(const sptr &bundleOption, int notificationId, const std::string &label, int32_t removeReason) { @@ -1985,6 +2057,13 @@ ErrCode AnsManagerStub::Subscribe(const sptr &subscriber return ERR_INVALID_OPERATION; } +ErrCode AnsManagerStub::SubscribeLocalLiveView(const sptr &subscriber, + const sptr &info) +{ + ANS_LOGE("AnsManagerStub::SubscribeLocalLiveView called!"); + return ERR_INVALID_OPERATION; +} + ErrCode AnsManagerStub::Unsubscribe(const sptr &subscriber, const sptr &info) { diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index c9b79d153..6aaf042c6 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -19,6 +19,8 @@ #include "ans_log_wrapper.h" #include "hitrace_meter.h" #include "iservice_registry.h" +#include "notification_button_option.h" +#include "notification_local_live_view_subscriber.h" #include "reminder_request_alarm.h" #include "reminder_request_calendar.h" #include "reminder_request_timer.h" @@ -377,6 +379,22 @@ ErrCode AnsNotification::SubscribeNotification(const NotificationSubscriber &sub return ansManagerProxy_->Subscribe(subscriberSptr, nullptr); } +ErrCode AnsNotification::SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber &subscriber) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + sptr subscriberSptr = subscriber.GetImpl(); + if (subscriberSptr == nullptr) { + ANS_LOGE("Failed to subscribe with SubscriberImpl null ptr."); + return ERR_ANS_INVALID_PARAM; + } + return ansManagerProxy_->SubscribeLocalLiveView(subscriberSptr, nullptr); +} + ErrCode AnsNotification::SubscribeNotification( const NotificationSubscriber &subscriber, const NotificationSubscribeInfo &subscribeInfo) { @@ -439,6 +457,30 @@ ErrCode AnsNotification::UnSubscribeNotification( return ansManagerProxy_->Unsubscribe(subscriberSptr, sptrInfo); } +ErrCode AnsNotification::TriggerLocalLiveView(const NotificationBundleOption &bundleOption, + const int32_t notificationId, const NotificationButtonOption &buttonOption) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + if (bundleOption.GetBundleName().empty()) { + ANS_LOGE("Invalid bundle name."); + return ERR_ANS_INVALID_PARAM; + } + + if (buttonOption.GetButtonName().empty()) { + ANS_LOGE("Invalid button name."); + return ERR_ANS_INVALID_PARAM; + } + + if (!GetAnsManagerProxy()) { + ANS_LOGE("Fail to GetAnsManagerProxy."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); + sptr button(new (std::nothrow) NotificationButtonOption(buttonOption)); + return ansManagerProxy_->TriggerLocalLiveView(bo, notificationId, button); +} + ErrCode AnsNotification::RemoveNotification(const std::string &key, int32_t removeReason) { HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); diff --git a/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp b/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp new file mode 100644 index 000000000..7a1cac44c --- /dev/null +++ b/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ans_subscriber_local_live_view_proxy.h" + +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" +#include "message_option.h" +#include "message_parcel.h" + +namespace OHOS { +namespace Notification { +AnsSubscriberLocalLiveViewProxy::AnsSubscriberLocalLiveViewProxy(const sptr &impl) : IRemoteProxy(impl) +{} + +AnsSubscriberLocalLiveViewProxy::~AnsSubscriberLocalLiveViewProxy() +{} + +ErrCode AnsSubscriberLocalLiveViewProxy::InnerTransact( + NotificationInterfaceCode code, MessageOption &flags, MessageParcel &data, MessageParcel &reply) +{ + auto remote = Remote(); + if (remote == nullptr) { + ANS_LOGE("[InnerTransact] fail: get Remote fail code %{public}u", code); + return ERR_DEAD_OBJECT; + } + + int32_t err = remote->SendRequest(static_cast(code), data, reply, flags); + switch (err) { + case NO_ERROR: { + return ERR_OK; + } + case DEAD_OBJECT: { + ANS_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code); + return ERR_DEAD_OBJECT; + } + default: { + ANS_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code); + return ERR_ANS_TRANSACT_FAILED; + } + } +} + +void AnsSubscriberLocalLiveViewProxy::OnConnected() +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsSubscriberLocalLiveViewProxy::GetDescriptor())) { + ANS_LOGE("[OnConnected] fail: write interface token failed."); + return; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_ASYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::ON_CONNECTED, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[OnConnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + return; + } +} + +void AnsSubscriberLocalLiveViewProxy::OnDisconnected() +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsSubscriberLocalLiveViewProxy::GetDescriptor())) { + ANS_LOGE("[OnDisconnected] fail: write interface token failed."); + return; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_ASYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::ON_DISCONNECTED, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[OnDisconnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + return; + } +} + + +void AnsSubscriberLocalLiveViewProxy::OnResponse(int32_t notificationId, sptr buttonOption) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsSubscriberLocalLiveViewProxy::GetDescriptor())) { + ANS_LOGE("[OnResponse] fail: write interface token failed."); + return; + } + + if (!data.WriteInt32(notificationId)) { + ANS_LOGE("[OnResponse] fail: write notificationId failed"); + return; + } + + if (!data.WriteParcelable(buttonOption)) { + ANS_LOGE("[OnResponse] fail: write buttonName failed"); + return; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_ASYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::ON_RESPONSE, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[OnResponse] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + return; + } +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/core/src/ans_subscriber_local_live_view_stub.cpp b/frameworks/core/src/ans_subscriber_local_live_view_stub.cpp new file mode 100644 index 000000000..f4cdb5469 --- /dev/null +++ b/frameworks/core/src/ans_subscriber_local_live_view_stub.cpp @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ans_subscriber_local_live_view_stub.h" + +#include "ans_const_define.h" +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" +#include "message_option.h" +#include "message_parcel.h" +#include "notification_bundle_option.h" +#include "notification_button_option.h" +#include "parcel.h" +#include "refbase.h" +#include + +namespace OHOS { +namespace Notification { +AnsSubscriberLocalLiveViewStub::AnsSubscriberLocalLiveViewStub() +{ + interfaces_.emplace(NotificationInterfaceCode::ON_CONNECTED, + std::bind(&AnsSubscriberLocalLiveViewStub::HandleOnConnected, this, std::placeholders::_1, std::placeholders::_2)); + interfaces_.emplace(NotificationInterfaceCode::ON_DISCONNECTED, + std::bind(&AnsSubscriberLocalLiveViewStub::HandleOnDisconnected, this, std::placeholders::_1, std::placeholders::_2)); + interfaces_.emplace(NotificationInterfaceCode::ON_RESPONSE, + std::bind(&AnsSubscriberLocalLiveViewStub::HandleOnResponse, this, std::placeholders::_1, std::placeholders::_2)); +} + +AnsSubscriberLocalLiveViewStub::~AnsSubscriberLocalLiveViewStub() +{} + +int32_t AnsSubscriberLocalLiveViewStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &flags) +{ + std::u16string descriptor = AnsSubscriberLocalLiveViewStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!"); + return OBJECT_NULL; + } + + auto it = interfaces_.find(static_cast(code)); + if (it == interfaces_.end()) { + ANS_LOGW("[OnRemoteRequest] fail: unknown code!"); + return IPCObjectStub::OnRemoteRequest(code, data, reply, flags); + } + + auto fun = it->second; + if (fun == nullptr) { + ANS_LOGW("[OnRemoteRequest] fail: not find function!"); + return IPCObjectStub::OnRemoteRequest(code, data, reply, flags); + } + + fun(data, reply); + return NO_ERROR; +} + +ErrCode AnsSubscriberLocalLiveViewStub::HandleOnConnected(MessageParcel &data, MessageParcel &reply) +{ + OnConnected(); + return ERR_OK; +} + +ErrCode AnsSubscriberLocalLiveViewStub::HandleOnDisconnected(MessageParcel &data, MessageParcel &reply) +{ + OnDisconnected(); + return ERR_OK; +} + +template +bool AnsSubscriberLocalLiveViewStub::ReadParcelableVector(std::vector> &parcelableInfos, MessageParcel &data) +{ + int32_t infoSize = 0; + if (!data.ReadInt32(infoSize)) { + ANS_LOGE("read Parcelable size failed."); + return false; + } + + parcelableInfos.clear(); + infoSize = (infoSize < MAX_PARCELABLE_VECTOR_NUM) ? infoSize : MAX_PARCELABLE_VECTOR_NUM; + for (int32_t index = 0; index < infoSize; index++) { + sptr info = data.ReadStrongParcelable(); + if (info == nullptr) { + ANS_LOGE("read Parcelable infos failed."); + return false; + } + parcelableInfos.emplace_back(info); + } + + return true; +} + +ErrCode AnsSubscriberLocalLiveViewStub::HandleOnResponse(MessageParcel &data, MessageParcel &reply) +{ + int32_t notificationId = 0; + if (!data.ReadInt32(notificationId)) { + ANS_LOGE("[HandleOnResponse] fail : read notificationId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + sptr buttonOption = nullptr; + buttonOption = data.ReadParcelable(); + if (buttonOption == nullptr) { + ANS_LOGE("[HandleOnResponse] fail : read buttonOption failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + OnResponse(notificationId, buttonOption); + return ERR_OK; +} + +void AnsSubscriberLocalLiveViewStub::OnConnected() +{} + +void AnsSubscriberLocalLiveViewStub::OnDisconnected() +{} + +void AnsSubscriberLocalLiveViewStub::OnResponse(int32_t notificationId, sptr buttonOption) +{} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index d0457ea46..955a2079d 100755 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -246,6 +246,12 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode SubscribeLocalActivity(const sptr &subscriber, + const sptr &info) override + { + return ERR_ANS_INVALID_PARAM; + } + ErrCode Unsubscribe( const sptr &subscriber, const sptr &info) override { @@ -425,6 +431,12 @@ public: { return ERR_ANS_INVALID_PARAM; } + + ErrCode TriggerLocalActivity(const sptr &bundleOption, + const int32_t notificationId, const sptr &buttonOption) override + { + return ERR_ANS_INVALID_PARAM; + } }; class AnsNotificationBranchTest : public testing::Test { -- Gitee From d7ebd08cb91ff9c0c2274e25141744564be2fba8 Mon Sep 17 00:00:00 2001 From: markYao Date: Sun, 22 Oct 2023 20:23:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E5=AE=9E=E5=86=B5?= =?UTF-8?q?=E7=AA=97interface&service=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: markYao --- .../core/include/ans_manager_interface.h | 23 -- frameworks/core/include/ans_manager_proxy.h | 23 -- frameworks/core/include/ans_manager_stub.h | 18 -- frameworks/core/include/ans_notification.h | 29 -- ...ans_subscriber_local_live_view_interface.h | 56 ---- .../ans_subscriber_local_live_view_proxy.h | 68 ----- .../ans_subscriber_local_live_view_stub.h | 67 ----- ..._notification_service_ipc_interface_code.h | 3 - frameworks/core/src/ans_manager_proxy.cpp | 94 ------ frameworks/core/src/ans_manager_stub.cpp | 79 ----- frameworks/core/src/ans_notification.cpp | 42 --- .../ans_subscriber_local_live_view_proxy.cpp | 118 -------- .../ans_subscriber_local_live_view_stub.cpp | 131 --------- .../ans_notification_branch_test.cpp | 12 - interfaces/inner_api/notification_constant.h | 1 + interfaces/inner_api/notification_content.h | 17 +- interfaces/inner_api/notification_helper.h | 27 ++ services/ans/BUILD.gn | 1 + .../include/advanced_notification_service.h | 22 ++ .../ans/src/advanced_notification_service.cpp | 79 +++++ ...ion_local_live_view_subscriber_manager.cpp | 270 ++++++++++++++++++ 21 files changed, 416 insertions(+), 764 deletions(-) delete mode 100644 frameworks/core/include/ans_subscriber_local_live_view_interface.h delete mode 100644 frameworks/core/include/ans_subscriber_local_live_view_proxy.h delete mode 100644 frameworks/core/include/ans_subscriber_local_live_view_stub.h delete mode 100644 frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp delete mode 100644 frameworks/core/src/ans_subscriber_local_live_view_stub.cpp create mode 100644 services/ans/src/notification_local_live_view_subscriber_manager.cpp diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index 3a595b2cb..6159fd145 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -20,7 +20,6 @@ #include #include "ans_subscriber_interface.h" -#include "ans_subscriber_local_live_view_interface.h" #include "iremote_broker.h" #include "notification_bundle_option.h" #include "notification_constant.h" @@ -247,18 +246,6 @@ public: */ virtual ErrCode HasNotificationPolicyAccessPermission(bool &granted) = 0; - /** - * @brief Trigger the local activity after the button has been clicked. - * @note Your application must have platform signature to use this method. - * - * @param bundleOption Indicates the bundle name and uid of the application whose notifications has been clicked. - * @param notificationId Indicates the id of the notification. - * @param buttonOption Indicates which button has been clicked. - * @return Returns trigger localLiveView result. - */ - virtual ErrCode TriggerLocalLiveView(const sptr &bundleOption, - const int32_t notificationId, const sptr &buttonOption) = 0; - /** * @brief Delete notification based on key. * @@ -397,16 +384,6 @@ public: virtual ErrCode Subscribe(const sptr &subscriber, const sptr &info) = 0; - /** - * @brief Subscribes system activity notifications. - * - * @param subscriber Indicates the subscriber. - * @param info Indicates the NotificationSubscribeInfo object. - * @return Returns ERR_OK on success, others on failure. - */ - virtual ErrCode SubscribeLocalLiveView(const sptr &subscriber, - const sptr &info) = 0; - /** * @brief Unsubscribes notifications. * diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index f938d548b..c31fc4330 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -235,18 +235,6 @@ public: */ ErrCode HasNotificationPolicyAccessPermission(bool &granted) override; - /** - * @brief Trigger the local activity after the button has been clicked. - * @note Your application must have platform signature to use this method. - * - * @param bundleOption Indicates the bundle name and uid of the application whose notifications has been clicked. - * @param notificationId Indicates the id of the notification. - * @param buttonOption Indicates which button has been clicked. - * @return Returns trigger localLiveView result. - */ - ErrCode TriggerLocalLiveView(const sptr &bundleOption, - const int32_t notificationId, const sptr &buttonOption) override; - /** * @brief Delete notification. * @@ -385,17 +373,6 @@ public: ErrCode Subscribe(const sptr &subscriber, const sptr &info) override; - /** - * @brief Subscribes notifications. - * - * @param subscriber Indicates the subscriber. - * @param info Indicates the NotificationSubscribeInfo object. - * @return Returns ERR_OK on success, others on failure. - */ - ErrCode SubscribeLocalLiveView(const sptr &subscriber, - const sptr &info) override; - - /** * @brief Unsubscribes notifications. * diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index 2b1317a6c..e901da196 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -20,7 +20,6 @@ #include #include "ans_manager_interface.h" -#include "ans_subscriber_local_live_view_interface.h" #include "distributed_notification_service_ipc_interface_code.h" #include "iremote_stub.h" @@ -251,18 +250,6 @@ public: */ virtual ErrCode HasNotificationPolicyAccessPermission(bool &granted) override; - /** - * @brief Trigger the local activity after the button has been clicked. - * @note Your application must have platform signature to use this method. - * - * @param bundleOption Indicates the bundle name and uid of the application whose notifications has been clicked. - * @param notificationId Indicates the id of the notification. - * @param buttonOption Indicates which button has been clicked. - * @return Returns trigger localLiveView result. - */ - virtual ErrCode TriggerLocalLiveView(const sptr &bundleOption, - const int32_t notificationId, const sptr &buttonOption) override; - /** * @brief Delete notification. * @@ -404,9 +391,6 @@ public: virtual ErrCode Subscribe( const sptr &subscriber, const sptr &info) override; - virtual ErrCode SubscribeLocalLiveView( - const sptr &subscriber, const sptr &info) override; - /** * @brief Unsubscribes notifications. * @@ -776,8 +760,6 @@ private: ErrCode HandleSetBadgeNumber(MessageParcel &data, MessageParcel &reply); ErrCode HandleRegisterPushCallback(MessageParcel &data, MessageParcel &reply); ErrCode HandleUnregisterPushCallback(MessageParcel &data, MessageParcel &reply); - ErrCode HandleSubscribeLocalLiveView(MessageParcel &data, MessageParcel &reply); - ErrCode HandleTriggerLocalLiveView(MessageParcel &data, MessageParcel &reply); template bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &reply, ErrCode &result) diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index 3ea593bd3..b91ea0dec 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -21,7 +21,6 @@ #include "ans_manager_death_recipient.h" #include "ans_manager_interface.h" #include "notification_subscriber.h" -#include "notification_local_live_view_subscriber.h" namespace OHOS { namespace Notification { @@ -298,22 +297,6 @@ public: */ ErrCode SubscribeNotification(const NotificationSubscriber &subscriber); - /** - * @brief Subscribes to notifications from all applications. This method can be called only by applications - * with required system permissions. - * @note To subscribe to a notification, inherit the {NotificationSubscriber} class, override its - * callback methods and create a subscriber. The subscriber will be used as a parameter of this method. - * After the notification is published, subscribers that meet the filter criteria can receive the - * notification. To subscribe to notifications published only by specified sources, for example, notifications from - * certain applications, call the {SubscribeNotification(NotificationSubscriber, NotificationSubscribeInfo)} - * method. - * - * @param subscriber Indicates the {NotificationSubscriber} to receive notifications. - * This parameter must be specified. - * @return Returns subscribe notification result. - */ - ErrCode SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber &subscriber); - /** * @brief Subscribes to all notifications based on the filtering criteria. This method can be called only * by applications with required system permissions. @@ -364,18 +347,6 @@ public: */ ErrCode UnSubscribeNotification(NotificationSubscriber &subscriber, NotificationSubscribeInfo subscribeInfo); - /** - * @brief Trigger the local activity after the button has been clicked. - * @note Your application must have platform signature to use this method. - * - * @param bundleOption Indicates the bundle name and uid of the application whose notifications has been clicked. - * @param notificationId Indicates the id of the notification. - * @param buttonOption Indicates which button has been clicked. - * @return Returns trigger localLiveView result. - */ - ErrCode TriggerLocalLiveView(const NotificationBundleOption &bundleOption, - const int32_t notificationId, const NotificationButtonOption &buttonOption); - /** * @brief Removes a specified removable notification of other applications. * @note Your application must have platform signature to use this method. diff --git a/frameworks/core/include/ans_subscriber_local_live_view_interface.h b/frameworks/core/include/ans_subscriber_local_live_view_interface.h deleted file mode 100644 index 44cd09767..000000000 --- a/frameworks/core/include/ans_subscriber_local_live_view_interface.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_LOCAL_LIVE_VIEW_SUBSCRIBER_INTERFACE_H -#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_LOCAL_LIVE_VIEW_SUBSCRIBER_INTERFACE_H - -#include "iremote_broker.h" - -#include "badge_number_callback_data.h" -#include "enabled_notification_callback_data.h" -#include "notification.h" -#include "notification_constant.h" -#include "notification_do_not_disturb_date.h" -#include "notification_request.h" -#include "notification_sorting.h" -#include "notification_sorting_map.h" - -namespace OHOS { -namespace Notification { -class AnsSubscriberLocalLiveViewInterface : public IRemoteBroker { -public: - AnsSubscriberLocalLiveViewInterface() = default; - virtual ~AnsSubscriberLocalLiveViewInterface() override = default; - DISALLOW_COPY_AND_MOVE(AnsSubscriberLocalLiveViewInterface); - - DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Notification.AnsSubscriberLocalLiveViewInterface"); - - /** - * @brief The callback function for the subscriber to establish a connection. - */ - virtual void OnConnected() = 0; - - /** - * @brief The callback function for subscriber disconnected. - */ - virtual void OnDisconnected() = 0; - - virtual void OnResponse(int32_t notificationId, sptr buttonOption) = 0; - -}; -} // namespace Notification -} // namespace OHOS - -#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_LOCAL_LIVE_VIEW_SUBSCRIBER_INTERFACE_H diff --git a/frameworks/core/include/ans_subscriber_local_live_view_proxy.h b/frameworks/core/include/ans_subscriber_local_live_view_proxy.h deleted file mode 100644 index 36cf7d8c8..000000000 --- a/frameworks/core/include/ans_subscriber_local_live_view_proxy.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_SYSTEM_ACTIVITY_PROXY_H -#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_SYSTEM_ACTIVITY_PROXY_H - -#include "ans_subscriber_local_live_view_interface.h" -#include "distributed_notification_service_ipc_interface_code.h" -#include "iremote_proxy.h" - -namespace OHOS { -namespace Notification { -class AnsSubscriberLocalLiveViewProxy : public IRemoteProxy { -public: - AnsSubscriberLocalLiveViewProxy() = delete; - explicit AnsSubscriberLocalLiveViewProxy(const sptr &impl); - ~AnsSubscriberLocalLiveViewProxy() override; - DISALLOW_COPY_AND_MOVE(AnsSubscriberLocalLiveViewProxy); - - /** - * @brief The callback function for the subscriber to establish a connection. - */ - void OnConnected() override; - - /** - * @brief The callback function for subscriber disconnected. - */ - void OnDisconnected() override; - - void OnResponse(int32_t notificationId, sptr buttonOption) override; - -private: - ErrCode InnerTransact(NotificationInterfaceCode code, MessageOption &flags, MessageParcel &data, MessageParcel &reply); - static inline BrokerDelegator delegator_; - - template - bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &data) - { - if (!data.WriteInt32(parcelableVector.size())) { - ANS_LOGE("write ParcelableVector size failed"); - return false; - } - - for (auto &parcelable : parcelableVector) { - if (!data.WriteStrongParcelable(parcelable)) { - ANS_LOGE("write ParcelableVector failed"); - return false; - } - } - return true; - } -}; -} // namespace Notification -} // namespace OHOS - -#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_SYSTEM_ACTIVITY_PROXY_H diff --git a/frameworks/core/include/ans_subscriber_local_live_view_stub.h b/frameworks/core/include/ans_subscriber_local_live_view_stub.h deleted file mode 100644 index 63e9080ca..000000000 --- a/frameworks/core/include/ans_subscriber_local_live_view_stub.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_LOCAL_LIVE_VIEW_STUB_H -#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_LOCAL_LIVE_VIEW_STUB_H - -#include "ans_subscriber_local_live_view_interface.h" -#include "distributed_notification_service_ipc_interface_code.h" -#include "iremote_stub.h" - -namespace OHOS { -namespace Notification { -class AnsSubscriberLocalLiveViewStub : public IRemoteStub { -public: - AnsSubscriberLocalLiveViewStub(); - ~AnsSubscriberLocalLiveViewStub() override; - DISALLOW_COPY_AND_MOVE(AnsSubscriberLocalLiveViewStub); - - /** - * @brief Handle remote request. - * - * @param data Indicates the input parcel. - * @param reply Indicates the output parcel. - * @param option Indicates the message option. - * @return Returns ERR_OK on success, others on failure. - */ - virtual int32_t OnRemoteRequest( - uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; - - /** - * @brief The callback function for the subscriber to establish a connection. - */ - void OnConnected() override; - - /** - * @brief The callback function for subscriber disconnected. - */ - void OnDisconnected() override; - - void OnResponse(int32_t notificationId, sptr buttonOption) override; - -private: - std::map> interfaces_; - - ErrCode HandleOnConnected(MessageParcel &data, MessageParcel &reply); - ErrCode HandleOnDisconnected(MessageParcel &data, MessageParcel &reply); - ErrCode HandleOnResponse(MessageParcel &data, MessageParcel &reply); - - template - bool ReadParcelableVector(std::vector> &parcelableInfos, MessageParcel &data); -}; -} // namespace Notification -} // namespace OHOS - -#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_SUBSCRIBER_LOCAL_LIVE_VIEW_STUB_H diff --git a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h index 78f24ac5d..959baaa8d 100644 --- a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h +++ b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h @@ -117,9 +117,6 @@ namespace Notification { ON_BADGE_CHANGED, // push_callback_interface ON_CHECK_NOTIFICATION, - ON_RESPONSE, - SUBSCRIBE_LOCAL_LIVE_VIEW_NOTIFICATION, - TRIGGER_LOCAL_LIVE_VIEW_NOTIFICATION, }; } } diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index f2acb74e3..e50a8445f 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -18,9 +18,7 @@ #include "ans_const_define.h" #include "ans_inner_errors.h" #include "ans_log_wrapper.h" -#include "ans_subscriber_local_live_view_interface.h" #include "distributed_notification_service_ipc_interface_code.h" -#include "errors.h" #include "message_option.h" #include "message_parcel.h" #include "parcel.h" @@ -741,51 +739,6 @@ ErrCode AnsManagerProxy::HasNotificationPolicyAccessPermission(bool &granted) return result; } -ErrCode AnsManagerProxy::TriggerLocalLiveView(const sptr &bundleOption, - const int32_t notificationId, const sptr &buttonOption) -{ - if (bundleOption == nullptr) { - ANS_LOGE("[TriggerLocalLiveView] fail: bundle is empty."); - return ERR_ANS_INVALID_PARAM; - } - - MessageParcel data; - if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { - ANS_LOGE("[TriggerLocalLiveView] fail:, write interface token failed."); - return ERR_ANS_PARCELABLE_FAILED; - } - - if (!data.WriteStrongParcelable(bundleOption)) { - ANS_LOGE("[TriggerLocalLiveView] fail:: write bundle failed"); - return ERR_ANS_PARCELABLE_FAILED; - } - - if (!data.WriteInt32(notificationId)) { - ANS_LOGE("[TriggerLocalLiveView] fail: write notificationId failed"); - return ERR_ANS_PARCELABLE_FAILED; - } - - if (!data.WriteStrongParcelable(buttonOption)) { - ANS_LOGE("[TriggerLocalLiveView] fail: write label failed"); - return ERR_ANS_PARCELABLE_FAILED; - } - - MessageParcel reply; - MessageOption option = {MessageOption::TF_SYNC}; - ErrCode result = InnerTransact(NotificationInterfaceCode::TRIGGER_LOCAL_LIVE_VIEW_NOTIFICATION, option, data, reply); - if (result != ERR_OK) { - ANS_LOGE("[TriggerLocalLiveView] fail: transact ErrCode=%{public}d", result); - return ERR_ANS_TRANSACT_FAILED; - } - - if (!reply.ReadInt32(result)) { - ANS_LOGE("[TriggerLocalLiveView] fail: read result error."); - return ERR_ANS_PARCELABLE_FAILED; - } - - return result; -} - ErrCode AnsManagerProxy::RemoveNotification(const sptr &bundleOption, int32_t notificationId, const std::string &label, int32_t removeReason) { @@ -1405,53 +1358,6 @@ ErrCode AnsManagerProxy::Subscribe(const sptr &subscribe return result; } -ErrCode AnsManagerProxy::SubscribeLocalLiveView(const sptr &subscriber, const sptr &info) -{ - if (subscriber == nullptr) { - ANS_LOGE("[Subscribe] fail: subscriber is empty."); - return ERR_ANS_INVALID_PARAM; - } - - MessageParcel data; - if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { - ANS_LOGE("[Subscribe] fail: write interface token failed."); - return ERR_ANS_PARCELABLE_FAILED; - } - - bool ret = data.WriteRemoteObject(subscriber->AsObject()); - if (!ret) { - ANS_LOGE("[Subscribe] fail: write subscriber failed."); - return ERR_ANS_PARCELABLE_FAILED; - } - - if (!data.WriteBool(info != nullptr)) { - ANS_LOGE("[Subscribe] fail: write isSubcribeInfo failed"); - return ERR_ANS_PARCELABLE_FAILED; - } - - if (info != nullptr) { - if (!data.WriteParcelable(info)) { - ANS_LOGE("[Subscribe] fail: write subcribeInfo failed"); - return ERR_ANS_PARCELABLE_FAILED; - } - } - - MessageParcel reply; - MessageOption option = {MessageOption::TF_SYNC}; - ErrCode result = InnerTransact(NotificationInterfaceCode::SUBSCRIBE_LOCAL_LIVE_VIEW_NOTIFICATION, option, data, reply); - if (result != ERR_OK) { - ANS_LOGE("[Subscribe] fail: transact ErrCode=%{public}d", result); - return ERR_ANS_TRANSACT_FAILED; - } - - if (!reply.ReadInt32(result)) { - ANS_LOGE("[Subscribe] fail: read result failed."); - return ERR_ANS_PARCELABLE_FAILED; - } - - return result; -} - ErrCode AnsManagerProxy::Unsubscribe( const sptr &subscriber, const sptr &info) { diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index 2c7b5c090..d34a92c41 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -17,10 +17,8 @@ #include "ans_const_define.h" #include "ans_inner_errors.h" #include "ans_log_wrapper.h" -#include "ans_subscriber_local_live_view_interface.h" #include "message_option.h" #include "message_parcel.h" -#include "notification_button_option.h" #include "parcel.h" #include "reminder_request_alarm.h" #include "reminder_request_calendar.h" @@ -253,10 +251,6 @@ const std::map bundleOption = data.ReadStrongParcelable(); - if (bundleOption == nullptr) { - ANS_LOGE("[HandleTriggerLocalLiveView] fail: read bundle failed."); - return ERR_ANS_PARCELABLE_FAILED; - } - - int32_t notificationId = 0; - if (!data.ReadInt32(notificationId)) { - ANS_LOGE("[HandleTriggerLocalLiveView] fail: read notificationId failed"); - return ERR_ANS_PARCELABLE_FAILED; - } - - sptr buttonOption = data.ReadStrongParcelable(); - if (buttonOption == nullptr) { - ANS_LOGE("[HandleTriggerLocalLiveView] fail: read button failed."); - return ERR_ANS_PARCELABLE_FAILED; - } - - ErrCode result = TriggerLocalLiveView(bundleOption, notificationId, buttonOption); - if (!reply.WriteInt32(result)) { - ANS_LOGE("[HandleTriggerLocalLiveView] fail: write result failed, ErrCode=%{public}d", result); - return ERR_ANS_PARCELABLE_FAILED; - } - return ERR_OK; -} - ErrCode AnsManagerStub::HandleRemoveNotification(MessageParcel &data, MessageParcel &reply) { sptr bundleOption = data.ReadStrongParcelable(); @@ -1137,37 +1103,6 @@ ErrCode AnsManagerStub::HandleSubscribe(MessageParcel &data, MessageParcel &repl return ERR_OK; } -ErrCode AnsManagerStub::HandleSubscribeLocalLiveView(MessageParcel &data, MessageParcel &reply) -{ - sptr subscriber = data.ReadRemoteObject(); - if (subscriber == nullptr) { - ANS_LOGE("[HandleSubscribeLocalLiveView] fail: read subscriber failed"); - return ERR_ANS_PARCELABLE_FAILED; - } - - bool subcribeInfo = false; - if (!data.ReadBool(subcribeInfo)) { - ANS_LOGE("[HandleSubscribeLocalLiveView] fail: read isSubcribeInfo failed"); - return ERR_ANS_PARCELABLE_FAILED; - } - - sptr info = nullptr; - if (subcribeInfo) { - info = data.ReadParcelable(); - if (info == nullptr) { - ANS_LOGE("[HandleSubscribeLocalLiveView] fail: read info failed"); - return ERR_ANS_PARCELABLE_FAILED; - } - } - - ErrCode result = SubscribeLocalLiveView(iface_cast(subscriber), info); - if (!reply.WriteInt32(result)) { - ANS_LOGE("[HandleSubscribeLocalLiveView] fail: write result failed, ErrCode=%{public}d", result); - return ERR_ANS_PARCELABLE_FAILED; - } - return ERR_OK; -} - ErrCode AnsManagerStub::HandleUnsubscribe(MessageParcel &data, MessageParcel &reply) { sptr subscriber = data.ReadRemoteObject(); @@ -1949,13 +1884,6 @@ ErrCode AnsManagerStub::HasNotificationPolicyAccessPermission(bool &granted) return ERR_INVALID_OPERATION; } -ErrCode AnsManagerStub::TriggerLocalLiveView(const sptr &bundleOption, - const int32_t notificationId, const sptr &buttonOption) -{ - ANS_LOGE("AnsManagerStub::TriggerLocalLiveView called!"); - return ERR_INVALID_OPERATION; -} - ErrCode AnsManagerStub::RemoveNotification(const sptr &bundleOption, int notificationId, const std::string &label, int32_t removeReason) { @@ -2057,13 +1985,6 @@ ErrCode AnsManagerStub::Subscribe(const sptr &subscriber return ERR_INVALID_OPERATION; } -ErrCode AnsManagerStub::SubscribeLocalLiveView(const sptr &subscriber, - const sptr &info) -{ - ANS_LOGE("AnsManagerStub::SubscribeLocalLiveView called!"); - return ERR_INVALID_OPERATION; -} - ErrCode AnsManagerStub::Unsubscribe(const sptr &subscriber, const sptr &info) { diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 6aaf042c6..c9b79d153 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -19,8 +19,6 @@ #include "ans_log_wrapper.h" #include "hitrace_meter.h" #include "iservice_registry.h" -#include "notification_button_option.h" -#include "notification_local_live_view_subscriber.h" #include "reminder_request_alarm.h" #include "reminder_request_calendar.h" #include "reminder_request_timer.h" @@ -379,22 +377,6 @@ ErrCode AnsNotification::SubscribeNotification(const NotificationSubscriber &sub return ansManagerProxy_->Subscribe(subscriberSptr, nullptr); } -ErrCode AnsNotification::SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber &subscriber) -{ - HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); - if (!GetAnsManagerProxy()) { - ANS_LOGE("GetAnsManagerProxy fail."); - return ERR_ANS_SERVICE_NOT_CONNECTED; - } - - sptr subscriberSptr = subscriber.GetImpl(); - if (subscriberSptr == nullptr) { - ANS_LOGE("Failed to subscribe with SubscriberImpl null ptr."); - return ERR_ANS_INVALID_PARAM; - } - return ansManagerProxy_->SubscribeLocalLiveView(subscriberSptr, nullptr); -} - ErrCode AnsNotification::SubscribeNotification( const NotificationSubscriber &subscriber, const NotificationSubscribeInfo &subscribeInfo) { @@ -457,30 +439,6 @@ ErrCode AnsNotification::UnSubscribeNotification( return ansManagerProxy_->Unsubscribe(subscriberSptr, sptrInfo); } -ErrCode AnsNotification::TriggerLocalLiveView(const NotificationBundleOption &bundleOption, - const int32_t notificationId, const NotificationButtonOption &buttonOption) -{ - HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); - if (bundleOption.GetBundleName().empty()) { - ANS_LOGE("Invalid bundle name."); - return ERR_ANS_INVALID_PARAM; - } - - if (buttonOption.GetButtonName().empty()) { - ANS_LOGE("Invalid button name."); - return ERR_ANS_INVALID_PARAM; - } - - if (!GetAnsManagerProxy()) { - ANS_LOGE("Fail to GetAnsManagerProxy."); - return ERR_ANS_SERVICE_NOT_CONNECTED; - } - - sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); - sptr button(new (std::nothrow) NotificationButtonOption(buttonOption)); - return ansManagerProxy_->TriggerLocalLiveView(bo, notificationId, button); -} - ErrCode AnsNotification::RemoveNotification(const std::string &key, int32_t removeReason) { HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); diff --git a/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp b/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp deleted file mode 100644 index 7a1cac44c..000000000 --- a/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "ans_subscriber_local_live_view_proxy.h" - -#include "ans_inner_errors.h" -#include "ans_log_wrapper.h" -#include "message_option.h" -#include "message_parcel.h" - -namespace OHOS { -namespace Notification { -AnsSubscriberLocalLiveViewProxy::AnsSubscriberLocalLiveViewProxy(const sptr &impl) : IRemoteProxy(impl) -{} - -AnsSubscriberLocalLiveViewProxy::~AnsSubscriberLocalLiveViewProxy() -{} - -ErrCode AnsSubscriberLocalLiveViewProxy::InnerTransact( - NotificationInterfaceCode code, MessageOption &flags, MessageParcel &data, MessageParcel &reply) -{ - auto remote = Remote(); - if (remote == nullptr) { - ANS_LOGE("[InnerTransact] fail: get Remote fail code %{public}u", code); - return ERR_DEAD_OBJECT; - } - - int32_t err = remote->SendRequest(static_cast(code), data, reply, flags); - switch (err) { - case NO_ERROR: { - return ERR_OK; - } - case DEAD_OBJECT: { - ANS_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code); - return ERR_DEAD_OBJECT; - } - default: { - ANS_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code); - return ERR_ANS_TRANSACT_FAILED; - } - } -} - -void AnsSubscriberLocalLiveViewProxy::OnConnected() -{ - MessageParcel data; - if (!data.WriteInterfaceToken(AnsSubscriberLocalLiveViewProxy::GetDescriptor())) { - ANS_LOGE("[OnConnected] fail: write interface token failed."); - return; - } - - MessageParcel reply; - MessageOption option = {MessageOption::TF_ASYNC}; - ErrCode result = InnerTransact(NotificationInterfaceCode::ON_CONNECTED, option, data, reply); - if (result != ERR_OK) { - ANS_LOGE("[OnConnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); - return; - } -} - -void AnsSubscriberLocalLiveViewProxy::OnDisconnected() -{ - MessageParcel data; - if (!data.WriteInterfaceToken(AnsSubscriberLocalLiveViewProxy::GetDescriptor())) { - ANS_LOGE("[OnDisconnected] fail: write interface token failed."); - return; - } - - MessageParcel reply; - MessageOption option = {MessageOption::TF_ASYNC}; - ErrCode result = InnerTransact(NotificationInterfaceCode::ON_DISCONNECTED, option, data, reply); - if (result != ERR_OK) { - ANS_LOGE("[OnDisconnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); - return; - } -} - - -void AnsSubscriberLocalLiveViewProxy::OnResponse(int32_t notificationId, sptr buttonOption) -{ - MessageParcel data; - if (!data.WriteInterfaceToken(AnsSubscriberLocalLiveViewProxy::GetDescriptor())) { - ANS_LOGE("[OnResponse] fail: write interface token failed."); - return; - } - - if (!data.WriteInt32(notificationId)) { - ANS_LOGE("[OnResponse] fail: write notificationId failed"); - return; - } - - if (!data.WriteParcelable(buttonOption)) { - ANS_LOGE("[OnResponse] fail: write buttonName failed"); - return; - } - - MessageParcel reply; - MessageOption option = {MessageOption::TF_ASYNC}; - ErrCode result = InnerTransact(NotificationInterfaceCode::ON_RESPONSE, option, data, reply); - if (result != ERR_OK) { - ANS_LOGE("[OnResponse] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); - return; - } -} -} // namespace Notification -} // namespace OHOS diff --git a/frameworks/core/src/ans_subscriber_local_live_view_stub.cpp b/frameworks/core/src/ans_subscriber_local_live_view_stub.cpp deleted file mode 100644 index f4cdb5469..000000000 --- a/frameworks/core/src/ans_subscriber_local_live_view_stub.cpp +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "ans_subscriber_local_live_view_stub.h" - -#include "ans_const_define.h" -#include "ans_inner_errors.h" -#include "ans_log_wrapper.h" -#include "message_option.h" -#include "message_parcel.h" -#include "notification_bundle_option.h" -#include "notification_button_option.h" -#include "parcel.h" -#include "refbase.h" -#include - -namespace OHOS { -namespace Notification { -AnsSubscriberLocalLiveViewStub::AnsSubscriberLocalLiveViewStub() -{ - interfaces_.emplace(NotificationInterfaceCode::ON_CONNECTED, - std::bind(&AnsSubscriberLocalLiveViewStub::HandleOnConnected, this, std::placeholders::_1, std::placeholders::_2)); - interfaces_.emplace(NotificationInterfaceCode::ON_DISCONNECTED, - std::bind(&AnsSubscriberLocalLiveViewStub::HandleOnDisconnected, this, std::placeholders::_1, std::placeholders::_2)); - interfaces_.emplace(NotificationInterfaceCode::ON_RESPONSE, - std::bind(&AnsSubscriberLocalLiveViewStub::HandleOnResponse, this, std::placeholders::_1, std::placeholders::_2)); -} - -AnsSubscriberLocalLiveViewStub::~AnsSubscriberLocalLiveViewStub() -{} - -int32_t AnsSubscriberLocalLiveViewStub::OnRemoteRequest( - uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &flags) -{ - std::u16string descriptor = AnsSubscriberLocalLiveViewStub::GetDescriptor(); - std::u16string remoteDescriptor = data.ReadInterfaceToken(); - if (descriptor != remoteDescriptor) { - ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!"); - return OBJECT_NULL; - } - - auto it = interfaces_.find(static_cast(code)); - if (it == interfaces_.end()) { - ANS_LOGW("[OnRemoteRequest] fail: unknown code!"); - return IPCObjectStub::OnRemoteRequest(code, data, reply, flags); - } - - auto fun = it->second; - if (fun == nullptr) { - ANS_LOGW("[OnRemoteRequest] fail: not find function!"); - return IPCObjectStub::OnRemoteRequest(code, data, reply, flags); - } - - fun(data, reply); - return NO_ERROR; -} - -ErrCode AnsSubscriberLocalLiveViewStub::HandleOnConnected(MessageParcel &data, MessageParcel &reply) -{ - OnConnected(); - return ERR_OK; -} - -ErrCode AnsSubscriberLocalLiveViewStub::HandleOnDisconnected(MessageParcel &data, MessageParcel &reply) -{ - OnDisconnected(); - return ERR_OK; -} - -template -bool AnsSubscriberLocalLiveViewStub::ReadParcelableVector(std::vector> &parcelableInfos, MessageParcel &data) -{ - int32_t infoSize = 0; - if (!data.ReadInt32(infoSize)) { - ANS_LOGE("read Parcelable size failed."); - return false; - } - - parcelableInfos.clear(); - infoSize = (infoSize < MAX_PARCELABLE_VECTOR_NUM) ? infoSize : MAX_PARCELABLE_VECTOR_NUM; - for (int32_t index = 0; index < infoSize; index++) { - sptr info = data.ReadStrongParcelable(); - if (info == nullptr) { - ANS_LOGE("read Parcelable infos failed."); - return false; - } - parcelableInfos.emplace_back(info); - } - - return true; -} - -ErrCode AnsSubscriberLocalLiveViewStub::HandleOnResponse(MessageParcel &data, MessageParcel &reply) -{ - int32_t notificationId = 0; - if (!data.ReadInt32(notificationId)) { - ANS_LOGE("[HandleOnResponse] fail : read notificationId failed"); - return ERR_ANS_PARCELABLE_FAILED; - } - sptr buttonOption = nullptr; - buttonOption = data.ReadParcelable(); - if (buttonOption == nullptr) { - ANS_LOGE("[HandleOnResponse] fail : read buttonOption failed"); - return ERR_ANS_PARCELABLE_FAILED; - } - OnResponse(notificationId, buttonOption); - return ERR_OK; -} - -void AnsSubscriberLocalLiveViewStub::OnConnected() -{} - -void AnsSubscriberLocalLiveViewStub::OnDisconnected() -{} - -void AnsSubscriberLocalLiveViewStub::OnResponse(int32_t notificationId, sptr buttonOption) -{} -} // namespace Notification -} // namespace OHOS diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index 955a2079d..d0457ea46 100755 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -246,12 +246,6 @@ public: return ERR_ANS_INVALID_PARAM; } - ErrCode SubscribeLocalActivity(const sptr &subscriber, - const sptr &info) override - { - return ERR_ANS_INVALID_PARAM; - } - ErrCode Unsubscribe( const sptr &subscriber, const sptr &info) override { @@ -431,12 +425,6 @@ public: { return ERR_ANS_INVALID_PARAM; } - - ErrCode TriggerLocalActivity(const sptr &bundleOption, - const int32_t notificationId, const sptr &buttonOption) override - { - return ERR_ANS_INVALID_PARAM; - } }; class AnsNotificationBranchTest : public testing::Test { diff --git a/interfaces/inner_api/notification_constant.h b/interfaces/inner_api/notification_constant.h index de3e87263..3413024da 100644 --- a/interfaces/inner_api/notification_constant.h +++ b/interfaces/inner_api/notification_constant.h @@ -62,6 +62,7 @@ public: CONTENT_INFORMATION, // the notificatin type is content information OTHER, // the notificatin type is other CUSTOM, // the notification type is custom + LIVE_VIEW, // the notification type is live view }; enum class VisiblenessType { diff --git a/interfaces/inner_api/notification_content.h b/interfaces/inner_api/notification_content.h index 41d1957f2..fb44a421d 100644 --- a/interfaces/inner_api/notification_content.h +++ b/interfaces/inner_api/notification_content.h @@ -24,6 +24,7 @@ #include "notification_multiline_content.h" #include "notification_normal_content.h" #include "notification_picture_content.h" +#include "notification_local_live_view_content.h" #include "parcel.h" namespace OHOS { @@ -63,7 +64,12 @@ public: * Indicates notifications that include a picture. * Such notifications are created using NotificationPictureContent. */ - PICTURE + PICTURE, + /** + * Indicates notifications that include system activity. + * Such notifications are created using NotificationLocalLiveViewContent. + */ + LOCAL_LIVE_VIEW }; /** @@ -120,6 +126,15 @@ public: */ explicit NotificationContent(const std::shared_ptr &mediaContent); + /** + * @brief A constructor used to create a NotificationLocalLiveViewContent instance (obtained by calling + * GetNotificationContent()) and set the content type to NotificationContent::Type::LOCAL_LIVE_VIEW (obtained by calling + * GetContentType()). + * + * @param mediaContent Indicates the NotificationLocalLiveViewContent object. + */ + explicit NotificationContent(const std::shared_ptr &localLiveViewContent); + virtual ~NotificationContent(); /** diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index fdff33476..b0b119b20 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -17,12 +17,14 @@ #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_HELPER_H #include "notification_bundle_option.h" +#include "notification_button_option.h" #include "notification_do_not_disturb_date.h" #include "enabled_notification_callback_data.h" #include "notification_request.h" #include "notification_slot.h" #include "notification_sorting_map.h" #include "notification_subscriber.h" +#include "notification_local_live_view_subscriber.h" namespace OHOS { namespace Notification { @@ -319,6 +321,19 @@ public: static ErrCode SubscribeNotification( const NotificationSubscriber &subscriber, const NotificationSubscribeInfo &subscribeInfo); + /** + * @brief Subscribes the localLiveView button click. This method can be called only + * by applications with required system permissions. + * @note To subscribe to a button click, inherit the {NotificationLocalLiveViewSubscriber} class, override its + * callback methods and create a subscriber. The subscriber will be used as a parameter of this method. + * After the button is clicked, subscribers that meet the filter criteria can receive the response + * + * @param subscriber Indicates the subscribers to receive notifications. This parameter must be specified. + * For details, see {NotificationSubscriber}. + * @return Returns subscribe notification result. + */ + static ErrCode SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber &subscriber); + /** * @brief Unsubscribes from all notifications. This method can be called only by applications with required * system permissions. @@ -349,6 +364,18 @@ public: */ static ErrCode UnSubscribeNotification(NotificationSubscriber &subscriber, NotificationSubscribeInfo subscribeInfo); + /** + * @brief Trigger the local activity after the button has been clicked. + * @note Your application must have platform signature to use this method. + * + * @param bundleOption Indicates the bundle name and uid of the application whose notifications has been clicked. + * @param notificationId Indicates the id of the notification. + * @param buttonOption Indicates which button has been clicked. + * @return Returns trigger localLiveView result. + */ + static ErrCode TriggerLocalLiveView(const NotificationBundleOption &bundleOption, + const int32_t notificationId, const NotificationButtonOption &buttonOption); + /** * @brief Removes a specified removable notification of other applications. * @note Your application must have platform signature to use this method. diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index edc6d381a..5b944341b 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -50,6 +50,7 @@ ohos_shared_library("libans") { "src/bundle_manager_helper.cpp", "src/event_report.cpp", "src/notification_dialog.cpp", + "src/notification_local_live_view_subscriber_manager.cpp", "src/notification_preferences.cpp", "src/notification_preferences_database.cpp", "src/notification_preferences_info.cpp", diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index c39458f3d..b91f28598 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -263,6 +263,18 @@ public: */ ErrCode HasNotificationPolicyAccessPermission(bool &granted) override; + /** + * @brief Trigger the local activity after the button has been clicked. + * @note Your application must have platform signature to use this method. + * + * @param bundleOption Indicates the bundle name and uid of the application whose notifications has been clicked. + * @param notificationId Indicates the id of the notification. + * @param buttonOption Indicates which button has been clicked. + * @return Returns trigger localLiveView result. + */ + ErrCode TriggerLocalLiveView(const sptr &bundleOption, + const int32_t notificationId, const sptr &buttonOption) override; + /** * @brief Delete notification. * @@ -401,6 +413,16 @@ public: ErrCode Subscribe(const sptr &subscriber, const sptr &info) override; + /** + * @brief Subscribes notifications. + * + * @param subscriber Indicates the subscriber. + * @param info Indicates the NotificationSubscribeInfo object. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode SubscribeLocalLiveView(const sptr &subscriber, + const sptr &info) override; + /** * @brief Unsubscribes notifications. * diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 88bf369f2..fae19ba78 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -45,6 +45,7 @@ #include "notification_slot.h" #include "notification_slot_filter.h" #include "notification_subscriber_manager.h" +#include "notification_local_live_view_subscriber_manager.h" #include "os_account_manager.h" #include "permission_filter.h" #include "push_callback_proxy.h" @@ -1512,6 +1513,41 @@ ErrCode AdvancedNotificationService::Subscribe( return errCode; } +ErrCode AdvancedNotificationService::SubscribeLocalLiveView( + const sptr &subscriber, const sptr &info) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + ANS_LOGD("%{public}s", __FUNCTION__); + + ErrCode errCode = ERR_OK; + do { + if (subscriber == nullptr) { + errCode = ERR_ANS_INVALID_PARAM; + break; + } + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGE("Client is not a system app or subsystem"); + errCode = ERR_ANS_NON_SYSTEM_APP; + break; + } + + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + errCode = ERR_ANS_PERMISSION_DENIED; + break; + } + + errCode = NotificationLocalLiveViewSubscriberManager::GetInstance()->AddLocalLiveViewSubscriber(subscriber, info); + if (errCode != ERR_OK) { + break; + } + } while (0); + + SendSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), info, errCode); + return errCode; +} + ErrCode AdvancedNotificationService::Unsubscribe( const sptr &subscriber, const sptr &info) { @@ -2573,6 +2609,49 @@ ErrCode AdvancedNotificationService::AddSlotByType(NotificationConstant::SlotTyp return result; } +ErrCode AdvancedNotificationService::TriggerLocalLiveView(const sptr &bundleOption, + const int32_t notificationId, const sptr &buttonOption) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGD("CheckPermission is bogus."); + return ERR_ANS_PERMISSION_DENIED; + } + + sptr bundle = GenerateValidBundleOption(bundleOption); + if (bundle == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + + ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + sptr notification = nullptr; + + for (auto record : notificationList_) { + if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) && + (record->bundleOption->GetUid() == bundle->GetUid()) && + (record->notification->GetId() == notificationId)) { + notification = record->notification; + result = ERR_OK; + break; + } + } + + if (notification != nullptr) { + NotificationLocalLiveViewSubscriberManager::GetInstance()->NotifyTriggerResponse(notification, buttonOption); + } + })); + notificationSvrQueue_->wait(handler); + return result; +} ErrCode AdvancedNotificationService::RemoveNotification(const sptr &bundleOption, int32_t notificationId, const std::string &label, int32_t removeReason) { diff --git a/services/ans/src/notification_local_live_view_subscriber_manager.cpp b/services/ans/src/notification_local_live_view_subscriber_manager.cpp new file mode 100644 index 000000000..54ac6ef23 --- /dev/null +++ b/services/ans/src/notification_local_live_view_subscriber_manager.cpp @@ -0,0 +1,270 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "notification_local_live_view_subscriber_manager.h" + +#include +#include +#include + +#include "ans_const_define.h" +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" +#include "ans_watchdog.h" +#include "hitrace_meter.h" +#include "ipc_skeleton.h" +#include "notification_bundle_option.h" +#include "notification_button_option.h" +#include "os_account_manager.h" +#include "remote_death_recipient.h" +#include "bundle_manager_helper.h" + +namespace OHOS { +namespace Notification { +struct NotificationLocalLiveViewSubscriberManager::LocalLiveViewSubscriberRecord { + sptr subscriber {nullptr}; + std::string bundleName {}; + int32_t userId {SUBSCRIBE_USER_INIT}; +}; + +NotificationLocalLiveViewSubscriberManager::NotificationLocalLiveViewSubscriberManager() +{ + ANS_LOGI("constructor"); + notificationButtonQueue_ = std::make_shared("NotificationLocalLiveViewMgr"); + recipient_ = new (std::nothrow) + RemoteDeathRecipient(std::bind(&NotificationLocalLiveViewSubscriberManager::OnRemoteDied, this, std::placeholders::_1)); + if (recipient_ == nullptr) { + ANS_LOGE("Failed to create RemoteDeathRecipient instance"); + } +} + +NotificationLocalLiveViewSubscriberManager::~NotificationLocalLiveViewSubscriberManager() +{ + ANS_LOGI("deconstructor"); + buttonRecordList_.clear(); +} + +void NotificationLocalLiveViewSubscriberManager::ResetFfrtQueue() +{ + if (notificationButtonQueue_ != nullptr) { + notificationButtonQueue_.reset(); + } +} + +ErrCode NotificationLocalLiveViewSubscriberManager::AddLocalLiveViewSubscriber( + const sptr &subscriber, const sptr &subscribeInfo) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + if (subscriber == nullptr) { + ANS_LOGE("subscriber is null."); + return ERR_ANS_INVALID_PARAM; + } + + sptr bundleOption; + std::string bundle; + int32_t callingUid = IPCSkeleton::GetCallingUid(); + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager != nullptr) { + bundle = bundleManager->GetBundleNameByUid(callingUid); + } + bundleOption = new (std::nothrow) NotificationBundleOption(bundle, callingUid); + ErrCode result = ERR_ANS_TASK_ERR; + if (bundleOption == nullptr) { + ANS_LOGE("Failed to create NotificationBundleOption instance"); + return ERR_ANS_NO_MEMORY; + } + ANS_LOGD("Get userId succeeded, callingUid = <%{public}d> bundleName = <%{public}s>", callingUid, bundle.c_str()); + if (notificationButtonQueue_ == nullptr) { + ANS_LOGE("queue is nullptr"); + return result; + } + ANS_LOGE("ffrt start!"); + ffrt::task_handle handler = notificationButtonQueue_->submit_h(std::bind([this, &subscriber, &bundleOption, &result]() { + ANS_LOGE("ffrt enter!"); + result = this->AddSubscriberInner(subscriber, bundleOption); + })); + notificationButtonQueue_->wait(handler); + ANS_LOGE("ffrt end!"); + return result; +} + +ErrCode NotificationLocalLiveViewSubscriberManager::RemoveLocalLiveViewSubscriber( + const sptr &subscriber, const sptr &subscribeInfo) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + if (subscriber == nullptr) { + ANS_LOGE("subscriber is null."); + return ERR_ANS_INVALID_PARAM; + } + + ErrCode result = ERR_ANS_TASK_ERR; + if (notificationButtonQueue_ == nullptr) { + ANS_LOGE("queue is nullptr"); + return result; + } + ANS_LOGE("ffrt start!"); + ffrt::task_handle handler = notificationButtonQueue_->submit_h(std::bind([this, &subscriber, + &subscribeInfo, &result]() { + ANS_LOGE("ffrt enter!"); + result = this->RemoveSubscriberInner(subscriber, subscribeInfo); + })); + notificationButtonQueue_->wait(handler); + ANS_LOGE("ffrt end!"); + return result; +} + +void NotificationLocalLiveViewSubscriberManager::NotifyTriggerResponse(const sptr ¬ification, + const sptr &buttonOption) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + if (notificationButtonQueue_ == nullptr) { + ANS_LOGE("queue is nullptr"); + return; + } + AppExecFwk::EventHandler::Callback NotifyTriggerResponseFunc = + std::bind(&NotificationLocalLiveViewSubscriberManager::NotifyTriggerResponseInner, this, notification, buttonOption); + + ANS_LOGE("ffrt start!"); + notificationButtonQueue_->submit(NotifyTriggerResponseFunc); + ANS_LOGE("ffrt end!"); +} + +void NotificationLocalLiveViewSubscriberManager::OnRemoteDied(const wptr &object) +{ + ANS_LOGI("OnRemoteDied"); + if (notificationButtonQueue_ == nullptr) { + ANS_LOGE("queue is nullptr"); + return; + } + ffrt::task_handle handler = notificationButtonQueue_->submit_h(std::bind([this, object]() { + ANS_LOGE("ffrt enter!"); + std::shared_ptr record = FindSubscriberRecord(object); + if (record != nullptr) { + ANS_LOGW("subscriber removed."); + buttonRecordList_.remove(record); + } + })); + ANS_LOGE("ffrt start!"); + notificationButtonQueue_->wait(handler); + ANS_LOGE("ffrt end!"); +} + +std::shared_ptr NotificationLocalLiveViewSubscriberManager::FindSubscriberRecord( + const wptr &object) +{ + auto iter = buttonRecordList_.begin(); + + for (; iter != buttonRecordList_.end(); iter++) { + if ((*iter)->subscriber->AsObject() == object) { + return (*iter); + } + } + return nullptr; +} + +std::shared_ptr NotificationLocalLiveViewSubscriberManager::FindSubscriberRecord( + const sptr &subscriber) +{ + auto iter = buttonRecordList_.begin(); + + for (; iter != buttonRecordList_.end(); iter++) { + if ((*iter)->subscriber->AsObject() == subscriber->AsObject()) { + return (*iter); + } + } + return nullptr; +} + +std::shared_ptr NotificationLocalLiveViewSubscriberManager::CreateSubscriberRecord( + const sptr &subscriber, const sptr &bundleOption) +{ + std::shared_ptr record = std::make_shared(); + // set bundleName and uid + if (record != nullptr) { + record->subscriber = subscriber; + record->bundleName = bundleOption->GetBundleName(); + record->userId = bundleOption->GetUid(); + ANS_LOGD("Get userId succeeded, callingUid = <%{public}d> bundleName = <%{public}s>", record->userId, record->bundleName.c_str()); + } + return record; +} + + +ErrCode NotificationLocalLiveViewSubscriberManager::AddSubscriberInner( + const sptr &subscriber, const sptr &bundleOption) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + std::shared_ptr record = FindSubscriberRecord(subscriber); + if (record == nullptr) { + record = CreateSubscriberRecord(subscriber, bundleOption); + if (record == nullptr) { + ANS_LOGE("CreateSubscriberRecord failed."); + return ERR_ANS_NO_MEMORY; + } + buttonRecordList_.push_back(record); + + record->subscriber->AsObject()->AddDeathRecipient(recipient_); + + record->subscriber->OnConnected(); + ANS_LOGI("subscriber is connected."); + } + + return ERR_OK; +} + +ErrCode NotificationLocalLiveViewSubscriberManager::RemoveSubscriberInner( + const sptr &subscriber, const sptr &subscribeInfo) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + std::shared_ptr record = FindSubscriberRecord(subscriber); + + if (record == nullptr) { + ANS_LOGE("subscriber not found."); + return ERR_ANS_INVALID_PARAM; + } + + record->subscriber->AsObject()->RemoveDeathRecipient(recipient_); + + buttonRecordList_.remove(record); + + record->subscriber->OnDisconnected(); + ANS_LOGI("subscriber is disconnected."); + + return ERR_OK; +} + +void NotificationLocalLiveViewSubscriberManager::NotifyTriggerResponseInner(const sptr ¬ification, const sptr buttonOption) +{ + ANS_LOGE("ffrt enter!"); + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + ANS_LOGD("%{public}s notification->GetUserId <%{public}d>, bundlename <%{public}s>", __FUNCTION__, notification->GetUid(), + notification->GetBundleName().c_str()); + int32_t sendUserId = notification->GetUid(); + for (auto record : buttonRecordList_) { + ANS_LOGD("%{public}s record->userId = <%{public}d>, bundlename <%{public}s>", __FUNCTION__, record->userId, record->bundleName.c_str()); + auto bundleName = notification->GetBundleName(); + if (record->bundleName == bundleName && record->userId == sendUserId) { + record->subscriber->OnResponse(notification->GetId(), buttonOption); + } + } +} + +bool NotificationLocalLiveViewSubscriberManager::IsSystemUser(int32_t userId) +{ + return ((userId >= SUBSCRIBE_USER_SYSTEM_BEGIN) && (userId <= SUBSCRIBE_USER_SYSTEM_END)); +} + +} // namespace Notification +} // namespace OHOS -- Gitee