From 591114f89301e9552e0ab20c7bc0ddae127511f3 Mon Sep 17 00:00:00 2001 From: zengsiyu Date: Sat, 12 Feb 2022 18:32:05 +0800 Subject: [PATCH] feat: add dialog Signed-off-by: zengsiyu Change-Id: Ia6bafba9b82db02c86578e861598abdee8cf62ac Signed-off-by: zengsiyu --- frameworks/ans/core/BUILD.gn | 1 + .../ans/core/include/ans_manager_interface.h | 4 + .../ans/core/include/ans_manager_proxy.h | 2 + .../ans/core/include/ans_manager_stub.h | 4 + .../ans/core/include/ans_notification.h | 20 ++- .../core/include/ans_subscriber_interface.h | 3 + .../ans/core/include/ans_subscriber_proxy.h | 1 + .../ans/core/include/ans_subscriber_stub.h | 2 + frameworks/ans/core/src/ans_manager_proxy.cpp | 58 ++++++++ frameworks/ans/core/src/ans_manager_stub.cpp | 50 +++++++ frameworks/ans/core/src/ans_notification.cpp | 20 +++ .../ans/core/src/ans_subscriber_proxy.cpp | 22 +++ .../ans/core/src/ans_subscriber_stub.cpp | 13 ++ frameworks/ans/dialog/dialog_ui/js/BUILD.gn | 27 ++++ .../ans/dialog/dialog_ui/js/common/.gitkeep | 0 .../ans/dialog/dialog_ui/js/i18n/en-US.json | 7 + .../ans/dialog/dialog_ui/js/i18n/zh-CN.json | 7 + .../dialog/dialog_ui/js/pages/index/index.css | 23 +++ .../dialog/dialog_ui/js/pages/index/index.hml | 15 ++ .../dialog/dialog_ui/js/pages/index/index.js | 22 +++ frameworks/ans/native/BUILD.gn | 1 + .../enabled_notification_callback_data.cpp | 105 ++++++++++++++ .../ans/native/src/notification_helper.cpp | 10 ++ .../native/src/notification_subscriber.cpp | 6 + .../test/moduletest/ans_fw_module_test.cpp | 27 ++++ .../ans_innerkits_module_publish_test.cpp | 4 + .../enabled_notification_callback_data.h | 88 ++++++++++++ .../ans/native/include/notification_helper.h | 21 ++- .../native/include/notification_subscriber.h | 9 ++ interfaces/kits/napi/ans/include/common.h | 3 + .../napi/ans/include/enable_notification.h | 2 + interfaces/kits/napi/ans/include/subscribe.h | 6 + interfaces/kits/napi/ans/src/common.cpp | 22 +++ .../kits/napi/ans/src/enable_notification.cpp | 93 ++++++++++++ interfaces/kits/napi/ans/src/init.cpp | 2 + interfaces/kits/napi/ans/src/subscribe.cpp | 98 +++++++++++++ services/ans/BUILD.gn | 3 + .../include/advanced_notification_service.h | 4 + .../ans/include/notification_preferences.h | 2 + .../notification_preferences_database.h | 3 + .../include/notification_preferences_info.h | 3 + .../include/notification_subscriber_manager.h | 2 + services/ans/include/preferences_constant.h | 3 + .../ans/src/advanced_notification_service.cpp | 132 ++++++++++++++++-- services/ans/src/notification_preferences.cpp | 32 ++++- .../src/notification_preferences_database.cpp | 36 +++++ .../ans/src/notification_preferences_info.cpp | 11 ++ .../src/notification_subscriber_manager.cpp | 24 +++- services/ans/test/unittest/BUILD.gn | 1 + .../advanced_notification_service_test.cpp | 3 + .../notification_subscriber_manager_test.cpp | 3 + services/test/moduletest/BUILD.gn | 1 + services/test/moduletest/ans_module_test.cpp | 3 + 53 files changed, 1052 insertions(+), 12 deletions(-) create mode 100644 frameworks/ans/dialog/dialog_ui/js/BUILD.gn create mode 100644 frameworks/ans/dialog/dialog_ui/js/common/.gitkeep create mode 100644 frameworks/ans/dialog/dialog_ui/js/i18n/en-US.json create mode 100644 frameworks/ans/dialog/dialog_ui/js/i18n/zh-CN.json create mode 100644 frameworks/ans/dialog/dialog_ui/js/pages/index/index.css create mode 100644 frameworks/ans/dialog/dialog_ui/js/pages/index/index.hml create mode 100644 frameworks/ans/dialog/dialog_ui/js/pages/index/index.js create mode 100644 frameworks/ans/native/src/enabled_notification_callback_data.cpp create mode 100644 interfaces/innerkits/ans/native/include/enabled_notification_callback_data.h diff --git a/frameworks/ans/core/BUILD.gn b/frameworks/ans/core/BUILD.gn index 004803bb5..9aaee019c 100644 --- a/frameworks/ans/core/BUILD.gn +++ b/frameworks/ans/core/BUILD.gn @@ -45,6 +45,7 @@ ohos_shared_library("ans_core") { "${core_path}/src/ans_notification.cpp", "${core_path}/src/ans_subscriber_proxy.cpp", "${core_path}/src/ans_subscriber_stub.cpp", + "${frameworks_path}/ans/native/src/enabled_notification_callback_data.cpp", "${frameworks_path}/ans/native/src/message_user.cpp", "${frameworks_path}/ans/native/src/notification.cpp", "${frameworks_path}/ans/native/src/notification_action_button.cpp", diff --git a/frameworks/ans/core/include/ans_manager_interface.h b/frameworks/ans/core/include/ans_manager_interface.h index 91be045eb..21c479c7f 100644 --- a/frameworks/ans/core/include/ans_manager_interface.h +++ b/frameworks/ans/core/include/ans_manager_interface.h @@ -88,6 +88,7 @@ public: const sptr &bundleOption, const std::vector> &slots) = 0; virtual ErrCode UpdateSlotGroups( const sptr &bundleOption, const std::vector> &groups) = 0; + virtual ErrCode RequestEnableNotification(const std::string &deviceId) = 0; virtual ErrCode SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) = 0; virtual ErrCode SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) = 0; virtual ErrCode SetNotificationsEnabledForSpecialBundle( @@ -101,6 +102,7 @@ public: virtual ErrCode AreNotificationsSuspended(bool &suspended) = 0; virtual ErrCode GetCurrentAppSorting(sptr &sortingMap) = 0; virtual ErrCode IsAllowedNotify(bool &allowed) = 0; + virtual ErrCode IsAllowedNotifySelf(bool &allowed) = 0; virtual ErrCode IsSpecialBundleAllowedNotify(const sptr &bundleOption, bool &allowed) = 0; virtual ErrCode SetDoNotDisturbDate(const sptr &date) = 0; @@ -170,6 +172,7 @@ protected: GET_SLOTS_BY_BUNDLE, UPDATE_SLOTS, UPDATE_SLOT_GROUPS, + REQUEST_ENABLE_NOTIFICATION, SET_NOTIFICATION_ENABLED_FOR_BUNDLE, SET_NOTIFICATION_ENABLED_FOR_ALL_BUNDLE, SET_NOTIFICATION_ENABLED_FOR_SPECIAL_BUNDLE, @@ -181,6 +184,7 @@ protected: ARE_NOTIFICATION_SUSPENDED, GET_CURRENT_APP_SORTING, IS_ALLOWED_NOTIFY, + IS_ALLOWED_NOTIFY_SELF, IS_SPECIAL_BUNDLE_ALLOWED_NOTIFY, SET_DO_NOT_DISTURB_DATE, GET_DO_NOT_DISTURB_DATE, diff --git a/frameworks/ans/core/include/ans_manager_proxy.h b/frameworks/ans/core/include/ans_manager_proxy.h index cb4afad06..5f8f568bf 100644 --- a/frameworks/ans/core/include/ans_manager_proxy.h +++ b/frameworks/ans/core/include/ans_manager_proxy.h @@ -77,6 +77,7 @@ public: const sptr &bundleOption, const std::vector> &slots) override; ErrCode UpdateSlotGroups(const sptr &bundleOption, const std::vector> &groups) override; + ErrCode RequestEnableNotification(const std::string &deviceId) override; ErrCode SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) override; ErrCode SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) override; ErrCode SetNotificationsEnabledForSpecialBundle( @@ -89,6 +90,7 @@ public: ErrCode AreNotificationsSuspended(bool &suspended) override; ErrCode GetCurrentAppSorting(sptr &sortingMap) override; ErrCode IsAllowedNotify(bool &allowed) override; + ErrCode IsAllowedNotifySelf(bool &allowed) override; ErrCode IsSpecialBundleAllowedNotify(const sptr &bundleOption, bool &allowed) override; ErrCode SetDoNotDisturbDate(const sptr &date) override; diff --git a/frameworks/ans/core/include/ans_manager_stub.h b/frameworks/ans/core/include/ans_manager_stub.h index f667eccc5..ff87901ab 100644 --- a/frameworks/ans/core/include/ans_manager_stub.h +++ b/frameworks/ans/core/include/ans_manager_stub.h @@ -77,6 +77,7 @@ public: const sptr &bundleOption, const std::vector> &slots) override; virtual ErrCode UpdateSlotGroups(const sptr &bundleOption, const std::vector> &groups) override; + virtual ErrCode RequestEnableNotification(const std::string &deviceId) override; virtual ErrCode SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) override; virtual ErrCode SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) override; virtual ErrCode SetNotificationsEnabledForSpecialBundle( @@ -93,6 +94,7 @@ public: virtual ErrCode AreNotificationsSuspended(bool &suspended) override; virtual ErrCode GetCurrentAppSorting(sptr &sortingMap) override; virtual ErrCode IsAllowedNotify(bool &allowed) override; + virtual ErrCode IsAllowedNotifySelf(bool &allowed) override; virtual ErrCode IsSpecialBundleAllowedNotify( const sptr &bundleOption, bool &allowed) override; @@ -166,6 +168,7 @@ private: ErrCode HandleGetSlotsByBundle(MessageParcel &data, MessageParcel &reply); ErrCode HandleUpdateSlots(MessageParcel &data, MessageParcel &reply); ErrCode HandleUpdateSlotGroups(MessageParcel &data, MessageParcel &reply); + ErrCode HandleRequestEnableNotification(MessageParcel &data, MessageParcel &reply); ErrCode HandleSetNotificationsEnabledForBundle(MessageParcel &data, MessageParcel &reply); ErrCode HandleSetNotificationsEnabledForAllBundles(MessageParcel &data, MessageParcel &reply); ErrCode HandleSetNotificationsEnabledForSpecialBundle(MessageParcel &data, MessageParcel &reply); @@ -177,6 +180,7 @@ private: ErrCode HandleAreNotificationsSuspended(MessageParcel &data, MessageParcel &reply); ErrCode HandleGetCurrentAppSorting(MessageParcel &data, MessageParcel &reply); ErrCode HandleIsAllowedNotify(MessageParcel &data, MessageParcel &reply); + ErrCode HandleIsAllowedNotifySelf(MessageParcel &data, MessageParcel &reply); ErrCode HandleIsSpecialBundleAllowedNotify(MessageParcel &data, MessageParcel &reply); ErrCode HandleIsDistributedEnabled(MessageParcel &data, MessageParcel &reply); ErrCode HandleEnableDistributed(MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/ans/core/include/ans_notification.h b/frameworks/ans/core/include/ans_notification.h index cf214a569..62a01657b 100644 --- a/frameworks/ans/core/include/ans_notification.h +++ b/frameworks/ans/core/include/ans_notification.h @@ -294,13 +294,31 @@ public: ErrCode SetNotificationBadgeNum(int32_t num); /** - * Checks whether this application has permission to publish notifications. + * Checks whether this application has permission to publish notifications. The caller must have + * system permissions to call this method. * * @param allowed True if this application has the permission; returns false otherwise * @return Returns is allowed notify result. */ ErrCode IsAllowedNotify(bool &allowed); + /** + * Checks whether this application has permission to publish notifications. + * + * @param allowed True if this application has the permission; returns false otherwise + * @return Returns is allowed notify result. + */ + ErrCode IsAllowedNotifySelf(bool &allowed); + + /** + * Allow the current application to publish notifications on a specified device. + * + * @param deviceId Indicates the ID of the device running the application. At present, this parameter can + * only be null or an empty string, indicating the current device. + * @return Returns set notifications enabled for default bundle result. + */ + ErrCode RequestEnableNotification(std::string &deviceId); + /** * Checks whether this application is in the suspended state.Applications in this state cannot publish * notifications. diff --git a/frameworks/ans/core/include/ans_subscriber_interface.h b/frameworks/ans/core/include/ans_subscriber_interface.h index 326680107..1c8616805 100644 --- a/frameworks/ans/core/include/ans_subscriber_interface.h +++ b/frameworks/ans/core/include/ans_subscriber_interface.h @@ -18,6 +18,7 @@ #include "iremote_broker.h" +#include "enabled_notification_callback_data.h" #include "notification.h" #include "notification_constant.h" #include "notification_do_not_disturb_date.h" @@ -45,6 +46,7 @@ public: int deleteReason) = 0; virtual void OnUpdated(const sptr ¬ificationMap) = 0; virtual void OnDoNotDisturbDateChange(const sptr &date) = 0; + virtual void OnEnabledNotificationChanged(const sptr &callbackData) = 0; protected: enum TransactId : uint32_t { @@ -56,6 +58,7 @@ protected: ON_CANCELED_MAP, ON_UPDATED, ON_DND_DATE_CHANGED, + ON_ENABLED_NOTIFICATION_CHANGED, }; }; } // namespace Notification diff --git a/frameworks/ans/core/include/ans_subscriber_proxy.h b/frameworks/ans/core/include/ans_subscriber_proxy.h index 8739caf27..1acdacf60 100644 --- a/frameworks/ans/core/include/ans_subscriber_proxy.h +++ b/frameworks/ans/core/include/ans_subscriber_proxy.h @@ -38,6 +38,7 @@ public: int deleteReason) override; void OnUpdated(const sptr ¬ificationMap) override; void OnDoNotDisturbDateChange(const sptr &date) override; + void OnEnabledNotificationChanged(const sptr &callbackData) override; private: ErrCode InnerTransact(uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/ans/core/include/ans_subscriber_stub.h b/frameworks/ans/core/include/ans_subscriber_stub.h index 26928cd84..303c81d29 100644 --- a/frameworks/ans/core/include/ans_subscriber_stub.h +++ b/frameworks/ans/core/include/ans_subscriber_stub.h @@ -40,6 +40,7 @@ public: int deleteReason) override; void OnUpdated(const sptr ¬ificationMap) override; void OnDoNotDisturbDateChange(const sptr &date) override; + void OnEnabledNotificationChanged(const sptr &callbackData) override; private: std::map> interfaces_; @@ -52,6 +53,7 @@ private: ErrCode HandleOnCanceledMap(MessageParcel &data, MessageParcel &reply); ErrCode HandleOnUpdated(MessageParcel &data, MessageParcel &reply); ErrCode HandleOnDoNotDisturbDateChange(MessageParcel &data, MessageParcel &reply); + ErrCode HandleOnEnabledNotificationChanged(MessageParcel &data, MessageParcel &reply); }; } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/src/ans_manager_proxy.cpp b/frameworks/ans/core/src/ans_manager_proxy.cpp index 4d9396dde..000af8c28 100644 --- a/frameworks/ans/core/src/ans_manager_proxy.cpp +++ b/frameworks/ans/core/src/ans_manager_proxy.cpp @@ -1242,6 +1242,35 @@ ErrCode AnsManagerProxy::UpdateSlotGroups( return result; } +ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGW("[RequestEnableNotification] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteString(deviceId)) { + ANS_LOGW("[RequestEnableNotification] fail: write deviceId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(REQUEST_ENABLE_NOTIFICATION, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[RequestEnableNotification] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGW("[RequestEnableNotification] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + ErrCode AnsManagerProxy::SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) { MessageParcel data; @@ -1645,6 +1674,35 @@ ErrCode AnsManagerProxy::IsAllowedNotify(bool &allowed) return result; } +ErrCode AnsManagerProxy::IsAllowedNotifySelf(bool &allowed) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGW("[IsAllowedNotifySelf] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(IS_ALLOWED_NOTIFY_SELF, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[IsAllowedNotifySelf] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGW("[IsAllowedNotifySelf] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!reply.ReadBool(allowed)) { + ANS_LOGW("[IsAllowedNotifySelf] fail: read allowed failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + ErrCode AnsManagerProxy::IsSpecialBundleAllowedNotify(const sptr &bundleOption, bool &allowed) { if (bundleOption == nullptr) { diff --git a/frameworks/ans/core/src/ans_manager_stub.cpp b/frameworks/ans/core/src/ans_manager_stub.cpp index cfcd7031a..9110ac4a8 100644 --- a/frameworks/ans/core/src/ans_manager_stub.cpp +++ b/frameworks/ans/core/src/ans_manager_stub.cpp @@ -136,6 +136,9 @@ const std::map bundleOption = data.ReadParcelable(); @@ -1897,6 +1935,12 @@ ErrCode AnsManagerStub::UpdateSlotGroups( return ERR_INVALID_OPERATION; } +ErrCode AnsManagerStub::RequestEnableNotification(const std::string &deviceId) +{ + ANS_LOGW("AnsManagerStub::RequestEnableNotification called!"); + return ERR_INVALID_OPERATION; +} + ErrCode AnsManagerStub::SetNotificationsEnabledForBundle(const std::string &bundle, bool enabled) { ANS_LOGW("AnsManagerStub::SetNotificationsEnabledForBundle called!"); @@ -1964,6 +2008,12 @@ ErrCode AnsManagerStub::IsAllowedNotify(bool &allowed) return ERR_INVALID_OPERATION; } +ErrCode AnsManagerStub::IsAllowedNotifySelf(bool &allowed) +{ + ANS_LOGW("AnsManagerStub::IsAllowedNotifySelf called!"); + return ERR_INVALID_OPERATION; +} + ErrCode AnsManagerStub::IsSpecialBundleAllowedNotify(const sptr &bundleOption, bool &allowed) { ANS_LOGW("AnsManagerStub::IsSpecialBundleAllowedNotify called!"); diff --git a/frameworks/ans/core/src/ans_notification.cpp b/frameworks/ans/core/src/ans_notification.cpp index 43b9fb64f..fb2439edb 100644 --- a/frameworks/ans/core/src/ans_notification.cpp +++ b/frameworks/ans/core/src/ans_notification.cpp @@ -406,6 +406,26 @@ ErrCode AnsNotification::IsAllowedNotify(bool &allowed) return ansManagerProxy_->IsAllowedNotify(allowed); } +ErrCode AnsNotification::IsAllowedNotifySelf(bool &allowed) +{ + ANS_LOGD("enter"); + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + return ansManagerProxy_->IsAllowedNotifySelf(allowed); +} + +ErrCode AnsNotification::RequestEnableNotification(std::string &deviceId) +{ + ANS_LOGD("enter"); + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + return ansManagerProxy_->RequestEnableNotification(deviceId); +} + ErrCode AnsNotification::AreNotificationsSuspended(bool &suspended) { if (!GetAnsManagerProxy()) { diff --git a/frameworks/ans/core/src/ans_subscriber_proxy.cpp b/frameworks/ans/core/src/ans_subscriber_proxy.cpp index 1952b3f6b..ede35cc11 100644 --- a/frameworks/ans/core/src/ans_subscriber_proxy.cpp +++ b/frameworks/ans/core/src/ans_subscriber_proxy.cpp @@ -274,5 +274,27 @@ void AnsSubscriberProxy::OnDoNotDisturbDateChange(const sptr &callbackData) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) { + ANS_LOGW("[OnEnabledNotificationChanged] fail: write interface token failed."); + return; + } + + if (!data.WriteParcelable(callbackData)) { + ANS_LOGW("[OnEnabledNotificationChanged] fail: write callbackData failed"); + return; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_ASYNC}; + ErrCode result = InnerTransact(ON_ENABLED_NOTIFICATION_CHANGED, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[OnEnabledNotificationChanged] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + return; + } +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/src/ans_subscriber_stub.cpp b/frameworks/ans/core/src/ans_subscriber_stub.cpp index 932238a92..7340a0c0c 100644 --- a/frameworks/ans/core/src/ans_subscriber_stub.cpp +++ b/frameworks/ans/core/src/ans_subscriber_stub.cpp @@ -43,6 +43,9 @@ AnsSubscriberStub::AnsSubscriberStub() interfaces_.emplace(ON_DND_DATE_CHANGED, std::bind( &AnsSubscriberStub::HandleOnDoNotDisturbDateChange, this, std::placeholders::_1, std::placeholders::_2)); + interfaces_.emplace(ON_ENABLED_NOTIFICATION_CHANGED, + std::bind(&AnsSubscriberStub::HandleOnEnabledNotificationChanged, this, std::placeholders::_1, + std::placeholders::_2)); } AnsSubscriberStub::~AnsSubscriberStub() @@ -189,6 +192,13 @@ ErrCode AnsSubscriberStub::HandleOnDoNotDisturbDateChange(MessageParcel &data, M return ERR_OK; } +ErrCode AnsSubscriberStub::HandleOnEnabledNotificationChanged(MessageParcel &data, MessageParcel &reply) +{ + sptr callbackData = data.ReadParcelable(); + OnEnabledNotificationChanged(callbackData); + return ERR_OK; +} + void AnsSubscriberStub::OnConnected() {} @@ -214,5 +224,8 @@ void AnsSubscriberStub::OnUpdated(const sptr ¬ificati void AnsSubscriberStub::OnDoNotDisturbDateChange(const sptr &date) {} + +void AnsSubscriberStub::OnEnabledNotificationChanged(const sptr &callbackData) +{} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/dialog/dialog_ui/js/BUILD.gn b/frameworks/ans/dialog/dialog_ui/js/BUILD.gn new file mode 100644 index 000000000..98f575e55 --- /dev/null +++ b/frameworks/ans/dialog/dialog_ui/js/BUILD.gn @@ -0,0 +1,27 @@ +# Copyright (c) 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. + +import("//base/notification/ans_standard/notification.gni") +import("//foundation/ace/ace_engine/build/ace_gen_sa_dialog_js.gni") + +gen_sa_dialog_js("dialog_js_files_etc") { + project_path = "${frameworks_path}/ans/dialog/dialog_ui/js/" + i18n_files = [ + "i18n/en-US.json", + "i18n/zh-CN.json", + ] + common_files = [] + dialog_name = "notification_dialog" + subsystem_name = "notification" + part_name = "ans_standard" +} diff --git a/frameworks/ans/dialog/dialog_ui/js/common/.gitkeep b/frameworks/ans/dialog/dialog_ui/js/common/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/frameworks/ans/dialog/dialog_ui/js/i18n/en-US.json b/frameworks/ans/dialog/dialog_ui/js/i18n/en-US.json new file mode 100644 index 000000000..01f54cf99 --- /dev/null +++ b/frameworks/ans/dialog/dialog_ui/js/i18n/en-US.json @@ -0,0 +1,7 @@ +{ + "message":{ + "requestNotification": "Allowed to send notification?", + "allowmButton": "Allow", + "cancelButton": "Cancel" + } +} diff --git a/frameworks/ans/dialog/dialog_ui/js/i18n/zh-CN.json b/frameworks/ans/dialog/dialog_ui/js/i18n/zh-CN.json new file mode 100644 index 000000000..b527187a6 --- /dev/null +++ b/frameworks/ans/dialog/dialog_ui/js/i18n/zh-CN.json @@ -0,0 +1,7 @@ +{ + "message": { + "requestNotification": "是否允许发送通知?", + "allowmButton": "允许", + "cancelButton": "取消" + } +} diff --git a/frameworks/ans/dialog/dialog_ui/js/pages/index/index.css b/frameworks/ans/dialog/dialog_ui/js/pages/index/index.css new file mode 100644 index 000000000..cf6a1e51c --- /dev/null +++ b/frameworks/ans/dialog/dialog_ui/js/pages/index/index.css @@ -0,0 +1,23 @@ +.container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + left: 0px; + top: 0px; + width: 100%; + height: 100%; + background-color: aqua; +} + +.btn { + width: 50%; + height: 100px; + font-size: 80px; + margin: 10px; +} + +text { + font-size: 100px; + margin: 10px; +} \ No newline at end of file diff --git a/frameworks/ans/dialog/dialog_ui/js/pages/index/index.hml b/frameworks/ans/dialog/dialog_ui/js/pages/index/index.hml new file mode 100644 index 000000000..4759aece1 --- /dev/null +++ b/frameworks/ans/dialog/dialog_ui/js/pages/index/index.hml @@ -0,0 +1,15 @@ +
+
+ + {{ requestNotification }} + +
+
+ + +
+
diff --git a/frameworks/ans/dialog/dialog_ui/js/pages/index/index.js b/frameworks/ans/dialog/dialog_ui/js/pages/index/index.js new file mode 100644 index 000000000..ea497ebbd --- /dev/null +++ b/frameworks/ans/dialog/dialog_ui/js/pages/index/index.js @@ -0,0 +1,22 @@ +import router from '@ohos.router' + +export default { + data: { + message: router.getParams().message, + requestNotification: router.getParams().requestNotification, + allowmButton: router.getParams().allowmButton, + cancelButton: router.getParams().cancelButton, + uid: router.getParams().uid, + }, + onInit() { + console.info('getParams: ' + JSON.stringify(router.getParams())); + }, + onAllow() { + console.info('clicked allow'); + callNativeHandler("EVENT_ALLOW", router.getParams().uid.toString()); + }, + onCancel() { + console.info('clicked cancel'); + callNativeHandler("EVENT_CANCEL", router.getParams().uid.toString()); + }, +} diff --git a/frameworks/ans/native/BUILD.gn b/frameworks/ans/native/BUILD.gn index 7a120ef30..475b8a611 100644 --- a/frameworks/ans/native/BUILD.gn +++ b/frameworks/ans/native/BUILD.gn @@ -40,6 +40,7 @@ ohos_shared_library("ans_innerkits") { ] sources = [ + "src/enabled_notification_callback_data.cpp", "src/message_user.cpp", "src/notification.cpp", "src/notification_action_button.cpp", diff --git a/frameworks/ans/native/src/enabled_notification_callback_data.cpp b/frameworks/ans/native/src/enabled_notification_callback_data.cpp new file mode 100644 index 000000000..32d678a67 --- /dev/null +++ b/frameworks/ans/native/src/enabled_notification_callback_data.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 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 "enabled_notification_callback_data.h" +#include "ans_log_wrapper.h" +#include "string_ex.h" + +namespace OHOS { +namespace Notification { +EnabledNotificationCallbackData::EnabledNotificationCallbackData(std::string bundle, uid_t uid, bool enable) + : bundle_(bundle), uid_(uid), enable_(enable) +{} + +void EnabledNotificationCallbackData::SetBundle(const std::string bundle) +{ + bundle_ = bundle; +} + +std::string EnabledNotificationCallbackData::GetBundle() const +{ + return bundle_; +} + +void EnabledNotificationCallbackData::SetUid(const uid_t uid) +{ + uid_ = uid; +} + +uid_t EnabledNotificationCallbackData::GetUid() const +{ + return uid_; +} + +void EnabledNotificationCallbackData::SetEnable(const bool enable) +{ + enable_ = enable; +} + +bool EnabledNotificationCallbackData::GetEnable() const +{ + return enable_; +} + +std::string EnabledNotificationCallbackData::Dump() +{ + return "EnabledNotificationCallbackData{ " + "bundle = " + bundle_ + + ", uid = " + std::to_string(uid_) + + ", enable = " + std::to_string(enable_) + + " }"; +} + +bool EnabledNotificationCallbackData::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteString16(Str8ToStr16(bundle_))) { + ANS_LOGE("Failed to write bundle name"); + return false; + } + + if (!parcel.WriteInt32(uid_)) { + ANS_LOGE("Failed to write uid"); + return false; + } + + if (!parcel.WriteBool(enable_)) { + ANS_LOGE("Failed to write enable"); + return false; + } + + return true; +} + +EnabledNotificationCallbackData *EnabledNotificationCallbackData::Unmarshalling(Parcel &parcel) +{ + auto objptr = new (std::nothrow) EnabledNotificationCallbackData(); + if ((objptr != nullptr) && !objptr->ReadFromParcel(parcel)) { + delete objptr; + objptr = nullptr; + } + + return objptr; +} + +bool EnabledNotificationCallbackData::ReadFromParcel(Parcel &parcel) +{ + bundle_ = Str16ToStr8(parcel.ReadString16()); + uid_ = parcel.ReadInt32(); + enable_ = parcel.ReadBool(); + + return true; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ans/native/src/notification_helper.cpp b/frameworks/ans/native/src/notification_helper.cpp index 6bc484f52..532ab1ed3 100644 --- a/frameworks/ans/native/src/notification_helper.cpp +++ b/frameworks/ans/native/src/notification_helper.cpp @@ -167,6 +167,16 @@ ErrCode NotificationHelper::IsAllowedNotify(bool &allowed) return DelayedSingleton::GetInstance()->IsAllowedNotify(allowed); } +ErrCode NotificationHelper::IsAllowedNotifySelf(bool &allowed) +{ + return DelayedSingleton::GetInstance()->IsAllowedNotifySelf(allowed); +} + +ErrCode NotificationHelper::RequestEnableNotification(std::string &deviceId) +{ + return DelayedSingleton::GetInstance()->RequestEnableNotification(deviceId); +} + ErrCode NotificationHelper::AreNotificationsSuspended(bool &suspended) { return DelayedSingleton::GetInstance()->AreNotificationsSuspended(suspended); diff --git a/frameworks/ans/native/src/notification_subscriber.cpp b/frameworks/ans/native/src/notification_subscriber.cpp index 291bc9ac7..e3a9a3c33 100644 --- a/frameworks/ans/native/src/notification_subscriber.cpp +++ b/frameworks/ans/native/src/notification_subscriber.cpp @@ -91,6 +91,12 @@ void NotificationSubscriber::SubscriberImpl::OnDoNotDisturbDateChange(const sptr subscriber_.OnDoNotDisturbDateChange(std::make_shared(*date)); } +void NotificationSubscriber::SubscriberImpl::OnEnabledNotificationChanged( + const sptr &callbackData) +{ + subscriber_.OnEnabledNotificationChanged(std::make_shared(*callbackData)); +} + bool NotificationSubscriber::SubscriberImpl::GetAnsManagerProxy() { if (nullptr == proxy_) { diff --git a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp index ec094df77..b91b520c5 100644 --- a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp +++ b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp @@ -74,6 +74,7 @@ enum class SubscriberEventType { ON_DIED, ON_UPDATE, ON_DND_CHANGED, + ON_ENABLED_NOTIFICATION_CHANGED, ON_CANCELED, ON_CANCELED_WITH_SORTINGMAP_AND_DELETEREASON, ON_CONSUMED, @@ -160,6 +161,24 @@ private: std::shared_ptr date_; }; +class OnEnabledNotificationChangedEvent : public SubscriberEvent { +public: + explicit OnEnabledNotificationChangedEvent(const std::shared_ptr &callbackData) + : SubscriberEvent(SubscriberEventType::ON_ENABLED_NOTIFICATION_CHANGED), callbackData_(callbackData) + {} + + ~OnEnabledNotificationChangedEvent() override + {} + + const std::shared_ptr &GetEnabledNotificationCallbackData() const + { + return callbackData_; + } + +private: + std::shared_ptr callbackData_; +}; + class OnOnCanceledEvent : public SubscriberEvent { public: OnOnCanceledEvent(const std::shared_ptr &request) @@ -283,6 +302,14 @@ public: std::unique_lock lck(mtx_); events_.push_back(event); } + void OnEnabledNotificationChanged( + const std::shared_ptr &callbackData) override + { + std::shared_ptr event = + std::make_shared(callbackData); + std::unique_lock lck(mtx_); + events_.push_back(event); + } void OnCanceled(const std::shared_ptr &request) override { std::shared_ptr event = std::make_shared(request); diff --git a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp index a8ac9f307..f87e7d3ce 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -93,6 +93,10 @@ public: virtual void OnDoNotDisturbDateChange(const std::shared_ptr &date) override {} + virtual void OnEnabledNotificationChanged( + const std::shared_ptr &callbackData) override + {} + virtual void OnCanceled(const std::shared_ptr &request) override { GTEST_LOG_(INFO) << "ANS_Interface_MT::OnCanceled request : " << request->GetNotificationRequest().Dump(); diff --git a/interfaces/innerkits/ans/native/include/enabled_notification_callback_data.h b/interfaces/innerkits/ans/native/include/enabled_notification_callback_data.h new file mode 100644 index 000000000..2d57d7eb5 --- /dev/null +++ b/interfaces/innerkits/ans/native/include/enabled_notification_callback_data.h @@ -0,0 +1,88 @@ +/* + * Copyright (c) 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_NATIVE_INCLUDE_NOTIFICATION_ENABLED_NOTIFICATION_DATA_H +#define BASE_NOTIFICATION_ANS_STANDARD_NATIVE_INCLUDE_NOTIFICATION_ENABLED_NOTIFICATION_DATA_H + +#include "notification_constant.h" +#include "parcel.h" + +namespace OHOS { +namespace Notification { +class EnabledNotificationCallbackData : public Parcelable { +public: + /** + * Default constructor used to create a EnabledNotificationCallbackData instance. + */ + EnabledNotificationCallbackData() = default; + + /** + * A constructor used to create a EnabledNotificationCallbackData instance with the input parameters passed. + * @param bundle Indicates the name of the application. + * @param uid Indicates the uid of the application. + * @param enable Indicates whether the application is allowed to send or receive notification. + */ + EnabledNotificationCallbackData(std::string bundle, uid_t uid, bool enable); + + /** + * Default deconstructor used to deconstruct. + */ + ~EnabledNotificationCallbackData() = default; + + void SetBundle(const std::string bundle); + + std::string GetBundle() const; + + void SetUid(const uid_t uid); + + uid_t GetUid() const; + + void SetEnable(const bool enable); + + bool GetEnable() const; + + /** + * Returns a string representation of the object. + * @return a string representation of the object. + */ + std::string Dump(); + + /** + * Marshal a object into a Parcel. + * @param parcel the object into the parcel + */ + virtual bool Marshalling(Parcel &parcel) const override; + + /** + * Unmarshal object from a Parcel. + * @return the EnabledNotificationCallbackData + */ + static EnabledNotificationCallbackData *Unmarshalling(Parcel &parcel); + +private: + /** + * Read a NotificationDoNotDisturbDate object from a Parcel. + * @param parcel the parcel + */ + bool ReadFromParcel(Parcel &parcel); + + std::string bundle_; + uid_t uid_; + bool enable_; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_ANS_STANDARD_NATIVE_INCLUDE_NOTIFICATION_ENABLED_NOTIFICATION_DATA_H \ No newline at end of file diff --git a/interfaces/innerkits/ans/native/include/notification_helper.h b/interfaces/innerkits/ans/native/include/notification_helper.h index 3c12ad24c..7a82980a1 100644 --- a/interfaces/innerkits/ans/native/include/notification_helper.h +++ b/interfaces/innerkits/ans/native/include/notification_helper.h @@ -18,6 +18,7 @@ #include "notification_bundle_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_slot_group.h" @@ -296,13 +297,31 @@ public: static ErrCode SetNotificationBadgeNum(int32_t num); /** - * Checks whether this application has permission to publish notifications. + * Checks whether this application has permission to publish notifications. The caller must have + * system permissions to call this method. * * @param allowed True if this application has the permission; returns false otherwise * @return Returns is allowed notify result. */ static ErrCode IsAllowedNotify(bool &allowed); + /** + * Checks whether this application has permission to publish notifications. + * + * @param allowed True if this application has the permission; returns false otherwise + * @return Returns is allowed notify result. + */ + static ErrCode IsAllowedNotifySelf(bool &allowed); + + /** + * Allow the current application to publish notifications on a specified device. + * + * @param deviceId Indicates the ID of the device running the application. At present, this parameter can + * only be null or an empty string, indicating the current device. + * @return Returns set notifications enabled for default bundle result. + */ + static ErrCode RequestEnableNotification(std::string &deviceId); + /** * Checks whether this application is in the suspended state.Applications in this state cannot publish * notifications. diff --git a/interfaces/innerkits/ans/native/include/notification_subscriber.h b/interfaces/innerkits/ans/native/include/notification_subscriber.h index aefa50b6a..84333bb97 100644 --- a/interfaces/innerkits/ans/native/include/notification_subscriber.h +++ b/interfaces/innerkits/ans/native/include/notification_subscriber.h @@ -100,6 +100,13 @@ public: **/ virtual void OnDoNotDisturbDateChange(const std::shared_ptr &date) = 0; + /** + * @brief Called when the notification permission changes. + * + * @param callbackData Indicates the properties of the application that notification permission has changed. + **/ + virtual void OnEnabledNotificationChanged(const std::shared_ptr &callbackData) = 0; + private: class SubscriberImpl final : public AnsSubscriberStub { public: @@ -137,6 +144,8 @@ private: void OnDoNotDisturbDateChange(const sptr &date) override; + void OnEnabledNotificationChanged(const sptr &callbackData) override; + bool GetAnsManagerProxy(); public: diff --git a/interfaces/kits/napi/ans/include/common.h b/interfaces/kits/napi/ans/include/common.h index ba4cd1aad..ac12c92f1 100644 --- a/interfaces/kits/napi/ans/include/common.h +++ b/interfaces/kits/napi/ans/include/common.h @@ -232,6 +232,9 @@ public: static napi_value SetDoNotDisturbDate( const napi_env &env, const NotificationDoNotDisturbDate &date, napi_value &result); + static napi_value SetEnabledNotificationCallbackData(const napi_env &env, + const EnabledNotificationCallbackData &data, napi_value &result); + static napi_value GetNotificationSubscriberInfo( const napi_env &env, const napi_value &value, NotificationSubscribeInfo &result); diff --git a/interfaces/kits/napi/ans/include/enable_notification.h b/interfaces/kits/napi/ans/include/enable_notification.h index d96094539..3926ae21f 100644 --- a/interfaces/kits/napi/ans/include/enable_notification.h +++ b/interfaces/kits/napi/ans/include/enable_notification.h @@ -23,6 +23,8 @@ using namespace OHOS::Notification; napi_value EnableNotification(napi_env env, napi_callback_info info); napi_value IsNotificationEnabled(napi_env env, napi_callback_info info); +napi_value IsNotificationEnabledSelf(napi_env env, napi_callback_info info); +napi_value RequestEnableNotification(napi_env env, napi_callback_info info); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NAPI_INCLUDE_ENABLE_NOTIFICATION_H \ No newline at end of file diff --git a/interfaces/kits/napi/ans/include/subscribe.h b/interfaces/kits/napi/ans/include/subscribe.h index b18e1de4b..7c7122b70 100644 --- a/interfaces/kits/napi/ans/include/subscribe.h +++ b/interfaces/kits/napi/ans/include/subscribe.h @@ -47,6 +47,9 @@ public: virtual void OnDoNotDisturbDateChange(const std::shared_ptr &date) override; + virtual void OnEnabledNotificationChanged( + const std::shared_ptr &callbackData) override; + void SetCallbackInfo(const napi_env &env, const std::string &type, const napi_ref &ref); private: @@ -66,6 +69,8 @@ private: void SetDisturbDateCallbackInfo(const napi_env &env, const napi_ref &ref); + void SetEnabledNotificationCallbackInfo(const napi_env &env, const napi_ref &ref); + private: struct CallbackInfo { napi_env env = nullptr; @@ -80,6 +85,7 @@ private: CallbackInfo dieCallbackInfo_; CallbackInfo disturbModeCallbackInfo_; CallbackInfo disturbDateCallbackInfo_; + CallbackInfo enabledNotificationCallbackInfo_; }; struct SubscriberInstancesInfo { diff --git a/interfaces/kits/napi/ans/src/common.cpp b/interfaces/kits/napi/ans/src/common.cpp index d9c7607c3..d9e8a18ae 100644 --- a/interfaces/kits/napi/ans/src/common.cpp +++ b/interfaces/kits/napi/ans/src/common.cpp @@ -1328,6 +1328,28 @@ napi_value Common::SetDoNotDisturbDate( return NapiGetBoolean(env, true); } +napi_value Common::SetEnabledNotificationCallbackData(const napi_env &env, const EnabledNotificationCallbackData &data, + napi_value &result) +{ + ANS_LOGI("enter"); + // bundle: string + napi_value bundleNapi = nullptr; + napi_create_string_utf8(env, data.GetBundle().c_str(), NAPI_AUTO_LENGTH, &bundleNapi); + napi_set_named_property(env, result, "bundle", bundleNapi); + + // uid: uid_t + napi_value uidNapi = nullptr; + napi_create_int32(env, data.GetUid(), &uidNapi); + napi_set_named_property(env, result, "uid", uidNapi); + + // enable: bool + napi_value enableNapi = nullptr; + napi_get_boolean(env, data.GetEnable(), &enableNapi); + napi_set_named_property(env, result, "enable", enableNapi); + + return NapiGetBoolean(env, true); +} + napi_value Common::GetNotificationSubscriberInfo( const napi_env &env, const napi_value &value, NotificationSubscribeInfo &subscriberInfo) { diff --git a/interfaces/kits/napi/ans/src/enable_notification.cpp b/interfaces/kits/napi/ans/src/enable_notification.cpp index e2802f748..ee5f32990 100644 --- a/interfaces/kits/napi/ans/src/enable_notification.cpp +++ b/interfaces/kits/napi/ans/src/enable_notification.cpp @@ -289,5 +289,98 @@ napi_value IsNotificationEnabled(napi_env env, napi_callback_info info) return promise; } } + +napi_value IsNotificationEnabledSelf(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + IsEnableParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoIsEnable *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoIsEnable {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "IsNotificationEnabledSelf", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsNotificationEnabledSelf napi_create_async_work start"); + AsyncCallbackInfoIsEnable *asynccallbackinfo = (AsyncCallbackInfoIsEnable *)data; + + if (asynccallbackinfo->params.hasBundleOption) { + ANS_LOGE("Not allowed to query another application"); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::IsAllowedNotifySelf(asynccallbackinfo->allowed); + } + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d, allowed = %{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->allowed); + }, + AsyncCompleteCallbackIsNotificationEnabled, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value RequestEnableNotification(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + IsEnableParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoIsEnable *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoIsEnable {.env = env, .asyncWork = nullptr, .params = params}; + + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "RequestEnableNotification", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("RequestEnableNotification napi_create_async_work start"); + AsyncCallbackInfoIsEnable *asynccallbackinfo = (AsyncCallbackInfoIsEnable *)data; + + std::string deviceId {""}; + asynccallbackinfo->info.errorCode = NotificationHelper::RequestEnableNotification(deviceId); + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode); + }, + AsyncCompleteCallbackIsNotificationEnabled, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/ans/src/init.cpp b/interfaces/kits/napi/ans/src/init.cpp index bbb3df5a3..9fae48ef7 100644 --- a/interfaces/kits/napi/ans/src/init.cpp +++ b/interfaces/kits/napi/ans/src/init.cpp @@ -63,6 +63,8 @@ napi_value NotificationInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("removeGroupByBundle", RemoveGroupByBundle), DECLARE_NAPI_FUNCTION("enableNotification", EnableNotification), DECLARE_NAPI_FUNCTION("isNotificationEnabled", IsNotificationEnabled), + DECLARE_NAPI_FUNCTION("isNotificationEnabledSelf", IsNotificationEnabledSelf), + DECLARE_NAPI_FUNCTION("requestEnableNotification", RequestEnableNotification), DECLARE_NAPI_FUNCTION("displayBadge", DisplayBadge), DECLARE_NAPI_FUNCTION("isBadgeDisplayed", IsBadgeDisplayed), DECLARE_NAPI_FUNCTION("setDoNotDisturbDate", SetDoNotDisturbDate), diff --git a/interfaces/kits/napi/ans/src/subscribe.cpp b/interfaces/kits/napi/ans/src/subscribe.cpp index a0a5b2cd0..71440fee3 100644 --- a/interfaces/kits/napi/ans/src/subscribe.cpp +++ b/interfaces/kits/napi/ans/src/subscribe.cpp @@ -30,6 +30,7 @@ const std::string DIS_CONNECTED = "onDisconnect"; const std::string DIE = "onDestroy"; const std::string DISTURB_MODE_CHANGE = "onDisturbModeChange"; const std::string DISTURB_DATE_CHANGE = "onDoNotDisturbDateChange"; +const std::string ENABLE_NOTIFICATION_CHANGED = "OnEnabledNotificationChanged"; struct NotificationReceiveDataWorker { napi_env env = nullptr; @@ -37,6 +38,7 @@ struct NotificationReceiveDataWorker { std::shared_ptr request; std::shared_ptr sortingMap; NotificationDoNotDisturbDate date; + EnabledNotificationCallbackData callbackData; int deleteReason = 0; int result = 0; int disturbMode = 0; @@ -139,6 +141,9 @@ SubscriberInstance::~SubscriberInstance() if (disturbModeCallbackInfo_.ref != nullptr) { napi_delete_reference(disturbModeCallbackInfo_.env, disturbModeCallbackInfo_.ref); } + if (enabledNotificationCallbackInfo_.ref != nullptr) { + napi_delete_reference(enabledNotificationCallbackInfo_.env, enabledNotificationCallbackInfo_.ref); + } } void SubscriberInstance::OnCanceled(const std::shared_ptr &request) @@ -709,6 +714,89 @@ void SubscriberInstance::OnDoNotDisturbDateChange(const std::shared_ptrdata; + if (dataWorkerData == nullptr) { + ANS_LOGE("dataWorkerData is null"); + delete work; + work = nullptr; + return; + } + + napi_value result = nullptr; + napi_create_object(dataWorkerData->env, &result); + + if (!Common::SetEnabledNotificationCallbackData(dataWorkerData->env, dataWorkerData->callbackData, result)) { + result = Common::NapiGetNull(dataWorkerData->env); + } + + Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, result); + + delete dataWorkerData; + dataWorkerData = nullptr; + delete work; + work = nullptr; +} + +void SubscriberInstance::OnEnabledNotificationChanged( + const std::shared_ptr &callbackData) +{ + ANS_LOGI("enter"); + + if (enabledNotificationCallbackInfo_.ref == nullptr) { + ANS_LOGI("enabledNotificationCallbackInfo_ callback unset"); + return; + } + + if (callbackData == nullptr) { + ANS_LOGE("callbackData is null"); + return; + } + + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(enabledNotificationCallbackInfo_.env, &loop); + if (loop == nullptr) { + ANS_LOGE("loop instance is nullptr"); + return; + } + + NotificationReceiveDataWorker *dataWorker = new (std::nothrow) NotificationReceiveDataWorker(); + if (dataWorker == nullptr) { + ANS_LOGE("new dataWorker failed"); + return; + } + + dataWorker->callbackData = *callbackData; + dataWorker->env = enabledNotificationCallbackInfo_.env; + dataWorker->ref = enabledNotificationCallbackInfo_.ref; + + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + ANS_LOGE("new work failed"); + delete dataWorker; + dataWorker = nullptr; + return; + } + + work->data = (void *)dataWorker; + + int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, UvQueueWorkOnEnabledNotificationChanged); + if (ret != 0) { + delete dataWorker; + dataWorker = nullptr; + delete work; + work = nullptr; + } +} + void SubscriberInstance::SetCancelCallbackInfo(const napi_env &env, const napi_ref &ref) { canceCallbackInfo_.env = env; @@ -751,6 +839,12 @@ void SubscriberInstance::SetDisturbModeCallbackInfo(const napi_env &env, const n disturbModeCallbackInfo_.ref = ref; } +void SubscriberInstance::SetEnabledNotificationCallbackInfo(const napi_env &env, const napi_ref &ref) +{ + enabledNotificationCallbackInfo_.env = env; + enabledNotificationCallbackInfo_.ref = ref; +} + void SubscriberInstance::SetDisturbDateCallbackInfo(const napi_env &env, const napi_ref &ref) { disturbDateCallbackInfo_.env = env; @@ -775,6 +869,8 @@ void SubscriberInstance::SetCallbackInfo(const napi_env &env, const std::string SetDisturbModeCallbackInfo(env, ref); } else if (type == DISTURB_DATE_CHANGE) { SetDisturbDateCallbackInfo(env, ref); + } else if (type == ENABLE_NOTIFICATION_CHANGED) { + SetEnabledNotificationCallbackInfo(env, ref); } else { ANS_LOGW("type is error"); } @@ -902,6 +998,8 @@ napi_value GetNotificationSubscriber( subscriberInfo.subscriber->SetCallbackInfo(env, DISTURB_DATE_CHANGE, result); } + // onEnabledNotificationChanged?:(data: notification.EnabledNotificationCallbackData) => void + return Common::NapiGetNull(env); } diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index 12c62d307..25ec1ffc3 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -54,8 +54,10 @@ ohos_shared_library("libans") { deps = [ "${core_path}:ans_core", + "${frameworks_path}/ans/dialog/dialog_ui/js:dialog_js_files_etc", "${frameworks_path}/ans/native:ans_innerkits", "${frameworks_path}/wantagent:wantagent_innerkits", + "//foundation/ace/ace_engine/interfaces/innerkits/ui_service_manager:ui_service_mgr", "//utils/native/base:utils", ] @@ -77,6 +79,7 @@ ohos_shared_library("libans") { "multimedia_media_standard:media_client", "os_account_standard:os_account_innerkits", "time_native:time_service", + "window_manager:libwm", ] external_deps += ans_standard_external_deps diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index c12096082..fb606b2bd 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -87,6 +87,7 @@ public: const sptr &bundleOption, const std::vector> &slots) override; ErrCode UpdateSlotGroups(const sptr &bundleOption, const std::vector> &groups) override; + ErrCode RequestEnableNotification(const std::string &deviceId) override; ErrCode SetNotificationsEnabledForBundle(const std::string &bundle, bool enabled) override; ErrCode SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) override; ErrCode SetNotificationsEnabledForSpecialBundle( @@ -97,6 +98,7 @@ public: ErrCode Subscribe(const sptr &subscriber, const sptr &info) override; ErrCode Unsubscribe(const sptr &subscriber, const sptr &info) override; ErrCode IsAllowedNotify(bool &allowed) override; + ErrCode IsAllowedNotifySelf(bool &allowed) override; ErrCode IsSpecialBundleAllowedNotify(const sptr &bundleOption, bool &allowed) override; ErrCode SetDoNotDisturbDate(const sptr &date) override; @@ -205,6 +207,8 @@ private: ErrCode SetDoNotDisturbDateByUser(const int32_t &userId, const sptr &date); ErrCode GetDoNotDisturbDateByUser(const int32_t &userId, sptr &date); + ErrCode SetHasPoppedDialog(const sptr bundleOption, bool hasPopped); + ErrCode GetHasPoppedDialog(const sptr bundleOption, bool &hasPopped); private: static sptr instance_; diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 516432ad8..8a50da7b5 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -66,6 +66,8 @@ public: ErrCode SetNotificationsEnabledForBundle(const sptr &bundleOption, const bool enabled); ErrCode GetNotificationsEnabled(const int32_t &userId, bool &enabled); ErrCode SetNotificationsEnabled(const int32_t &userId, const bool &enabled); + ErrCode GetHasPoppedDialog(const sptr &bundleOption, bool &hasPopped); + ErrCode SetHasPoppedDialog(const sptr &bundleOption, bool hasPopped); ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr &date); ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr date); ErrCode GetTemplateSupported(const std::string& templateName, bool &support); diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index 6b531e451..0d943c24a 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -41,6 +41,7 @@ public: bool PutNotificationsEnabledForBundle( const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled); bool PutNotificationsEnabled(const int32_t &userId, const bool &enabled); + bool PutHasPoppedDialog(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped); bool PutDoNotDisturbDate(const int32_t &userId, const sptr &date); bool ParseFromDisturbeDB(NotificationPreferencesInfo &info); @@ -119,6 +120,8 @@ private: NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleEnableNotification( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; + void ParseBundlePoppedDialog( + NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleUid(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseSlot( const std::string &findString, sptr &slot, const OHOS::DistributedKv::Entry &entry); diff --git a/services/ans/include/notification_preferences_info.h b/services/ans/include/notification_preferences_info.h index 943252dc5..732e1d736 100644 --- a/services/ans/include/notification_preferences_info.h +++ b/services/ans/include/notification_preferences_info.h @@ -46,6 +46,8 @@ public: bool GetIsPrivateAllowed() const; void SetEnableNotification(const bool &enable); bool GetEnableNotification() const; + void SetHasPoppedDialog(const bool &hasPopped); + bool GetHasPoppedDialog() const; void SetSlot(const sptr &slot); bool GetSlot(const NotificationConstant::SlotType &type, sptr &slot) const; bool GetAllSlots(std::vector> &slots); @@ -72,6 +74,7 @@ public: int badgeTotalNum_ = BUNDLE_BADGE_TOTAL_NUM; bool isPrivateAllowed_ = BUNDLE_PRIVATE_ALLOWED; bool isEnabledNotification_ = BUNDLE_ENABLE_NOTIFICATION; + bool hasPoppedDialog_ = BUNDLE_POPPED_DIALOG; std::map> slots_; std::map> groups_; }; diff --git a/services/ans/include/notification_subscriber_manager.h b/services/ans/include/notification_subscriber_manager.h index 062a6b3ba..2bf73706e 100644 --- a/services/ans/include/notification_subscriber_manager.h +++ b/services/ans/include/notification_subscriber_manager.h @@ -47,6 +47,7 @@ public: const sptr ¬ification, const sptr ¬ificationMap, int deleteReason); void NotifyUpdated(const sptr ¬ificationMap); void NotifyDoNotDisturbDateChanged(const sptr &date); + void NotifyEnabledNotificationChanged(const sptr &callbackData); void OnRemoteDied(const wptr &object); private: @@ -70,6 +71,7 @@ private: const sptr ¬ification, const sptr ¬ificationMap, int deleteReason); void NotifyUpdatedInner(const sptr ¬ificationMap); void NotifyDoNotDisturbDateChangedInner(const sptr &date); + void NotifyEnabledNotificationChangedInner(const sptr &callbackData); bool IsSystemUser(int32_t userId); private: diff --git a/services/ans/include/preferences_constant.h b/services/ans/include/preferences_constant.h index b5430c697..e2538daf3 100644 --- a/services/ans/include/preferences_constant.h +++ b/services/ans/include/preferences_constant.h @@ -32,6 +32,7 @@ const static std::string KEY_BUNDLE_SHOW_BADGE = "showBadge"; const static std::string KEY_BUNDLE_BADGE_TOTAL_NUM = "badgeTotalNum"; const static std::string KEY_BUNDLE_PRIVATE_ALLOWED = "privateAllowed"; const static std::string KEY_BUNDLE_ENABLE_NOTIFICATION = "enabledNotification"; +const static std::string KEY_BUNDLE_POPPED_DIALOG = "poppedDialog"; const static std::string KEY_BUNDLE_UID = "uid"; const static std::string KEY_GROUP = "group"; @@ -64,6 +65,7 @@ const static int BUNDLE_IMPORTANCE = 3; const static int BUNDLE_BADGE_TOTAL_NUM = 0; const static int BUNDLE_PRIVATE_ALLOWED = false; const static int BUNDLE_ENABLE_NOTIFICATION = true; +const static int BUNDLE_POPPED_DIALOG = false; const static bool BUNDLE_SHOW_BADGE = false; enum class BundleType { @@ -73,6 +75,7 @@ enum class BundleType { BUNDLE_BADGE_TOTAL_NUM_TYPE, BUNDLE_PRIVATE_ALLOWED_TYPE, BUNDLE_ENABLE_NOTIFICATION_TYPE, + BUNDLE_POPPED_DIALOG_TYPE, }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index d5c42d214..ec56db5b7 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -35,7 +35,9 @@ #include "permission_filter.h" #include "reminder_data_manager.h" #include "trigger_info.h" +#include "ui_service_mgr_client.h" #include "want_agent_helper.h" +#include "wm_common.h" #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED #include "distributed_notification_manager.h" @@ -61,6 +63,11 @@ constexpr int32_t DEFAULT_RECENT_COUNT = 16; constexpr int HOURS_IN_ONE_DAY = 24; +constexpr int DIALOG_POSTION_X = 150; +constexpr int DIALOG_POSTION_Y = 300; +constexpr int DIALOG_WIDTH = 450; +constexpr int DIALOG_HEIGHT = 300; + struct RecentNotification { sptr notification = nullptr; bool isActive = false; @@ -1053,11 +1060,6 @@ ErrCode AdvancedNotificationService::UpdateSlotGroups( return result; } -ErrCode AdvancedNotificationService::SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) -{ - return ERR_INVALID_OPERATION; -} - ErrCode AdvancedNotificationService::SetShowBadgeEnabledForBundle( const sptr &bundleOption, bool enabled) { @@ -1350,6 +1352,68 @@ ErrCode AdvancedNotificationService::GetSpecialActiveNotifications( return result; } +ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + ErrCode result = ERR_OK; + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + + bool allowedNotify = false; + result = IsSpecialBundleAllowedNotify(bundleOption, allowedNotify); + if (allowedNotify) { + ANS_LOGD("Already granted permission"); + return result; + } + + bool hasPopped = false; + result = GetHasPoppedDialog(bundleOption, hasPopped); + if (hasPopped) { + ANS_LOGD("Already shown dialog"); + return result; + } + + const std::string params = std::string("{\"requestNotification\":\"Allowed to send notification?\", ") + + std::string("\"allowButton\":\"Alllow\", \"cancelButton\":\"Cancel\", \"uid\":\"") + + std::to_string(bundleOption->GetUid()) + std::string("\"}"); + Ace::UIServiceMgrClient::GetInstance()->ShowDialog( + "notification_dialog", + params, + OHOS::Rosen::WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW, + DIALOG_POSTION_X, + DIALOG_POSTION_Y, + DIALOG_WIDTH, + DIALOG_HEIGHT, + [this](int32_t id, const std::string& event, const std::string& params) { + ANS_LOGD("Dialog callback: %{public}s, %{public}s", event.c_str(), params.c_str()); + int32_t uid = std::stoi(params, nullptr); + std::string bundle; + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager != nullptr) { + bundle = bundleManager->GetBundleNameByUid(uid); + } + sptr bundleOption = new NotificationBundleOption(bundle, uid); + if (event == "EVENT_ALLOW") { + this->SetNotificationsEnabledForSpecialBundle("", bundleOption, true); + Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); + } else { + this->SetNotificationsEnabledForSpecialBundle("", bundleOption, false); + Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); + } + this->SetHasPoppedDialog(bundleOption, true); + }); + + return result; +} + +ErrCode AdvancedNotificationService::SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) +{ + return ERR_INVALID_OPERATION; +} + ErrCode AdvancedNotificationService::SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -1397,11 +1461,20 @@ ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundle( return ERR_ANS_INVALID_BUNDLE; } + sptr bundleData = + new EnabledNotificationCallbackData(bundle->GetBundleName(), bundle->GetUid(), enabled); + ErrCode result = ERR_OK; handler_->PostSyncTask(std::bind([&]() { if (deviceId.empty()) { // Local device result = NotificationPreferences::GetInstance().SetNotificationsEnabledForBundle(bundle, enabled); + if (!enabled) { + result = RemoveAllNotifications(bundle); + } + if (result == ERR_OK) { + NotificationSubscriberManager::GetInstance()->NotifyEnabledNotificationChanged(bundleData); + } } else { // Remote revice } @@ -1434,6 +1507,27 @@ ErrCode AdvancedNotificationService::IsAllowedNotify(bool &allowed) return result; } +ErrCode AdvancedNotificationService::IsAllowedNotifySelf(bool &allowed) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + ErrCode result = ERR_OK; + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + + handler_->PostSyncTask(std::bind([&]() { + allowed = false; + result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(bundleOption, allowed); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + } + })); + + return result; +} + ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify( const sptr &bundleOption, bool &allowed) { @@ -1479,7 +1573,7 @@ ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify( result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(targetBundle, allowed); if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { result = ERR_OK; - allowed = true; + allowed = false; } } })); @@ -3018,7 +3112,7 @@ ErrCode AdvancedNotificationService::SetDoNotDisturbDateByUser(const int32_t &us NotificationSubscriberManager::GetInstance()->NotifyDoNotDisturbDateChanged(newConfig); } })); - + return ERR_OK; } @@ -3026,7 +3120,7 @@ ErrCode AdvancedNotificationService::GetDoNotDisturbDateByUser(const int32_t &us sptr &date) { ErrCode result = ERR_OK; - + handler_->PostSyncTask(std::bind([&]() { sptr currentConfig = nullptr; result = NotificationPreferences::GetInstance().GetDoNotDisturbDate(userId, currentConfig); @@ -3051,5 +3145,27 @@ ErrCode AdvancedNotificationService::GetDoNotDisturbDateByUser(const int32_t &us return ERR_OK; } + +ErrCode AdvancedNotificationService::SetHasPoppedDialog( + const sptr bundleOption, bool hasPopped) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + ErrCode result = ERR_OK; + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().SetHasPoppedDialog(bundleOption, hasPopped); + })); + return result; +} + +ErrCode AdvancedNotificationService::GetHasPoppedDialog( + const sptr bundleOption, bool &hasPopped) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + ErrCode result = ERR_OK; + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().GetHasPoppedDialog(bundleOption, hasPopped); + })); + return result; +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 209e0e1fa..a9c5e0d4f 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -540,6 +540,29 @@ ErrCode NotificationPreferences::SetNotificationsEnabled(const int32_t &userId, return result; } +ErrCode NotificationPreferences::GetHasPoppedDialog(const sptr &bundleOption, bool &hasPopped) +{ + if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { + return ERR_ANS_INVALID_PARAM; + } + return GetBundleProperty(bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped); +} + +ErrCode NotificationPreferences::SetHasPoppedDialog(const sptr &bundleOption, bool hasPopped) +{ + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_PARAM; + } + + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + ErrCode result = ERR_OK; + result = SetBundleProperty(preferencesInfo, bundleOption, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped); + if (result == ERR_OK) { + preferencesInfo_ = preferencesInfo; + } + return result; +} + ErrCode NotificationPreferences::GetDoNotDisturbDate(const int32_t &userId, sptr &date) { @@ -564,7 +587,7 @@ ErrCode NotificationPreferences::SetDoNotDisturbDate(const int32_t &userId, NotificationPreferencesInfo preferencesInfo = preferencesInfo_; preferencesInfo.SetDoNotDisturbDate(userId, date); - + ErrCode result = ERR_OK; if (!preferncesDB_->PutDoNotDisturbDate(userId, date)) { result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; @@ -769,6 +792,10 @@ ErrCode NotificationPreferences::SaveBundleProperty(NotificationPreferencesInfo: bundleInfo.SetEnableNotification(value); storeDBResult = preferncesDB_->PutNotificationsEnabledForBundle(bundleInfo, value); break; + case BundleType::BUNDLE_POPPED_DIALOG_TYPE: + bundleInfo.SetHasPoppedDialog(value); + storeDBResult = preferncesDB_->PutHasPoppedDialog(bundleInfo, value); + break; default: break; } @@ -798,6 +825,9 @@ ErrCode NotificationPreferences::GetBundleProperty( case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE: value = bundleInfo.GetEnableNotification(); break; + case BundleType::BUNDLE_POPPED_DIALOG_TYPE: + value = bundleInfo.GetHasPoppedDialog(); + break; default: result = ERR_ANS_INVALID_PARAM; break; diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 966e69a71..8dbcadc51 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -114,6 +114,11 @@ const std::map &date) { @@ -503,6 +526,9 @@ bool NotificationPreferencesDatabase::PutBundlePropertyValueToDisturbeDB( GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION), std::to_string(bundleInfo.GetEnableNotification()), entries); + GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_POPPED_DIALOG), + std::to_string(bundleInfo.GetHasPoppedDialog()), + entries); GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_UID), std::to_string(bundleInfo.GetBundleUid()), entries); if (!CheckKvStore()) { ANS_LOGE("KvStore is nullptr."); @@ -733,6 +759,9 @@ OHOS::DistributedKv::Status NotificationPreferencesDatabase::PutBundlePropertyTo case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE: keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION); break; + case BundleType::BUNDLE_POPPED_DIALOG_TYPE: + keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_POPPED_DIALOG); + break; default: break; } @@ -1214,6 +1243,13 @@ void NotificationPreferencesDatabase::ParseBundleEnableNotification( bundleInfo.SetEnableNotification(static_cast(StringToInt(value))); } +void NotificationPreferencesDatabase::ParseBundlePoppedDialog( + NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const +{ + ANS_LOGD("SetBundlePoppedDialog bundle has popped dialog is %{public}s.", value.c_str()); + bundleInfo.SetEnableNotification(static_cast(StringToInt(value))); +} + void NotificationPreferencesDatabase::ParseBundleUid( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const { diff --git a/services/ans/src/notification_preferences_info.cpp b/services/ans/src/notification_preferences_info.cpp index 586288ac3..55eabc6c0 100644 --- a/services/ans/src/notification_preferences_info.cpp +++ b/services/ans/src/notification_preferences_info.cpp @@ -81,6 +81,17 @@ bool NotificationPreferencesInfo::BundleInfo::GetEnableNotification() const return isEnabledNotification_; } + +void NotificationPreferencesInfo::BundleInfo::SetHasPoppedDialog(const bool &hasPopped) +{ + hasPoppedDialog_ = hasPopped; +} + +bool NotificationPreferencesInfo::BundleInfo::GetHasPoppedDialog() const +{ + return hasPoppedDialog_; +} + void NotificationPreferencesInfo::BundleInfo::SetSlot(const sptr &slot) { slots_.insert_or_assign(slot->GetType(), slot); diff --git a/services/ans/src/notification_subscriber_manager.cpp b/services/ans/src/notification_subscriber_manager.cpp index 68b11af00..a016d6f6a 100644 --- a/services/ans/src/notification_subscriber_manager.cpp +++ b/services/ans/src/notification_subscriber_manager.cpp @@ -148,6 +148,20 @@ void NotificationSubscriberManager::NotifyDoNotDisturbDateChanged(const sptrPostTask(func); } +void NotificationSubscriberManager::NotifyEnabledNotificationChanged( + const sptr &callbackData) +{ + if (handler_ == nullptr) { + ANS_LOGE("handler is nullptr"); + return; + } + + AppExecFwk::EventHandler::Callback func = + std::bind(&NotificationSubscriberManager::NotifyEnabledNotificationChangedInner, this, callbackData); + + handler_->PostTask(func); +} + void NotificationSubscriberManager::OnRemoteDied(const wptr &object) { ANS_LOGI("OnRemoteDied"); @@ -287,7 +301,7 @@ void NotificationSubscriberManager::NotifyConsumedInner( ANS_LOGD("%{public}s notification->GetUserId <%{public}d>", __FUNCTION__, notification->GetUserId()); for (auto record : subscriberRecordList_) { ANS_LOGD("%{public}s record->userId = <%{public}d>", __FUNCTION__, record->userId); - + int32_t recvUserId = notification->GetNotificationRequest().GetReceiverUserId(); auto BundleNames = notification->GetBundleName(); auto iter = std::find(record->bundleList_.begin(), record->bundleList_.end(), BundleNames); @@ -340,5 +354,13 @@ bool NotificationSubscriberManager::IsSystemUser(int32_t userId) return false; } + +void NotificationSubscriberManager::NotifyEnabledNotificationChangedInner( + const sptr &callbackData) +{ + for (auto record : subscriberRecordList_) { + record->subscriber->OnEnabledNotificationChanged(callbackData); + } +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 213e21b9c..433c15344 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -64,6 +64,7 @@ ohos_unittest("ans_unit_test") { "${core_path}:ans_core", "${frameworks_path}/ans/native:ans_innerkits", "${frameworks_path}/wantagent:wantagent_innerkits", + "//foundation/ace/ace_engine/interfaces/innerkits/ui_service_manager:ui_service_mgr", "//third_party/googletest:gtest_main", "//utils/native/base:utils", ] diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 441a16cfd..53f41da1b 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -90,6 +90,9 @@ public: {} void OnDoNotDisturbDateChange(const std::shared_ptr &date) override {} + void OnEnabledNotificationChanged( + const std::shared_ptr &callbackData) override + {} void OnCanceled(const std::shared_ptr &request) override {} void OnCanceled(const std::shared_ptr &request, diff --git a/services/ans/test/unittest/notification_subscriber_manager_test.cpp b/services/ans/test/unittest/notification_subscriber_manager_test.cpp index eb8d6f743..445e8c7cd 100644 --- a/services/ans/test/unittest/notification_subscriber_manager_test.cpp +++ b/services/ans/test/unittest/notification_subscriber_manager_test.cpp @@ -45,6 +45,9 @@ private: {} void OnDoNotDisturbDateChange(const std::shared_ptr &date) override {} + void OnEnabledNotificationChanged( + const std::shared_ptr &callbackData) override + {} void OnCanceled(const std::shared_ptr &request) override {} void OnCanceled(const std::shared_ptr &request, diff --git a/services/test/moduletest/BUILD.gn b/services/test/moduletest/BUILD.gn index b794fb129..225f65d35 100644 --- a/services/test/moduletest/BUILD.gn +++ b/services/test/moduletest/BUILD.gn @@ -57,6 +57,7 @@ ohos_moduletest("ans_module_test") { "${core_path}:ans_core", "${frameworks_path}/ans/native:ans_innerkits", "${frameworks_path}/wantagent:wantagent_innerkits", + "//foundation/ace/ace_engine/interfaces/innerkits/ui_service_manager:ui_service_mgr", "//third_party/googletest:gtest_main", "//utils/native/base:utils", ] diff --git a/services/test/moduletest/ans_module_test.cpp b/services/test/moduletest/ans_module_test.cpp index 0598cc8a4..5bc3a973b 100644 --- a/services/test/moduletest/ans_module_test.cpp +++ b/services/test/moduletest/ans_module_test.cpp @@ -51,6 +51,9 @@ public: {} void OnDoNotDisturbDateChange(const std::shared_ptr &date) override {} + void OnEnabledNotificationChanged( + const std::shared_ptr &callbackData) override + {} void OnCanceled(const std::shared_ptr &request) override {} void OnCanceled(const std::shared_ptr &request, -- Gitee