From 2179571e17ae9d8e11ba7f5f890c410f0d36f1ea Mon Sep 17 00:00:00 2001 From: fangjinliang Date: Mon, 7 Feb 2022 19:09:53 +0800 Subject: [PATCH] add multi user interface Signed-off-by: fangjinliang --- .../core/common/include/ans_inner_errors.h | 1 + .../ans/core/include/ans_manager_interface.h | 12 +- .../ans/core/include/ans_manager_proxy.h | 5 + .../ans/core/include/ans_manager_stub.h | 10 + .../ans/core/include/ans_notification.h | 50 +++ frameworks/ans/core/src/ans_manager_proxy.cpp | 178 ++++++++++ frameworks/ans/core/src/ans_manager_stub.cpp | 175 ++++++++- frameworks/ans/core/src/ans_notification.cpp | 95 +++++ .../ans/native/src/notification_helper.cpp | 27 ++ .../ans/native/src/notification_request.cpp | 40 +++ .../ans/native/include/notification_helper.h | 50 +++ .../ans/native/include/notification_request.h | 14 + interfaces/kits/napi/ans/src/common.cpp | 4 + interfaces/kits/napi/ans/src/disturb_mode.cpp | 119 +++++-- .../kits/napi/ans/src/enable_notification.cpp | 13 +- interfaces/kits/napi/ans/src/publish.cpp | 22 +- interfaces/kits/napi/ans/src/remove.cpp | 45 ++- .../include/advanced_notification_service.h | 11 +- .../ans/include/notification_preferences.h | 10 +- .../include/notification_subscriber_manager.h | 1 + .../ans/src/advanced_notification_service.cpp | 331 +++++++++++++----- services/ans/src/notification_preferences.cpp | 34 +- .../src/notification_subscriber_manager.cpp | 24 +- .../notification_preferences_test.cpp | 24 +- 24 files changed, 1115 insertions(+), 180 deletions(-) diff --git a/frameworks/ans/core/common/include/ans_inner_errors.h b/frameworks/ans/core/common/include/ans_inner_errors.h index c205fea2c..4c4cb02d3 100644 --- a/frameworks/ans/core/common/include/ans_inner_errors.h +++ b/frameworks/ans/core/common/include/ans_inner_errors.h @@ -72,6 +72,7 @@ enum ErrorCode : uint32_t { ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED, ERR_ANS_DISTRIBUTED_OPERATION_FAILED, ERR_ANS_DISTRIBUTED_GET_INFO_FAILED, + ERR_ANS_GET_ACTIVE_USER_FAILED, }; } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/include/ans_manager_interface.h b/frameworks/ans/core/include/ans_manager_interface.h index f2e65ab62..91be045eb 100644 --- a/frameworks/ans/core/include/ans_manager_interface.h +++ b/frameworks/ans/core/include/ans_manager_interface.h @@ -126,6 +126,11 @@ public: virtual ErrCode GetValidReminders(std::vector> &reminders) = 0; virtual ErrCode CancelAllReminders() = 0; virtual ErrCode IsSupportTemplate(const std::string &templateName, bool &support) = 0; + virtual ErrCode IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) = 0; + virtual ErrCode SetNotificationsEnabledByUser(const int32_t &deviceId, bool enabled) = 0; + virtual ErrCode DeleteAllByUser(const int32_t &userId) = 0; + virtual ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr &date) = 0; + virtual ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr &date) = 0; protected: enum TransactId : uint32_t { @@ -195,7 +200,12 @@ protected: CANCEL_REMINDER, CANCEL_ALL_REMINDERS, GET_ALL_VALID_REMINDERS, - IS_SUPPORT_TEMPLATE + IS_SUPPORT_TEMPLATE, + IS_SPECIAL_USER_ALLOWED_NOTIFY, + SET_NOTIFICATION_ENABLED_BY_USER, + DELETE_ALL_NOTIFICATIONS_BY_USER, + SET_DO_NOT_DISTURB_DATE_BY_USER, + GET_DO_NOT_DISTURB_DATE_BY_USER }; }; } // namespace Notification diff --git a/frameworks/ans/core/include/ans_manager_proxy.h b/frameworks/ans/core/include/ans_manager_proxy.h index feed3ff2a..cb4afad06 100644 --- a/frameworks/ans/core/include/ans_manager_proxy.h +++ b/frameworks/ans/core/include/ans_manager_proxy.h @@ -114,6 +114,11 @@ public: ErrCode CancelReminder(const int32_t reminderId) override; ErrCode GetValidReminders(std::vector> &reminders) override; ErrCode CancelAllReminders() override; + ErrCode IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) override; + ErrCode SetNotificationsEnabledByUser(const int32_t &userId, bool enabled) override; + ErrCode DeleteAllByUser(const int32_t &userId) override; + ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr &date) override; + ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr &date) override; private: ErrCode InnerTransact(uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/ans/core/include/ans_manager_stub.h b/frameworks/ans/core/include/ans_manager_stub.h index f65b42c3c..f667eccc5 100644 --- a/frameworks/ans/core/include/ans_manager_stub.h +++ b/frameworks/ans/core/include/ans_manager_stub.h @@ -120,6 +120,11 @@ public: virtual ErrCode GetValidReminders(std::vector> &reminders) override; virtual ErrCode CancelAllReminders() override; virtual ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override; + virtual ErrCode IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) override; + virtual ErrCode SetNotificationsEnabledByUser(const int32_t &deviceId, bool enabled) override; + virtual ErrCode DeleteAllByUser(const int32_t &userId) override; + virtual ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr &date) override; + virtual ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr &date) override; private: static const std::map> @@ -192,6 +197,11 @@ private: ErrCode HandleGetValidReminders(MessageParcel &data, MessageParcel &reply); ErrCode HandleCancelAllReminders(MessageParcel &data, MessageParcel &reply); ErrCode HandleIsSupportTemplate(MessageParcel &data, MessageParcel &reply); + ErrCode HandleIsSpecialUserAllowedNotifyByUser(MessageParcel &data, MessageParcel &reply); + ErrCode HandleSetNotificationsEnabledByUser(MessageParcel &data, MessageParcel &reply); + ErrCode HandleDeleteAllByUser(MessageParcel &data, MessageParcel &reply); + ErrCode HandleSetDoNotDisturbDateByUser(MessageParcel &data, MessageParcel &reply); + ErrCode HandleGetDoNotDisturbDateByUser(MessageParcel &data, MessageParcel &reply); template bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &reply, ErrCode &result); diff --git a/frameworks/ans/core/include/ans_notification.h b/frameworks/ans/core/include/ans_notification.h index a780fd5aa..6ada8d78a 100644 --- a/frameworks/ans/core/include/ans_notification.h +++ b/frameworks/ans/core/include/ans_notification.h @@ -736,6 +736,56 @@ public: */ ErrCode GetValidReminders(std::vector> &validReminders); + /** + * Obtains the notification enable under the specified user. + * + * @param userId Indicates the userId of the application. + * @param allowed True if the application has permissions; returns false otherwise. + * @return Returns get allowed result. + */ + ErrCode IsAllowedNotify(const int32_t &userId, bool &allowed); + + /** + * Sets whether to allow all applications to publish notifications on a specified user. + * The caller must have system permissions to call this method. + * + * @param userId Indicates the ID of the user running the application. + * @param enabled Specifies whether to allow all applications to publish notifications. The value true + * indicates that notifications are allowed, and the value false indicates that notifications + * are not allowed. + * @return Returns set notifications enabled for all bundles result. + */ + ErrCode SetNotificationsEnabledForAllBundles(const int32_t &userId, bool enabled) + + /** + * Removes notifications under specified user. + * @note Your application must have platform signature to use this method. + * + * @param userId Indicates the ID of user whose notifications are to be removed. + * @return Returns remove notification result. + */ + static ErrCode RemoveNotifications(const int32_t &userId); + + /** + * Sets the do not disturb time on a specified user. + * @note Your application must have system signature to call this method. + * + * @param userId Indicates the specific user. + * @param doNotDisturbDate Indicates the do not disturb time to set. + * @return Returns set do not disturb time result. + */ + static ErrCode SetDoNotDisturbDate(const int32_t &userId, const NotificationDoNotDisturbDate &doNotDisturbDate); + + /** + * Obtains the do not disturb time on a specified user. + * @note Your application must have system signature to call this method. + * + * @param userId Indicates the specific user. + * @param doNotDisturbDate Indicates the do not disturb time to get. + * @return Returns set do not disturb time result. + */ + static ErrCode GetDoNotDisturbDate(const int32_t &userId, NotificationDoNotDisturbDate &doNotDisturbDate); + private: /** * Get Ans Manager proxy. diff --git a/frameworks/ans/core/src/ans_manager_proxy.cpp b/frameworks/ans/core/src/ans_manager_proxy.cpp index b0ab15db8..a8f0926f9 100644 --- a/frameworks/ans/core/src/ans_manager_proxy.cpp +++ b/frameworks/ans/core/src/ans_manager_proxy.cpp @@ -2384,5 +2384,183 @@ ErrCode AnsManagerProxy::IsSupportTemplate(const std::string &templateName, bool return result; } + +ErrCode AnsManagerProxy::IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) +{ + if (userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("[IsSpecialUserAllowedNotify] fail: Input userId is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGW("[IsSpecialUserAllowedNotify] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(userId)) { + ANS_LOGW("[IsSpecialUserAllowedNotify] fail: write userId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(IS_SPECIAL_USER_ALLOWED_NOTIFY, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[IsSpecialBundleAllowedNotify] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGW("[IsSpecialBundleAllowedNotify] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!reply.ReadBool(allowed)) { + ANS_LOGW("[IsSpecialBundleAllowedNotify] fail: read allowed failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsManagerProxy::SetNotificationsEnabledByUser(const int32_t &userId, bool enabled) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGW("[SetNotificationsEnabledByUser] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(userId)) { + ANS_LOGW("[SetNotificationsEnabledByUser] fail: write userId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteBool(enabled)) { + ANS_LOGW("[SetNotificationsEnabledByUser] fail: write enabled failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(SET_NOTIFICATION_ENABLED_BY_USER, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[SetNotificationsEnabledByUser] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGW("[SetNotificationsEnabledByUser] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsManagerProxy::DeleteAllByUser(const int32_t &userId) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGW("[DeleteAllByUser] fail:, write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(userId)) { + ANS_LOGW("[DeleteAllByUser] fail: write userId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(DELETE_ALL_NOTIFICATIONS_BY_USER, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[DeleteAllByUser] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGW("[DeleteAllByUser] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsManagerProxy::SetDoNotDisturbDate(const int32_t &userId, const sptr &date) +{ + if (date == nullptr) { + ANS_LOGW("[SetDoNotDisturbDate] fail: date is empty."); + return ERR_ANS_INVALID_PARAM; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGW("[SetDoNotDisturbDate] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(userId)) { + ANS_LOGW("[SetDoNotDisturbDate] fail: write userId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteParcelable(date)) { + ANS_LOGW("[SetDoNotDisturbDate] fail: write date failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(SET_DO_NOT_DISTURB_DATE_BY_USER, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[SetDoNotDisturbDate] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGW("[SetDoNotDisturbDate] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsManagerProxy::GetDoNotDisturbDate(const int32_t &userId, sptr &date) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGW("[GetDoNotDisturbDate] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(userId)) { + ANS_LOGW("[GetDoNotDisturbDate] fail: write userId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(GET_DO_NOT_DISTURB_DATE_BY_USER, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[GetDoNotDisturbDate] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGW("[GetDoNotDisturbDate] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (result == ERR_OK) { + date = reply.ReadParcelable(); + if (date == nullptr) { + ANS_LOGW("[GetDoNotDisturbDate] fail: read date failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + } + + return result; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/src/ans_manager_stub.cpp b/frameworks/ans/core/src/ans_manager_stub.cpp index 9dee804e9..007fe1b86 100644 --- a/frameworks/ans/core/src/ans_manager_stub.cpp +++ b/frameworks/ans/core/src/ans_manager_stub.cpp @@ -232,6 +232,21 @@ const std::map date = data.ReadParcelable(); + if (date == nullptr) { + ANS_LOGW("[HandleSetDoNotDisturbDateByUser] fail: read date failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + ErrCode result = SetDoNotDisturbDate(userId, date); + if (!reply.WriteInt32(result)) { + ANS_LOGW("[HandleSetDoNotDisturbDateByUser] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + + return ERR_OK; +} + +ErrCode AnsManagerStub::HandleGetDoNotDisturbDateByUser(MessageParcel &data, MessageParcel &reply) +{ + int32_t userId = SUBSCRIBE_USER_INIT; + if (!data.ReadInt32(userId)) { + ANS_LOGW("[HandleSetDoNotDisturbDateByUser] fail: read userId failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + if (userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGW("[HandleSetDoNotDisturbDateByUser] fail: userId is invalid."); + return ERR_ANS_PARCELABLE_FAILED; + } + + sptr date = nullptr; + ErrCode result = GetDoNotDisturbDate(userId, date); + if (!reply.WriteInt32(result)) { + ANS_LOGW("[HandleGetDoNotDisturbDateByUser] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (result == ERR_OK) { + if (!reply.WriteParcelable(date)) { + ANS_LOGW("[HandleGetDoNotDisturbDateByUser] fail: write date failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + } + + return ERR_OK; +} + ErrCode AnsManagerStub::Publish(const std::string &label, const sptr ¬ification) { ANS_LOGW("AnsManagerStub::Publish called!"); @@ -1958,5 +2101,35 @@ ErrCode AnsManagerStub::IsSupportTemplate(const std::string &templateName, bool ANS_LOGW("AnsManagerStub::IsSupportTemplate called!"); return ERR_INVALID_OPERATION; } + +ErrCode AdvancedNotificationService::IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) +{ + ANS_LOGW("AnsManagerStub::IsSpecialUserAllowedNotify called!"); + return ERR_INVALID_OPERATION; +} + +ErrCode AdvancedNotificationService::SetNotificationsEnabledByUser(const int32_t &deviceId, bool enabled) +{ + ANS_LOGW("AnsManagerStub::SetNotificationsEnabledByUser called!"); + return ERR_INVALID_OPERATION; +} + +ErrCode AnsManagerStub::DeleteAllByUser(const int32_t &userId) +{ + ANS_LOGW("AnsManagerStub::DeleteAllByUser called!"); + return ERR_INVALID_OPERATION; +} + +ErrCode AnsManagerStub::SetDoNotDisturbDate(const int32_t &userId, const sptr &date) +{ + ANS_LOGW("AnsManagerStub::SetDoNotDisturbDate called!"); + return ERR_INVALID_OPERATION; +} + +ErrCode AnsManagerStub::GetDoNotDisturbDate(const int32_t &userId, sptr &date) +{ + ANS_LOGW("AnsManagerStub::GetDoNotDisturbDate called!"); + return ERR_INVALID_OPERATION; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/src/ans_notification.cpp b/frameworks/ans/core/src/ans_notification.cpp index a9249491d..11f7a7d1c 100644 --- a/frameworks/ans/core/src/ans_notification.cpp +++ b/frameworks/ans/core/src/ans_notification.cpp @@ -1212,5 +1212,100 @@ bool AnsNotification::IsNonDistributedNotificationType(const NotificationContent } return false; } + +ErrCode AnsNotification::IsAllowedNotify(const int32_t &userId, bool &allowed) +{ + if (userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("Input userId is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return ansManagerProxy_->IsSpecialUserAllowedNotify(userId, allowed); +} + +ErrCode AnsNotification::SetNotificationsEnabledForAllBundles(const int32_t &userId, bool enabled) +{ + if (userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("Input userId is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + return ansManagerProxy_->SetNotificationsEnabledByUser(userId, enabled); +} + +ErrCode AnsNotification::RemoveNotifications(const int32_t &userId) +{ + if (userId == SUBSCRIBE_USER_INIT) { + ANS_LOGE("Input userId is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return ansManagerProxy_->DeleteAllByUser(userId); +} + +ErrCode AnsNotification::SetDoNotDisturbDate(const int32_t &userId, + const NotificationDoNotDisturbDate &doNotDisturbDate) +{ + if (userId == SUBSCRIBE_USER_INIT) { + ANS_LOGE("Input userId is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + auto dndDatePtr = new (std::nothrow) NotificationDoNotDisturbDate(doNotDisturbDate); + if (dndDatePtr == nullptr) { + ANS_LOGE("create DoNotDisturbDate failed."); + return ERR_ANS_NO_MEMORY; + } + + sptr dndDate(dndDatePtr); + return ansManagerProxy_->SetDoNotDisturbDate(dndDate); +} + +ErrCode AnsNotification::GetDoNotDisturbDate(const int32_t &userId, NotificationDoNotDisturbDate &doNotDisturbDate) +{ + if (userId == SUBSCRIBE_USER_INIT) { + ANS_LOGE("Input userId is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + sptr dndDate; + auto ret = ansManagerProxy_->GetDoNotDisturbDate(dndDate); + if (ret != ERR_OK) { + ANS_LOGE("Get DoNotDisturbDate failed."); + return ret; + } + + if (!dndDate) { + ANS_LOGE("Invalid DoNotDisturbDate."); + return ERR_ANS_NO_MEMORY; + } + + doNotDisturbDate = *dndDate; + return ret; +} } // 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 9452c6a5c..32007286d 100644 --- a/frameworks/ans/native/src/notification_helper.cpp +++ b/frameworks/ans/native/src/notification_helper.cpp @@ -366,5 +366,32 @@ ErrCode NotificationHelper::IsSupportTemplate(const std::string &templateName, b { return DelayedSingleton::GetInstance()->IsSupportTemplate(templateName, support); } + +ErrCode NotificationHelper::IsAllowedNotify(const int32_t &userId, bool &allowed) +{ + return DelayedSingleton::GetInstance()->IsAllowedNotify(userId, allowed); +} + +ErrCode NotificationHelper::SetNotificationsEnabledForAllBundles(const int32_t &userId, bool enabled) +{ + return DelayedSingleton::GetInstance()->SetNotificationsEnabledForAllBundles( + deviceId, userId, enabled); +} + +ErrCode NotificationHelper::RemoveNotifications(const int32_t &userId) +{ + return DelayedSingleton::GetInstance()->RemoveNotifications(userId); +} + +ErrCode NotificationHelper::SetDoNotDisturbDate(const int32_t &userId, + const NotificationDoNotDisturbDate &doNotDisturbDate) +{ + return DelayedSingleton::GetInstance()->SetDoNotDisturbDate(doNotDisturbDate); +} + +ErrCode NotificationHelper::GetDoNotDisturbDate(const int32_t &userId, NotificationDoNotDisturbDate &doNotDisturbDate) +{ + return DelayedSingleton::GetInstance()->GetDoNotDisturbDate(doNotDisturbDate); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/frameworks/ans/native/src/notification_request.cpp b/frameworks/ans/native/src/notification_request.cpp index 9807dace3..71344ee02 100644 --- a/frameworks/ans/native/src/notification_request.cpp +++ b/frameworks/ans/native/src/notification_request.cpp @@ -680,6 +680,7 @@ std::string NotificationRequest::Dump() ", userInputHistory = " + (!userInputHistory_.empty() ? userInputHistory_.at(0) : "empty") + ", distributedOptions = " + distributedOptions_.Dump() + ", notificationFlags = " + (notificationFlags_ ? "not null" : "null") + + ", creatorUserId = " + std::to_string(creatorUserId_) + " }"; } @@ -711,6 +712,11 @@ bool NotificationRequest::ToJson(nlohmann::json &jsonObject) const jsonObject["isUnremovable"] = unremovable_; jsonObject["isFloatingIcon"] = floatingIcon_; + jsonObject["creatorBundleName"] = creatorBundleName_; + jsonObject["creatorUid"] = creatorUid_; + jsonObject["creatorPid"] = creatorPid_; + jsonObject["creatorUserId"] = creatorUserId_; + if (!ConvertObjectsToJson(jsonObject)) { ANS_LOGE("Cannot convert objects to JSON"); return false; @@ -840,6 +846,11 @@ bool NotificationRequest::Marshalling(Parcel &parcel) const return false; } + if (!parcel.WriteInt32(static_cast(creatorUserId_))) { + ANS_LOGE("Failed to write creator userId"); + return false; + } + // write std::string if (!parcel.WriteString(settingsText_)) { ANS_LOGE("Failed to write settings text"); @@ -1174,6 +1185,7 @@ bool NotificationRequest::ReadFromParcel(Parcel &parcel) creatorPid_ = static_cast(parcel.ReadInt32()); creatorUid_ = static_cast(parcel.ReadInt32()); + creatorUserId_ = parcel.ReadInt32(); if (!parcel.ReadString(settingsText_)) { ANS_LOGE("Failed to read settings text"); @@ -1398,6 +1410,16 @@ std::shared_ptr NotificationRequest::GetFlags() const return notificationFlags_; } +void NotificationRequest::SetReceiverUserId(int32_t userId) +{ + recviverUserId_ = userId; +} + +int32_t NotificationRequest::GetReceiverUserId() const +{ + return recviverUserId_; +} + void NotificationRequest::CopyBase(const NotificationRequest &other) { this->notificationId_ = other.notificationId_; @@ -1411,6 +1433,7 @@ void NotificationRequest::CopyBase(const NotificationRequest &other) this->creatorPid_ = other.creatorPid_; this->creatorUid_ = other.creatorUid_; + this->creatorUserId_ = other.creatorUserId_; this->slotType_ = other.slotType_; this->settingsText_ = other.settingsText_; @@ -1462,6 +1485,7 @@ void NotificationRequest::CopyOther(const NotificationRequest &other) this->notificationTemplate_ = other.notificationTemplate_; this->notificationFlags_ = other.notificationFlags_; + this->recviverUserId_ = other.recviverUserId_; } bool NotificationRequest::ConvertObjectsToJson(nlohmann::json &jsonObject) const @@ -1544,6 +1568,18 @@ void NotificationRequest::ConvertJsonToNum(NotificationRequest *target, const nl if (jsonObject.find("autoDeletedTime") != jsonEnd) { target->autoDeletedTime_ = jsonObject.at("autoDeletedTime").get(); } + + if (jsonObject.find("creatorUid") != jsonEnd) { + target->creatorUid_ = jsonObject.at("creatorUid").get(); + } + + if (jsonObject.find("creatorPid") != jsonEnd) { + target->creatorPid_ = jsonObject.at("creatorPid").get(); + } + + if (jsonObject.find("creatorUserId") != jsonEnd) { + target->creatorUserId_ = jsonObject.at("creatorUserId").get(); + } } void NotificationRequest::ConvertJsonToString(NotificationRequest *target, const nlohmann::json &jsonObject) @@ -1574,6 +1610,10 @@ void NotificationRequest::ConvertJsonToString(NotificationRequest *target, const if (jsonObject.find("classification") != jsonEnd) { target->classification_ = jsonObject.at("classification").get(); } + + if (jsonObject.find("creatorBundleName") != jsonEnd) { + target->creatorBundleName_ = jsonObject.at("creatorBundleName").get(); + } } void NotificationRequest::ConvertJsonToEnum(NotificationRequest *target, const nlohmann::json &jsonObject) diff --git a/interfaces/innerkits/ans/native/include/notification_helper.h b/interfaces/innerkits/ans/native/include/notification_helper.h index 665303846..861910fcd 100644 --- a/interfaces/innerkits/ans/native/include/notification_helper.h +++ b/interfaces/innerkits/ans/native/include/notification_helper.h @@ -696,6 +696,56 @@ public: * @return Returns check result. */ static ErrCode IsSupportTemplate(const std::string &templateName, bool &support); + + /** + * Obtains the notification enable under the specified user. + * + * @param userId Indicates the userId of the application. + * @param allowed True if the application has permissions; returns false otherwise. + * @return Returns get allowed result. + */ + static ErrCode IsAllowedNotify(const int32_t &userId, bool &allowed); + + /** + * Sets whether to allow all applications to publish notifications on a specified user. + * The caller must have system permissions to call this method. + * + * @param userId Indicates the ID of the user running the application. + * @param enabled Specifies whether to allow all applications to publish notifications. The value true + * indicates that notifications are allowed, and the value false indicates that notifications + * are not allowed. + * @return Returns set notifications enabled for all bundles result. + */ + static ErrCode SetNotificationsEnabledForAllBundles(const int32_t &userId, bool enabled); + + /** + * Removes notifications under specified user. + * @note Your application must have platform signature to use this method. + * + * @param userId Indicates the ID of user whose notifications are to be removed. + * @return Returns remove notification result. + */ + static ErrCode RemoveNotifications(const int32_t &userId); + + /** + * Sets the do not disturb time on a specified user. + * @note Your application must have system signature to call this method. + * + * @param userId Indicates the specific user. + * @param doNotDisturbDate Indicates the do not disturb time to set. + * @return Returns set do not disturb time result. + */ + static ErrCode SetDoNotDisturbDate(const int32_t &userId, const NotificationDoNotDisturbDate &doNotDisturbDate); + + /** + * Obtains the do not disturb time on a specified user. + * @note Your application must have system signature to call this method. + * + * @param userId Indicates the specific user. + * @param doNotDisturbDate Indicates the do not disturb time to get. + * @return Returns set do not disturb time result. + */ + static ErrCode GetDoNotDisturbDate(const int32_t &userId, NotificationDoNotDisturbDate &doNotDisturbDate); }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/innerkits/ans/native/include/notification_request.h b/interfaces/innerkits/ans/native/include/notification_request.h index e2f6190f8..b8ea1cf9d 100644 --- a/interfaces/innerkits/ans/native/include/notification_request.h +++ b/interfaces/innerkits/ans/native/include/notification_request.h @@ -16,6 +16,7 @@ #ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_REQUEST_H #define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_REQUEST_H +#include "ans_const_define.h" #include "message_user.h" #include "notification_action_button.h" #include "notification_content.h" @@ -928,6 +929,18 @@ public: */ std::shared_ptr GetFlags() const; + /** + * Sets the UserId of the notification receiver. + * @param userId the UserId of the notification receiver. + */ + void SetReceiverUserId(int32_t userId); + + /** + * Obtains the UserId of the notification receiver. + * @return the UserId of the notification receiver. + */ + int32_t GetReceiverUserId() const; + private: /** * Indicates the color mask, used for calculation with the ARGB value set by setColor(int32_t). @@ -986,6 +999,7 @@ private: pid_t creatorPid_ {0}; pid_t creatorUid_ {0}; int32_t creatorUserId_ {-1}; + int32_t recviverUserId_ {SUBSCRIBE_USER_INIT}; std::string settingsText_ {}; std::string creatorBundleName_ {}; diff --git a/interfaces/kits/napi/ans/src/common.cpp b/interfaces/kits/napi/ans/src/common.cpp index d8d8a57c1..f214a0da4 100644 --- a/interfaces/kits/napi/ans/src/common.cpp +++ b/interfaces/kits/napi/ans/src/common.cpp @@ -251,6 +251,10 @@ napi_value Common::SetNotification( napi_create_int32(env, notification->GetPid(), &value); napi_set_named_property(env, result, "creatorPid", value); + // readonly creatorUserId?: number + napi_create_int32(env, notification->GetUserId(), &value); + napi_set_named_property(env, result, "creatorUserId", value); + // distributedOption?:DistributedOptions napi_value distributedResult = nullptr; napi_create_object(env, &distributedResult); diff --git a/interfaces/kits/napi/ans/src/disturb_mode.cpp b/interfaces/kits/napi/ans/src/disturb_mode.cpp index 8ad364e5d..e579cbaa3 100644 --- a/interfaces/kits/napi/ans/src/disturb_mode.cpp +++ b/interfaces/kits/napi/ans/src/disturb_mode.cpp @@ -17,11 +17,20 @@ namespace OHOS { namespace NotificationNapi { -const int SET_DISTURB_MAX_PARA = 2; +const int SET_DISTURB_MAX_PARA = 3; const int SET_DISTURB_MIN_PARA = 1; +const int GET_DISTURB_MAX_PARA = 2; struct SetDoNotDisturbDateParams { NotificationDoNotDisturbDate date; + bool hasUserId = false; + int32_t userId = SUBSCRIBE_USER_INIT; + napi_ref callback = nullptr; +}; + +struct GetDoNotDisturbDateParams { + bool hasUserId = false; + int32_t userId = SUBSCRIBE_USER_INIT; napi_ref callback = nullptr; }; @@ -35,7 +44,7 @@ struct AsyncCallbackInfoSetDoNotDisturb { struct AsyncCallbackInfoGetDoNotDisturb { napi_env env = nullptr; napi_async_work asyncWork = nullptr; - napi_ref callback = nullptr; + GetDoNotDisturbDateParams params; NotificationDoNotDisturbDate date; CallbackPromiseInfo info; }; @@ -118,11 +127,25 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, return nullptr; } - // argv[1]:callback - if (argc >= SET_DISTURB_MAX_PARA) { + // argv[1] : userId / callback + if (argc >= SET_DISTURB_MAX_PARA - 1) { NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + NAPI_ASSERT(env, + (valuetype == napi_number) || (valuetype == napi_function), + "Wrong argument type. Function or object expected."); + if (valuetype == napi_number) { + params.hasUserId = true; + NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM1], ¶ms.userId)); + } else { + napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback); + } + } + + // argv[2]:callback + if (argc >= SET_DISTURB_MAX_PARA) { + NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype)); NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); - napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback); + napi_create_reference(env, argv[PARAM2], 1, ¶ms.callback); } return Common::NapiGetNull(env); @@ -149,14 +172,21 @@ napi_value SetDoNotDisturbDate(napi_env env, napi_callback_info info) napi_create_string_latin1(env, "setDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call napi_create_async_work(env, - nullptr, - resourceName, - [](napi_env env, void *data) { + nullptr, resourceName, [](napi_env env, void *data) { ANS_LOGI("SetDoNotDisturbDate napi_create_async_work start"); AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = (AsyncCallbackInfoSetDoNotDisturb *)data; - asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate(asynccallbackinfo->params.date); - ANS_LOGI("SetDoNotDisturbDate date=%{public}s errorCode=%{public}d", - asynccallbackinfo->params.date.Dump().c_str(), asynccallbackinfo->info.errorCode); + if (asynccallbackinfo->params.hasUserId) { + asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate( + asynccallbackinfo->params.userId, asynccallbackinfo->params.date); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate( + asynccallbackinfo->params.date); + } + + ANS_LOGI("SetDoNotDisturbDate date=%{public}s errorCode=%{public}d,\ + hasUserId=%{public}d userId=%{public}d", + asynccallbackinfo->params.date.Dump().c_str(), asynccallbackinfo->info.errorCode, + asynccallbackinfo->params.hasUserId, asynccallbackinfo->params.userId); }, [](napi_env env, napi_status status, void *data) { ANS_LOGI("SetDoNotDisturbDate napi_create_async_work end"); @@ -174,8 +204,7 @@ napi_value SetDoNotDisturbDate(napi_env env, napi_callback_info info) asynccallbackinfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork); NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); @@ -215,22 +244,60 @@ void AsyncCompleteCallbackGetDoNotDisturbDate(napi_env env, napi_status status, } } +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetDoNotDisturbDateParams ¶ms) +{ + ANS_LOGI("enter"); + + size_t argc = GET_DISTURB_MAX_PARA; + napi_value argv[GET_DISTURB_MAX_PARA] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + + napi_valuetype valuetype = napi_undefined; + // argv[0]: userId / callback + if (argc >= GET_DISTURB_MAX_PARA - 1) { + NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); + if ((valuetype != napi_number) && (valuetype != napi_function)) { + ANS_LOGW("Wrong argument type. Function or object expected."); + return nullptr; + } + if (valuetype == napi_number) { + params.hasUserId = true; + NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], ¶ms.userId)); + } else { + napi_create_reference(env, argv[PARAM0], 1, ¶ms.callback); + } + } + + // argv[1]:callback + if (argc >= GET_DISTURB_MAX_PARA) { + NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + if (valuetype != napi_function) { + ANS_LOGW("Wrong argument type. Function expected."); + return nullptr; + } + napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback); + } + + return Common::NapiGetNull(env); +} + napi_value GetDoNotDisturbDate(napi_env env, napi_callback_info info) { ANS_LOGI("enter"); - napi_ref callback = nullptr; - if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + SetDoNotDisturbDateParams params {}; + if (ParseParameters(env, info, params) == nullptr) { return Common::NapiGetUndefined(env); } - AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = - new (std::nothrow) AsyncCallbackInfoGetDoNotDisturb {.env = env, .asyncWork = nullptr, .callback = callback}; + AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoSetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params}; if (!asynccallbackinfo) { - return Common::JSParaError(env, callback); + return Common::JSParaError(env, params.callback); } napi_value promise = nullptr; - Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); napi_value resourceName = nullptr; napi_create_string_latin1(env, "getDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName); @@ -241,9 +308,17 @@ napi_value GetDoNotDisturbDate(napi_env env, napi_callback_info info) [](napi_env env, void *data) { ANS_LOGI("GetDoNotDisturbDate napi_create_async_work start"); AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = (AsyncCallbackInfoGetDoNotDisturb *)data; - asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate(asynccallbackinfo->date); - ANS_LOGI("GetDoNotDisturbDate errorCode=%{public}d date=%{public}s", - asynccallbackinfo->info.errorCode, asynccallbackinfo->date.Dump().c_str()); + if (asynccallbackinfo->params.hasUserId) { + asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate( + asynccallbackinfo->params.userId, asynccallbackinfo->date); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate(asynccallbackinfo->date); + } + + ANS_LOGI("GetDoNotDisturbDate errorCode=%{public}d date=%{public}s,\ + hasUserId=%{public}d userId=%{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->date.Dump().c_str(), + asynccallbackinfo->params.hasUserId, asynccallbackinfo->params.userId); }, AsyncCompleteCallbackGetDoNotDisturbDate, (void *)asynccallbackinfo, diff --git a/interfaces/kits/napi/ans/src/enable_notification.cpp b/interfaces/kits/napi/ans/src/enable_notification.cpp index c9b896d17..e2802f748 100644 --- a/interfaces/kits/napi/ans/src/enable_notification.cpp +++ b/interfaces/kits/napi/ans/src/enable_notification.cpp @@ -38,6 +38,8 @@ struct IsEnableParams { NotificationBundleOption option; napi_ref callback = nullptr; bool hasBundleOption = false; + int32_t userId = SUBSCRIBE_USER_INIT; + bool hasUserId = false; }; struct AsyncCallbackInfoIsEnable { @@ -108,10 +110,10 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, return Common::NapiGetNull(env); } - // argv[0]: bundle / callback + // argv[0]: bundle / userId / callback napi_valuetype valuetype = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); - if ((valuetype != napi_function) && (valuetype != napi_object)) { + if ((valuetype != napi_object) && (valuetype != napi_number) && (valuetype != napi_function)) { ANS_LOGW("Wrong argument type. Function or object expected."); return nullptr; } @@ -122,6 +124,9 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, return nullptr; } params.hasBundleOption = true; + } else if (valuetype == napi_number) { + NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], ¶ms.userId)); + params.hasUserId = true; } else { napi_create_reference(env, argv[PARAM0], 1, ¶ms.callback); } @@ -262,6 +267,10 @@ napi_value IsNotificationEnabled(napi_env env, napi_callback_info info) asynccallbackinfo->params.option.GetUid()); asynccallbackinfo->info.errorCode = NotificationHelper::IsAllowedNotify(asynccallbackinfo->params.option, asynccallbackinfo->allowed); + } else if (asynccallbackinfo->params.hasUserId) { + ANS_LOGI("userId = %{public}d", asynccallbackinfo->params.userId); + asynccallbackinfo->info.errorCode = + NotificationHelper::IsAllowedNotify(asynccallbackinfo->params.userId, asynccallbackinfo->allowed); } else { asynccallbackinfo->info.errorCode = NotificationHelper::IsAllowedNotify(asynccallbackinfo->allowed); } diff --git a/interfaces/kits/napi/ans/src/publish.cpp b/interfaces/kits/napi/ans/src/publish.cpp index b544ea7d8..b2131b758 100644 --- a/interfaces/kits/napi/ans/src/publish.cpp +++ b/interfaces/kits/napi/ans/src/publish.cpp @@ -17,7 +17,7 @@ namespace OHOS { namespace NotificationNapi { -static const int32_t PUBLISH_NOTIFICATION_MAX = 2; +static const int32_t PUBLISH_NOTIFICATION_MAX = 3; struct AsyncCallbackInfoPublish { napi_env env = nullptr; @@ -61,9 +61,25 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, return nullptr; } - // argv[1] : callback + // argv[1] : userId / callback + if (argc >= PUBLISH_NOTIFICATION_MAX - 1) { + NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + NAPI_ASSERT(env, + (valuetype == napi_number) || (valuetype == napi_function), + "Wrong argument type. Function or object expected."); + + if (valuetype == napi_number) { + int32_t recvUserId = SUBSCRIBE_USER_INIT; + NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM1], &recvUserId)); + params.request.SetReceiverUserId(recvUserId); + } else { + napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback); + } + } + + // argv[2] : callback if (argc >= PUBLISH_NOTIFICATION_MAX) { - if (GetCallback(env, argv[PARAM1], params) == nullptr) { + if (GetCallback(env, argv[PARAM2], params) == nullptr) { return nullptr; } } diff --git a/interfaces/kits/napi/ans/src/remove.cpp b/interfaces/kits/napi/ans/src/remove.cpp index 0757bbea3..54a76264f 100644 --- a/interfaces/kits/napi/ans/src/remove.cpp +++ b/interfaces/kits/napi/ans/src/remove.cpp @@ -38,6 +38,8 @@ struct BundleAndKeyInfo { struct RemoveParams { std::optional hashcode {}; std::optional bundleAndKeyInfo {}; + int32_t userId = SUBSCRIBE_USER_INIT; + bool hasUserId = false; napi_ref callback = nullptr; }; @@ -129,31 +131,38 @@ napi_value ParseParametersByRemoveAll(const napi_env &env, const napi_callback_i napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - if (argc < REMOVE_ALL_WHEN_HAS_PARA_MIN_PARA) { + if (argc == 0) { return Common::NapiGetNull(env); } + // argv[0]: bundle / userId / callback napi_valuetype valuetype = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); - NAPI_ASSERT(env, - (valuetype == napi_function) || (valuetype == napi_object), - "Wrong argument type. Function or object expected."); - if (valuetype == napi_function) { - // argv[0]: callback - napi_create_reference(env, argv[PARAM0], 1, ¶ms.callback); + if ((valuetype != napi_object) && (valuetype != napi_number) && (valuetype != napi_function)) { + ANS_LOGW("Wrong argument type. Function or object expected."); + return nullptr; + } + if (valuetype == napi_object) { + auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option); + if (retValue == nullptr) { + ANS_LOGE("GetBundleOption failed."); + return nullptr; + } + } else if (valuetype == napi_number) { + NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], ¶ms.userId)); + params.hasUserId = true; } else { - BundleAndKeyInfo info {}; - // argv[0]: BundleOption - auto retValue = Common::GetBundleOption(env, argv[PARAM0], info.option); - NAPI_ASSERT(env, retValue != nullptr, "GetBundleOption failed."); - params.bundleAndKeyInfo = info; + napi_create_reference(env, argv[PARAM0], 1, ¶ms.callback); + } - // argv[1]:callback - if (argc >= REMOVE_ALL_MAX_PARA) { - NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); - napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback); + // argv[1]:callback + if (argc >= REMOVE_ALL_MAX_PARA) { + NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + if (valuetype != napi_function) { + ANS_LOGW("Wrong argument type. Function expected."); + return nullptr; } + napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback); } return Common::NapiGetNull(env); @@ -294,6 +303,8 @@ napi_value RemoveAll(napi_env env, napi_callback_info info) auto &infos = asynccallbackinfo->params.bundleAndKeyInfo.value(); asynccallbackinfo->info.errorCode = NotificationHelper::RemoveAllNotifications(infos.option); + } else if (asynccallbackinfo->params.hasUserId) { + asynccallbackinfo->info.errorCode = NotificationHelper::RemoveNotifications(infos.userId); } else { asynccallbackinfo->info.errorCode = NotificationHelper::RemoveNotifications(); } diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index f7029a8c4..6608901d9 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -121,6 +121,11 @@ public: ErrCode GetValidReminders(std::vector> &reminders) override; ErrCode CancelAllReminders() override; ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override; + ErrCode IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) override; + ErrCode SetNotificationsEnabledByUser(const int32_t &deviceId, bool enabled) override; + ErrCode DeleteAllByUser(const int32_t &userId) override; + ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr &date) override; + ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr &date) override; // SystemEvent void OnBundleRemoved(const sptr &bundleOption); @@ -150,7 +155,8 @@ private: ErrCode RemoveFromNotificationList(const sptr &bundleOption, const std::string &label, int notificationId, sptr ¬ification, bool isCancel = false); ErrCode RemoveFromNotificationList(const std::string &key, sptr ¬ification, bool isCancel = false); - ErrCode RemoveFromNotificationListForDeleteAll(const std::string &key, sptr ¬ification); + ErrCode RemoveFromNotificationListForDeleteAll(const std::string &key, + const int &userId, sptr ¬ification); std::vector GetNotificationKeys(const sptr &bundleOption); bool IsNotificationExists(const std::string &key); void SortNotificationList(); @@ -194,6 +200,9 @@ private: const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id); #endif + ErrCode SetDoNotDisturbDateByUser(const int32_t &userId, const sptr &date); + ErrCode GetDoNotDisturbDateByUser(const int32_t &userId, sptr &date); + private: static sptr instance_; static std::mutex instanceMutex_; diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 04ef9233c..516432ad8 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -64,12 +64,10 @@ public: ErrCode SetPrivateNotificationsAllowed(const sptr &bundleOption, const bool allow); ErrCode GetNotificationsEnabledForBundle(const sptr &bundleOption, bool &enabled); ErrCode SetNotificationsEnabledForBundle(const sptr &bundleOption, const bool enabled); - ErrCode GetNotificationsEnabled(const sptr &bundleOption, bool &enabled); - ErrCode SetNotificationsEnabled(const sptr &bundleOption, const bool &enabled); - ErrCode GetDoNotDisturbDate(const sptr &bundleOption, - sptr &date); - ErrCode SetDoNotDisturbDate(const sptr &bundleOption, - const sptr date); + ErrCode GetNotificationsEnabled(const int32_t &userId, bool &enabled); + ErrCode SetNotificationsEnabled(const int32_t &userId, const bool &enabled); + 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); ErrCode ClearNotificationInRestoreFactorySettings(); diff --git a/services/ans/include/notification_subscriber_manager.h b/services/ans/include/notification_subscriber_manager.h index f6e2ae645..062a6b3ba 100644 --- a/services/ans/include/notification_subscriber_manager.h +++ b/services/ans/include/notification_subscriber_manager.h @@ -70,6 +70,7 @@ private: const sptr ¬ification, const sptr ¬ificationMap, int deleteReason); void NotifyUpdatedInner(const sptr ¬ificationMap); void NotifyDoNotDisturbDateChangedInner(const sptr &date); + bool IsSystemUser(int32_t userId); private: std::list> subscriberRecordList_ {}; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 795c9ca54..0efa8bc54 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -174,9 +174,6 @@ ErrCode PrepereNotificationRequest(const sptr &request) int userId = SUBSCRIBE_USER_INIT; OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId); - if (userId >= SUBSCRIBE_USER_SYSTEM_BEGIN && userId <= SUBSCRIBE_USER_SYSTEM_END) { - userId = SUBSCRIBE_USER_ALL; - } request->SetCreatorUserId(userId); ErrCode result = CheckPictureSize(request); @@ -898,12 +895,12 @@ ErrCode AdvancedNotificationService::DeleteAll() #endif sptr notification = nullptr; - result = RemoveFromNotificationListForDeleteAll(key, notification); + result = RemoveFromNotificationListForDeleteAll(key, activeUserId, notification); if (result != ERR_OK || notification == nullptr) { continue; } - if ((notification->GetUserId() == activeUserId) || (notification->GetUserId() == SUBSCRIBE_USER_ALL)) { + if (notification->GetUserId() == activeUserId) { int reason = NotificationConstant::CANCEL_ALL_REASON_DELETE; UpdateRecentNotification(notification, true, reason); sptr sortingMap = GenerateSortingMap(); @@ -1155,13 +1152,13 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationList( } ErrCode AdvancedNotificationService::RemoveFromNotificationListForDeleteAll( - const std::string &key, sptr ¬ification) + const std::string &key, const int &userId, sptr ¬ification) { for (auto record : notificationList_) { if (!record->notification->IsRemoveAllowed()) { continue; } - if (record->notification->GetKey() == key) { + if ((record->notification->GetKey() == key) && (record->notification->GetUserId() == userId)) { if (record->request->IsUnremovable()) { return ERR_ANS_NOTIFICATION_IS_UNREMOVABLE; } @@ -1268,7 +1265,9 @@ ErrCode AdvancedNotificationService::GetAllActiveNotifications(std::vectorPostSyncTask(std::bind([&]() { @@ -1334,16 +1333,15 @@ ErrCode AdvancedNotificationService::SetNotificationsEnabledForAllBundles(const return ERR_ANS_PERMISSION_DENIED; } - ErrCode result = ERR_OK; - sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - return ERR_ANS_INVALID_BUNDLE; + int userId = SUBSCRIBE_USER_INIT; + if (!GetActiveUserId(userId)) { + return ERR_ANS_GET_ACTIVE_USER_FAILED; } handler_->PostSyncTask(std::bind([&]() { if (deviceId.empty()) { // Local device - result = NotificationPreferences::GetInstance().SetNotificationsEnabled(bundleOption, enabled); + result = NotificationPreferences::GetInstance().SetNotificationsEnabled(userId, enabled); } else { // Remote device } @@ -1393,15 +1391,14 @@ ErrCode AdvancedNotificationService::IsAllowedNotify(bool &allowed) return ERR_ANS_PERMISSION_DENIED; } - ErrCode result = ERR_OK; - sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - return ERR_ANS_INVALID_BUNDLE; + int userId = SUBSCRIBE_USER_INIT; + if (!GetActiveUserId(userId)) { + return ERR_ANS_GET_ACTIVE_USER_FAILED; } handler_->PostSyncTask(std::bind([&]() { allowed = false; - result = NotificationPreferences::GetInstance().GetNotificationsEnabled(bundleOption, allowed); + result = NotificationPreferences::GetInstance().GetNotificationsEnabled(userId, allowed); })); return result; } @@ -1438,10 +1435,15 @@ ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify( return ERR_ANS_INVALID_BUNDLE; } + int userId = SUBSCRIBE_USER_INIT; + if (!GetActiveUserId(userId)) { + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + ErrCode result = ERR_OK; handler_->PostSyncTask(std::bind([&]() { allowed = false; - result = NotificationPreferences::GetInstance().GetNotificationsEnabled(bundleOption, allowed); + result = NotificationPreferences::GetInstance().GetNotificationsEnabled(userId, allowed); if (result == ERR_OK && allowed) { result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(targetBundle, allowed); if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { @@ -2266,51 +2268,12 @@ ErrCode AdvancedNotificationService::SetDoNotDisturbDate(const sptrGetBeginDate()); - int64_t endDate = ResetSeconds(date->GetEndDate()); - - switch (date->GetDoNotDisturbType()) { - case NotificationConstant::DoNotDisturbType::NONE: - beginDate = 0; - endDate = 0; - break; - case NotificationConstant::DoNotDisturbType::ONCE: - AdjustDateForDndTypeOnce(beginDate, endDate); - break; - case NotificationConstant::DoNotDisturbType::CLEARLY: - if (beginDate >= endDate) { - return ERR_ANS_INVALID_PARAM; - } - break; - default: - break; - } - - const sptr newConfig = new NotificationDoNotDisturbDate( - date->GetDoNotDisturbType(), - beginDate, - endDate - ); - - sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - return ERR_ANS_INVALID_BUNDLE; + int userId = SUBSCRIBE_USER_INIT; + if (!GetActiveUserId(userId)) { + return ERR_ANS_GET_ACTIVE_USER_FAILED; } - handler_->PostSyncTask(std::bind([&]() { - result = NotificationPreferences::GetInstance().SetDoNotDisturbDate(bundleOption, newConfig); - if (result == ERR_OK) { - NotificationSubscriberManager::GetInstance()->NotifyDoNotDisturbDateChanged(newConfig); - } - })); - - return ERR_OK; + return SetDoNotDisturbDateByUser(userId, date); } ErrCode AdvancedNotificationService::GetDoNotDisturbDate(sptr &date) @@ -2325,35 +2288,12 @@ ErrCode AdvancedNotificationService::GetDoNotDisturbDate(sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - return ERR_ANS_INVALID_BUNDLE; + int userId = SUBSCRIBE_USER_INIT; + if (!GetActiveUserId(userId)) { + return ERR_ANS_GET_ACTIVE_USER_FAILED; } - handler_->PostSyncTask(std::bind([&]() { - sptr currentConfig = nullptr; - result = NotificationPreferences::GetInstance().GetDoNotDisturbDate(bundleOption, currentConfig); - if (result == ERR_OK) { - int64_t now = GetCurrentTime(); - switch (currentConfig->GetDoNotDisturbType()) { - case NotificationConstant::DoNotDisturbType::CLEARLY: - case NotificationConstant::DoNotDisturbType::ONCE: - if (now >= currentConfig->GetEndDate()) { - date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); - NotificationPreferences::GetInstance().SetDoNotDisturbDate(bundleOption, date); - } else { - date = currentConfig; - } - break; - default: - date = currentConfig; - break; - } - } - })); - - return ERR_OK; + return GetDoNotDisturbDateByUser(userId, data); } ErrCode AdvancedNotificationService::DoesSupportDoNotDisturbMode(bool &doesSupport) @@ -2840,5 +2780,218 @@ void AdvancedNotificationService::TriggerRemoveWantAgent(const sptr agent = request->GetRemovalWantAgent(); WantAgent::WantAgentHelper::TriggerWantAgent(agent, nullptr, triggerInfo); } + +ErrCode AdvancedNotificationService::IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + if (!IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!CheckPermission(GetClientBundleName())) { + return ERR_ANS_PERMISSION_DENIED; + } + + handler_->PostSyncTask(std::bind([&]() { + allowed = false; + result = NotificationPreferences::GetInstance().GetNotificationsEnabled(userId, allowed); + })); + return result; +} + +ErrCode AdvancedNotificationService::SetNotificationsEnabledByUser(const int32_t &userId, bool enabled) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + if (!IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!CheckPermission(GetClientBundleName())) { + return ERR_ANS_PERMISSION_DENIED; + } + + int userId = SUBSCRIBE_USER_INIT; + if (!GetActiveUserId(userId)) { + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + + handler_->PostSyncTask(std::bind([&]() { + if (deviceId.empty()) { + // Local device + result = NotificationPreferences::GetInstance().SetNotificationsEnabled(userId, enabled); + } else { + // Remote device + } + })); + return result; +} + +ErrCode AdvancedNotificationService::DeleteAllByUser(const int32_t &userId) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + if (!IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("Input userId is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + ErrCode result = ERR_OK; + handler_->PostSyncTask(std::bind([&]() { + std::vector keys = GetNotificationKeys(nullptr); + for (auto key : keys) { +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::string deviceId = GetNotificationDeviceId(key); +#endif + sptr notification = nullptr; + + result = RemoveFromNotificationListForDeleteAll(key, userId, notification); + if (result != ERR_OK || notification == nullptr) { + continue; + } + + if (notification->GetUserId() == userId) { + int reason = NotificationConstant::CANCEL_ALL_REASON_DELETE; + UpdateRecentNotification(notification, true, reason); + sptr sortingMap = GenerateSortingMap(); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + DoDistributedDelete(deviceId, notification); +#endif + } + } + + result = ERR_OK; + })); + + return result; +} + +ErrCode AdvancedNotificationService::SetDoNotDisturbDate(const int32_t &userId, + const sptr &date) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + if (userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("Input userId is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + if (!IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!CheckPermission(GetClientBundleName())) { + return ERR_ANS_PERMISSION_DENIED; + } + + return SetDoNotDisturbDateByUser(userId, date); +} + +ErrCode AdvancedNotificationService::GetDoNotDisturbDate(const int32_t &userId, + sptr &date) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + if (userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("Input userId is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + if (!IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!CheckPermission(GetClientBundleName())) { + return ERR_ANS_PERMISSION_DENIED; + } + + return GetDoNotDisturbDateByUser(userId, date); +} + +ErrCode AdvancedNotificationService::SetDoNotDisturbDateByUser(const int32_t &userId, + const sptr &date) +{ + if (date == nullptr) { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode result = ERR_OK; + + int64_t beginDate = ResetSeconds(date->GetBeginDate()); + int64_t endDate = ResetSeconds(date->GetEndDate()); + + switch (date->GetDoNotDisturbType()) { + case NotificationConstant::DoNotDisturbType::NONE: + beginDate = 0; + endDate = 0; + break; + case NotificationConstant::DoNotDisturbType::ONCE: + AdjustDateForDndTypeOnce(beginDate, endDate); + break; + case NotificationConstant::DoNotDisturbType::CLEARLY: + if (beginDate >= endDate) { + return ERR_ANS_INVALID_PARAM; + } + break; + default: + break; + } + + const sptr newConfig = new NotificationDoNotDisturbDate( + date->GetDoNotDisturbType(), + beginDate, + endDate + ); + + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().SetDoNotDisturbDate(userId, newConfig); + if (result == ERR_OK) { + NotificationSubscriberManager::GetInstance()->NotifyDoNotDisturbDateChanged(newConfig); + } + })); + + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetDoNotDisturbDateByUser(const int32_t &userId, + sptr &date) +{ + ErrCode result = ERR_OK; + + handler_->PostSyncTask(std::bind([&]() { + sptr currentConfig = nullptr; + result = NotificationPreferences::GetInstance().GetDoNotDisturbDate(userId, currentConfig); + if (result == ERR_OK) { + int64_t now = GetCurrentTime(); + switch (currentConfig->GetDoNotDisturbType()) { + case NotificationConstant::DoNotDisturbType::CLEARLY: + case NotificationConstant::DoNotDisturbType::ONCE: + if (now >= currentConfig->GetEndDate()) { + date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + NotificationPreferences::GetInstance().SetDoNotDisturbDate(userId, date); + } else { + date = currentConfig; + } + break; + default: + date = currentConfig; + break; + } + } + })); + + return ERR_OK; +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 4dd6b4008..209e0e1fa 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -508,16 +508,12 @@ bool NotificationPreferences::GetActiveUserId(int& userId) return false; } -ErrCode NotificationPreferences::GetNotificationsEnabled( - const sptr &bundleOption, bool &enabled) +ErrCode NotificationPreferences::GetNotificationsEnabled(const int32_t &userId, bool &enabled) { - if (bundleOption == nullptr) { + if (userId <= SUBSCRIBE_USER_INIT) { return ERR_ANS_INVALID_PARAM; } - int userId = SUBSCRIBE_USER_INIT; - (void)GetActiveUserId(userId); - ErrCode result = ERR_OK; if (!preferencesInfo_.GetEnabledAllNotification(userId, enabled)) { result = ERR_ANS_INVALID_PARAM; @@ -525,16 +521,12 @@ ErrCode NotificationPreferences::GetNotificationsEnabled( return result; } -ErrCode NotificationPreferences::SetNotificationsEnabled( - const sptr &bundleOption, const bool &enabled) +ErrCode NotificationPreferences::SetNotificationsEnabled(const int32_t &userId, const bool &enabled) { - if (bundleOption == nullptr) { + if (userId <= SUBSCRIBE_USER_INIT) { return ERR_ANS_INVALID_PARAM; } - int userId = SUBSCRIBE_USER_INIT; - (void)GetActiveUserId(userId); - NotificationPreferencesInfo preferencesInfo = preferencesInfo_; preferencesInfo.SetEnabledAllNotification(userId, enabled); ErrCode result = ERR_OK; @@ -548,16 +540,13 @@ ErrCode NotificationPreferences::SetNotificationsEnabled( return result; } -ErrCode NotificationPreferences::GetDoNotDisturbDate( - const sptr &bundleOption, sptr &date) +ErrCode NotificationPreferences::GetDoNotDisturbDate(const int32_t &userId, + sptr &date) { - if (bundleOption == nullptr) { + if (userId <= SUBSCRIBE_USER_INIT) { return ERR_ANS_INVALID_PARAM; } - int userId = SUBSCRIBE_USER_INIT; - (void)GetActiveUserId(userId); - ErrCode result = ERR_OK; NotificationPreferencesInfo preferencesInfo = preferencesInfo_; if (!preferencesInfo.GetDoNotDisturbDate(userId, date)) { @@ -566,16 +555,13 @@ ErrCode NotificationPreferences::GetDoNotDisturbDate( return result; } -ErrCode NotificationPreferences::SetDoNotDisturbDate( - const sptr &bundleOption, const sptr date) +ErrCode NotificationPreferences::SetDoNotDisturbDate(const int32_t &userId, + const sptr date) { - if (bundleOption == nullptr) { + if (userId <= SUBSCRIBE_USER_INIT) { return ERR_ANS_INVALID_PARAM; } - int userId = SUBSCRIBE_USER_INIT; - (void)GetActiveUserId(userId); - NotificationPreferencesInfo preferencesInfo = preferencesInfo_; preferencesInfo.SetDoNotDisturbDate(userId, date); diff --git a/services/ans/src/notification_subscriber_manager.cpp b/services/ans/src/notification_subscriber_manager.cpp index 9c2dcda42..e294c6150 100644 --- a/services/ans/src/notification_subscriber_manager.cpp +++ b/services/ans/src/notification_subscriber_manager.cpp @@ -282,12 +282,23 @@ void NotificationSubscriberManager::NotifyConsumedInner( const sptr ¬ification, const sptr ¬ificationMap) { for (auto record : subscriberRecordList_) { + // notification to the specified user + sptr notificationRequest = notification->GetNotificationRequest(); + if (notificationRequest != nullptr) + int32_t recvUserId = notificationRequest->GetReceiverUserId(); + if ((recvUserId != SUBSCRIBE_USER_INIT) && (recvUserId == record->userId)) { + record->subscriber->OnConsumed(notification, notificationMap); + record->subscriber->OnConsumed(notification); + return; + } + } + auto BundleNames = notification->GetBundleName(); auto iter = std::find(record->bundleList_.begin(), record->bundleList_.end(), BundleNames); if (!record->subscribedAll == (iter != record->bundleList_.end()) && (notification->GetUserId() == record->userId || - notification->GetUserId() == SUBSCRIBE_USER_ALL || - record->userId == SUBSCRIBE_USER_ALL)) { + notification->GetUserId() == SUBSCRIBE_USER_ALL || + IsSystemUser(record->userId))) { record->subscriber->OnConsumed(notification, notificationMap); record->subscriber->OnConsumed(notification); } @@ -322,5 +333,14 @@ void NotificationSubscriberManager::NotifyDoNotDisturbDateChangedInner(const spt record->subscriber->OnDoNotDisturbDateChange(date); } } + +bool NotificationSubscriberManager::IsSystemUser(int32_t userId) +{ + if (userId >= SUBSCRIBE_USER_SYSTEM_BEGIN && userId <= SUBSCRIBE_USER_SYSTEM_END) { + return true; + } + + return false; +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/notification_preferences_test.cpp b/services/ans/test/unittest/notification_preferences_test.cpp index c909c9b04..31382d429 100644 --- a/services/ans/test/unittest/notification_preferences_test.cpp +++ b/services/ans/test/unittest/notification_preferences_test.cpp @@ -38,7 +38,7 @@ public: }; sptr NotificationPreferencesTest::bundleOption_ = - new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID); + new NotificationBundleOption(SYSTEM_APP_UID, NON_SYSTEM_APP_UID); sptr NotificationPreferencesTest::noExsitbundleOption_ = new NotificationBundleOption(std::string("notExsitBundleName"), NON_SYSTEM_APP_UID); sptr NotificationPreferencesTest::bundleEmptyOption_ = @@ -1017,7 +1017,7 @@ HWTEST_F(NotificationPreferencesTest, NotificationPreferencesTest_02900, Functio */ HWTEST_F(NotificationPreferencesTest, SetNotificationsEnabled_00100, Function | SmallTest | Level1) { - EXPECT_EQ((int)NotificationPreferences::GetInstance().SetNotificationsEnabled(bundleOption_, true), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetNotificationsEnabled(100, true), (int)ERR_OK); } /** @@ -1027,9 +1027,9 @@ HWTEST_F(NotificationPreferencesTest, SetNotificationsEnabled_00100, Function | */ HWTEST_F(NotificationPreferencesTest, GetNotificationsEnabled_00100, Function | SmallTest | Level1) { - EXPECT_EQ((int)NotificationPreferences::GetInstance().SetNotificationsEnabled(bundleOption_, true), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetNotificationsEnabled(100, true), (int)ERR_OK); bool enable = false; - EXPECT_EQ((int)NotificationPreferences::GetInstance().GetNotificationsEnabled(bundleOption_, enable), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetNotificationsEnabled(100, enable), (int)ERR_OK); EXPECT_TRUE(enable); } @@ -1049,7 +1049,7 @@ HWTEST_F(NotificationPreferencesTest, SetDoNotDisturbDate_00100, Function | Smal sptr date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::ONCE, beginDate, endDate); - EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDoNotDisturbDate(bundleOption_, date), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDoNotDisturbDate(SYSTEM_APP_UID, date), (int)ERR_OK); } /** @@ -1067,10 +1067,10 @@ HWTEST_F(NotificationPreferencesTest, GetDoNotDisturbDate_00100, Function | Smal int64_t endDate = endDuration.count(); sptr date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::DAILY, beginDate, endDate); - EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDoNotDisturbDate(bundleOption_, date), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDoNotDisturbDate(SYSTEM_APP_UID, date), (int)ERR_OK); sptr getDate; - EXPECT_EQ((int)NotificationPreferences::GetInstance().GetDoNotDisturbDate(bundleOption_, getDate), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetDoNotDisturbDate(SYSTEM_APP_UID, getDate), (int)ERR_OK); EXPECT_EQ(getDate->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::DAILY); EXPECT_EQ(getDate->GetBeginDate(), beginDate); EXPECT_EQ(getDate->GetEndDate(), endDate); @@ -1083,13 +1083,13 @@ HWTEST_F(NotificationPreferencesTest, GetDoNotDisturbDate_00100, Function | Smal */ HWTEST_F(NotificationPreferencesTest, GetNotificationsEnabled_00200, Function | SmallTest | Level1) { - EXPECT_EQ((int)NotificationPreferences::GetInstance().SetNotificationsEnabled(bundleOption_, true), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetNotificationsEnabled(100, true), (int)ERR_OK); bool enable = false; - EXPECT_EQ((int)NotificationPreferences::GetInstance().GetNotificationsEnabled(bundleOption_, enable), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetNotificationsEnabled(100, enable), (int)ERR_OK); EXPECT_TRUE(enable); enable = false; - EXPECT_EQ((int)NotificationPreferences::GetInstance().GetNotificationsEnabled(bundleOption_, enable), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetNotificationsEnabled(101, enable), (int)ERR_OK); EXPECT_FALSE(enable); } @@ -1108,10 +1108,10 @@ HWTEST_F(NotificationPreferencesTest, GetDoNotDisturbDate_00200, Function | Smal int64_t endDate = endDuration.count(); sptr date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::DAILY, beginDate, endDate); - EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDoNotDisturbDate(bundleOption_, date), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDoNotDisturbDate(SYSTEM_APP_UID, date), (int)ERR_OK); sptr getDate; - EXPECT_EQ((int)NotificationPreferences::GetInstance().GetDoNotDisturbDate(bundleOption_, getDate), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetDoNotDisturbDate(SYSTEM_APP_UID, getDate), (int)ERR_OK); EXPECT_EQ(getDate->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::DAILY); EXPECT_EQ(getDate->GetBeginDate(), beginDate); EXPECT_EQ(getDate->GetEndDate(), endDate); -- Gitee