From a75ce12d195cbf0bc394c94435be231cb401cee8 Mon Sep 17 00:00:00 2001 From: markYao Date: Sun, 22 Oct 2023 20:10:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E5=AE=9E=E5=86=B5=E7=AA=97fr?= =?UTF-8?q?ameworks/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 | 24 ++++ 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 | 1 + ..._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 ++ 12 files changed, 574 insertions(+) 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..f1bec794b 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -20,8 +20,10 @@ #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_button_option.h" #include "notification_constant.h" #include "notification_do_not_disturb_date.h" #include "notification_request.h" @@ -246,6 +248,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 +398,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 index 3cd6acbd7..52cefe3d9 100644 --- a/frameworks/core/include/ans_subscriber_local_live_view_interface.h +++ b/frameworks/core/include/ans_subscriber_local_live_view_interface.h @@ -26,6 +26,7 @@ #include "notification_request.h" #include "notification_sorting.h" #include "notification_sorting_map.h" +#include "notification_button_option.h" namespace OHOS { namespace Notification { 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