diff --git a/frameworks/ans/core/BUILD.gn b/frameworks/ans/core/BUILD.gn index 004803bb5e35392aeda834fe30cc3c27775a0b36..9aaee019c6c83aaf1b3979ca9144180397e68676 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 91be045ebeaa445120440e1c8afa98f24b352cf1..21c479c7fcfb13ed72239d3bd709aa9038536026 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 cb4afad06b50e2a23409e2d8619efb28db13c4cb..5f8f568bf3ab051c9be413de7dbcbcef7c85bf23 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 f667eccc50fc135add65e436585062300f10b994..ff87901abd4b3431973ed9fdbddaeef3be66e0b7 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 cf214a569d5ae4a5a766f5a337fcc25fc441fcc1..62a01657b99ff6a36fce3f05bac9abb24f3b22c3 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 326680107b45f00493ef7b0a534d35aa3f507272..1c861680531d5706fe964d346d8d3f3420104703 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 8739caf27fc5ee1086965993f72f7e99838f5c7d..1acdacf6048b6a48761771b9160185c666418c0c 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 26928cd84e11ac350ddbc43caaa60a5bce17ee15..303c81d29726c1be2423955177493e9d7c23a8d0 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 4d9396dde286f69fcf880995d7f27d681f3335cd..000af8c28c72701d84bbbe4629a744e71ab8f8e5 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 cfcd7031ade9fe840e933db9f66cd3134f60df7c..9110ac4a871329f69faf89ae93d574a730439db7 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 43b9fb64f82e9c933207b8fc45739b6b1d4b4d52..fb2439edb46e917393addc84ce7b0b6eed089a26 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 1952b3f6b602b45aa23cdccb6f5e44814212c782..ede35cc11c8254e534bc7b806f140d0c113f5b43 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 932238a92000cf14a82eeab5bff1448ec901f131..7340a0c0c5b8ede50900cc610722f146fc4c98ad 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 0000000000000000000000000000000000000000..98f575e55a5c6551fa9ec3fde35208aca82211f0 --- /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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 0000000000000000000000000000000000000000..01f54cf99d9178de6020aeede822fbc6c1a557b8 --- /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 0000000000000000000000000000000000000000..b527187a62cf11a9ac3f8909b43cfdeec2d5d821 --- /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 0000000000000000000000000000000000000000..cf6a1e51c1f0ea48e7e44258d6839b83c8e4cb0d --- /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 0000000000000000000000000000000000000000..4759aece15404f0fd64fc19ce185a5ce8630eb10 --- /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 0000000000000000000000000000000000000000..ea497ebbd30bcdd0efa1ef1a8f8fbaf7ffa3e865 --- /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 7a120ef302366e6ab8e315b37456580cf5d346e8..475b8a6118d13b27cee268ceaf528fd345a7db2a 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 0000000000000000000000000000000000000000..32d678a67126baa5d56c2cd8e04de5dbcd2288ff --- /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 6bc484f52e7f45eecfc6a81a9b6072abb4b75b48..532ab1ed3871d59638d71ba432b6b2bbda0d9652 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 291bc9ac75f5d2d7c86af8fa8254fb1baf452d13..e3a9a3c334858baa5391f370ed84d6b87a2a8676 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 ec094df775522b4b11f5a78ad63d2f3815e10e52..b91b520c59f78cd650b29c5dd056ef179fad3919 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 a8ac9f30751ead3851743819d44022d910b2073a..f87e7d3ce5b94f5b3fe439815e748ef5e712fb85 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 0000000000000000000000000000000000000000..2d57d7eb5b33e7e136eb4013bd7fe66e4baf5f65 --- /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 3c12ad24cbf41a98f92a8fe8abe54571bec3cba8..7a82980a17566f9428f03ca290bd244a0e03deb4 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 aefa50b6a58683522c91b38fa43de816ff147172..84333bb975d92a55be9726c3db1b6b19256be9a4 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 ba4cd1aadaee35e5170f873578c06b42e05a6729..ac12c92f1472d99a08852855191ac0d0a4b7b784 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 d96094539a89f199ba1cb470c9f2df6f2b5ef006..3926ae21f468fbd900b27da11b78967b7c72e266 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 b18e1de4b0bc547caf963db874b64bed9d702eae..7c7122b70f35e184de5e281a961938df3e59cd2e 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 d9c7607c39d96330b64a050ab6d75ad84812bfbe..d9e8a18ae4d1bef627f0860b8858b9c2be8cf556 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 e2802f748695fa2c2ce7189d4b707f45e8bc33db..ee5f3299000e22439a3963d51a10b02a21765ef5 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 bbb3df5a3b0ad8ea9efe940e548c7f24646c4a40..9fae48ef7863ceddd8d9c0fa9d1fddafd9705f74 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 a0a5b2cd04364da4ef552e4ec7af97b3fcd1badf..71440fee38b73f6f5c346c68dc9ca591c71dbcdc 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 12c62d3079d00a2f0828f1a3df7197bd455b9da4..25ec1ffc35eb9df7acd541967b7304d870646a19 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 c120960828c81035ab5e76e101f65001cfbec4fd..fb606b2bd83b31798c530507c67d6fa194b91b76 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 516432ad89f57bbbf224106df2d1995e076feceb..8a50da7b58f1502f6e4c5c898604e4146b5bd1f4 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 6b531e4515ecd4242f30a8888b049db1954909ff..0d943c24a47c75261334dd4c9a4293d041a88710 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 943252dc58036f4fa90477da57ec426f749f3d59..732e1d736fc0bfe5e7e9fd217b72dfd1bca24631 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 062a6b3ba63c851cacab45b1adc8185884a4b0da..2bf73706e65db4d6ca52d2241abefd91c5f82379 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 b5430c697dbc41372481a6455bac9aa86055aec5..e2538daf3b3411e05a278276f3f177933c530bf1 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 d5c42d2142f93c96c105b661a0ec97f11b9a9ead..ec56db5b775408e4b0ccf63be0b6e74fe203a41f 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 209e0e1fa1c63cbd98b29530dc4bae9a3f0c4f60..a9c5e0d4f953e66af576d0dc4b86cf775bf4c9e0 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 966e69a71df7ae695dfc6e972522edebabc6e7b8..8dbcadc514f92e3d034d5fc1c0a5992689f91d21 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 586288ac323ca216e3b71f50051294929fb90815..55eabc6c099c80fe72c571bed32c3c083ea98bda 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 68b11af00da9aba63758f3230bfb6df774dd44fe..a016d6f6a24ab83fcb13f3c5efa94042f2adbaa6 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 213e21b9c42b6745f44d525c2f80e132375096c7..433c153443be22dffb4043c69f39f3ba4daaeeb8 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 441a16cfd72bcff86cbcda95678a0bbb4288d751..53f41da1bb7d41fd908f0dfba4f6eea0341d66c3 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 eb8d6f743b304b824deb98ca8738bc223a9594dd..445e8c7cd25deb57b08236e387bb2045be8f6639 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 b794fb12953be540056ee4489e587ccb947bd73e..225f65d35fe853454758e0e4d498226f6b2f5484 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 0598cc8a4418d33093e180bf3880749e042a0766..5bc3a973ba0256cb4ecea308d7396b1ff6eb67af 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,