From f5f5918ef729519162b7ff2c78aacb562e04f58c Mon Sep 17 00:00:00 2001 From: xinking129 Date: Mon, 18 Mar 2024 17:18:08 +0800 Subject: [PATCH 01/15] Notification support for focused mode scene adaptation Signed-off-by: xinking129 --- frameworks/ans/BUILD.gn | 3 +- .../notification_do_not_disturb_profile.cpp | 160 ++++++++++++++++++ frameworks/ans/src/notification_helper.cpp | 11 ++ .../core/common/include/ans_const_define.h | 2 + .../core/include/ans_manager_interface.h | 17 ++ frameworks/core/include/ans_manager_proxy.h | 16 ++ frameworks/core/include/ans_manager_stub.h | 19 ++- frameworks/core/include/ans_notification.h | 18 ++ ..._notification_service_ipc_interface_code.h | 4 +- .../core/src/ans_manager_proxy_disturb.cpp | 69 +++++++- frameworks/core/src/ans_manager_stub.cpp | 52 +++++- .../core/src/ans_manager_stub_invalid.cpp | 12 ++ frameworks/core/src/ans_notification.cpp | 26 +++ frameworks/js/napi/include/disturb_mode.h | 12 +- .../napi/include/manager/napi_disturb_mode.h | 4 +- frameworks/js/napi/src/disturb_mode.cpp | 127 ++++++++++++++ .../js/napi/src/manager/init_module.cpp | 2 + .../js/napi/src/manager/napi_disturb_mode.cpp | 92 +++++++++- .../notification_do_not_disturb_profile.h | 115 +++++++++++++ interfaces/inner_api/notification_helper.h | 19 +++ services/ans/BUILD.gn | 3 +- .../ans/include/advanced_datashare_helper.h | 48 ++++++ .../include/advanced_notification_service.h | 18 ++ .../ans/include/notification_preferences.h | 22 +++ .../notification_preferences_database.h | 6 + .../include/notification_preferences_info.h | 8 +- .../ans/src/advanced_datashare_helper.cpp | 69 ++++++++ .../ans/src/advanced_notification_service.cpp | 68 +++++++- .../ans/src/advanced_notification_utils.cpp | 58 +++++++ services/ans/src/notification_preferences.cpp | 61 +++++++ .../src/notification_preferences_database.cpp | 121 +++++++++++++ .../ans/src/notification_preferences_info.cpp | 37 ++++ 32 files changed, 1283 insertions(+), 16 deletions(-) create mode 100644 frameworks/ans/src/notification_do_not_disturb_profile.cpp create mode 100644 interfaces/inner_api/notification_do_not_disturb_profile.h create mode 100644 services/ans/include/advanced_datashare_helper.h create mode 100644 services/ans/src/advanced_datashare_helper.cpp diff --git a/frameworks/ans/BUILD.gn b/frameworks/ans/BUILD.gn index 72c27ac5d..5ed1b1abc 100644 --- a/frameworks/ans/BUILD.gn +++ b/frameworks/ans/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2023 Huawei Device Co., Ltd. +# Copyright (c) 2021-2024 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 @@ -87,6 +87,7 @@ ohos_shared_library("ans_innerkits") { "${frameworks_module_ans_path}/src/notification_conversational_message.cpp", "${frameworks_module_ans_path}/src/notification_distributed_options.cpp", "${frameworks_module_ans_path}/src/notification_do_not_disturb_date.cpp", + "${frameworks_module_ans_path}/src/notification_do_not_disturb_profile.cpp", "${frameworks_module_ans_path}/src/notification_flags.cpp", "${frameworks_module_ans_path}/src/notification_helper.cpp", "${frameworks_module_ans_path}/src/notification_live_view_content.cpp", diff --git a/frameworks/ans/src/notification_do_not_disturb_profile.cpp b/frameworks/ans/src/notification_do_not_disturb_profile.cpp new file mode 100644 index 000000000..025a8f33f --- /dev/null +++ b/frameworks/ans/src/notification_do_not_disturb_profile.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "notification_do_not_disturb_profile.h" + +#include "ans_const_define.h" +#include "nlohmann/json.hpp" + +namespace OHOS { +namespace Notification { +namespace { +constexpr const char *DO_NOT_DISTURB_PROFILE_ID = "id"; +constexpr const char *DO_NOT_DISTURB_PROFILE_NAME = "name"; +constexpr const char *DO_NOT_DISTURB_PROFILE_TRUSTLIST = "trustlist"; +} // namespace +NotificationDoNotDisturbProfile::NotificationDoNotDisturbProfile( + int32_t id, const std::string &name, const std::vector &trustList) + : id_(id), name_(name), trustList_(trustList) +{} + +void NotificationDoNotDisturbProfile::SetProfileId(int32_t id) +{ + id_ = id; +} + +void NotificationDoNotDisturbProfile::SetProfileName(const std::string &name) +{ + name_ = name; +} + +void NotificationDoNotDisturbProfile::SetProfileTrustList(const std::vector &trustList) +{ + trustList_ = trustList; +} + +int32_t NotificationDoNotDisturbProfile::GetProfileId() const +{ + return id_; +} + +std::string NotificationDoNotDisturbProfile::GetProfileName() const +{ + return name_; +} + +std::vector NotificationDoNotDisturbProfile::GetProfileTrustlist() const +{ + return trustList_; +} + +bool NotificationDoNotDisturbProfile::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteInt32(id_)) { + ANS_LOGE("Failed to write do not disturb id."); + return false; + } + if (!parcel.WriteString(name_)) { + ANS_LOGE("Failed to write do not disturb name."); + return false; + } + auto size = trustList_.size(); + if (size > MAX_PARCELABLE_VECTOR_NUM) { + ANS_LOGE("Size exceeds the range."); + return false; + } + if (!parcel.WriteInt32(size)) { + ANS_LOGE("Failed to write do not disturb trust list size."); + return false; + } + for (uint32_t index = 0; index < size; ++index) { + if (!parcel.WriteParcelable(&trustList_[index])) { + ANS_LOGE("Failed to write do not disturb trust list."); + return false; + } + } + return true; +} + +NotificationDoNotDisturbProfile *NotificationDoNotDisturbProfile::Unmarshalling(Parcel &parcel) +{ + auto objptr = new (std::nothrow) NotificationDoNotDisturbProfile(); + if ((objptr != nullptr) && !objptr->ReadFromParcel(parcel)) { + delete objptr; + objptr = nullptr; + } + return objptr; +} + +bool NotificationDoNotDisturbProfile::ReadFromParcel(Parcel &parcel) +{ + id_ = parcel.ReadInt32(); + name_ = parcel.ReadString(); + auto size = parcel.ReadUint32(); + if (size > MAX_PARCELABLE_VECTOR_NUM) { + ANS_LOGE("Size exceeds the range."); + return false; + } + for (uint32_t index = 0; index < size; ++index) { + sptr bundleOption = parcel.ReadParcelable(); + if (bundleOption == nullptr) { + ANS_LOGE("Failed to read bundle option."); + return false; + } + trustList_.emplace_back(*bundleOption); + } + return true; +} + +std::string NotificationDoNotDisturbProfile::ToJson() +{ + nlohmann::json jsonNodes = nlohmann::json::array(); + for (size_t index = 0; index < trustList_.size(); index++) { + nlohmann::json jsonNode; + if (trustList_[index].ToJson(jsonNode)) { + jsonNodes.emplace_back(jsonNode); + } + } + nlohmann::json jsonObject { + {DO_NOT_DISTURB_PROFILE_ID, id_}, + {DO_NOT_DISTURB_PROFILE_NAME, name_}, + {DO_NOT_DISTURB_PROFILE_TRUSTLIST, jsonNodes} + }; + return jsonObject.dump(); +} + +void NotificationDoNotDisturbProfile::FromJson(const std::string &jsonObj) +{ + nlohmann::json jsonObject = nlohmann::json::parse(jsonObj, nullptr, false); + if (jsonObject.is_discarded()) { + ANS_LOGE("Failed to parse json string."); + return; + } + if (jsonObject.contains(DO_NOT_DISTURB_PROFILE_ID) && jsonObject[DO_NOT_DISTURB_PROFILE_ID].is_number()) { + id_ = jsonObject.at(DO_NOT_DISTURB_PROFILE_ID).get(); + } + if (jsonObject.contains(DO_NOT_DISTURB_PROFILE_NAME) && jsonObject[DO_NOT_DISTURB_PROFILE_NAME].is_string()) { + name_ = jsonObject.at(DO_NOT_DISTURB_PROFILE_NAME).get(); + } + if (jsonObject.contains(DO_NOT_DISTURB_PROFILE_TRUSTLIST) && + jsonObject[DO_NOT_DISTURB_PROFILE_TRUSTLIST].is_array()) { + for (auto &trust : jsonObject.at(DO_NOT_DISTURB_PROFILE_TRUSTLIST)) { + sptr bundleOption = new (std::nothrow) NotificationBundleOption(); + trustList_.emplace_back(*bundleOption->FromJson(trust)); + } + } +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 1ee5203b3..d73aafa36 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -345,6 +345,17 @@ ErrCode NotificationHelper::GetDoNotDisturbDate(NotificationDoNotDisturbDate &do return DelayedSingleton::GetInstance()->GetDoNotDisturbDate(doNotDisturbDate); } +ErrCode NotificationHelper::AddDoNotDisturbProfiles(const std::vector> &profiles) +{ + return DelayedSingleton::GetInstance()->AddDoNotDisturbProfiles(profiles); +} + +ErrCode NotificationHelper::RemoveDoNotDisturbProfiles( + const std::vector> &profiles) +{ + return DelayedSingleton::GetInstance()->RemoveDoNotDisturbProfiles(profiles); +} + ErrCode NotificationHelper::DoesSupportDoNotDisturbMode(bool &doesSupport) { return DelayedSingleton::GetInstance()->DoesSupportDoNotDisturbMode(doesSupport); diff --git a/frameworks/core/common/include/ans_const_define.h b/frameworks/core/common/include/ans_const_define.h index 7df4a2322..49e1dd0e6 100644 --- a/frameworks/core/common/include/ans_const_define.h +++ b/frameworks/core/common/include/ans_const_define.h @@ -50,6 +50,8 @@ constexpr int32_t SUBSCRIBE_USER_SYSTEM_BEGIN = 0; constexpr int32_t SUBSCRIBE_USER_SYSTEM_END = 99; constexpr int32_t NOTIFICATION_MIN_COUNT = 0; constexpr int32_t NOTIFICATION_MAX_COUNT = 1024; +constexpr int32_t FOCUS_MODE_PROFILE_MIN_ID = 1; +constexpr int32_t FOCUS_MODE_PROFILE_MAX_ID = 10; // Default sound for notification const static Uri DEFAULT_NOTIFICATION_SOUND("file://system/etc/Light.ogg"); diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index 2da82c9eb..445893b2a 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -26,6 +26,7 @@ #include "notification_bundle_option.h" #include "notification_constant.h" #include "notification_do_not_disturb_date.h" +#include "notification_do_not_disturb_profile.h" #include "notification_request.h" #include "notification_slot.h" #include "notification_subscribe_info.h" @@ -520,6 +521,22 @@ public: */ virtual ErrCode GetDoNotDisturbDate(sptr &date) = 0; + /** + * @brief Add do not disturb profiles. + * + * @param profiles Indicates the NotificationDoNotDisturbProfile objects. + * @return Returns ERR_OK on success, others on failure. + */ + virtual ErrCode AddDoNotDisturbProfiles(const std::vector> &profiles) = 0; + + /** + * @brief Remove do not disturb profiles. + * + * @param profiles Indicates the NotificationDoNotDisturbProfile objects. + * @return Returns ERR_OK on success, others on failure. + */ + virtual ErrCode RemoveDoNotDisturbProfiles(const std::vector> &profiles) = 0; + /** * @brief Get whether Do Not Disturb mode is supported. * diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index 9f9665b58..888949f56 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -510,6 +510,22 @@ public: */ ErrCode GetDoNotDisturbDate(sptr &date) override; + /** + * @brief Add do not disturb Profiles. + * + * @param profiles Indicates the NotificationDoNotDisturbProfile objects. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode AddDoNotDisturbProfiles(const std::vector> &profiles) override; + + /** + * @brief Remove do not disturb Profiles. + * + * @param profiles Indicates the NotificationDoNotDisturbProfile objects. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode RemoveDoNotDisturbProfiles(const std::vector> &profiles) override; + /** * @brief Get whether Do Not Disturb mode is supported. * diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index 69896a37d..9f974afb1 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -510,6 +510,22 @@ public: */ virtual ErrCode GetDoNotDisturbDate(sptr &date) override; + /** + * @brief Add do not disturb profiles. + * + * @param profiles Indicates the NotificationDoNotDisturbProfile objects. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode AddDoNotDisturbProfiles(const std::vector> &profiles) override; + + /** + * @brief Remove do not disturb profiles. + * + * @param profiles Indicates the NotificationDoNotDisturbProfile objects. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode RemoveDoNotDisturbProfiles(const std::vector> &profiles) override; + /** * @brief Get whether Do Not Disturb mode is supported. * @@ -952,7 +968,8 @@ private: ErrCode HandleIsDistributedEnabledByBundle(MessageParcel &data, MessageParcel &reply); ErrCode HandleSetSmartReminderEnabled(MessageParcel &data, MessageParcel &reply); ErrCode HandleIsSmartReminderEnabled(MessageParcel &data, MessageParcel &reply); - ErrCode HandleCancelAsBundleWithAgent(MessageParcel &data, MessageParcel &reply); + ErrCode HandleAddDoNotDisturbProfiles(MessageParcel &data, MessageParcel &reply); + ErrCode HandleRemoveDoNotDisturbProfiles(MessageParcel &data, MessageParcel &reply); template bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &reply, ErrCode &result) { diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index 055171e07..a2971b80b 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -646,6 +646,24 @@ public: */ ErrCode GetDoNotDisturbDate(NotificationDoNotDisturbDate &doNotDisturbDate); + /** + * @brief Add the do not disturb profiles. + * @note Your application must have system signature to call this method. + * + * @param doNotDisturbProfiles Indicates the do not disturb profiles to add. + * @return Returns add do not disturb profiles result. + */ + ErrCode AddDoNotDisturbProfiles(const std::vector> &profiles); + + /** + * @brief Remove the do not disturb profiles. + * @note Your application must have system signature to call this method. + * + * @param doNotDisturbProfiles Indicates the do not disturb profiles to remove. + * @return Returns remove do not disturb profiles result. + */ + ErrCode RemoveDoNotDisturbProfiles(const std::vector> &profiles); + /** * @brief Obtains the flag that whether to support do not disturb mode. * diff --git a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h index e2c335a9a..29b40ede8 100644 --- a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h +++ b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h @@ -136,8 +136,8 @@ namespace Notification { GET_DISTRIBUTED_ENABLED_BY_BUNDLE, SET_SMART_REMINDER_ENABLED, GET_SMART_REMINDER_ENABLED, - GET_SLOT_BY_BUNDLE, - CANCEL_AS_BUNDLE_WITH_AGENT, + ADD_DO_NOTDISTURB_PROFILES, + REMOVE_DO_NOT_DISTURB_PROFILES, }; } } diff --git a/frameworks/core/src/ans_manager_proxy_disturb.cpp b/frameworks/core/src/ans_manager_proxy_disturb.cpp index 5cd75da82..2d000c3d4 100644 --- a/frameworks/core/src/ans_manager_proxy_disturb.cpp +++ b/frameworks/core/src/ans_manager_proxy_disturb.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 @@ -93,6 +93,73 @@ ErrCode AnsManagerProxy::GetDoNotDisturbDate(sptr return result; } +ErrCode AnsManagerProxy::AddDoNotDisturbProfiles(const std::vector> &profiles) +{ + if (profiles.empty()) { + ANS_LOGW("The profiles is empty."); + return ERR_ANS_INVALID_PARAM; + } + if (profiles.size() > MAX_STATUS_VECTOR_NUM) { + ANS_LOGE("The profiles is exceeds limit."); + return ERR_ANS_INVALID_PARAM; + } + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("Write interface token error."); + return ERR_ANS_PARCELABLE_FAILED; + } + if (!WriteParcelableVector(profiles, data)) { + ANS_LOGE("Write interface token error."); + return ERR_ANS_PARCELABLE_FAILED; + } + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::ADD_DO_NOTDISTURB_PROFILES, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("Transact ErrCode is %{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("Read result error."); + return ERR_ANS_PARCELABLE_FAILED; + } + return result; +} + +ErrCode AnsManagerProxy::RemoveDoNotDisturbProfiles(const std::vector> &profiles) +{ + if (profiles.empty()) { + ANS_LOGW("The profiles is empty."); + return ERR_ANS_INVALID_PARAM; + } + if (profiles.size() > MAX_STATUS_VECTOR_NUM) { + ANS_LOGE("The profiles is exceeds limit."); + return ERR_ANS_INVALID_PARAM; + } + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("Write interface token error."); + return ERR_ANS_PARCELABLE_FAILED; + } + if (!WriteParcelableVector(profiles, data)) { + ANS_LOGE("Write interface token error."); + return ERR_ANS_PARCELABLE_FAILED; + } + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::REMOVE_DO_NOT_DISTURB_PROFILES, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("Transact ErrCode is %{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + if (!reply.ReadInt32(result)) { + ANS_LOGE("Read result error."); + return ERR_ANS_PARCELABLE_FAILED; + } + return result; +} + ErrCode AnsManagerProxy::DoesSupportDoNotDisturbMode(bool &doesSupport) { MessageParcel data; diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index 09731e50a..44e3b51c1 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -304,12 +304,12 @@ const std::map> profiles; + if (!ReadParcelableVector(profiles, data)) { + ANS_LOGE("Read profiles failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (profiles.size() > MAX_STATUS_VECTOR_NUM) { + ANS_LOGE("The profiles is exceeds limit."); + return ERR_ANS_INVALID_PARAM; + } + + ErrCode result = AddDoNotDisturbProfiles(profiles); + if (!reply.WriteInt32(result)) { + ANS_LOGE("Write result failed, ErrCode is %{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + return ERR_OK; +} + ErrCode AnsManagerStub::HandleSetDistributedEnabledByBundle(MessageParcel &data, MessageParcel &reply) { ANS_LOGD("enter"); @@ -2165,6 +2186,27 @@ ErrCode AnsManagerStub::HandleSetDistributedEnabledByBundle(MessageParcel &data, return ERR_OK; } +ErrCode AnsManagerStub::HandleRemoveDoNotDisturbProfiles(MessageParcel &data, MessageParcel &reply) +{ + std::vector> profiles; + if (!ReadParcelableVector(profiles, data)) { + ANS_LOGE("Read profiles failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (profiles.size() > MAX_STATUS_VECTOR_NUM) { + ANS_LOGE("The profiles is exceeds limit."); + return ERR_ANS_INVALID_PARAM; + } + + ErrCode result = RemoveDoNotDisturbProfiles(profiles); + if (!reply.WriteInt32(result)) { + ANS_LOGE("Write result failed, ErrCode is %{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + return ERR_OK; +} + ErrCode AnsManagerStub::HandleIsDistributedEnabledByBundle(MessageParcel &data, MessageParcel &reply) { ANS_LOGD("enter"); diff --git a/frameworks/core/src/ans_manager_stub_invalid.cpp b/frameworks/core/src/ans_manager_stub_invalid.cpp index cd246d93f..66b621fb9 100644 --- a/frameworks/core/src/ans_manager_stub_invalid.cpp +++ b/frameworks/core/src/ans_manager_stub_invalid.cpp @@ -374,6 +374,18 @@ ErrCode AnsManagerStub::GetDoNotDisturbDate(sptr & return ERR_INVALID_OPERATION; } +ErrCode AnsManagerStub::AddDoNotDisturbProfiles(const std::vector> &profiles) +{ + ANS_LOGD("Called."); + return ERR_INVALID_OPERATION; +} + +ErrCode AnsManagerStub::RemoveDoNotDisturbProfiles(const std::vector> &profiles) +{ + ANS_LOGD("Called."); + return ERR_INVALID_OPERATION; +} + ErrCode AnsManagerStub::DoesSupportDoNotDisturbMode(bool &doesSupport) { ANS_LOGE("AnsManagerStub::DoesSupportDoNotDisturbMode called!"); diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index da76381d4..f6945e655 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -904,6 +904,32 @@ ErrCode AnsNotification::GetDoNotDisturbDate(NotificationDoNotDisturbDate &doNot return ret; } +ErrCode AnsNotification::AddDoNotDisturbProfiles(const std::vector> &profiles) +{ + if (profiles.empty()) { + ANS_LOGW("The profiles is empty."); + return ERR_ANS_INVALID_PARAM; + } + if (!GetAnsManagerProxy()) { + ANS_LOGW("Get ans manager proxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + return ansManagerProxy_->AddDoNotDisturbProfiles(profiles); +} + +ErrCode AnsNotification::RemoveDoNotDisturbProfiles(const std::vector> &profiles) +{ + if (profiles.empty()) { + ANS_LOGW("The profiles is empty."); + return ERR_ANS_INVALID_PARAM; + } + if (!GetAnsManagerProxy()) { + ANS_LOGW("Get ans manager proxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + return ansManagerProxy_->RemoveDoNotDisturbProfiles(profiles); +} + ErrCode AnsNotification::DoesSupportDoNotDisturbMode(bool &doesSupport) { if (!GetAnsManagerProxy()) { diff --git a/frameworks/js/napi/include/disturb_mode.h b/frameworks/js/napi/include/disturb_mode.h index a0309b909..fed1626af 100644 --- a/frameworks/js/napi/include/disturb_mode.h +++ b/frameworks/js/napi/include/disturb_mode.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 @@ -41,6 +41,13 @@ struct AsyncCallbackInfoSetDoNotDisturb { CallbackPromiseInfo info; }; +struct AsyncCallbackInfoDoNotDisturbProfile { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + std::vector> profiles; + CallbackPromiseInfo info; +}; + struct AsyncCallbackInfoGetDoNotDisturb { napi_env env = nullptr; napi_async_work asyncWork = nullptr; @@ -63,6 +70,9 @@ napi_value SupportDoNotDisturbMode(napi_env env, napi_callback_info info); napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, SetDoNotDisturbDateParams ¶ms); napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetDoNotDisturbDateParams ¶ms); +bool ParseProfilesParameters( + const napi_env &env, const napi_callback_info &info, std::vector> &profiles); +bool AnalysisTrustlist(const napi_env &env, const napi_value &value, sptr &profile); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_DISTURB_MODE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_disturb_mode.h b/frameworks/js/napi/include/manager/napi_disturb_mode.h index 4cbf88d3e..4bf0af5b2 100644 --- a/frameworks/js/napi/include/manager/napi_disturb_mode.h +++ b/frameworks/js/napi/include/manager/napi_disturb_mode.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 @@ -24,6 +24,8 @@ using namespace OHOS::Notification; napi_value NapiSetDoNotDisturbDate(napi_env env, napi_callback_info info); napi_value NapiGetDoNotDisturbDate(napi_env env, napi_callback_info info); napi_value NapiSupportDoNotDisturbMode(napi_env env, napi_callback_info info); +napi_value NapiAddDoNotDisturbProfiles(napi_env env, napi_callback_info info); +napi_value NapiRemoveDoNotDisturbProfiles(napi_env env, napi_callback_info info); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTURB_MODE_H \ No newline at end of file diff --git a/frameworks/js/napi/src/disturb_mode.cpp b/frameworks/js/napi/src/disturb_mode.cpp index e1938e4ef..719b64fd7 100644 --- a/frameworks/js/napi/src/disturb_mode.cpp +++ b/frameworks/js/napi/src/disturb_mode.cpp @@ -20,6 +20,7 @@ namespace NotificationNapi { const int SET_DISTURB_MAX_PARA = 3; const int SET_DISTURB_MIN_PARA = 1; const int GET_DISTURB_MAX_PARA = 2; +const int DISTURB_PROFILES_PARA = 1; napi_value GetDoNotDisturbDate(const napi_env &env, const napi_value &argv, SetDoNotDisturbDateParams ¶ms) { @@ -85,6 +86,90 @@ napi_value GetDoNotDisturbDate(const napi_env &env, const napi_value &argv, SetD return Common::NapiGetNull(env); } +bool GetDoNotDisturbProfile( + const napi_env &env, const napi_value &value, sptr &profile) +{ + ANS_LOGD("Called."); + bool hasProperty = false; + NAPI_CALL_BASE(env, napi_has_named_property(env, value, "id", &hasProperty), false); + if (!hasProperty) { + ANS_LOGE("Wrong argument type. Property type expected."); + return false; + } + int profileId = 0; + napi_value obj = nullptr; + napi_get_named_property(env, value, "id", &obj); + napi_valuetype valuetype = napi_undefined; + NAPI_CALL_BASE(env, napi_typeof(env, obj, &valuetype), false); + if (valuetype != napi_number) { + ANS_LOGE("Wrong argument type. Number expected."); + return false; + } + napi_get_value_int32(env, obj, &profileId); + ANS_LOGE("ProfileId is: %{public}d", profileId); + profile->SetProfileId(profileId); + + NAPI_CALL_BASE(env, napi_has_named_property(env, value, "name", &hasProperty), false); + if (!hasProperty) { + ANS_LOGE("Wrong argument type. Property type expected."); + return false; + } + char name[STR_MAX_SIZE] = {0}; + napi_get_named_property(env, value, "name", &obj); + NAPI_CALL_BASE(env, napi_typeof(env, obj, &valuetype), false); + if (valuetype != napi_string) { + ANS_LOGE("Wrong argument type. String expected."); + return false; + } + size_t strLen = 0; + NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, obj, name, STR_MAX_SIZE - 1, &strLen), false); + ANS_LOGD("The do not disturb profile name is: %{public}s", name); + profile->SetProfileName(name); + + return AnalysisTrustlist(env, value, profile); +} + +bool AnalysisTrustlist(const napi_env &env, const napi_value &value, sptr &profile) +{ + bool hasProperty = false; + NAPI_CALL_BASE(env, napi_has_named_property(env, value, "trustlist", &hasProperty), false); + if (!hasProperty) { + return true; + } + napi_value obj = nullptr; + napi_get_named_property(env, value, "trustlist", &obj); + bool isArray = false; + NAPI_CALL_BASE(env, napi_is_array(env, obj, &isArray), false); + if (!isArray) { + ANS_LOGE("Value is not an array."); + return false; + } + uint32_t length = 0; + napi_get_array_length(env, obj, &length); + if (length == 0) { + ANS_LOGD("The array is empty."); + return true; + } + std::vector options; + for (size_t index = 0; index < length; index++) { + napi_value nOption = nullptr; + napi_get_element(env, obj, index, &nOption); + napi_valuetype valuetype = napi_undefined; + NAPI_CALL_BASE(env, napi_typeof(env, nOption, &valuetype), false); + if (valuetype != napi_object) { + ANS_LOGE("Wrong argument type. Object expected."); + return false; + } + NotificationBundleOption option; + if (!Common::GetBundleOption(env, nOption, option)) { + return false; + } + options.emplace_back(option); + } + profile->SetProfileTrustList(options); + return true; +} + napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, SetDoNotDisturbDateParams ¶ms) { ANS_LOGD("enter"); @@ -138,6 +223,48 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, return Common::NapiGetNull(env); } +bool ParseProfilesParameters( + const napi_env &env, const napi_callback_info &info, std::vector> &profiles) +{ + ANS_LOGD("Called."); + size_t argc = DISTURB_PROFILES_PARA; + napi_value argv[DISTURB_PROFILES_PARA] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL), false); + if (argc != DISTURB_PROFILES_PARA) { + ANS_LOGE("Wrong number of arguments."); + return false; + } + napi_valuetype valuetype = napi_undefined; + bool isArray = false; + napi_is_array(env, argv[PARAM0], &isArray); + if (!isArray) { + ANS_LOGE("Wrong argument type. Array expected."); + return false; + } + uint32_t length = 0; + napi_get_array_length(env, argv[PARAM0], &length); + if (length == 0) { + ANS_LOGD("The array is empty."); + return false; + } + for (size_t index = 0; index < length; index++) { + napi_value nProfile = nullptr; + napi_get_element(env, argv[PARAM0], index, &nProfile); + NAPI_CALL_BASE(env, napi_typeof(env, nProfile, &valuetype), false); + if (valuetype != napi_object) { + ANS_LOGE("Wrong argument type. Object expected."); + return false; + } + sptr profile = new (std::nothrow) NotificationDoNotDisturbProfile(); + if (!GetDoNotDisturbProfile(env, nProfile, profile)) { + return false; + } + profiles.emplace_back(profile); + } + return true; +} + napi_value SetDoNotDisturbDate(napi_env env, napi_callback_info info) { ANS_LOGD("enter"); diff --git a/frameworks/js/napi/src/manager/init_module.cpp b/frameworks/js/napi/src/manager/init_module.cpp index fb3d48756..4e35fb40f 100644 --- a/frameworks/js/napi/src/manager/init_module.cpp +++ b/frameworks/js/napi/src/manager/init_module.cpp @@ -97,6 +97,8 @@ napi_value NotificationManagerInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("isSupportTemplate", NapiIsSupportTemplate), DECLARE_NAPI_FUNCTION("setDoNotDisturbDate", NapiSetDoNotDisturbDate), DECLARE_NAPI_FUNCTION("getDoNotDisturbDate", NapiGetDoNotDisturbDate), + DECLARE_NAPI_FUNCTION("addDoNotDisturbProfile", NapiAddDoNotDisturbProfiles), + DECLARE_NAPI_FUNCTION("removeDoNotDisturbProfile", NapiRemoveDoNotDisturbProfiles), DECLARE_NAPI_FUNCTION("supportDoNotDisturbMode", NapiSupportDoNotDisturbMode), DECLARE_NAPI_FUNCTION("isSupportDoNotDisturbMode", NapiSupportDoNotDisturbMode), DECLARE_NAPI_FUNCTION("isDistributedEnabled", NapiIsDistributedEnabled), diff --git a/frameworks/js/napi/src/manager/napi_disturb_mode.cpp b/frameworks/js/napi/src/manager/napi_disturb_mode.cpp index 1adeac126..aabd91167 100644 --- a/frameworks/js/napi/src/manager/napi_disturb_mode.cpp +++ b/frameworks/js/napi/src/manager/napi_disturb_mode.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 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 @@ -85,6 +85,96 @@ napi_value NapiSetDoNotDisturbDate(napi_env env, napi_callback_info info) } } +napi_value NapiAddDoNotDisturbProfiles(napi_env env, napi_callback_info info) +{ + ANS_LOGD("Called."); + std::vector> profiles; + if (!ParseProfilesParameters(env, info, profiles)) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + AsyncCallbackInfoDoNotDisturbProfile *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoDoNotDisturbProfile{.env = env, .asyncWork = nullptr, .profiles = profiles}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, nullptr); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "AddDoNotDisturbProfiles", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, resourceName, [](napi_env env, void *data) { + ANS_LOGE("Napi add do not disturb profiles work excute."); + AsyncCallbackInfoDoNotDisturbProfile *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::AddDoNotDisturbProfiles(asynccallbackinfo->profiles); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGE("Napi add do not disturb profiles work complete."); + AsyncCallbackInfoDoNotDisturbProfile *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + return promise; +} + +napi_value NapiRemoveDoNotDisturbProfiles(napi_env env, napi_callback_info info) +{ + ANS_LOGD("Called."); + std::vector> profiles; + if (!ParseProfilesParameters(env, info, profiles)) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + AsyncCallbackInfoDoNotDisturbProfile *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoDoNotDisturbProfile{.env = env, .asyncWork = nullptr, .profiles = profiles}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, nullptr); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "RemoveDoNotDisturbProfiles", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, resourceName, [](napi_env env, void *data) { + ANS_LOGE("Napi remove do not disturb profiles work excute."); + AsyncCallbackInfoDoNotDisturbProfile *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::RemoveDoNotDisturbProfiles(asynccallbackinfo->profiles); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGE("Napi remove do not disturb profiles work complete."); + AsyncCallbackInfoDoNotDisturbProfile *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + return promise; +} + void AsyncCompleteCallbackNapiGetDoNotDisturbDate(napi_env env, napi_status status, void *data) { ANS_LOGD("enter"); diff --git a/interfaces/inner_api/notification_do_not_disturb_profile.h b/interfaces/inner_api/notification_do_not_disturb_profile.h new file mode 100644 index 000000000..d145a5884 --- /dev/null +++ b/interfaces/inner_api/notification_do_not_disturb_profile.h @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2024 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_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_DO_NOT_DISTURB_PROFILE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_DO_NOT_DISTURB_PROFILE_H + +#include "notification_bundle_option.h" +#include "parcel.h" + +namespace OHOS { +namespace Notification { +class NotificationDoNotDisturbProfile : public Parcelable { +public: + /** + * Default constructor used to create a NotificationDoNotDisturbProfile instance. + */ + NotificationDoNotDisturbProfile() = default; + + /** + * A constructor used to create a NotificationDoNotDisturbProfile instance with the input parameters passed. + * + * @param id Indicates the profile id to add. + * @param name Indicates the profile name to add. + * @param trustlist Indicates the profile trustlist to add. + */ + NotificationDoNotDisturbProfile( + int32_t id, const std::string &name, const std::vector &trustList); + + /** + * Default deconstructor used to deconstruct. + */ + ~NotificationDoNotDisturbProfile() = default; + + /** + * Sets profile id for this NotificationDoNotDisturbProfile. + * + * @param profileId Indicates the profile id to add. + */ + void SetProfileId(int32_t id); + + /** + * Sets profile name for this NotificationDoNotDisturbProfile. + * + * @param profileName Indicates the profile name to add. + */ + void SetProfileName(const std::string &name); + + /** + * Sets profile trustlist for this NotificationDoNotDisturbProfile. + * + * @param profileTrustlist Indicates the profile trustlist to add. + * For available values, see NotificationBundleOption. + */ + void SetProfileTrustList(const std::vector &trustList); + + /** + * Obtains the profile id of this NotificationDoNotDisturbProfile. + * + * @return the profile id of this NotificationDoNotDisturbProfile. + */ + int32_t GetProfileId() const; + + /** + * Obtains the profile name of this NotificationDoNotDisturbProfile. + * + * @return the profile name of this NotificationDoNotDisturbProfile. + */ + std::string GetProfileName() const; + + /** + * Obtains the profile trustlist of this NotificationDoNotDisturbProfile. + * + * @return the profile trustlist of this NotificationDoNotDisturbProfile, + * For available values, see NotificationBundleOption. + */ + std::vector GetProfileTrustlist() const; + + /** + * Marshal a object into a Parcel. + * + * @param parcel the object into the parcel + */ + bool Marshalling(Parcel &parcel) const override; + + /** + * Read a NotificationDoNotDisturbProfile object from a Parcel. + * + * @param parcel the parcel + */ + bool ReadFromParcel(Parcel &parcel); + + static NotificationDoNotDisturbProfile *Unmarshalling(Parcel &parcel); + std::string ToJson(); + void FromJson(const std::string &value); + +private: + int32_t id_; + std::string name_; + std::vector trustList_; +}; +} // namespace Notification +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_DO_NOT_DISTURB_PROFILE_H \ No newline at end of file diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 879e34f44..5f506d20d 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -20,6 +20,7 @@ #include "notification_bundle_option.h" #include "notification_button_option.h" #include "notification_do_not_disturb_date.h" +#include "notification_do_not_disturb_profile.h" #include "enabled_notification_callback_data.h" #include "notification_request.h" #include "notification_slot.h" @@ -790,6 +791,24 @@ public: */ static ErrCode GetDoNotDisturbDate(const int32_t &userId, NotificationDoNotDisturbDate &doNotDisturbDate); + /** + * @brief Obtains the do not disturb on a specified user. + * @note Your application must have system signature to call this method. + * + * @param profiles Indicates the do not disturb time to add. + * @return Returns set do not disturb time result. + */ + static ErrCode AddDoNotDisturbProfiles(const std::vector> &profiles); + + /** + * @brief Obtains the do not disturb on a specified user. + * @note Your application must have system signature to call this method. + * + * @param profiles Indicates the do not disturb time to remove. + * @return Returns set do not disturb time result. + */ + static ErrCode RemoveDoNotDisturbProfiles(const std::vector> &profiles); + /** * Set whether the application slot is enabled. * diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index f6b83c08d..80ca33387 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2023 Huawei Device Co., Ltd. +# Copyright (c) 2021-2024 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 @@ -45,6 +45,7 @@ ohos_shared_library("libans") { sources = [ "src/access_token_helper.cpp", + "src/advanced_datashare_helper.cpp", "src/advanced_notification_event_service.cpp", "src/advanced_notification_inline.cpp", "src/advanced_notification_live_view_service.cpp", diff --git a/services/ans/include/advanced_datashare_helper.h b/services/ans/include/advanced_datashare_helper.h new file mode 100644 index 000000000..fe55e6373 --- /dev/null +++ b/services/ans/include/advanced_datashare_helper.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2024 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 NOTIFICATION_ADVANCED_DATASHAER_HELPER_H +#define NOTIFICATION_ADVANCED_DATASHAER_HELPER_H + +#include "data_ability_observer_stub.h" +#include "data_ability_observer_interface.h" +#include "datashare_helper.h" +#include "uri.h" + +namespace OHOS { +namespace Notification { +namespace { +constexpr const char *FOCUS_MODE_ENABLE_URI = "datashare://com.ohos.settingsdata/entry/settingsdata/" + "USER_SETTINGSDATA_100?Proxy=true&key==focus_mode_enable"; +constexpr const char *FOCUS_MODE_PROFILE_URI = "datashare://com.ohos.settingsdata/entry/settingsdata/" + "USER_SETTINGSDATA_100?Proxy=true&key==focus_mode_profile"; +constexpr const char *KEY_FOCUS_MODE_ENABLE = "focus_mode_enable"; +constexpr const char *KEY_FOCUS_MODE_PROFILE = "focus_mode_profile"; + +} // namespace + +class AdvancedDatashareHelper { +public: + AdvancedDatashareHelper(); + ~AdvancedDatashareHelper() = default; + bool Query(Uri &uri, const std::string &key, int32_t &value); + +private: + std::shared_ptr CreateDataShareHelper(); + std::shared_ptr dataShareHelper_ = nullptr; +}; +} // namespace Notification +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 06e18d018..083f046a0 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -35,6 +35,7 @@ #include "notification.h" #include "notification_bundle_option.h" #include "notification_dialog_manager.h" +#include "notification_do_not_disturb_profile.h" #include "notification_record.h" #include "notification_slot_filter.h" #include "notification_sorting_map.h" @@ -555,6 +556,22 @@ public: */ ErrCode GetDoNotDisturbDate(sptr &date) override; + /** + * @brief Add Do Not Disturb profiles. + * + * @param profiles Indicates the list of NotificationDoNotDisturbProfile objects to add. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode AddDoNotDisturbProfiles(const std::vector> &profiles) override; + + /** + * @brief Remove Do Not Disturb profiles. + * + * @param profiles Indicates the list of NotificationDoNotDisturbProfile objects to remove. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode RemoveDoNotDisturbProfiles(const std::vector> &profiles) override; + /** * @brief Get whether Do Not Disturb mode is supported. * @@ -1175,6 +1192,7 @@ private: void StartArchiveTimer(const std::shared_ptr &record); void CancelArchiveTimer(const std::shared_ptr &record); void ProcForDeleteLiveView(const std::shared_ptr &record); + void CheckDoNotDisturbProfile(const std::shared_ptr &record); ErrCode CheckCommonParams(); std::shared_ptr GetRecordFromNotificationList( int32_t notificationId, int32_t uid, const std::string &label, const std::string &bundleName); diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 736b224c7..8b1529e7a 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -249,6 +249,25 @@ public: ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr date); ErrCode GetTemplateSupported(const std::string &templateName, bool &support); + /** + * @brief Add do not disturb profiles from DB. + * + * @param userId Indicates user. + * @param profiles Indicates to add do not disturb profiles. + * @return Return ERR_OK on success, others on failure. + */ + ErrCode AddDoNotDisturbProfiles(int32_t userId, const std::vector> profiles); + + /** + * @brief Remove do not disturb profiles from DB. + * + * @param userId Indicates user. + * @param profiles Indicates to remove do not disturb profiles. + * @return Return ERR_OK on success, others on failure. + */ + ErrCode RemoveDoNotDisturbProfiles( + int32_t userId, const std::vector> profiles); + /** * @brief Obtains allow notification application list. * @@ -331,6 +350,9 @@ public: int32_t GetKvFromDb(const std::string &key, std::string &value); int32_t GetBatchKvsFromDb(const std::string &key, std::unordered_map &values); int32_t DeleteKvFromDb(const std::string &key); + ErrCode GetDonotDisturbProfile(int32_t profileId, int32_t userId, sptr &profile); + bool CheckDoNotDisturbProfileID(int32_t profileId); + private: ErrCode CheckSlotForCreateSlot(const sptr &bundleOption, const sptr &slot, NotificationPreferencesInfo &preferencesInfo) const; diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index d07c5c8c9..6bd5ff549 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -207,6 +207,10 @@ public: bool RemoveNotificationEnable(const int32_t userId); bool RemoveDoNotDisturbDate(const int32_t userId); bool RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid); + bool AddDoNotDisturbProfiles(int32_t userId, const std::vector> &profiles); + bool RemoveDoNotDisturbProfiles( + int32_t userId, const std::vector> &profiles); + bool GetDoNotDisturbProfiles(const std::string &key, sptr &profile); bool RemoveEnabledDbByBundleName(std::string bundleName); int32_t SetKvToDb(const std::string &key, const std::string &value); int32_t GetKvFromDb(const std::string &key, std::string &value); @@ -259,6 +263,7 @@ private: void ParseDoNotDisturbBeginDate(NotificationPreferencesInfo &info); void ParseDoNotDisturbEndDate(NotificationPreferencesInfo &info); void ParseEnableAllNotification(NotificationPreferencesInfo &info); + void ParseGetDoNotDisturbProfile(NotificationPreferencesInfo &info); void ParseBundleName(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleImportance(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleSlotFlags(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; @@ -294,6 +299,7 @@ private: void GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId); void GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId); void GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId); + void GetDoNotDisturbProfile(NotificationPreferencesInfo &info, int32_t userId); static const std::map &, std::string &)>> diff --git a/services/ans/include/notification_preferences_info.h b/services/ans/include/notification_preferences_info.h index a6fb9c353..4d8ae19d6 100644 --- a/services/ans/include/notification_preferences_info.h +++ b/services/ans/include/notification_preferences_info.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 @@ -310,9 +310,15 @@ public: void RemoveNotificationEnable(const int32_t userId); void RemoveDoNotDisturbDate(const int32_t userId); void SetBundleInfoFromDb(const BundleInfo &info, std::string bundleKey); + std::string MakeDoNotDisturbProfileKey(int32_t userId, int32_t profileId); + void AddDoNotDisturbProfiles(int32_t userId, const std::vector> &profiles); + void RemoveDoNotDisturbProfiles(int32_t userId, const std::vector> &profiles); + bool GetDoNotDisturbProfiles(int32_t profileId, int32_t userId, sptr &profiles); + private: std::map isEnabledAllNotification_; std::map> doNotDisturbDate_; + std::map> doNotDisturbProfiles_; std::map infos_; }; } // namespace Notification diff --git a/services/ans/src/advanced_datashare_helper.cpp b/services/ans/src/advanced_datashare_helper.cpp new file mode 100644 index 000000000..c05b579f5 --- /dev/null +++ b/services/ans/src/advanced_datashare_helper.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2024 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 "advanced_datashare_helper.h" + +#include "ans_log_wrapper.h" +#include "singleton.h" +#include "message_parcel.h" + +namespace OHOS { +namespace Notification { +constexpr const char *ADVANCED_DATASHARE_URI = + "datashare://com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_100?Proxy=true"; +constexpr const char *ADVANCED_DATA_COLUMN_KEYWORD = "KEYWORD"; +constexpr const char *ADVANCED_DATA_COLUMN_VALUE = "VALUE"; +AdvancedDatashareHelper::AdvancedDatashareHelper() +{ + dataShareHelper_ = CreateDataShareHelper(); +} + +std::shared_ptr AdvancedDatashareHelper::CreateDataShareHelper() +{ + DataShare::CreateOptions options; + options.enabled_ = true; + return DataShare::DataShareHelper::Creator(ADVANCED_DATASHARE_URI, options); +} + +bool AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, int32_t &value) +{ + if (dataShareHelper_ == nullptr) { + ANS_LOGE("The dataShareHelper_ is nullptr."); + return false; + } + DataShare::DataSharePredicates predicates; + std::vector columns; + predicates.EqualTo(ADVANCED_DATA_COLUMN_KEYWORD, key); + auto result = dataShareHelper_->Query(uri, predicates, columns); + if (result == nullptr) { + ANS_LOGE("Query error, result is null."); + return false; + } + + if (result->GoToFirstRow() != DataShare::E_OK) { + ANS_LOGE("Query failed, go to first row error."); + result->Close(); + return false; + } + + // int columnIndex; + result->GetColumnIndex(ADVANCED_DATA_COLUMN_VALUE, value); + // result->GetString(columnIndex, value); + result->Close(); + ANS_LOGD("Query success, value[%{public}d]", value); + return true; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 7cf666d40..aedf2d54c 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -23,6 +23,7 @@ #include "ability_info.h" #include "access_token_helper.h" #include "accesstoken_kit.h" +#include "advanced_datashare_helper.h" #include "ans_const_define.h" #include "ans_inner_errors.h" #include "ans_log_wrapper.h" @@ -81,7 +82,8 @@ constexpr int32_t UI_HALF = 2; constexpr int32_t MAX_LIVEVIEW_HINT_COUNT = 3; const std::string NOTIFICATION_ANS_CHECK_SA_PERMISSION = "notification.ans.check.sa.permission"; - +const std::string MMS_BUNDLE_NAME = "com.ohos.mms"; +const std::string CONTACTS_BUNDLE_NAME = "com.ohos.contacts"; } // namespace sptr AdvancedNotificationService::instance_; @@ -588,6 +590,69 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( return result; } +void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr &record) +{ + if (record == nullptr && record->request == nullptr && record->notification == nullptr) { + ANS_LOGE("Make notification record failed."); + return; + } + auto datashareHelper = std::make_shared(); + Uri enableUri(FOCUS_MODE_ENABLE_URI); + std::string key = KEY_FOCUS_MODE_ENABLE; + int32_t focus_mode_enable; + if (!datashareHelper->Query(enableUri, KEY_FOCUS_MODE_ENABLE, focus_mode_enable)) { + ANS_LOGE("Query focus mode enable is failed focus_mode_enable is %{public}d.", focus_mode_enable); + return; + } + ANS_LOGE("Query focus mode enable is %{public}d.", focus_mode_enable); + if(focus_mode_enable == 0){ + return; + } + Uri profileUri(FOCUS_MODE_PROFILE_URI); + int32_t profileId; + if (!datashareHelper->Query(profileUri, KEY_FOCUS_MODE_PROFILE, profileId)) { + return; + } + // int32_t profileId = 1; + ANS_LOGD("Currently in focus mode."); + std::string bundleName = GetClientBundleName(); + if (bundleName == MMS_BUNDLE_NAME || bundleName == CONTACTS_BUNDLE_NAME) { + ANS_LOGI("The bundle name is mms or contacts."); + return; + } + sptr profile = new (std::nothrow) NotificationDoNotDisturbProfile(); + int32_t userId = record->notification->GetUserId(); + if (NotificationPreferences::GetInstance().GetDonotDisturbProfile(profileId, userId, profile) != ERR_OK) { + ANS_LOGE("Get do not disturb profile failed."); + return; + } + if (profile == nullptr) { + ANS_LOGE("The do not disturb profile is nullptr."); + return; + } + std::vector trustlist = profile->GetProfileTrustlist(); + for (auto &trust : trustlist) { + if (bundleName == trust.GetBundleName()) { + ANS_LOGW("Do not disturb profile bundle name is in trust."); + return; + } + } + auto flags = record->request->GetFlags(); + if (flags == nullptr) { + ANS_LOGE("The flags is nullptr."); + return; + } + flags->SetSoundEnabled(NotificationConstant::FlagStatus::CLOSE); + record->notification->SetEnableSound(false); + flags->SetLockScreenVisblenessEnabled(false); + record->request->SetVisibleness(NotificationConstant::VisiblenessType::SECRET); + flags->SetBannerEnabled(false); + flags->SetLightScreenEnabled(false); + flags->SetVibrationEnabled(NotificationConstant::FlagStatus::CLOSE); + record->notification->SetEnableVibration(false); + flags->SetStatusIconEnabled(false); +} + ErrCode AdvancedNotificationService::UpdateSlotAuthInfo(const std::shared_ptr &record) { ErrCode result = ERR_OK; @@ -1804,6 +1869,7 @@ ErrCode AdvancedNotificationService::AddRecordToMemory( if (isSystemApp) { ChangeNotificationByControlFlags(record); + CheckDoNotDisturbProfile(record); } result = AssignToNotificationList(record); diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index 5844273db..c24a5086e 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -779,6 +779,64 @@ ErrCode AdvancedNotificationService::GetDoNotDisturbDate(sptr> &profiles) +{ + ANS_LOGD("Called."); + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + int32_t userId = SUBSCRIBE_USER_INIT; + if (!GetActiveUserId(userId)) { + ANS_LOGW("No active user found."); + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + ffrt::task_handle handler = + notificationSvrQueue_->submit_h(std::bind([copyUserId = userId, copyProfiles = profiles]() { + ANS_LOGD("The ffrt enter."); + NotificationPreferences::GetInstance().AddDoNotDisturbProfiles(copyUserId, copyProfiles); + })); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} + +ErrCode AdvancedNotificationService::RemoveDoNotDisturbProfiles( + const std::vector> &profiles) +{ + ANS_LOGD("Called."); + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + int32_t userId = SUBSCRIBE_USER_INIT; + if (!GetActiveUserId(userId)) { + ANS_LOGW("No active user found."); + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + ffrt::task_handle handler = + notificationSvrQueue_->submit_h(std::bind([copyUserId = userId, copyProfiles = profiles]() { + ANS_LOGD("The ffrt enter."); + NotificationPreferences::GetInstance().RemoveDoNotDisturbProfiles(copyUserId, copyProfiles); + })); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} + ErrCode AdvancedNotificationService::DoesSupportDoNotDisturbMode(bool &doesSupport) { ANS_LOGD("%{public}s", __FUNCTION__); diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 06a6fa6f3..d4c7bcde6 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -473,6 +473,53 @@ ErrCode NotificationPreferences::SetDoNotDisturbDate(const int32_t &userId, return result; } +bool NotificationPreferences::CheckDoNotDisturbProfileID(int32_t profileId) +{ + if (profileId < FOCUS_MODE_PROFILE_MIN_ID || profileId > FOCUS_MODE_PROFILE_MAX_ID) { + ANS_LOGE("The profile id is out of range."); + return false; + } + return true; +} + +ErrCode NotificationPreferences::AddDoNotDisturbProfiles( + int32_t userId, std::vector> profiles) +{ + ANS_LOGE("Called."); + for (auto profile : profiles) { + if (!CheckDoNotDisturbProfileID(profile->GetProfileId())) { + return ERR_ANS_INVALID_PARAM; + } + } + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + preferencesInfo.AddDoNotDisturbProfiles(userId, profiles); + if (!preferncesDB_->AddDoNotDisturbProfiles(userId, profiles)) { + return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; + } + preferencesInfo_ = preferencesInfo; + return ERR_OK; +} + +ErrCode NotificationPreferences::RemoveDoNotDisturbProfiles( + int32_t userId, const std::vector> profiles) +{ + ANS_LOGE("Called."); + for (auto profile : profiles) { + if (!CheckDoNotDisturbProfileID(profile->GetProfileId())) { + return ERR_ANS_INVALID_PARAM; + } + } + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + preferencesInfo.RemoveDoNotDisturbProfiles(userId, profiles); + if (!preferncesDB_->RemoveDoNotDisturbProfiles(userId, profiles)) { + return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; + } + preferencesInfo_ = preferencesInfo; + return ERR_OK; +} + ErrCode NotificationPreferences::GetAllNotificationEnabledBundles(std::vector &bundleOption) { ANS_LOGD("Called."); @@ -504,6 +551,20 @@ ErrCode NotificationPreferences::ClearNotificationInRestoreFactorySettings() return result; } +ErrCode NotificationPreferences::GetDonotDisturbProfile( + int32_t profileId, int32_t userId, sptr &profile) +{ + if (!CheckDoNotDisturbProfileID(profileId)) { + return ERR_ANS_INVALID_PARAM; + } + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + if (!preferencesInfo.GetDoNotDisturbProfiles(profileId, userId, profile)) { + return ERR_ANS_INVALID_PARAM; + } + return ERR_OK; +} + ErrCode NotificationPreferences::CheckSlotForCreateSlot(const sptr &bundleOption, const sptr &slot, NotificationPreferencesInfo &preferencesInfo) const { diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 5bcd4fcb7..bf50f6acc 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -42,6 +42,11 @@ const static std::string KEY_DO_NOT_DISTURB_BEGIN_DATE = "ans_doNotDisturbBeginD */ const static std::string KEY_DO_NOT_DISTURB_END_DATE = "ans_doNotDisturbEndDate"; +/** + * Indicates that disturbe key which do not disturbe id. + */ +const static std::string KEY_DO_NOT_DISTURB_ID = "ans_doNotDisturbId"; + /** * Indicates that disturbe key which enable all notification. */ @@ -575,6 +580,86 @@ bool NotificationPreferencesDatabase::PutDoNotDisturbDate( return true; } +bool NotificationPreferencesDatabase::AddDoNotDisturbProfiles( + int32_t userId, const std::vector> &profiles) +{ + if (profiles.empty()) { + ANS_LOGE("Invalid dates."); + return false; + } + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + std::unordered_map values; + for (auto profile : profiles) { + if (profile == nullptr) { + ANS_LOGE("The profile is null."); + return false; + } + std::string key = std::string().append(KEY_DO_NOT_DISTURB_ID).append(KEY_UNDER_LINE).append( + std::to_string(userId)).append(KEY_UNDER_LINE).append(std::to_string((int32_t)profile->GetProfileId())); + values[key] = profile->ToJson(); + } + int32_t result = rdbDataManager_->InsertBatchData(values); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Add do not disturb profiles failed."); + return false; + } + return true; +} + +bool NotificationPreferencesDatabase::RemoveDoNotDisturbProfiles( + int32_t userId, const std::vector> &profiles) +{ + if (profiles.empty()) { + ANS_LOGW("Invalid dates."); + return false; + } + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + std::vector keys; + for (auto profile : profiles) { + if (profile == nullptr) { + ANS_LOGE("The profile is null."); + return false; + } + std::string key = std::string().append(KEY_DO_NOT_DISTURB_ID).append(KEY_UNDER_LINE).append( + std::to_string(userId)).append(KEY_UNDER_LINE).append(std::to_string((int32_t)profile->GetProfileId())); + keys.push_back(key); + } + int32_t result = rdbDataManager_->DeleteBathchData(keys); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Delete do not disturb profiles failed."); + return false; + } + return true; +} + +bool NotificationPreferencesDatabase::GetDoNotDisturbProfiles( + const std::string &key, sptr &profile) +{ + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + std::string values; + int32_t result = rdbDataManager_->QueryData(key, values); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Use default value. error code is %{public}d", result); + return false; + } + profile = new (std::nothrow) NotificationDoNotDisturbProfile(); + if (profile == nullptr) { + ANS_LOGE("The profile is null."); + return false; + } + profile->FromJson(values); + return true; +} + void NotificationPreferencesDatabase::GetValueFromDisturbeDB( const std::string &key, std::function callback) { @@ -668,6 +753,7 @@ bool NotificationPreferencesDatabase::ParseFromDisturbeDB(NotificationPreference ParseDoNotDisturbBeginDate(info); ParseDoNotDisturbEndDate(info); ParseEnableAllNotification(info); + ParseGetDoNotDisturbProfile(info); if (!CheckRdbStore()) { ANS_LOGE("RdbStore is nullptr."); @@ -1211,6 +1297,16 @@ void NotificationPreferencesDatabase::ParseEnableAllNotification(NotificationPre } } +void NotificationPreferencesDatabase::ParseGetDoNotDisturbProfile(NotificationPreferencesInfo &info) +{ + std::vector activeUserId; + OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); + + for (auto iter : activeUserId) { + NotificationPreferencesDatabase::GetDoNotDisturbProfile(info, iter); + } +} + void NotificationPreferencesDatabase::ParseBundleName( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const { @@ -1491,6 +1587,31 @@ void NotificationPreferencesDatabase::GetEnableAllNotification(NotificationPrefe }); } +void NotificationPreferencesDatabase::GetDoNotDisturbProfile(NotificationPreferencesInfo &info, int32_t userId) +{ + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return; + } + std::unordered_map datas; + int32_t result = rdbDataManager_->QueryAllData(datas); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Query all data failed."); + return; + } + std::vector> profiles; + for (const auto &data : datas) { + std::string key = data.first; + auto result = key.find(KEY_DO_NOT_DISTURB_ID); + if (result != std::string::npos) { + sptr profile; + GetDoNotDisturbProfiles(data.first, profile); + profiles.emplace_back(profile); + } + } + info.AddDoNotDisturbProfiles(userId, profiles); +} + bool NotificationPreferencesDatabase::RemoveNotificationEnable(const int32_t userId) { ANS_LOGD("%{public}s", __FUNCTION__); diff --git a/services/ans/src/notification_preferences_info.cpp b/services/ans/src/notification_preferences_info.cpp index 60590e9e5..07996ccc1 100644 --- a/services/ans/src/notification_preferences_info.cpp +++ b/services/ans/src/notification_preferences_info.cpp @@ -17,6 +17,7 @@ namespace OHOS { namespace Notification { +const static std::string KEY_UNDER_LINE = "_"; NotificationPreferencesInfo::BundleInfo::BundleInfo() { } @@ -266,6 +267,42 @@ void NotificationPreferencesInfo::SetDoNotDisturbDate(const int32_t &userId, doNotDisturbDate_.insert_or_assign(userId, doNotDisturbDate); } +std::string NotificationPreferencesInfo::MakeDoNotDisturbProfileKey(int32_t userId, int32_t profileId) +{ + std::string key = std::to_string(userId).append(KEY_UNDER_LINE).append(std::to_string(profileId)); + return key; +} + +void NotificationPreferencesInfo::AddDoNotDisturbProfiles( + int32_t userId, const std::vector> &profiles) +{ + for (auto profile : profiles) { + std::string key = MakeDoNotDisturbProfileKey(userId, profile->GetProfileId()); + doNotDisturbProfiles_.insert_or_assign(key, profile); + } +} + +void NotificationPreferencesInfo::RemoveDoNotDisturbProfiles( + int32_t userId, const std::vector> &profiles) +{ + for (auto profile : profiles) { + std::string key = MakeDoNotDisturbProfileKey(userId, profile->GetProfileId()); + doNotDisturbProfiles_.erase(key); + } +} + +bool NotificationPreferencesInfo::GetDoNotDisturbProfiles( + int32_t profileId, int32_t userId, sptr &profile) +{ + std::string key = MakeDoNotDisturbProfileKey(userId, profileId); + auto iter = doNotDisturbProfiles_.find(key); + if (iter != doNotDisturbProfiles_.end()) { + profile = iter->second; + return true; + } + return false; +} + bool NotificationPreferencesInfo::GetDoNotDisturbDate(const int32_t &userId, sptr &doNotDisturbDate) const { -- Gitee From 570ae8ad09b1e628920b883b5cc5bc711c7fd7e9 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Mon, 18 Mar 2024 17:34:21 +0800 Subject: [PATCH 02/15] modify code Signed-off-by: xinking129 --- .../distributed_notification_service_ipc_interface_code.h | 2 ++ frameworks/core/src/ans_manager_stub.cpp | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h index 29b40ede8..8a3ca4863 100644 --- a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h +++ b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h @@ -136,6 +136,8 @@ namespace Notification { GET_DISTRIBUTED_ENABLED_BY_BUNDLE, SET_SMART_REMINDER_ENABLED, GET_SMART_REMINDER_ENABLED, + GET_SLOT_BY_BUNDLE, + CANCEL_AS_BUNDLE_WITH_AGENT, ADD_DO_NOTDISTURB_PROFILES, REMOVE_DO_NOT_DISTURB_PROFILES, }; diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index 44e3b51c1..3954b077f 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -304,6 +304,12 @@ const std::map Date: Mon, 18 Mar 2024 20:13:39 +0800 Subject: [PATCH 03/15] mofidy code Signed-off-by: xinking129 --- frameworks/core/include/ans_manager_stub.h | 1 + 1 file changed, 1 insertion(+) diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index 9f974afb1..c051099ea 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -968,6 +968,7 @@ private: ErrCode HandleIsDistributedEnabledByBundle(MessageParcel &data, MessageParcel &reply); ErrCode HandleSetSmartReminderEnabled(MessageParcel &data, MessageParcel &reply); ErrCode HandleIsSmartReminderEnabled(MessageParcel &data, MessageParcel &reply); + ErrCode HandleCancelAsBundleWithAgent(MessageParcel &data, MessageParcel &reply); ErrCode HandleAddDoNotDisturbProfiles(MessageParcel &data, MessageParcel &reply); ErrCode HandleRemoveDoNotDisturbProfiles(MessageParcel &data, MessageParcel &reply); template -- Gitee From 6902eaba6e7317f6b7ab935da93959681b6f6761 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Tue, 19 Mar 2024 09:36:37 +0800 Subject: [PATCH 04/15] modify tdd code Signed-off-by: xinking129 --- .../ans_notification_branch_test.cpp | 1696 +++++++++-------- 1 file changed, 853 insertions(+), 843 deletions(-) diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index 86aa99b45..9bbd530d3 100755 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -1,843 +1,853 @@ -/* - * Copyright (c) 2023-2024 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 - -#define private public -#define protected public -#include "ans_notification.h" -#include "ans_subscriber_proxy.h" -#include "ans_manager_interface.h" -#include "ans_manager_proxy.h" -#undef private -#undef protected -#include "ans_dialog_callback_interface.h" -#include "ans_inner_errors.h" -#include "ipc_types.h" -#include "notification.h" -#include "notification_request.h" -#include "singleton.h" -#include "notification_subscriber.h" - -extern void MockGetAnsManagerProxy(bool mockRet); - -using namespace testing; -using namespace testing::ext; -using namespace OHOS; -using namespace OHOS::Notification; - -namespace OHOS { -namespace Notification { -class MockAnsManagerInterface : public AnsManagerInterface { -public: - MockAnsManagerInterface() = default; - virtual ~MockAnsManagerInterface() - {}; - sptr AsObject() override - { - return nullptr; - } - - ErrCode Publish(const std::string &label, const sptr ¬ification) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode Cancel(int notificationId, const std::string &label) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelAll() override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelAsBundle( - int32_t notificationId, const std::string &representativeBundle, int32_t userId) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelAsBundle( - const sptr &bundleOption, int32_t notificationId) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelAsBundle( - const sptr &bundleOption, int32_t notificationId, int32_t userId) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode AddSlotByType(NotificationConstant::SlotType slotType) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode AddSlots(const std::vector> &slots) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveSlotByType(const NotificationConstant::SlotType &slotType) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveAllSlots() override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSlotByType(const NotificationConstant::SlotType &slotType, sptr &slot) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSlots(std::vector> &slots) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSlotNumAsBundle(const sptr &bundleOption, uint64_t &num) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetActiveNotifications(std::vector> ¬ifications) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetActiveNotificationNums(uint64_t &num) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetAllActiveNotifications(std::vector> ¬ifications) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSpecialActiveNotifications( - const std::vector &key, std::vector> ¬ifications) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetNotificationAgent(const std::string &agent) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetNotificationAgent(std::string &agent) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CanPublishAsBundle(const std::string &representativeBundle, bool &canPublish) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode PublishAsBundle( - const sptr notification, const std::string &representativeBundle) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetNotificationBadgeNum(int num) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetBundleImportance(int &importance) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode HasNotificationPolicyAccessPermission(bool &granted) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode Delete(const std::string &key, int32_t removeReason) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveNotification(const sptr &bundleOption, int notificationId, - const std::string &label, int32_t removeReason) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveAllNotifications(const sptr &bundleOption) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveNotifications(const std::vector &hashcodes, int32_t removeReason) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode DeleteByBundle(const sptr &bundleOption) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode DeleteAll() override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSlotsByBundle( - const sptr &bundleOption, std::vector> &slots) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSlotByBundle( - const sptr &bundleOption, const NotificationConstant::SlotType &slotType, - sptr &slot) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode UpdateSlots( - const sptr &bundleOption, const std::vector> &slots) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RequestEnableNotification(const std::string &deviceId, - const sptr &callback, - const sptr &callerToken) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetNotificationsEnabledForSpecialBundle( - const std::string &deviceId, const sptr &bundleOption, bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetShowBadgeEnabledForBundle(const sptr &bundleOption, bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetShowBadgeEnabledForBundle(const sptr &bundleOption, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetShowBadgeEnabled(bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode Subscribe(const sptr &subscriber, - const sptr &info) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SubscribeSelf(const sptr &subscriber) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetAllNotificationEnabledBundles(std::vector &bundleOption)override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SubscribeLocalLiveView(const sptr &subscriber, - const sptr &info) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode Unsubscribe( - const sptr &subscriber, const sptr &info) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsAllowedNotify(bool &allowed) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsAllowedNotifySelf(bool &allowed) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsSpecialBundleAllowedNotify(const sptr &bundleOption, bool &allowed) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetDoNotDisturbDate(const sptr &date) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetDoNotDisturbDate(sptr &date) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode DoesSupportDoNotDisturbMode(bool &doesSupport) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelGroup(const std::string &groupName) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveGroupByBundle( - const sptr &bundleOption, const std::string &groupName) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsDistributedEnabled(bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode EnableDistributed(bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode EnableDistributedByBundle(const sptr &bundleOption, bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode EnableDistributedSelf(bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsDistributedEnableByBundle(const sptr &bundleOption, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode PublishContinuousTaskNotification(const sptr &request) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode PublishReminder(sptr &reminder) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelReminder(const int32_t reminderId) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetValidReminders(std::vector> &reminders) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelAllReminders() override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetNotificationsEnabledByUser(const int32_t &deviceId, bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode DeleteAllByUser(const int32_t &userId) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr &date) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr &date) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetEnabledForBundleSlot(const sptr &bundleOption, - const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetEnabledForBundleSlot(const sptr &bundleOption, - const NotificationConstant::SlotType &slotType, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType &slotType, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, - std::vector &dumpInfo) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetBadgeNumber(int32_t badgeNumber) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetBadgeNumberByBundle(const sptr& bundleOption, int32_t badgeNumber) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSlotFlagsAsBundle(const sptr& bundleOption, uint32_t &slotFlags) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetSlotFlagsAsBundle(const sptr& bundleOption, uint32_t slotFlags) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RegisterPushCallback(const sptr &pushCallback, - const sptr ¬ificationCheckRequest) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode UnregisterPushCallback() override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetActiveNotificationByFilter(const sptr &bundleOption, - const int32_t notificationId, const std::string &label, std::vector extraInfoKeys, - sptr &request) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode TriggerLocalLiveView(const sptr &bundleOption, - const int32_t notificationId, const sptr &buttonOption) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetAdditionConfig(const std::string &key, const std::string &value) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetDistributedEnabledByBundle(const sptr &bundleOption, - const std::string &deviceType, const bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsDistributedEnabledByBundle(const sptr &bundleOption, - const std::string &deviceType, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsSmartReminderEnabled(const std::string &deviceType, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetSmartReminderEnabled(const std::string &deviceType, const bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelAsBundleWithAgent(const sptr &bundleOption, const int32_t id) - { - return ERR_ANS_INVALID_PARAM; - } -}; - -class AnsNotificationBranchTest : public testing::Test { -public: - AnsNotificationBranchTest() {} - - virtual ~AnsNotificationBranchTest() {} - - static void SetUpTestCase(); - - static void TearDownTestCase(); - - void SetUp(); -}; - -void AnsNotificationBranchTest::SetUpTestCase() {} - -void AnsNotificationBranchTest::TearDownTestCase() {} - -void AnsNotificationBranchTest::SetUp() {} - -/* - * @tc.name: RemoveNotifications_0100 - * @tc.desc: Test RemoveNotifications and hashcodes is empty - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, RemoveNotifications_0100, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - std::vector hashcodes; - int32_t removeReason = 1; - ErrCode ret = ansNotification->RemoveNotifications(hashcodes, removeReason); - EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); -} - -/* - * @tc.name: RemoveNotifications_0200 - * @tc.desc: 1.Test RemoveNotifications and hashcodes is not empty - * 2.GetAnsManagerProxy is false - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, RemoveNotifications_0200, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - std::string hashcode = "aa"; - std::vector hashcodes; - hashcodes.emplace_back(hashcode); - int32_t removeReason = 1; - MockGetAnsManagerProxy(false); - ErrCode ret = ansNotification->RemoveNotifications(hashcodes, removeReason); - EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); -} - -/* - * @tc.name: RemoveNotifications_0300 - * @tc.desc: 1.Test RemoveNotifications and hashcodes is not empty - * 2.GetAnsManagerProxy is true - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, RemoveNotifications_0300, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - std::string hashcode = "aa"; - std::vector hashcodes; - hashcodes.emplace_back(hashcode); - int32_t removeReason = 1; - MockGetAnsManagerProxy(true); - ansNotification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); - ansNotification->RemoveNotifications(hashcodes, removeReason); -} - -/* - * @tc.name: RegisterPushCallback_0100 - * @tc.desc: 1.Test RegisterPushCallback - * 2.GetAnsManagerProxy is false - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, RegisterPushCallback_0100, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - sptr pushCallback = nullptr; - MockGetAnsManagerProxy(false); - sptr checkRequest = new (std::nothrow) NotificationCheckRequest(); - ErrCode ret = ansNotification->RegisterPushCallback(pushCallback, checkRequest); - EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); -} - -/* - * @tc.name: RegisterPushCallback_0200 - * @tc.desc: 1.Test RegisterPushCallback - * 2.GetAnsManagerProxy is true - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, RegisterPushCallback_0200, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - sptr pushCallback = nullptr; - MockGetAnsManagerProxy(true); - ansNotification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); - sptr checkRequest = new (std::nothrow) NotificationCheckRequest(); - ansNotification->RegisterPushCallback(pushCallback, checkRequest); -} - -/* - * @tc.name: UnregisterPushCallback_0100 - * @tc.desc: 1.Test UnregisterPushCallback - * 2.GetAnsManagerProxy is false - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, UnregisterPushCallback_0100, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - MockGetAnsManagerProxy(false); - ErrCode ret = ansNotification->UnregisterPushCallback(); - EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); -} - -/* - * @tc.name: UnregisterPushCallback_0200 - * @tc.desc: 1.Test UnregisterPushCallback - * 2.GetAnsManagerProxy is true - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, UnregisterPushCallback_0200, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - MockGetAnsManagerProxy(true); - ansNotification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); - ansNotification->UnregisterPushCallback(); -} - -/* - * @tc.name: CanPublishLiveViewContent_0100 - * @tc.desc: CanPublishLiveViewContent - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0100, Function | MediumTest | Level1) -{ - NotificationRequest request; - auto notification = std::make_shared(); - EXPECT_TRUE(notification->CanPublishLiveViewContent(request)); -} - -/* - * @tc.name: CanPublishLiveViewContent_0110 - * @tc.desc: CanPublishLiveViewContent - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0110, Function | MediumTest | Level1) -{ - NotificationRequest request; - request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); - auto liveViewContent = std::make_shared(); - liveViewContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT); - auto content = std::make_shared(liveViewContent); - request.SetContent(content); - - auto notification = std::make_shared(); - EXPECT_FALSE(notification->CanPublishLiveViewContent(request)); -} - -/* - * @tc.name: CanPublishLiveViewContent_0120 - * @tc.desc: CanPublishLiveViewContent - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0120, Function | MediumTest | Level1) -{ - NotificationRequest request; - request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); - auto liveViewContent = std::make_shared(); - auto content = std::make_shared(liveViewContent); - request.SetContent(content); - - auto notification = std::make_shared(); - EXPECT_TRUE(notification->CanPublishLiveViewContent(request)); -} - -/* - * @tc.name: CanPublishLiveViewContent_0130 - * @tc.desc: CanPublishLiveViewContent - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0130, Function | MediumTest | Level1) -{ - NotificationRequest request; - request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); - auto liveViewContent = std::make_shared(); - liveViewContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_FULL_UPDATE); - auto content = std::make_shared(liveViewContent); - request.SetContent(content); - - auto notification = std::make_shared(); - EXPECT_TRUE(notification->CanPublishLiveViewContent(request)); -} - -/* - * @tc.name: CanPublishLiveViewContent_0140 - * @tc.desc: CanPublishLiveViewContent - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0140, Function | MediumTest | Level1) -{ - NotificationRequest request; - request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); - auto liveViewContent = std::make_shared(); - auto content = std::make_shared(liveViewContent); - request.SetContent(content); - request.notificationContent_ = nullptr; - - auto notification = std::make_shared(); - EXPECT_FALSE(notification->CanPublishLiveViewContent(request)); -} - -/* - * @tc.name: CanPublishLiveViewContent_0150 - * @tc.desc: CanPublishLiveViewContent - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0150, Function | MediumTest | Level1) -{ - NotificationRequest request; - request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); - auto liveViewContent = std::make_shared(); - auto content = std::make_shared(liveViewContent); - content->content_ = nullptr; - request.SetContent(content); - - auto notification = std::make_shared(); - EXPECT_FALSE(notification->CanPublishLiveViewContent(request)); -} - -/* - * @tc.name: SetNotificationSlotFlagsAsBundle_0001 - * @tc.desc: SetNotificationSlotFlagsAsBundle - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, SetNotificationSlotFlagsAsBundle_0001, Function | MediumTest | Level1) -{ - NotificationBundleOption bundle; - uint32_t slotFlags = 1; - auto notification = std::make_shared(); - ErrCode ret = notification->SetNotificationSlotFlagsAsBundle(bundle, slotFlags); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); - ret = notification->GetNotificationSlotFlagsAsBundle(bundle, slotFlags); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); - - notification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); - bundle.SetBundleName("test"); - bundle.SetUid(1); - ret = notification->SetNotificationSlotFlagsAsBundle(bundle, slotFlags); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); - ret = notification->GetNotificationSlotFlagsAsBundle(bundle, slotFlags); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); -} - -/* - * @tc.name: PublishNotification_0001 - * @tc.desc: PublishNotification - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, PublishNotification_0001, Function | MediumTest | Level1) -{ - auto notification = std::make_shared(); - notification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); - NotificationRequest req; - std::shared_ptr mediaContent = std::make_shared(); - auto content = std::make_shared(mediaContent); - content->content_ = nullptr; - req.SetContent(content); - - auto ret = notification->PublishNotification("label", req); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); - ret = notification->PublishNotificationAsBundle("label", req); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); - - auto liveViewContent = std::make_shared(); - auto content1 = std::make_shared(liveViewContent); - content1->content_ = nullptr; - req.SetContent(content1); - req.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); - ret = notification->PublishNotification("label", req); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); - ret = notification->PublishNotificationAsBundle("label", req); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); -} - -} // namespace Notification -} // namespace OHOS +/* + * Copyright (c) 2023-2024 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 + +#define private public +#define protected public +#include "ans_notification.h" +#include "ans_subscriber_proxy.h" +#include "ans_manager_interface.h" +#include "ans_manager_proxy.h" +#undef private +#undef protected +#include "ans_dialog_callback_interface.h" +#include "ans_inner_errors.h" +#include "ipc_types.h" +#include "notification.h" +#include "notification_request.h" +#include "singleton.h" +#include "notification_subscriber.h" + +extern void MockGetAnsManagerProxy(bool mockRet); + +using namespace testing; +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Notification; + +namespace OHOS { +namespace Notification { +class MockAnsManagerInterface : public AnsManagerInterface { +public: + MockAnsManagerInterface() = default; + virtual ~MockAnsManagerInterface() + {}; + sptr AsObject() override + { + return nullptr; + } + + ErrCode Publish(const std::string &label, const sptr ¬ification) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode Cancel(int notificationId, const std::string &label) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelAll() override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelAsBundle( + int32_t notificationId, const std::string &representativeBundle, int32_t userId) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelAsBundle( + const sptr &bundleOption, int32_t notificationId) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelAsBundle( + const sptr &bundleOption, int32_t notificationId, int32_t userId) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode AddSlotByType(NotificationConstant::SlotType slotType) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode AddSlots(const std::vector> &slots) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveSlotByType(const NotificationConstant::SlotType &slotType) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveAllSlots() override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSlotByType(const NotificationConstant::SlotType &slotType, sptr &slot) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSlots(std::vector> &slots) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSlotNumAsBundle(const sptr &bundleOption, uint64_t &num) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetActiveNotifications(std::vector> ¬ifications) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetActiveNotificationNums(uint64_t &num) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetAllActiveNotifications(std::vector> ¬ifications) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSpecialActiveNotifications( + const std::vector &key, std::vector> ¬ifications) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetNotificationAgent(const std::string &agent) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetNotificationAgent(std::string &agent) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CanPublishAsBundle(const std::string &representativeBundle, bool &canPublish) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode PublishAsBundle( + const sptr notification, const std::string &representativeBundle) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetNotificationBadgeNum(int num) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetBundleImportance(int &importance) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode HasNotificationPolicyAccessPermission(bool &granted) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode Delete(const std::string &key, int32_t removeReason) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveNotification(const sptr &bundleOption, int notificationId, + const std::string &label, int32_t removeReason) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveAllNotifications(const sptr &bundleOption) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveNotifications(const std::vector &hashcodes, int32_t removeReason) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode DeleteByBundle(const sptr &bundleOption) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode DeleteAll() override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSlotsByBundle( + const sptr &bundleOption, std::vector> &slots) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSlotByBundle( + const sptr &bundleOption, const NotificationConstant::SlotType &slotType, + sptr &slot) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode UpdateSlots( + const sptr &bundleOption, const std::vector> &slots) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RequestEnableNotification(const std::string &deviceId, + const sptr &callback, + const sptr &callerToken) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetNotificationsEnabledForSpecialBundle( + const std::string &deviceId, const sptr &bundleOption, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetShowBadgeEnabledForBundle(const sptr &bundleOption, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetShowBadgeEnabledForBundle(const sptr &bundleOption, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetShowBadgeEnabled(bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode Subscribe(const sptr &subscriber, + const sptr &info) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SubscribeSelf(const sptr &subscriber) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetAllNotificationEnabledBundles(std::vector &bundleOption)override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SubscribeLocalLiveView(const sptr &subscriber, + const sptr &info) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode Unsubscribe( + const sptr &subscriber, const sptr &info) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsAllowedNotify(bool &allowed) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsAllowedNotifySelf(bool &allowed) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsSpecialBundleAllowedNotify(const sptr &bundleOption, bool &allowed) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetDoNotDisturbDate(const sptr &date) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetDoNotDisturbDate(sptr &date) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode AddDoNotDisturbProfiles(const std::vector> &profiles) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveDoNotDisturbProfiles(const std::vector> &profiles) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode DoesSupportDoNotDisturbMode(bool &doesSupport) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelGroup(const std::string &groupName) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveGroupByBundle( + const sptr &bundleOption, const std::string &groupName) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsDistributedEnabled(bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode EnableDistributed(bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode EnableDistributedByBundle(const sptr &bundleOption, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode EnableDistributedSelf(bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsDistributedEnableByBundle(const sptr &bundleOption, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode PublishContinuousTaskNotification(const sptr &request) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode PublishReminder(sptr &reminder) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelReminder(const int32_t reminderId) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetValidReminders(std::vector> &reminders) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelAllReminders() override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetNotificationsEnabledByUser(const int32_t &deviceId, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode DeleteAllByUser(const int32_t &userId) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr &date) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr &date) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetEnabledForBundleSlot(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetEnabledForBundleSlot(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType &slotType, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, + std::vector &dumpInfo) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetBadgeNumber(int32_t badgeNumber) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetBadgeNumberByBundle(const sptr& bundleOption, int32_t badgeNumber) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSlotFlagsAsBundle(const sptr& bundleOption, uint32_t &slotFlags) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetSlotFlagsAsBundle(const sptr& bundleOption, uint32_t slotFlags) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RegisterPushCallback(const sptr &pushCallback, + const sptr ¬ificationCheckRequest) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode UnregisterPushCallback() override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetActiveNotificationByFilter(const sptr &bundleOption, + const int32_t notificationId, const std::string &label, std::vector extraInfoKeys, + sptr &request) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode TriggerLocalLiveView(const sptr &bundleOption, + const int32_t notificationId, const sptr &buttonOption) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetAdditionConfig(const std::string &key, const std::string &value) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetDistributedEnabledByBundle(const sptr &bundleOption, + const std::string &deviceType, const bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsDistributedEnabledByBundle(const sptr &bundleOption, + const std::string &deviceType, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsSmartReminderEnabled(const std::string &deviceType, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetSmartReminderEnabled(const std::string &deviceType, const bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelAsBundleWithAgent(const sptr &bundleOption, const int32_t id) + { + return ERR_ANS_INVALID_PARAM; + } +}; + +class AnsNotificationBranchTest : public testing::Test { +public: + AnsNotificationBranchTest() {} + + virtual ~AnsNotificationBranchTest() {} + + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); +}; + +void AnsNotificationBranchTest::SetUpTestCase() {} + +void AnsNotificationBranchTest::TearDownTestCase() {} + +void AnsNotificationBranchTest::SetUp() {} + +/* + * @tc.name: RemoveNotifications_0100 + * @tc.desc: Test RemoveNotifications and hashcodes is empty + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, RemoveNotifications_0100, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + std::vector hashcodes; + int32_t removeReason = 1; + ErrCode ret = ansNotification->RemoveNotifications(hashcodes, removeReason); + EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: RemoveNotifications_0200 + * @tc.desc: 1.Test RemoveNotifications and hashcodes is not empty + * 2.GetAnsManagerProxy is false + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, RemoveNotifications_0200, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + std::string hashcode = "aa"; + std::vector hashcodes; + hashcodes.emplace_back(hashcode); + int32_t removeReason = 1; + MockGetAnsManagerProxy(false); + ErrCode ret = ansNotification->RemoveNotifications(hashcodes, removeReason); + EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); +} + +/* + * @tc.name: RemoveNotifications_0300 + * @tc.desc: 1.Test RemoveNotifications and hashcodes is not empty + * 2.GetAnsManagerProxy is true + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, RemoveNotifications_0300, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + std::string hashcode = "aa"; + std::vector hashcodes; + hashcodes.emplace_back(hashcode); + int32_t removeReason = 1; + MockGetAnsManagerProxy(true); + ansNotification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); + ansNotification->RemoveNotifications(hashcodes, removeReason); +} + +/* + * @tc.name: RegisterPushCallback_0100 + * @tc.desc: 1.Test RegisterPushCallback + * 2.GetAnsManagerProxy is false + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, RegisterPushCallback_0100, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + sptr pushCallback = nullptr; + MockGetAnsManagerProxy(false); + sptr checkRequest = new (std::nothrow) NotificationCheckRequest(); + ErrCode ret = ansNotification->RegisterPushCallback(pushCallback, checkRequest); + EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); +} + +/* + * @tc.name: RegisterPushCallback_0200 + * @tc.desc: 1.Test RegisterPushCallback + * 2.GetAnsManagerProxy is true + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, RegisterPushCallback_0200, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + sptr pushCallback = nullptr; + MockGetAnsManagerProxy(true); + ansNotification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); + sptr checkRequest = new (std::nothrow) NotificationCheckRequest(); + ansNotification->RegisterPushCallback(pushCallback, checkRequest); +} + +/* + * @tc.name: UnregisterPushCallback_0100 + * @tc.desc: 1.Test UnregisterPushCallback + * 2.GetAnsManagerProxy is false + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, UnregisterPushCallback_0100, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + MockGetAnsManagerProxy(false); + ErrCode ret = ansNotification->UnregisterPushCallback(); + EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); +} + +/* + * @tc.name: UnregisterPushCallback_0200 + * @tc.desc: 1.Test UnregisterPushCallback + * 2.GetAnsManagerProxy is true + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, UnregisterPushCallback_0200, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + MockGetAnsManagerProxy(true); + ansNotification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); + ansNotification->UnregisterPushCallback(); +} + +/* + * @tc.name: CanPublishLiveViewContent_0100 + * @tc.desc: CanPublishLiveViewContent + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0100, Function | MediumTest | Level1) +{ + NotificationRequest request; + auto notification = std::make_shared(); + EXPECT_TRUE(notification->CanPublishLiveViewContent(request)); +} + +/* + * @tc.name: CanPublishLiveViewContent_0110 + * @tc.desc: CanPublishLiveViewContent + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0110, Function | MediumTest | Level1) +{ + NotificationRequest request; + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + liveViewContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT); + auto content = std::make_shared(liveViewContent); + request.SetContent(content); + + auto notification = std::make_shared(); + EXPECT_FALSE(notification->CanPublishLiveViewContent(request)); +} + +/* + * @tc.name: CanPublishLiveViewContent_0120 + * @tc.desc: CanPublishLiveViewContent + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0120, Function | MediumTest | Level1) +{ + NotificationRequest request; + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + auto content = std::make_shared(liveViewContent); + request.SetContent(content); + + auto notification = std::make_shared(); + EXPECT_TRUE(notification->CanPublishLiveViewContent(request)); +} + +/* + * @tc.name: CanPublishLiveViewContent_0130 + * @tc.desc: CanPublishLiveViewContent + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0130, Function | MediumTest | Level1) +{ + NotificationRequest request; + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + liveViewContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_FULL_UPDATE); + auto content = std::make_shared(liveViewContent); + request.SetContent(content); + + auto notification = std::make_shared(); + EXPECT_TRUE(notification->CanPublishLiveViewContent(request)); +} + +/* + * @tc.name: CanPublishLiveViewContent_0140 + * @tc.desc: CanPublishLiveViewContent + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0140, Function | MediumTest | Level1) +{ + NotificationRequest request; + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + auto content = std::make_shared(liveViewContent); + request.SetContent(content); + request.notificationContent_ = nullptr; + + auto notification = std::make_shared(); + EXPECT_FALSE(notification->CanPublishLiveViewContent(request)); +} + +/* + * @tc.name: CanPublishLiveViewContent_0150 + * @tc.desc: CanPublishLiveViewContent + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0150, Function | MediumTest | Level1) +{ + NotificationRequest request; + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + auto content = std::make_shared(liveViewContent); + content->content_ = nullptr; + request.SetContent(content); + + auto notification = std::make_shared(); + EXPECT_FALSE(notification->CanPublishLiveViewContent(request)); +} + +/* + * @tc.name: SetNotificationSlotFlagsAsBundle_0001 + * @tc.desc: SetNotificationSlotFlagsAsBundle + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, SetNotificationSlotFlagsAsBundle_0001, Function | MediumTest | Level1) +{ + NotificationBundleOption bundle; + uint32_t slotFlags = 1; + auto notification = std::make_shared(); + ErrCode ret = notification->SetNotificationSlotFlagsAsBundle(bundle, slotFlags); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); + ret = notification->GetNotificationSlotFlagsAsBundle(bundle, slotFlags); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); + + notification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); + bundle.SetBundleName("test"); + bundle.SetUid(1); + ret = notification->SetNotificationSlotFlagsAsBundle(bundle, slotFlags); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); + ret = notification->GetNotificationSlotFlagsAsBundle(bundle, slotFlags); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: PublishNotification_0001 + * @tc.desc: PublishNotification + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, PublishNotification_0001, Function | MediumTest | Level1) +{ + auto notification = std::make_shared(); + notification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); + NotificationRequest req; + std::shared_ptr mediaContent = std::make_shared(); + auto content = std::make_shared(mediaContent); + content->content_ = nullptr; + req.SetContent(content); + + auto ret = notification->PublishNotification("label", req); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); + ret = notification->PublishNotificationAsBundle("label", req); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); + + auto liveViewContent = std::make_shared(); + auto content1 = std::make_shared(liveViewContent); + content1->content_ = nullptr; + req.SetContent(content1); + req.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + ret = notification->PublishNotification("label", req); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); + ret = notification->PublishNotificationAsBundle("label", req); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); +} + +} // namespace Notification +} // namespace OHOS -- Gitee From 9386f8419b2789e9bca86f6526d2608e42a071d7 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Tue, 19 Mar 2024 09:50:55 +0800 Subject: [PATCH 05/15] modify mock tdd code Signed-off-by: xinking129 --- .../ans_notification_branch_test.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index 86aa99b45..30cc6e066 100755 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -316,6 +316,16 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode AddDoNotDisturbProfiles(const std::vector> &profiles) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveDoNotDisturbProfiles(const std::vector> &profiles) override + { + return ERR_ANS_INVALID_PARAM; + } + ErrCode DoesSupportDoNotDisturbMode(bool &doesSupport) override { return ERR_ANS_INVALID_PARAM; -- Gitee From e7a43bc3c84a0be4ab73e2b8049a09dceb6289a9 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Tue, 19 Mar 2024 10:13:06 +0800 Subject: [PATCH 06/15] modify mock tdd code Signed-off-by: xinking129 --- .../ans_notification_branch_test.cpp | 1706 ++++++++--------- 1 file changed, 853 insertions(+), 853 deletions(-) diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index 9bbd530d3..30cc6e066 100755 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -1,853 +1,853 @@ -/* - * Copyright (c) 2023-2024 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 - -#define private public -#define protected public -#include "ans_notification.h" -#include "ans_subscriber_proxy.h" -#include "ans_manager_interface.h" -#include "ans_manager_proxy.h" -#undef private -#undef protected -#include "ans_dialog_callback_interface.h" -#include "ans_inner_errors.h" -#include "ipc_types.h" -#include "notification.h" -#include "notification_request.h" -#include "singleton.h" -#include "notification_subscriber.h" - -extern void MockGetAnsManagerProxy(bool mockRet); - -using namespace testing; -using namespace testing::ext; -using namespace OHOS; -using namespace OHOS::Notification; - -namespace OHOS { -namespace Notification { -class MockAnsManagerInterface : public AnsManagerInterface { -public: - MockAnsManagerInterface() = default; - virtual ~MockAnsManagerInterface() - {}; - sptr AsObject() override - { - return nullptr; - } - - ErrCode Publish(const std::string &label, const sptr ¬ification) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode Cancel(int notificationId, const std::string &label) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelAll() override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelAsBundle( - int32_t notificationId, const std::string &representativeBundle, int32_t userId) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelAsBundle( - const sptr &bundleOption, int32_t notificationId) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelAsBundle( - const sptr &bundleOption, int32_t notificationId, int32_t userId) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode AddSlotByType(NotificationConstant::SlotType slotType) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode AddSlots(const std::vector> &slots) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveSlotByType(const NotificationConstant::SlotType &slotType) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveAllSlots() override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSlotByType(const NotificationConstant::SlotType &slotType, sptr &slot) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSlots(std::vector> &slots) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSlotNumAsBundle(const sptr &bundleOption, uint64_t &num) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetActiveNotifications(std::vector> ¬ifications) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetActiveNotificationNums(uint64_t &num) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetAllActiveNotifications(std::vector> ¬ifications) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSpecialActiveNotifications( - const std::vector &key, std::vector> ¬ifications) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetNotificationAgent(const std::string &agent) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetNotificationAgent(std::string &agent) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CanPublishAsBundle(const std::string &representativeBundle, bool &canPublish) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode PublishAsBundle( - const sptr notification, const std::string &representativeBundle) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetNotificationBadgeNum(int num) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetBundleImportance(int &importance) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode HasNotificationPolicyAccessPermission(bool &granted) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode Delete(const std::string &key, int32_t removeReason) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveNotification(const sptr &bundleOption, int notificationId, - const std::string &label, int32_t removeReason) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveAllNotifications(const sptr &bundleOption) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveNotifications(const std::vector &hashcodes, int32_t removeReason) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode DeleteByBundle(const sptr &bundleOption) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode DeleteAll() override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSlotsByBundle( - const sptr &bundleOption, std::vector> &slots) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSlotByBundle( - const sptr &bundleOption, const NotificationConstant::SlotType &slotType, - sptr &slot) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode UpdateSlots( - const sptr &bundleOption, const std::vector> &slots) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RequestEnableNotification(const std::string &deviceId, - const sptr &callback, - const sptr &callerToken) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetNotificationsEnabledForSpecialBundle( - const std::string &deviceId, const sptr &bundleOption, bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetShowBadgeEnabledForBundle(const sptr &bundleOption, bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetShowBadgeEnabledForBundle(const sptr &bundleOption, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetShowBadgeEnabled(bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode Subscribe(const sptr &subscriber, - const sptr &info) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SubscribeSelf(const sptr &subscriber) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetAllNotificationEnabledBundles(std::vector &bundleOption)override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SubscribeLocalLiveView(const sptr &subscriber, - const sptr &info) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode Unsubscribe( - const sptr &subscriber, const sptr &info) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsAllowedNotify(bool &allowed) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsAllowedNotifySelf(bool &allowed) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsSpecialBundleAllowedNotify(const sptr &bundleOption, bool &allowed) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetDoNotDisturbDate(const sptr &date) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetDoNotDisturbDate(sptr &date) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode AddDoNotDisturbProfiles(const std::vector> &profiles) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveDoNotDisturbProfiles(const std::vector> &profiles) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode DoesSupportDoNotDisturbMode(bool &doesSupport) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelGroup(const std::string &groupName) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RemoveGroupByBundle( - const sptr &bundleOption, const std::string &groupName) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsDistributedEnabled(bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode EnableDistributed(bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode EnableDistributedByBundle(const sptr &bundleOption, bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode EnableDistributedSelf(bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsDistributedEnableByBundle(const sptr &bundleOption, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode PublishContinuousTaskNotification(const sptr &request) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode PublishReminder(sptr &reminder) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelReminder(const int32_t reminderId) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetValidReminders(std::vector> &reminders) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelAllReminders() override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetNotificationsEnabledByUser(const int32_t &deviceId, bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode DeleteAllByUser(const int32_t &userId) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr &date) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr &date) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetEnabledForBundleSlot(const sptr &bundleOption, - const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetEnabledForBundleSlot(const sptr &bundleOption, - const NotificationConstant::SlotType &slotType, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType &slotType, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, - std::vector &dumpInfo) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetBadgeNumber(int32_t badgeNumber) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetBadgeNumberByBundle(const sptr& bundleOption, int32_t badgeNumber) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetSlotFlagsAsBundle(const sptr& bundleOption, uint32_t &slotFlags) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetSlotFlagsAsBundle(const sptr& bundleOption, uint32_t slotFlags) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode RegisterPushCallback(const sptr &pushCallback, - const sptr ¬ificationCheckRequest) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode UnregisterPushCallback() override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode GetActiveNotificationByFilter(const sptr &bundleOption, - const int32_t notificationId, const std::string &label, std::vector extraInfoKeys, - sptr &request) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode TriggerLocalLiveView(const sptr &bundleOption, - const int32_t notificationId, const sptr &buttonOption) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetAdditionConfig(const std::string &key, const std::string &value) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetDistributedEnabledByBundle(const sptr &bundleOption, - const std::string &deviceType, const bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsDistributedEnabledByBundle(const sptr &bundleOption, - const std::string &deviceType, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode IsSmartReminderEnabled(const std::string &deviceType, bool &enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode SetSmartReminderEnabled(const std::string &deviceType, const bool enabled) override - { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode CancelAsBundleWithAgent(const sptr &bundleOption, const int32_t id) - { - return ERR_ANS_INVALID_PARAM; - } -}; - -class AnsNotificationBranchTest : public testing::Test { -public: - AnsNotificationBranchTest() {} - - virtual ~AnsNotificationBranchTest() {} - - static void SetUpTestCase(); - - static void TearDownTestCase(); - - void SetUp(); -}; - -void AnsNotificationBranchTest::SetUpTestCase() {} - -void AnsNotificationBranchTest::TearDownTestCase() {} - -void AnsNotificationBranchTest::SetUp() {} - -/* - * @tc.name: RemoveNotifications_0100 - * @tc.desc: Test RemoveNotifications and hashcodes is empty - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, RemoveNotifications_0100, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - std::vector hashcodes; - int32_t removeReason = 1; - ErrCode ret = ansNotification->RemoveNotifications(hashcodes, removeReason); - EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); -} - -/* - * @tc.name: RemoveNotifications_0200 - * @tc.desc: 1.Test RemoveNotifications and hashcodes is not empty - * 2.GetAnsManagerProxy is false - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, RemoveNotifications_0200, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - std::string hashcode = "aa"; - std::vector hashcodes; - hashcodes.emplace_back(hashcode); - int32_t removeReason = 1; - MockGetAnsManagerProxy(false); - ErrCode ret = ansNotification->RemoveNotifications(hashcodes, removeReason); - EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); -} - -/* - * @tc.name: RemoveNotifications_0300 - * @tc.desc: 1.Test RemoveNotifications and hashcodes is not empty - * 2.GetAnsManagerProxy is true - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, RemoveNotifications_0300, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - std::string hashcode = "aa"; - std::vector hashcodes; - hashcodes.emplace_back(hashcode); - int32_t removeReason = 1; - MockGetAnsManagerProxy(true); - ansNotification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); - ansNotification->RemoveNotifications(hashcodes, removeReason); -} - -/* - * @tc.name: RegisterPushCallback_0100 - * @tc.desc: 1.Test RegisterPushCallback - * 2.GetAnsManagerProxy is false - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, RegisterPushCallback_0100, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - sptr pushCallback = nullptr; - MockGetAnsManagerProxy(false); - sptr checkRequest = new (std::nothrow) NotificationCheckRequest(); - ErrCode ret = ansNotification->RegisterPushCallback(pushCallback, checkRequest); - EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); -} - -/* - * @tc.name: RegisterPushCallback_0200 - * @tc.desc: 1.Test RegisterPushCallback - * 2.GetAnsManagerProxy is true - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, RegisterPushCallback_0200, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - sptr pushCallback = nullptr; - MockGetAnsManagerProxy(true); - ansNotification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); - sptr checkRequest = new (std::nothrow) NotificationCheckRequest(); - ansNotification->RegisterPushCallback(pushCallback, checkRequest); -} - -/* - * @tc.name: UnregisterPushCallback_0100 - * @tc.desc: 1.Test UnregisterPushCallback - * 2.GetAnsManagerProxy is false - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, UnregisterPushCallback_0100, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - MockGetAnsManagerProxy(false); - ErrCode ret = ansNotification->UnregisterPushCallback(); - EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); -} - -/* - * @tc.name: UnregisterPushCallback_0200 - * @tc.desc: 1.Test UnregisterPushCallback - * 2.GetAnsManagerProxy is true - * @tc.type: FUNC - * @tc.require: #I62SME - */ -HWTEST_F(AnsNotificationBranchTest, UnregisterPushCallback_0200, Function | MediumTest | Level1) -{ - auto ansNotification = std::make_shared(); - EXPECT_NE(ansNotification, nullptr); - MockGetAnsManagerProxy(true); - ansNotification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); - ansNotification->UnregisterPushCallback(); -} - -/* - * @tc.name: CanPublishLiveViewContent_0100 - * @tc.desc: CanPublishLiveViewContent - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0100, Function | MediumTest | Level1) -{ - NotificationRequest request; - auto notification = std::make_shared(); - EXPECT_TRUE(notification->CanPublishLiveViewContent(request)); -} - -/* - * @tc.name: CanPublishLiveViewContent_0110 - * @tc.desc: CanPublishLiveViewContent - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0110, Function | MediumTest | Level1) -{ - NotificationRequest request; - request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); - auto liveViewContent = std::make_shared(); - liveViewContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT); - auto content = std::make_shared(liveViewContent); - request.SetContent(content); - - auto notification = std::make_shared(); - EXPECT_FALSE(notification->CanPublishLiveViewContent(request)); -} - -/* - * @tc.name: CanPublishLiveViewContent_0120 - * @tc.desc: CanPublishLiveViewContent - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0120, Function | MediumTest | Level1) -{ - NotificationRequest request; - request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); - auto liveViewContent = std::make_shared(); - auto content = std::make_shared(liveViewContent); - request.SetContent(content); - - auto notification = std::make_shared(); - EXPECT_TRUE(notification->CanPublishLiveViewContent(request)); -} - -/* - * @tc.name: CanPublishLiveViewContent_0130 - * @tc.desc: CanPublishLiveViewContent - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0130, Function | MediumTest | Level1) -{ - NotificationRequest request; - request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); - auto liveViewContent = std::make_shared(); - liveViewContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_FULL_UPDATE); - auto content = std::make_shared(liveViewContent); - request.SetContent(content); - - auto notification = std::make_shared(); - EXPECT_TRUE(notification->CanPublishLiveViewContent(request)); -} - -/* - * @tc.name: CanPublishLiveViewContent_0140 - * @tc.desc: CanPublishLiveViewContent - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0140, Function | MediumTest | Level1) -{ - NotificationRequest request; - request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); - auto liveViewContent = std::make_shared(); - auto content = std::make_shared(liveViewContent); - request.SetContent(content); - request.notificationContent_ = nullptr; - - auto notification = std::make_shared(); - EXPECT_FALSE(notification->CanPublishLiveViewContent(request)); -} - -/* - * @tc.name: CanPublishLiveViewContent_0150 - * @tc.desc: CanPublishLiveViewContent - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0150, Function | MediumTest | Level1) -{ - NotificationRequest request; - request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); - auto liveViewContent = std::make_shared(); - auto content = std::make_shared(liveViewContent); - content->content_ = nullptr; - request.SetContent(content); - - auto notification = std::make_shared(); - EXPECT_FALSE(notification->CanPublishLiveViewContent(request)); -} - -/* - * @tc.name: SetNotificationSlotFlagsAsBundle_0001 - * @tc.desc: SetNotificationSlotFlagsAsBundle - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, SetNotificationSlotFlagsAsBundle_0001, Function | MediumTest | Level1) -{ - NotificationBundleOption bundle; - uint32_t slotFlags = 1; - auto notification = std::make_shared(); - ErrCode ret = notification->SetNotificationSlotFlagsAsBundle(bundle, slotFlags); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); - ret = notification->GetNotificationSlotFlagsAsBundle(bundle, slotFlags); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); - - notification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); - bundle.SetBundleName("test"); - bundle.SetUid(1); - ret = notification->SetNotificationSlotFlagsAsBundle(bundle, slotFlags); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); - ret = notification->GetNotificationSlotFlagsAsBundle(bundle, slotFlags); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); -} - -/* - * @tc.name: PublishNotification_0001 - * @tc.desc: PublishNotification - * @tc.type: FUNC - * @tc.require: issule - */ -HWTEST_F(AnsNotificationBranchTest, PublishNotification_0001, Function | MediumTest | Level1) -{ - auto notification = std::make_shared(); - notification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); - NotificationRequest req; - std::shared_ptr mediaContent = std::make_shared(); - auto content = std::make_shared(mediaContent); - content->content_ = nullptr; - req.SetContent(content); - - auto ret = notification->PublishNotification("label", req); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); - ret = notification->PublishNotificationAsBundle("label", req); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); - - auto liveViewContent = std::make_shared(); - auto content1 = std::make_shared(liveViewContent); - content1->content_ = nullptr; - req.SetContent(content1); - req.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); - ret = notification->PublishNotification("label", req); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); - ret = notification->PublishNotificationAsBundle("label", req); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); -} - -} // namespace Notification -} // namespace OHOS +/* + * Copyright (c) 2023-2024 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 + +#define private public +#define protected public +#include "ans_notification.h" +#include "ans_subscriber_proxy.h" +#include "ans_manager_interface.h" +#include "ans_manager_proxy.h" +#undef private +#undef protected +#include "ans_dialog_callback_interface.h" +#include "ans_inner_errors.h" +#include "ipc_types.h" +#include "notification.h" +#include "notification_request.h" +#include "singleton.h" +#include "notification_subscriber.h" + +extern void MockGetAnsManagerProxy(bool mockRet); + +using namespace testing; +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Notification; + +namespace OHOS { +namespace Notification { +class MockAnsManagerInterface : public AnsManagerInterface { +public: + MockAnsManagerInterface() = default; + virtual ~MockAnsManagerInterface() + {}; + sptr AsObject() override + { + return nullptr; + } + + ErrCode Publish(const std::string &label, const sptr ¬ification) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode Cancel(int notificationId, const std::string &label) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelAll() override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelAsBundle( + int32_t notificationId, const std::string &representativeBundle, int32_t userId) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelAsBundle( + const sptr &bundleOption, int32_t notificationId) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelAsBundle( + const sptr &bundleOption, int32_t notificationId, int32_t userId) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode AddSlotByType(NotificationConstant::SlotType slotType) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode AddSlots(const std::vector> &slots) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveSlotByType(const NotificationConstant::SlotType &slotType) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveAllSlots() override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSlotByType(const NotificationConstant::SlotType &slotType, sptr &slot) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSlots(std::vector> &slots) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSlotNumAsBundle(const sptr &bundleOption, uint64_t &num) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetActiveNotifications(std::vector> ¬ifications) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetActiveNotificationNums(uint64_t &num) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetAllActiveNotifications(std::vector> ¬ifications) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSpecialActiveNotifications( + const std::vector &key, std::vector> ¬ifications) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetNotificationAgent(const std::string &agent) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetNotificationAgent(std::string &agent) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CanPublishAsBundle(const std::string &representativeBundle, bool &canPublish) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode PublishAsBundle( + const sptr notification, const std::string &representativeBundle) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetNotificationBadgeNum(int num) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetBundleImportance(int &importance) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode HasNotificationPolicyAccessPermission(bool &granted) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode Delete(const std::string &key, int32_t removeReason) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveNotification(const sptr &bundleOption, int notificationId, + const std::string &label, int32_t removeReason) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveAllNotifications(const sptr &bundleOption) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveNotifications(const std::vector &hashcodes, int32_t removeReason) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode DeleteByBundle(const sptr &bundleOption) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode DeleteAll() override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSlotsByBundle( + const sptr &bundleOption, std::vector> &slots) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSlotByBundle( + const sptr &bundleOption, const NotificationConstant::SlotType &slotType, + sptr &slot) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode UpdateSlots( + const sptr &bundleOption, const std::vector> &slots) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RequestEnableNotification(const std::string &deviceId, + const sptr &callback, + const sptr &callerToken) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetNotificationsEnabledForSpecialBundle( + const std::string &deviceId, const sptr &bundleOption, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetShowBadgeEnabledForBundle(const sptr &bundleOption, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetShowBadgeEnabledForBundle(const sptr &bundleOption, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetShowBadgeEnabled(bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode Subscribe(const sptr &subscriber, + const sptr &info) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SubscribeSelf(const sptr &subscriber) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetAllNotificationEnabledBundles(std::vector &bundleOption)override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SubscribeLocalLiveView(const sptr &subscriber, + const sptr &info) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode Unsubscribe( + const sptr &subscriber, const sptr &info) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsAllowedNotify(bool &allowed) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsAllowedNotifySelf(bool &allowed) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsSpecialBundleAllowedNotify(const sptr &bundleOption, bool &allowed) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetDoNotDisturbDate(const sptr &date) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetDoNotDisturbDate(sptr &date) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode AddDoNotDisturbProfiles(const std::vector> &profiles) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveDoNotDisturbProfiles(const std::vector> &profiles) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode DoesSupportDoNotDisturbMode(bool &doesSupport) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelGroup(const std::string &groupName) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RemoveGroupByBundle( + const sptr &bundleOption, const std::string &groupName) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsDistributedEnabled(bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode EnableDistributed(bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode EnableDistributedByBundle(const sptr &bundleOption, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode EnableDistributedSelf(bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsDistributedEnableByBundle(const sptr &bundleOption, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode PublishContinuousTaskNotification(const sptr &request) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode PublishReminder(sptr &reminder) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelReminder(const int32_t reminderId) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetValidReminders(std::vector> &reminders) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelAllReminders() override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetNotificationsEnabledByUser(const int32_t &deviceId, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode DeleteAllByUser(const int32_t &userId) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr &date) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr &date) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetEnabledForBundleSlot(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetEnabledForBundleSlot(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType &slotType, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, + std::vector &dumpInfo) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetBadgeNumber(int32_t badgeNumber) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetBadgeNumberByBundle(const sptr& bundleOption, int32_t badgeNumber) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetSlotFlagsAsBundle(const sptr& bundleOption, uint32_t &slotFlags) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetSlotFlagsAsBundle(const sptr& bundleOption, uint32_t slotFlags) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode RegisterPushCallback(const sptr &pushCallback, + const sptr ¬ificationCheckRequest) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode UnregisterPushCallback() override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetActiveNotificationByFilter(const sptr &bundleOption, + const int32_t notificationId, const std::string &label, std::vector extraInfoKeys, + sptr &request) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode TriggerLocalLiveView(const sptr &bundleOption, + const int32_t notificationId, const sptr &buttonOption) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetAdditionConfig(const std::string &key, const std::string &value) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetDistributedEnabledByBundle(const sptr &bundleOption, + const std::string &deviceType, const bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsDistributedEnabledByBundle(const sptr &bundleOption, + const std::string &deviceType, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsSmartReminderEnabled(const std::string &deviceType, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetSmartReminderEnabled(const std::string &deviceType, const bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode CancelAsBundleWithAgent(const sptr &bundleOption, const int32_t id) + { + return ERR_ANS_INVALID_PARAM; + } +}; + +class AnsNotificationBranchTest : public testing::Test { +public: + AnsNotificationBranchTest() {} + + virtual ~AnsNotificationBranchTest() {} + + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); +}; + +void AnsNotificationBranchTest::SetUpTestCase() {} + +void AnsNotificationBranchTest::TearDownTestCase() {} + +void AnsNotificationBranchTest::SetUp() {} + +/* + * @tc.name: RemoveNotifications_0100 + * @tc.desc: Test RemoveNotifications and hashcodes is empty + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, RemoveNotifications_0100, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + std::vector hashcodes; + int32_t removeReason = 1; + ErrCode ret = ansNotification->RemoveNotifications(hashcodes, removeReason); + EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: RemoveNotifications_0200 + * @tc.desc: 1.Test RemoveNotifications and hashcodes is not empty + * 2.GetAnsManagerProxy is false + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, RemoveNotifications_0200, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + std::string hashcode = "aa"; + std::vector hashcodes; + hashcodes.emplace_back(hashcode); + int32_t removeReason = 1; + MockGetAnsManagerProxy(false); + ErrCode ret = ansNotification->RemoveNotifications(hashcodes, removeReason); + EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); +} + +/* + * @tc.name: RemoveNotifications_0300 + * @tc.desc: 1.Test RemoveNotifications and hashcodes is not empty + * 2.GetAnsManagerProxy is true + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, RemoveNotifications_0300, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + std::string hashcode = "aa"; + std::vector hashcodes; + hashcodes.emplace_back(hashcode); + int32_t removeReason = 1; + MockGetAnsManagerProxy(true); + ansNotification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); + ansNotification->RemoveNotifications(hashcodes, removeReason); +} + +/* + * @tc.name: RegisterPushCallback_0100 + * @tc.desc: 1.Test RegisterPushCallback + * 2.GetAnsManagerProxy is false + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, RegisterPushCallback_0100, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + sptr pushCallback = nullptr; + MockGetAnsManagerProxy(false); + sptr checkRequest = new (std::nothrow) NotificationCheckRequest(); + ErrCode ret = ansNotification->RegisterPushCallback(pushCallback, checkRequest); + EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); +} + +/* + * @tc.name: RegisterPushCallback_0200 + * @tc.desc: 1.Test RegisterPushCallback + * 2.GetAnsManagerProxy is true + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, RegisterPushCallback_0200, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + sptr pushCallback = nullptr; + MockGetAnsManagerProxy(true); + ansNotification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); + sptr checkRequest = new (std::nothrow) NotificationCheckRequest(); + ansNotification->RegisterPushCallback(pushCallback, checkRequest); +} + +/* + * @tc.name: UnregisterPushCallback_0100 + * @tc.desc: 1.Test UnregisterPushCallback + * 2.GetAnsManagerProxy is false + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, UnregisterPushCallback_0100, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + MockGetAnsManagerProxy(false); + ErrCode ret = ansNotification->UnregisterPushCallback(); + EXPECT_EQ(ret, ERR_ANS_SERVICE_NOT_CONNECTED); +} + +/* + * @tc.name: UnregisterPushCallback_0200 + * @tc.desc: 1.Test UnregisterPushCallback + * 2.GetAnsManagerProxy is true + * @tc.type: FUNC + * @tc.require: #I62SME + */ +HWTEST_F(AnsNotificationBranchTest, UnregisterPushCallback_0200, Function | MediumTest | Level1) +{ + auto ansNotification = std::make_shared(); + EXPECT_NE(ansNotification, nullptr); + MockGetAnsManagerProxy(true); + ansNotification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); + ansNotification->UnregisterPushCallback(); +} + +/* + * @tc.name: CanPublishLiveViewContent_0100 + * @tc.desc: CanPublishLiveViewContent + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0100, Function | MediumTest | Level1) +{ + NotificationRequest request; + auto notification = std::make_shared(); + EXPECT_TRUE(notification->CanPublishLiveViewContent(request)); +} + +/* + * @tc.name: CanPublishLiveViewContent_0110 + * @tc.desc: CanPublishLiveViewContent + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0110, Function | MediumTest | Level1) +{ + NotificationRequest request; + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + liveViewContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT); + auto content = std::make_shared(liveViewContent); + request.SetContent(content); + + auto notification = std::make_shared(); + EXPECT_FALSE(notification->CanPublishLiveViewContent(request)); +} + +/* + * @tc.name: CanPublishLiveViewContent_0120 + * @tc.desc: CanPublishLiveViewContent + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0120, Function | MediumTest | Level1) +{ + NotificationRequest request; + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + auto content = std::make_shared(liveViewContent); + request.SetContent(content); + + auto notification = std::make_shared(); + EXPECT_TRUE(notification->CanPublishLiveViewContent(request)); +} + +/* + * @tc.name: CanPublishLiveViewContent_0130 + * @tc.desc: CanPublishLiveViewContent + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0130, Function | MediumTest | Level1) +{ + NotificationRequest request; + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + liveViewContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_FULL_UPDATE); + auto content = std::make_shared(liveViewContent); + request.SetContent(content); + + auto notification = std::make_shared(); + EXPECT_TRUE(notification->CanPublishLiveViewContent(request)); +} + +/* + * @tc.name: CanPublishLiveViewContent_0140 + * @tc.desc: CanPublishLiveViewContent + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0140, Function | MediumTest | Level1) +{ + NotificationRequest request; + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + auto content = std::make_shared(liveViewContent); + request.SetContent(content); + request.notificationContent_ = nullptr; + + auto notification = std::make_shared(); + EXPECT_FALSE(notification->CanPublishLiveViewContent(request)); +} + +/* + * @tc.name: CanPublishLiveViewContent_0150 + * @tc.desc: CanPublishLiveViewContent + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, CanPublishLiveViewContent_0150, Function | MediumTest | Level1) +{ + NotificationRequest request; + request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto liveViewContent = std::make_shared(); + auto content = std::make_shared(liveViewContent); + content->content_ = nullptr; + request.SetContent(content); + + auto notification = std::make_shared(); + EXPECT_FALSE(notification->CanPublishLiveViewContent(request)); +} + +/* + * @tc.name: SetNotificationSlotFlagsAsBundle_0001 + * @tc.desc: SetNotificationSlotFlagsAsBundle + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, SetNotificationSlotFlagsAsBundle_0001, Function | MediumTest | Level1) +{ + NotificationBundleOption bundle; + uint32_t slotFlags = 1; + auto notification = std::make_shared(); + ErrCode ret = notification->SetNotificationSlotFlagsAsBundle(bundle, slotFlags); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); + ret = notification->GetNotificationSlotFlagsAsBundle(bundle, slotFlags); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); + + notification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); + bundle.SetBundleName("test"); + bundle.SetUid(1); + ret = notification->SetNotificationSlotFlagsAsBundle(bundle, slotFlags); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); + ret = notification->GetNotificationSlotFlagsAsBundle(bundle, slotFlags); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); +} + +/* + * @tc.name: PublishNotification_0001 + * @tc.desc: PublishNotification + * @tc.type: FUNC + * @tc.require: issule + */ +HWTEST_F(AnsNotificationBranchTest, PublishNotification_0001, Function | MediumTest | Level1) +{ + auto notification = std::make_shared(); + notification->ansManagerProxy_ = new (std::nothrow) MockAnsManagerInterface(); + NotificationRequest req; + std::shared_ptr mediaContent = std::make_shared(); + auto content = std::make_shared(mediaContent); + content->content_ = nullptr; + req.SetContent(content); + + auto ret = notification->PublishNotification("label", req); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); + ret = notification->PublishNotificationAsBundle("label", req); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); + + auto liveViewContent = std::make_shared(); + auto content1 = std::make_shared(liveViewContent); + content1->content_ = nullptr; + req.SetContent(content1); + req.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + ret = notification->PublishNotification("label", req); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); + ret = notification->PublishNotificationAsBundle("label", req); + EXPECT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); +} + +} // namespace Notification +} // namespace OHOS -- Gitee From d602ff0a402e355ec40988e0d7baf2ba96ad24d3 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Tue, 19 Mar 2024 11:55:50 +0800 Subject: [PATCH 07/15] modify code Signed-off-by: xinking129 --- frameworks/core/src/ans_manager_proxy_disturb.cpp | 4 ++-- frameworks/js/napi/include/disturb_mode.h | 2 +- frameworks/js/napi/src/disturb_mode.cpp | 6 +++--- .../notification_do_not_disturb_profile.h | 6 +++--- .../ans/include/advanced_notification_service.h | 1 + services/ans/src/advanced_datashare_helper.cpp | 2 -- services/ans/src/advanced_notification_service.cpp | 12 ++++++++++-- .../mock_notification_rdb_data_mgr.cpp | 14 ++++++++++++++ 8 files changed, 34 insertions(+), 13 deletions(-) diff --git a/frameworks/core/src/ans_manager_proxy_disturb.cpp b/frameworks/core/src/ans_manager_proxy_disturb.cpp index 2d000c3d4..16e141ade 100644 --- a/frameworks/core/src/ans_manager_proxy_disturb.cpp +++ b/frameworks/core/src/ans_manager_proxy_disturb.cpp @@ -109,7 +109,7 @@ ErrCode AnsManagerProxy::AddDoNotDisturbProfiles(const std::vector> &profiles); -bool AnalysisTrustlist(const napi_env &env, const napi_value &value, sptr &profile); +bool AnalyseTrustlist(const napi_env &env, const napi_value &value, sptr &profile); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_DISTURB_MODE_H \ No newline at end of file diff --git a/frameworks/js/napi/src/disturb_mode.cpp b/frameworks/js/napi/src/disturb_mode.cpp index 719b64fd7..36568f262 100644 --- a/frameworks/js/napi/src/disturb_mode.cpp +++ b/frameworks/js/napi/src/disturb_mode.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 @@ -126,10 +126,10 @@ bool GetDoNotDisturbProfile( ANS_LOGD("The do not disturb profile name is: %{public}s", name); profile->SetProfileName(name); - return AnalysisTrustlist(env, value, profile); + return AnalyseTrustlist(env, value, profile); } -bool AnalysisTrustlist(const napi_env &env, const napi_value &value, sptr &profile) +bool AnalyseTrustlist(const napi_env &env, const napi_value &value, sptr &profile) { bool hasProperty = false; NAPI_CALL_BASE(env, napi_has_named_property(env, value, "trustlist", &hasProperty), false); diff --git a/interfaces/inner_api/notification_do_not_disturb_profile.h b/interfaces/inner_api/notification_do_not_disturb_profile.h index d145a5884..72bdfe191 100644 --- a/interfaces/inner_api/notification_do_not_disturb_profile.h +++ b/interfaces/inner_api/notification_do_not_disturb_profile.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_DO_NOT_DISTURB_PROFILE_H -#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_DO_NOT_DISTURB_PROFILE_H +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_DO_NOT_DISTURB_PROFILE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_DO_NOT_DISTURB_PROFILE_H #include "notification_bundle_option.h" #include "parcel.h" @@ -112,4 +112,4 @@ private: }; } // namespace Notification } // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_DO_NOT_DISTURB_PROFILE_H \ No newline at end of file +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_DO_NOT_DISTURB_PROFILE_H \ No newline at end of file diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 083f046a0..34973bdcf 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1193,6 +1193,7 @@ private: void CancelArchiveTimer(const std::shared_ptr &record); void ProcForDeleteLiveView(const std::shared_ptr &record); void CheckDoNotDisturbProfile(const std::shared_ptr &record); + void DoNotDisturbUpdataFlags(const std::shared_ptr &record); ErrCode CheckCommonParams(); std::shared_ptr GetRecordFromNotificationList( int32_t notificationId, int32_t uid, const std::string &label, const std::string &bundleName); diff --git a/services/ans/src/advanced_datashare_helper.cpp b/services/ans/src/advanced_datashare_helper.cpp index c05b579f5..36b5a1aa5 100644 --- a/services/ans/src/advanced_datashare_helper.cpp +++ b/services/ans/src/advanced_datashare_helper.cpp @@ -58,9 +58,7 @@ bool AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, int32_t &v return false; } - // int columnIndex; result->GetColumnIndex(ADVANCED_DATA_COLUMN_VALUE, value); - // result->GetString(columnIndex, value); result->Close(); ANS_LOGD("Query success, value[%{public}d]", value); return true; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index aedf2d54c..347725a35 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -605,7 +605,7 @@ void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr return; } ANS_LOGE("Query focus mode enable is %{public}d.", focus_mode_enable); - if(focus_mode_enable == 0){ + if (focus_mode_enable == 0) { return; } Uri profileUri(FOCUS_MODE_PROFILE_URI); @@ -613,7 +613,6 @@ void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr if (!datashareHelper->Query(profileUri, KEY_FOCUS_MODE_PROFILE, profileId)) { return; } - // int32_t profileId = 1; ANS_LOGD("Currently in focus mode."); std::string bundleName = GetClientBundleName(); if (bundleName == MMS_BUNDLE_NAME || bundleName == CONTACTS_BUNDLE_NAME) { @@ -637,6 +636,15 @@ void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr return; } } + DoNotDisturbUpdataFlags(record); +} + +void AdvancedNotificationService::DoNotDisturbUpdataFlags(const std::shared_ptr &record) +{ + if (record == nullptr && record->request == nullptr && record->notification == nullptr) { + ANS_LOGE("Make notification record failed."); + return; + } auto flags = record->request->GetFlags(); if (flags == nullptr) { ANS_LOGE("The flags is nullptr."); diff --git a/services/ans/test/unittest/notification_preferences_database_branch_test/mock_notification_rdb_data_mgr.cpp b/services/ans/test/unittest/notification_preferences_database_branch_test/mock_notification_rdb_data_mgr.cpp index e26ed128a..0768bcce5 100755 --- a/services/ans/test/unittest/notification_preferences_database_branch_test/mock_notification_rdb_data_mgr.cpp +++ b/services/ans/test/unittest/notification_preferences_database_branch_test/mock_notification_rdb_data_mgr.cpp @@ -22,6 +22,7 @@ namespace { bool g_mockQueryDataBeginWithKeyRet = true; bool g_mockDeleteBathchDataRet = true; bool g_mockDeleteDataRet = true; + bool g_mockQueryAllData = true; } void MockInit(bool mockRet) @@ -59,6 +60,11 @@ void MockDeleteData(bool mockRet) g_mockDeleteDataRet = mockRet; } +void MockQueryAllData(bool mockRet) +{ + g_mockQueryAllData = mockRet; +} + namespace OHOS { namespace Notification { NotificationDataMgr::NotificationDataMgr(const NotificationRdbConfig ¬ificationRdbConfig) @@ -130,5 +136,13 @@ int32_t NotificationDataMgr::DeleteData(const std::string &key) } return NativeRdb::E_OK; } + +int32_t NotificationDataMgr::QueryAllData(std::unordered_map &values) +{ + if (g_mockQueryAllData == false) { + return NativeRdb::E_ERROR; + } + return NativeRdb::E_OK; +} } } -- Gitee From a151155b715fb33287720fa521608e0ee12236a1 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Thu, 21 Mar 2024 20:31:37 +0800 Subject: [PATCH 08/15] datashare test Signed-off-by: xinking129 --- .../ans/include/advanced_datashare_helper.h | 15 +++-- .../ans/src/advanced_datashare_helper.cpp | 55 +++++++++++++++---- .../ans/src/advanced_notification_service.cpp | 24 +++----- 3 files changed, 59 insertions(+), 35 deletions(-) diff --git a/services/ans/include/advanced_datashare_helper.h b/services/ans/include/advanced_datashare_helper.h index fe55e6373..29be20d9f 100644 --- a/services/ans/include/advanced_datashare_helper.h +++ b/services/ans/include/advanced_datashare_helper.h @@ -18,27 +18,26 @@ #include "data_ability_observer_stub.h" #include "data_ability_observer_interface.h" +#include "system_ability_definition.h" #include "datashare_helper.h" #include "uri.h" - +#include "iremote_broker.h" namespace OHOS { namespace Notification { namespace { -constexpr const char *FOCUS_MODE_ENABLE_URI = "datashare://com.ohos.settingsdata/entry/settingsdata/" - "USER_SETTINGSDATA_100?Proxy=true&key==focus_mode_enable"; -constexpr const char *FOCUS_MODE_PROFILE_URI = "datashare://com.ohos.settingsdata/entry/settingsdata/" - "USER_SETTINGSDATA_100?Proxy=true&key==focus_mode_profile"; +constexpr const char *FOCUS_MODE_ENABLE_URI = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_100?Proxy=true&key==focus_mode_enable"; +constexpr const char *FOCUS_MODE_PROFILE_URI = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_100?Proxy=true&key==focus_mode_profile"; constexpr const char *KEY_FOCUS_MODE_ENABLE = "focus_mode_enable"; constexpr const char *KEY_FOCUS_MODE_PROFILE = "focus_mode_profile"; - } // namespace + class AdvancedDatashareHelper { public: AdvancedDatashareHelper(); ~AdvancedDatashareHelper() = default; - bool Query(Uri &uri, const std::string &key, int32_t &value); - + int32_t Query(Uri &uri, const std::string &key, std::string &value); + int32_t Insert(Uri &uri, const std::string &key, const std::string &value); private: std::shared_ptr CreateDataShareHelper(); std::shared_ptr dataShareHelper_ = nullptr; diff --git a/services/ans/src/advanced_datashare_helper.cpp b/services/ans/src/advanced_datashare_helper.cpp index 36b5a1aa5..c6b1210bf 100644 --- a/services/ans/src/advanced_datashare_helper.cpp +++ b/services/ans/src/advanced_datashare_helper.cpp @@ -16,15 +16,18 @@ #include "advanced_datashare_helper.h" #include "ans_log_wrapper.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" #include "singleton.h" #include "message_parcel.h" namespace OHOS { namespace Notification { -constexpr const char *ADVANCED_DATASHARE_URI = - "datashare://com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_100?Proxy=true"; +constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; constexpr const char *ADVANCED_DATA_COLUMN_KEYWORD = "KEYWORD"; constexpr const char *ADVANCED_DATA_COLUMN_VALUE = "VALUE"; + AdvancedDatashareHelper::AdvancedDatashareHelper() { dataShareHelper_ = CreateDataShareHelper(); @@ -32,16 +35,22 @@ AdvancedDatashareHelper::AdvancedDatashareHelper() std::shared_ptr AdvancedDatashareHelper::CreateDataShareHelper() { - DataShare::CreateOptions options; - options.enabled_ = true; - return DataShare::DataShareHelper::Creator(ADVANCED_DATASHARE_URI, options); + sptr saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saManager == nullptr) { + return nullptr; + } + sptr remoteObj = saManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID); + if (remoteObj == nullptr) { + return nullptr; + } + return DataShare::DataShareHelper::Creator(remoteObj, SETTINGS_DATA_EXT_URI); } -bool AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, int32_t &value) +int32_t AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, std::string &value) { if (dataShareHelper_ == nullptr) { ANS_LOGE("The dataShareHelper_ is nullptr."); - return false; + return -1; } DataShare::DataSharePredicates predicates; std::vector columns; @@ -49,19 +58,41 @@ bool AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, int32_t &v auto result = dataShareHelper_->Query(uri, predicates, columns); if (result == nullptr) { ANS_LOGE("Query error, result is null."); - return false; + return -1; } if (result->GoToFirstRow() != DataShare::E_OK) { ANS_LOGE("Query failed, go to first row error."); result->Close(); - return false; + return -1; } - result->GetColumnIndex(ADVANCED_DATA_COLUMN_VALUE, value); + int columnIndex; + result->GetColumnIndex(ADVANCED_DATA_COLUMN_VALUE, columnIndex); + result->GetString(columnIndex, value); result->Close(); - ANS_LOGD("Query success, value[%{public}d]", value); - return true; + ANS_LOGD("Query success, value[%{public}s]", value.c_str()); + return 0; +} +int32_t AdvancedDatashareHelper::Insert(Uri &uri, const std::string &key, const std::string &value) +{ + if (dataShareHelper_ == nullptr) { + ANS_LOGE("dataShareHelper_ is nullptr"); + return -1; + } + DataShare::DataShareValuesBucket valuesBucket; + DataShare::DataShareValueObject keyObj(key); + DataShare::DataShareValueObject valueObj(value); + valuesBucket.Put(ADVANCED_DATA_COLUMN_KEYWORD, keyObj); + valuesBucket.Put(ADVANCED_DATA_COLUMN_VALUE, valueObj); + int32_t result = dataShareHelper_->Insert(uri, valuesBucket); + if (result == -1) { + ANS_LOGE("DataShareHelper insert failed, insert result:%{public}d", result); + return -1; + } + dataShareHelper_->NotifyChange(uri); + ANS_LOGE("DataShareHelper insert success"); + return 0; } } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 347725a35..733665c1e 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -597,23 +597,17 @@ void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr return; } auto datashareHelper = std::make_shared(); - Uri enableUri(FOCUS_MODE_ENABLE_URI); - std::string key = KEY_FOCUS_MODE_ENABLE; - int32_t focus_mode_enable; - if (!datashareHelper->Query(enableUri, KEY_FOCUS_MODE_ENABLE, focus_mode_enable)) { - ANS_LOGE("Query focus mode enable is failed focus_mode_enable is %{public}d.", focus_mode_enable); + std::string enable; + Uri uri(FOCUS_MODE_ENABLE_URI); + int ret1 = datashareHelper->Insert(uri, KEY_FOCUS_MODE_ENABLE, "1"); + int ret = datashareHelper->Query(uri, KEY_FOCUS_MODE_ENABLE, enable); + if (ret != 0) { + ANS_LOGE("Query focus mode enable fail, focus_mode_enable is %{public}s.", enable.c_str()); return; } - ANS_LOGE("Query focus mode enable is %{public}d.", focus_mode_enable); - if (focus_mode_enable == 0) { - return; - } - Uri profileUri(FOCUS_MODE_PROFILE_URI); - int32_t profileId; - if (!datashareHelper->Query(profileUri, KEY_FOCUS_MODE_PROFILE, profileId)) { - return; - } - ANS_LOGD("Currently in focus mode."); + ANS_LOGE("Query focus mode enable is focus_mode_enable is %{public}s.", enable.c_str()); + + int32_t profileId = 1; std::string bundleName = GetClientBundleName(); if (bundleName == MMS_BUNDLE_NAME || bundleName == CONTACTS_BUNDLE_NAME) { ANS_LOGI("The bundle name is mms or contacts."); -- Gitee From 30def3993a4bbd3ef9c6a636e0f0b899de978553 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Mon, 25 Mar 2024 16:44:36 +0800 Subject: [PATCH 09/15] modify data share helper code Signed-off-by: xinking129 --- .../ans/include/advanced_datashare_helper.h | 21 +++--- .../include/advanced_notification_service.h | 2 +- .../ans/src/advanced_datashare_helper.cpp | 71 +++++++++++-------- .../ans/src/advanced_notification_service.cpp | 39 ++++++---- 4 files changed, 74 insertions(+), 59 deletions(-) diff --git a/services/ans/include/advanced_datashare_helper.h b/services/ans/include/advanced_datashare_helper.h index 29be20d9f..16b3a4dae 100644 --- a/services/ans/include/advanced_datashare_helper.h +++ b/services/ans/include/advanced_datashare_helper.h @@ -16,32 +16,29 @@ #ifndef NOTIFICATION_ADVANCED_DATASHAER_HELPER_H #define NOTIFICATION_ADVANCED_DATASHAER_HELPER_H -#include "data_ability_observer_stub.h" -#include "data_ability_observer_interface.h" -#include "system_ability_definition.h" #include "datashare_helper.h" -#include "uri.h" #include "iremote_broker.h" +#include "system_ability_definition.h" +#include "uri.h" + namespace OHOS { namespace Notification { namespace { -constexpr const char *FOCUS_MODE_ENABLE_URI = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_100?Proxy=true&key==focus_mode_enable"; -constexpr const char *FOCUS_MODE_PROFILE_URI = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_100?Proxy=true&key==focus_mode_profile"; constexpr const char *KEY_FOCUS_MODE_ENABLE = "focus_mode_enable"; constexpr const char *KEY_FOCUS_MODE_PROFILE = "focus_mode_profile"; -} // namespace - +} // namespace class AdvancedDatashareHelper { public: AdvancedDatashareHelper(); ~AdvancedDatashareHelper() = default; - int32_t Query(Uri &uri, const std::string &key, std::string &value); - int32_t Insert(Uri &uri, const std::string &key, const std::string &value); + bool Query(Uri &uri, const std::string &key, std::string &value); + std::string GetFocusModeEnableUri() const; + std::string GetFocusModeProfileUri() const; private: - std::shared_ptr CreateDataShareHelper(); + void CreateDataShareHelper(); std::shared_ptr dataShareHelper_ = nullptr; }; } // namespace Notification } // namespace OHOS -#endif \ No newline at end of file +#endif // NOTIFICATION_ADVANCED_DATASHAER_HELPER_H \ No newline at end of file diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 34973bdcf..3801582dd 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1193,7 +1193,7 @@ private: void CancelArchiveTimer(const std::shared_ptr &record); void ProcForDeleteLiveView(const std::shared_ptr &record); void CheckDoNotDisturbProfile(const std::shared_ptr &record); - void DoNotDisturbUpdataFlags(const std::shared_ptr &record); + void DoNotDisturbUpdataReminderFlags(const std::shared_ptr &record); ErrCode CheckCommonParams(); std::shared_ptr GetRecordFromNotificationList( int32_t notificationId, int32_t uid, const std::string &label, const std::string &bundleName); diff --git a/services/ans/src/advanced_datashare_helper.cpp b/services/ans/src/advanced_datashare_helper.cpp index c6b1210bf..86725543b 100644 --- a/services/ans/src/advanced_datashare_helper.cpp +++ b/services/ans/src/advanced_datashare_helper.cpp @@ -18,39 +18,46 @@ #include "ans_log_wrapper.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" -#include "system_ability_definition.h" -#include "singleton.h" #include "message_parcel.h" +#include "os_account_manager.h" +#include "singleton.h" +#include "system_ability_definition.h" namespace OHOS { namespace Notification { +namespace { constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; +constexpr const char *USER_SETTINGS_DATA_URI = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_"; +constexpr const char *FOCUS_MODE_ENABLE_URI = "?Proxy=true&key=focus_mode_enable"; +constexpr const char *FOCUS_MODE_PROFILE_URI = "?Proxy=true&key=focus_mode_profile"; constexpr const char *ADVANCED_DATA_COLUMN_KEYWORD = "KEYWORD"; constexpr const char *ADVANCED_DATA_COLUMN_VALUE = "VALUE"; - +} // namespace AdvancedDatashareHelper::AdvancedDatashareHelper() { - dataShareHelper_ = CreateDataShareHelper(); + CreateDataShareHelper(); } -std::shared_ptr AdvancedDatashareHelper::CreateDataShareHelper() +void AdvancedDatashareHelper::CreateDataShareHelper() { sptr saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (saManager == nullptr) { - return nullptr; + ANS_LOGE("The sa manager is nullptr."); + return; } sptr remoteObj = saManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID); if (remoteObj == nullptr) { - return nullptr; + ANS_LOGE("The remoteObj is nullptr."); + return; } - return DataShare::DataShareHelper::Creator(remoteObj, SETTINGS_DATA_EXT_URI); + dataShareHelper_ = DataShare::DataShareHelper::Creator(remoteObj, SETTINGS_DATA_EXT_URI); } -int32_t AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, std::string &value) +bool AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, std::string &value) { if (dataShareHelper_ == nullptr) { - ANS_LOGE("The dataShareHelper_ is nullptr."); - return -1; + ANS_LOGE("The data share helper is nullptr."); + return false; } DataShare::DataSharePredicates predicates; std::vector columns; @@ -58,41 +65,43 @@ int32_t AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, std::st auto result = dataShareHelper_->Query(uri, predicates, columns); if (result == nullptr) { ANS_LOGE("Query error, result is null."); - return -1; + return false; } if (result->GoToFirstRow() != DataShare::E_OK) { ANS_LOGE("Query failed, go to first row error."); result->Close(); - return -1; + return false; } - int columnIndex; + int32_t columnIndex; result->GetColumnIndex(ADVANCED_DATA_COLUMN_VALUE, columnIndex); result->GetString(columnIndex, value); result->Close(); ANS_LOGD("Query success, value[%{public}s]", value.c_str()); - return 0; + return true; } -int32_t AdvancedDatashareHelper::Insert(Uri &uri, const std::string &key, const std::string &value) + +std::string AdvancedDatashareHelper::GetFocusModeEnableUri() const { - if (dataShareHelper_ == nullptr) { - ANS_LOGE("dataShareHelper_ is nullptr"); - return -1; + std::vector accountIds; + OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(accountIds); + std::string userId = "100"; + if (!accountIds.empty()) { + userId = std::to_string(accountIds[0]); } - DataShare::DataShareValuesBucket valuesBucket; - DataShare::DataShareValueObject keyObj(key); - DataShare::DataShareValueObject valueObj(value); - valuesBucket.Put(ADVANCED_DATA_COLUMN_KEYWORD, keyObj); - valuesBucket.Put(ADVANCED_DATA_COLUMN_VALUE, valueObj); - int32_t result = dataShareHelper_->Insert(uri, valuesBucket); - if (result == -1) { - ANS_LOGE("DataShareHelper insert failed, insert result:%{public}d", result); - return -1; + return USER_SETTINGS_DATA_URI + userId + FOCUS_MODE_ENABLE_URI; +} + +std::string AdvancedDatashareHelper::GetFocusModeProfileUri() const +{ + std::vector accountIds; + OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(accountIds); + std::string userId = "100"; + if (!accountIds.empty()) { + userId = std::to_string(accountIds[0]); } - dataShareHelper_->NotifyChange(uri); - ANS_LOGE("DataShareHelper insert success"); - return 0; + return USER_SETTINGS_DATA_URI + userId + FOCUS_MODE_PROFILE_URI; } } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 733665c1e..7ec8bcad2 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -84,6 +84,7 @@ constexpr int32_t MAX_LIVEVIEW_HINT_COUNT = 3; const std::string NOTIFICATION_ANS_CHECK_SA_PERMISSION = "notification.ans.check.sa.permission"; const std::string MMS_BUNDLE_NAME = "com.ohos.mms"; const std::string CONTACTS_BUNDLE_NAME = "com.ohos.contacts"; +const std::string DO_NOT_DISTURB_MODE = "1"; } // namespace sptr AdvancedNotificationService::instance_; @@ -592,30 +593,38 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr &record) { - if (record == nullptr && record->request == nullptr && record->notification == nullptr) { - ANS_LOGE("Make notification record failed."); + auto datashareHelper = std::make_shared(); + if (datashareHelper == nullptr) { + ANS_LOGE("The data share helper is nullptr."); return; } - auto datashareHelper = std::make_shared(); std::string enable; - Uri uri(FOCUS_MODE_ENABLE_URI); - int ret1 = datashareHelper->Insert(uri, KEY_FOCUS_MODE_ENABLE, "1"); - int ret = datashareHelper->Query(uri, KEY_FOCUS_MODE_ENABLE, enable); - if (ret != 0) { - ANS_LOGE("Query focus mode enable fail, focus_mode_enable is %{public}s.", enable.c_str()); + Uri enableUri(datashareHelper->GetFocusModeEnableUri()); + bool ret = datashareHelper->Query(enableUri, KEY_FOCUS_MODE_ENABLE, enable); + if (!ret) { + ANS_LOGE("Query focus mode enable fail, focus mode enable is %{public}s.", enable.c_str()); + return; + } + if (enable != DO_NOT_DISTURB_MODE) { + ANS_LOGI("Currently not is do not disturb mode."); return; } - ANS_LOGE("Query focus mode enable is focus_mode_enable is %{public}s.", enable.c_str()); - - int32_t profileId = 1; std::string bundleName = GetClientBundleName(); if (bundleName == MMS_BUNDLE_NAME || bundleName == CONTACTS_BUNDLE_NAME) { - ANS_LOGI("The bundle name is mms or contacts."); + ANS_LOGI("Currently in do not disturb mode, the bundle name is mms or contacts, keep reminder method."); + return; + } + std::string profileId; + Uri idUri(datashareHelper->GetFocusModeProfileUri()); + ret = datashareHelper->Query(idUri, KEY_FOCUS_MODE_PROFILE, profileId); + if (!ret) { + ANS_LOGE("Query focus mode id fail, focus mode profile id is %{public}s.", profileId.c_str()); return; } sptr profile = new (std::nothrow) NotificationDoNotDisturbProfile(); int32_t userId = record->notification->GetUserId(); - if (NotificationPreferences::GetInstance().GetDonotDisturbProfile(profileId, userId, profile) != ERR_OK) { + if (NotificationPreferences::GetInstance().GetDonotDisturbProfile(atoi(profileId.c_str()), userId, profile) != + ERR_OK) { ANS_LOGE("Get do not disturb profile failed."); return; } @@ -630,10 +639,10 @@ void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr return; } } - DoNotDisturbUpdataFlags(record); + DoNotDisturbUpdataReminderFlags(record); } -void AdvancedNotificationService::DoNotDisturbUpdataFlags(const std::shared_ptr &record) +void AdvancedNotificationService::DoNotDisturbUpdataReminderFlags(const std::shared_ptr &record) { if (record == nullptr && record->request == nullptr && record->notification == nullptr) { ANS_LOGE("Make notification record failed."); -- Gitee From 1bac8fccd8cf3180715b0f5934707bcd14670f53 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Mon, 25 Mar 2024 17:32:31 +0800 Subject: [PATCH 10/15] modify code Signed-off-by: xinking129 --- services/ans/src/advanced_datashare_helper.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/ans/src/advanced_datashare_helper.cpp b/services/ans/src/advanced_datashare_helper.cpp index 86725543b..4e328277a 100644 --- a/services/ans/src/advanced_datashare_helper.cpp +++ b/services/ans/src/advanced_datashare_helper.cpp @@ -27,7 +27,8 @@ namespace OHOS { namespace Notification { namespace { constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; -constexpr const char *USER_SETTINGS_DATA_URI = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_"; +constexpr const char *USER_SETTINGS_DATA_URI = + "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_"; constexpr const char *FOCUS_MODE_ENABLE_URI = "?Proxy=true&key=focus_mode_enable"; constexpr const char *FOCUS_MODE_PROFILE_URI = "?Proxy=true&key=focus_mode_profile"; constexpr const char *ADVANCED_DATA_COLUMN_KEYWORD = "KEYWORD"; -- Gitee From 78069b89efafd9a4774f7f51420783488ba301ea Mon Sep 17 00:00:00 2001 From: xinking129 Date: Thu, 28 Mar 2024 11:40:39 +0800 Subject: [PATCH 11/15] add observer code Signed-off-by: xinking129 --- services/ans/BUILD.gn | 1 + .../ans/include/advanced_datashare_helper.h | 15 +++- .../include/advanced_notification_service.h | 5 ++ .../ans/src/advanced_datashare_helper.cpp | 23 ++++++- .../ans/src/advanced_notification_service.cpp | 69 ++++++++++++++----- 5 files changed, 93 insertions(+), 20 deletions(-) diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index 80ca33387..4214f4edb 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -106,6 +106,7 @@ ohos_shared_library("libans") { external_deps = [ "ability_runtime:ability_manager", "ability_runtime:app_manager", + "ability_runtime:dataobs_manager", "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", diff --git a/services/ans/include/advanced_datashare_helper.h b/services/ans/include/advanced_datashare_helper.h index 16b3a4dae..7b3e59239 100644 --- a/services/ans/include/advanced_datashare_helper.h +++ b/services/ans/include/advanced_datashare_helper.h @@ -16,6 +16,9 @@ #ifndef NOTIFICATION_ADVANCED_DATASHAER_HELPER_H #define NOTIFICATION_ADVANCED_DATASHAER_HELPER_H +#include + +#include "data_ability_observer_stub.h" #include "datashare_helper.h" #include "iremote_broker.h" #include "system_ability_definition.h" @@ -28,13 +31,23 @@ constexpr const char *KEY_FOCUS_MODE_ENABLE = "focus_mode_enable"; constexpr const char *KEY_FOCUS_MODE_PROFILE = "focus_mode_profile"; } // namespace -class AdvancedDatashareHelper { +class DoNotDisturbEnableObserver : public AAFwk::DataAbilityObserverStub { +public: + ~DoNotDisturbEnableObserver() = default; + + void OnChange() override; +}; + +class AdvancedDatashareHelper : DelayedSingleton { public: AdvancedDatashareHelper(); ~AdvancedDatashareHelper() = default; + bool Query(Uri &uri, const std::string &key, std::string &value); + bool RegisterObserver(const Uri &uri, const sptr &observer); std::string GetFocusModeEnableUri() const; std::string GetFocusModeProfileUri() const; + private: void CreateDataShareHelper(); std::shared_ptr dataShareHelper_ = nullptr; diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 3801582dd..6a111efe6 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -762,6 +762,8 @@ public: ErrCode GetEnabledForBundleSlot(const sptr &bundleOption, const NotificationConstant::SlotType &slotType, bool &enabled) override; ErrCode GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType &slotType, bool &enabled) override; + void QueryDoNotDisturbState(); + void RegisterDoNotDisturbObserver(); // SystemEvent @@ -1235,6 +1237,8 @@ private: std::shared_ptr notificationSvrQueue_ = nullptr; static std::string supportCheckSaPermission_; std::map> publishProcess_; + std::string focusModeEnable_; + std::string focusModeProfile_; #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED NotificationConstant::DistributedReminderPolicy distributedReminderPolicy_ = DEFAULT_DISTRIBUTED_REMINDER_POLICY; bool localScreenOn_ = true; @@ -1243,6 +1247,7 @@ private: std::shared_ptr notificationSlotFilter_ = nullptr; std::shared_ptr dialogManager_ = nullptr; std::list> uniqueKeyList_; + bool queryDoNotDisturbOnceFlag_ = false; }; /** diff --git a/services/ans/src/advanced_datashare_helper.cpp b/services/ans/src/advanced_datashare_helper.cpp index 4e328277a..15c208fdb 100644 --- a/services/ans/src/advanced_datashare_helper.cpp +++ b/services/ans/src/advanced_datashare_helper.cpp @@ -15,6 +15,7 @@ #include "advanced_datashare_helper.h" +#include "advanced_notification_service.h" #include "ans_log_wrapper.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" @@ -34,6 +35,11 @@ constexpr const char *FOCUS_MODE_PROFILE_URI = "?Proxy=true&key=focus_mode_profi constexpr const char *ADVANCED_DATA_COLUMN_KEYWORD = "KEYWORD"; constexpr const char *ADVANCED_DATA_COLUMN_VALUE = "VALUE"; } // namespace +void DoNotDisturbEnableObserver::OnChange() +{ + AdvancedNotificationService::GetInstance()->QueryDoNotDisturbState(); +} + AdvancedDatashareHelper::AdvancedDatashareHelper() { CreateDataShareHelper(); @@ -68,13 +74,11 @@ bool AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, std::strin ANS_LOGE("Query error, result is null."); return false; } - if (result->GoToFirstRow() != DataShare::E_OK) { ANS_LOGE("Query failed, go to first row error."); result->Close(); return false; } - int32_t columnIndex; result->GetColumnIndex(ADVANCED_DATA_COLUMN_VALUE, columnIndex); result->GetString(columnIndex, value); @@ -83,6 +87,21 @@ bool AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, std::strin return true; } +bool AdvancedDatashareHelper::RegisterObserver(const Uri &uri, const sptr &observer) +{ + if (dataShareHelper_ == nullptr) { + ANS_LOGE("The data share helper is nullptr."); + return false; + } + if (observer == nullptr) { + ANS_LOGE("The observer is nullptr."); + return false; + } + dataShareHelper_->RegisterObserver(uri, observer); + dataShareHelper_->Release(); + return false; +} + std::string AdvancedDatashareHelper::GetFocusModeEnableUri() const { std::vector accountIds; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 7ec8bcad2..d0ee0f07c 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -591,59 +591,93 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( return result; } -void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr &record) +void AdvancedNotificationService::QueryDoNotDisturbState() { - auto datashareHelper = std::make_shared(); + ANS_LOGD("Called."); + auto datashareHelper = DelayedSingleton::GetInstance(); if (datashareHelper == nullptr) { ANS_LOGE("The data share helper is nullptr."); return; } - std::string enable; Uri enableUri(datashareHelper->GetFocusModeEnableUri()); - bool ret = datashareHelper->Query(enableUri, KEY_FOCUS_MODE_ENABLE, enable); + bool ret = datashareHelper->Query(enableUri, KEY_FOCUS_MODE_ENABLE, focusModeEnable_); if (!ret) { - ANS_LOGE("Query focus mode enable fail, focus mode enable is %{public}s.", enable.c_str()); + ANS_LOGE("Query focus mode enable fail"); + return; + } + ANS_LOGD("Query focus mode enable success, focus mode enable is %{public}s.", focusModeEnable_.c_str()); + + Uri idUri(datashareHelper->GetFocusModeProfileUri()); + ret = datashareHelper->Query(idUri, KEY_FOCUS_MODE_PROFILE, focusModeProfile_); + if (!ret) { + ANS_LOGE("Query focus mode profile fail."); + return; + } + ANS_LOGD("Query focus mode profile success, profile id is %{public}s.", focusModeProfile_.c_str()); +} + +void AdvancedNotificationService::RegisterDoNotDisturbObserver() +{ + ANS_LOGD("Called."); + auto datashareHelper = DelayedSingleton::GetInstance(); + if (datashareHelper == nullptr) { + ANS_LOGE("The data share helper is nullptr."); + return; + } + auto observer = sptr(new (std::nothrow) DoNotDisturbEnableObserver()); + if (observer == nullptr) { + ANS_LOGE("Observer is nullptr."); return; } - if (enable != DO_NOT_DISTURB_MODE) { + Uri uri(datashareHelper->GetFocusModeEnableUri()); + datashareHelper->RegisterObserver(uri, observer); +} + +void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr &record) +{ + if (!queryDoNotDisturbOnceFlag_) { + QueryDoNotDisturbState(); + RegisterDoNotDisturbObserver(); + queryDoNotDisturbOnceFlag_ = true; + } + + if (focusModeEnable_ != DO_NOT_DISTURB_MODE) { ANS_LOGI("Currently not is do not disturb mode."); return; } + std::string bundleName = GetClientBundleName(); if (bundleName == MMS_BUNDLE_NAME || bundleName == CONTACTS_BUNDLE_NAME) { ANS_LOGI("Currently in do not disturb mode, the bundle name is mms or contacts, keep reminder method."); return; } - std::string profileId; - Uri idUri(datashareHelper->GetFocusModeProfileUri()); - ret = datashareHelper->Query(idUri, KEY_FOCUS_MODE_PROFILE, profileId); - if (!ret) { - ANS_LOGE("Query focus mode id fail, focus mode profile id is %{public}s.", profileId.c_str()); - return; - } - sptr profile = new (std::nothrow) NotificationDoNotDisturbProfile(); + + sptr profile; int32_t userId = record->notification->GetUserId(); - if (NotificationPreferences::GetInstance().GetDonotDisturbProfile(atoi(profileId.c_str()), userId, profile) != - ERR_OK) { + if (NotificationPreferences::GetInstance().GetDonotDisturbProfile( + atoi(focusModeProfile_.c_str()), userId, profile) != ERR_OK) { ANS_LOGE("Get do not disturb profile failed."); return; } + if (profile == nullptr) { ANS_LOGE("The do not disturb profile is nullptr."); return; } - std::vector trustlist = profile->GetProfileTrustlist(); + auto trustlist = profile->GetProfileTrustlist(); for (auto &trust : trustlist) { if (bundleName == trust.GetBundleName()) { ANS_LOGW("Do not disturb profile bundle name is in trust."); return; } } + DoNotDisturbUpdataReminderFlags(record); } void AdvancedNotificationService::DoNotDisturbUpdataReminderFlags(const std::shared_ptr &record) { + ANS_LOGD("Called."); if (record == nullptr && record->request == nullptr && record->notification == nullptr) { ANS_LOGE("Make notification record failed."); return; @@ -653,6 +687,7 @@ void AdvancedNotificationService::DoNotDisturbUpdataReminderFlags(const std::sha ANS_LOGE("The flags is nullptr."); return; } + flags->SetSoundEnabled(NotificationConstant::FlagStatus::CLOSE); record->notification->SetEnableSound(false); flags->SetLockScreenVisblenessEnabled(false); -- Gitee From 3b772f42cd779940c9e819074d0de9f824a683a6 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Thu, 28 Mar 2024 13:48:50 +0800 Subject: [PATCH 12/15] modify code Signed-off-by: xinking129 --- services/ans/src/advanced_notification_service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index d0ee0f07c..9491aee15 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -655,7 +655,7 @@ void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr sptr profile; int32_t userId = record->notification->GetUserId(); if (NotificationPreferences::GetInstance().GetDonotDisturbProfile( - atoi(focusModeProfile_.c_str()), userId, profile) != ERR_OK) { + atoi(focusModeProfile_.c_str()), userId, profile) != ERR_OK) { ANS_LOGE("Get do not disturb profile failed."); return; } -- Gitee From 1878d18bf070f8c50bbf9ee78319a1156601e403 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Thu, 28 Mar 2024 20:28:49 +0800 Subject: [PATCH 13/15] modify code Signed-off-by: xinking129 --- services/ans/BUILD.gn | 1 - .../ans/include/advanced_datashare_helper.h | 13 +--- .../include/advanced_notification_service.h | 5 -- .../ans/src/advanced_datashare_helper.cpp | 21 ------ .../ans/src/advanced_notification_service.cpp | 67 +++++-------------- 5 files changed, 19 insertions(+), 88 deletions(-) diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index 4214f4edb..80ca33387 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -106,7 +106,6 @@ ohos_shared_library("libans") { external_deps = [ "ability_runtime:ability_manager", "ability_runtime:app_manager", - "ability_runtime:dataobs_manager", "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", diff --git a/services/ans/include/advanced_datashare_helper.h b/services/ans/include/advanced_datashare_helper.h index 7b3e59239..b86d666ed 100644 --- a/services/ans/include/advanced_datashare_helper.h +++ b/services/ans/include/advanced_datashare_helper.h @@ -16,11 +16,9 @@ #ifndef NOTIFICATION_ADVANCED_DATASHAER_HELPER_H #define NOTIFICATION_ADVANCED_DATASHAER_HELPER_H -#include - -#include "data_ability_observer_stub.h" #include "datashare_helper.h" #include "iremote_broker.h" +#include "singleton.h" #include "system_ability_definition.h" #include "uri.h" @@ -31,20 +29,11 @@ constexpr const char *KEY_FOCUS_MODE_ENABLE = "focus_mode_enable"; constexpr const char *KEY_FOCUS_MODE_PROFILE = "focus_mode_profile"; } // namespace -class DoNotDisturbEnableObserver : public AAFwk::DataAbilityObserverStub { -public: - ~DoNotDisturbEnableObserver() = default; - - void OnChange() override; -}; - class AdvancedDatashareHelper : DelayedSingleton { public: AdvancedDatashareHelper(); ~AdvancedDatashareHelper() = default; - bool Query(Uri &uri, const std::string &key, std::string &value); - bool RegisterObserver(const Uri &uri, const sptr &observer); std::string GetFocusModeEnableUri() const; std::string GetFocusModeProfileUri() const; diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 6a111efe6..3801582dd 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -762,8 +762,6 @@ public: ErrCode GetEnabledForBundleSlot(const sptr &bundleOption, const NotificationConstant::SlotType &slotType, bool &enabled) override; ErrCode GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType &slotType, bool &enabled) override; - void QueryDoNotDisturbState(); - void RegisterDoNotDisturbObserver(); // SystemEvent @@ -1237,8 +1235,6 @@ private: std::shared_ptr notificationSvrQueue_ = nullptr; static std::string supportCheckSaPermission_; std::map> publishProcess_; - std::string focusModeEnable_; - std::string focusModeProfile_; #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED NotificationConstant::DistributedReminderPolicy distributedReminderPolicy_ = DEFAULT_DISTRIBUTED_REMINDER_POLICY; bool localScreenOn_ = true; @@ -1247,7 +1243,6 @@ private: std::shared_ptr notificationSlotFilter_ = nullptr; std::shared_ptr dialogManager_ = nullptr; std::list> uniqueKeyList_; - bool queryDoNotDisturbOnceFlag_ = false; }; /** diff --git a/services/ans/src/advanced_datashare_helper.cpp b/services/ans/src/advanced_datashare_helper.cpp index 15c208fdb..9fa8b56a8 100644 --- a/services/ans/src/advanced_datashare_helper.cpp +++ b/services/ans/src/advanced_datashare_helper.cpp @@ -15,7 +15,6 @@ #include "advanced_datashare_helper.h" -#include "advanced_notification_service.h" #include "ans_log_wrapper.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" @@ -35,11 +34,6 @@ constexpr const char *FOCUS_MODE_PROFILE_URI = "?Proxy=true&key=focus_mode_profi constexpr const char *ADVANCED_DATA_COLUMN_KEYWORD = "KEYWORD"; constexpr const char *ADVANCED_DATA_COLUMN_VALUE = "VALUE"; } // namespace -void DoNotDisturbEnableObserver::OnChange() -{ - AdvancedNotificationService::GetInstance()->QueryDoNotDisturbState(); -} - AdvancedDatashareHelper::AdvancedDatashareHelper() { CreateDataShareHelper(); @@ -87,21 +81,6 @@ bool AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, std::strin return true; } -bool AdvancedDatashareHelper::RegisterObserver(const Uri &uri, const sptr &observer) -{ - if (dataShareHelper_ == nullptr) { - ANS_LOGE("The data share helper is nullptr."); - return false; - } - if (observer == nullptr) { - ANS_LOGE("The observer is nullptr."); - return false; - } - dataShareHelper_->RegisterObserver(uri, observer); - dataShareHelper_->Release(); - return false; -} - std::string AdvancedDatashareHelper::GetFocusModeEnableUri() const { std::vector accountIds; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 9491aee15..439a0f35e 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -591,7 +591,7 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( return result; } -void AdvancedNotificationService::QueryDoNotDisturbState() +void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr &record) { ANS_LOGD("Called."); auto datashareHelper = DelayedSingleton::GetInstance(); @@ -599,79 +599,49 @@ void AdvancedNotificationService::QueryDoNotDisturbState() ANS_LOGE("The data share helper is nullptr."); return; } + std::string enable; Uri enableUri(datashareHelper->GetFocusModeEnableUri()); - bool ret = datashareHelper->Query(enableUri, KEY_FOCUS_MODE_ENABLE, focusModeEnable_); - if (!ret) { - ANS_LOGE("Query focus mode enable fail"); - return; - } - ANS_LOGD("Query focus mode enable success, focus mode enable is %{public}s.", focusModeEnable_.c_str()); - - Uri idUri(datashareHelper->GetFocusModeProfileUri()); - ret = datashareHelper->Query(idUri, KEY_FOCUS_MODE_PROFILE, focusModeProfile_); + bool ret = datashareHelper->Query(enableUri, KEY_FOCUS_MODE_ENABLE, enable); if (!ret) { - ANS_LOGE("Query focus mode profile fail."); - return; - } - ANS_LOGD("Query focus mode profile success, profile id is %{public}s.", focusModeProfile_.c_str()); -} - -void AdvancedNotificationService::RegisterDoNotDisturbObserver() -{ - ANS_LOGD("Called."); - auto datashareHelper = DelayedSingleton::GetInstance(); - if (datashareHelper == nullptr) { - ANS_LOGE("The data share helper is nullptr."); - return; - } - auto observer = sptr(new (std::nothrow) DoNotDisturbEnableObserver()); - if (observer == nullptr) { - ANS_LOGE("Observer is nullptr."); + ANS_LOGE("Query focus mode enable fail."); return; } - Uri uri(datashareHelper->GetFocusModeEnableUri()); - datashareHelper->RegisterObserver(uri, observer); -} - -void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr &record) -{ - if (!queryDoNotDisturbOnceFlag_) { - QueryDoNotDisturbState(); - RegisterDoNotDisturbObserver(); - queryDoNotDisturbOnceFlag_ = true; - } - - if (focusModeEnable_ != DO_NOT_DISTURB_MODE) { + ANS_LOGD("Query focus mode enable success, focus mode enable is %{public}s.", enable.c_str()); + if (enable != DO_NOT_DISTURB_MODE) { ANS_LOGI("Currently not is do not disturb mode."); return; } - std::string bundleName = GetClientBundleName(); if (bundleName == MMS_BUNDLE_NAME || bundleName == CONTACTS_BUNDLE_NAME) { ANS_LOGI("Currently in do not disturb mode, the bundle name is mms or contacts, keep reminder method."); return; } - - sptr profile; + std::string profileId; + Uri idUri(datashareHelper->GetFocusModeProfileUri()); + ret = datashareHelper->Query(idUri, KEY_FOCUS_MODE_PROFILE, profileId); + if (!ret) { + ANS_LOGE("Query focus mode id fail."); + return; + } + ANS_LOGD("Query focus mode id success, focus mode profile id is %{public}s.", profileId.c_str()); + sptr profile = new (std::nothrow) NotificationDoNotDisturbProfile(); int32_t userId = record->notification->GetUserId(); - if (NotificationPreferences::GetInstance().GetDonotDisturbProfile( - atoi(focusModeProfile_.c_str()), userId, profile) != ERR_OK) { + if (NotificationPreferences::GetInstance().GetDonotDisturbProfile(atoi(profileId.c_str()), userId, profile) != + ERR_OK) { ANS_LOGE("Get do not disturb profile failed."); return; } - if (profile == nullptr) { ANS_LOGE("The do not disturb profile is nullptr."); return; } - auto trustlist = profile->GetProfileTrustlist(); + std::vector trustlist = profile->GetProfileTrustlist(); for (auto &trust : trustlist) { if (bundleName == trust.GetBundleName()) { ANS_LOGW("Do not disturb profile bundle name is in trust."); return; } } - DoNotDisturbUpdataReminderFlags(record); } @@ -687,7 +657,6 @@ void AdvancedNotificationService::DoNotDisturbUpdataReminderFlags(const std::sha ANS_LOGE("The flags is nullptr."); return; } - flags->SetSoundEnabled(NotificationConstant::FlagStatus::CLOSE); record->notification->SetEnableSound(false); flags->SetLockScreenVisblenessEnabled(false); -- Gitee From 8e166c1c3d56d5ddcd8758371baa4191334db115 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Thu, 28 Mar 2024 20:53:46 +0800 Subject: [PATCH 14/15] remove log Signed-off-by: xinking129 --- services/ans/src/advanced_notification_service.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 439a0f35e..e350a905c 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -593,7 +593,6 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr &record) { - ANS_LOGD("Called."); auto datashareHelper = DelayedSingleton::GetInstance(); if (datashareHelper == nullptr) { ANS_LOGE("The data share helper is nullptr."); @@ -606,7 +605,6 @@ void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr ANS_LOGE("Query focus mode enable fail."); return; } - ANS_LOGD("Query focus mode enable success, focus mode enable is %{public}s.", enable.c_str()); if (enable != DO_NOT_DISTURB_MODE) { ANS_LOGI("Currently not is do not disturb mode."); return; @@ -623,7 +621,6 @@ void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr ANS_LOGE("Query focus mode id fail."); return; } - ANS_LOGD("Query focus mode id success, focus mode profile id is %{public}s.", profileId.c_str()); sptr profile = new (std::nothrow) NotificationDoNotDisturbProfile(); int32_t userId = record->notification->GetUserId(); if (NotificationPreferences::GetInstance().GetDonotDisturbProfile(atoi(profileId.c_str()), userId, profile) != -- Gitee From b2585f6f02d89f5dbc0c2075ed8db776e3d87ffa Mon Sep 17 00:00:00 2001 From: xinking129 Date: Mon, 1 Apr 2024 15:20:07 +0800 Subject: [PATCH 15/15] modify code Signed-off-by: xinking129 --- .../notification_do_not_disturb_profile.cpp | 7 +++-- .../core/common/include/ans_const_define.h | 2 +- .../include/advanced_notification_service.h | 1 + .../ans/src/advanced_notification_service.cpp | 30 ++++++++++++++----- services/ans/src/notification_preferences.cpp | 14 +++++++++ 5 files changed, 43 insertions(+), 11 deletions(-) diff --git a/frameworks/ans/src/notification_do_not_disturb_profile.cpp b/frameworks/ans/src/notification_do_not_disturb_profile.cpp index 025a8f33f..de3fede4e 100644 --- a/frameworks/ans/src/notification_do_not_disturb_profile.cpp +++ b/frameworks/ans/src/notification_do_not_disturb_profile.cpp @@ -151,8 +151,11 @@ void NotificationDoNotDisturbProfile::FromJson(const std::string &jsonObj) if (jsonObject.contains(DO_NOT_DISTURB_PROFILE_TRUSTLIST) && jsonObject[DO_NOT_DISTURB_PROFILE_TRUSTLIST].is_array()) { for (auto &trust : jsonObject.at(DO_NOT_DISTURB_PROFILE_TRUSTLIST)) { - sptr bundleOption = new (std::nothrow) NotificationBundleOption(); - trustList_.emplace_back(*bundleOption->FromJson(trust)); + auto bundleOption = NotificationBundleOption::FromJson(trust); + if (bundleOption == nullptr) { + continue; + } + trustList_.emplace_back(*bundleOption); } } } diff --git a/frameworks/core/common/include/ans_const_define.h b/frameworks/core/common/include/ans_const_define.h index 49e1dd0e6..2ca4ce9a7 100644 --- a/frameworks/core/common/include/ans_const_define.h +++ b/frameworks/core/common/include/ans_const_define.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 3801582dd..285ce9f80 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1192,6 +1192,7 @@ private: void StartArchiveTimer(const std::shared_ptr &record); void CancelArchiveTimer(const std::shared_ptr &record); void ProcForDeleteLiveView(const std::shared_ptr &record); + void QueryDoNotDisturbProfile(std::string &enable, std::string &profileId); void CheckDoNotDisturbProfile(const std::shared_ptr &record); void DoNotDisturbUpdataReminderFlags(const std::shared_ptr &record); ErrCode CheckCommonParams(); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index e350a905c..1400e75c6 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -591,14 +591,13 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( return result; } -void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr &record) +void AdvancedNotificationService::QueryDoNotDisturbProfile(std::string &enable, std::string &profileId) { auto datashareHelper = DelayedSingleton::GetInstance(); if (datashareHelper == nullptr) { ANS_LOGE("The data share helper is nullptr."); return; } - std::string enable; Uri enableUri(datashareHelper->GetFocusModeEnableUri()); bool ret = datashareHelper->Query(enableUri, KEY_FOCUS_MODE_ENABLE, enable); if (!ret) { @@ -609,18 +608,33 @@ void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr ANS_LOGI("Currently not is do not disturb mode."); return; } - std::string bundleName = GetClientBundleName(); - if (bundleName == MMS_BUNDLE_NAME || bundleName == CONTACTS_BUNDLE_NAME) { - ANS_LOGI("Currently in do not disturb mode, the bundle name is mms or contacts, keep reminder method."); - return; - } - std::string profileId; Uri idUri(datashareHelper->GetFocusModeProfileUri()); ret = datashareHelper->Query(idUri, KEY_FOCUS_MODE_PROFILE, profileId); if (!ret) { ANS_LOGE("Query focus mode id fail."); return; } +} + +void AdvancedNotificationService::CheckDoNotDisturbProfile(const std::shared_ptr &record) +{ + ANS_LOGD("Called."); + if (record == nullptr && record->notification == nullptr) { + ANS_LOGE("Make notification record failed."); + return; + } + std::string enable; + std::string profileId; + QueryDoNotDisturbProfile(enable, profileId); + if (enable != DO_NOT_DISTURB_MODE) { + ANS_LOGI("Currently not is do not disturb mode."); + return; + } + std::string bundleName = GetClientBundleName(); + if (bundleName == MMS_BUNDLE_NAME || bundleName == CONTACTS_BUNDLE_NAME) { + ANS_LOGI("Currently in do not disturb mode, the bundle name is mms or contacts, keep reminder method."); + return; + } sptr profile = new (std::nothrow) NotificationDoNotDisturbProfile(); int32_t userId = record->notification->GetUserId(); if (NotificationPreferences::GetInstance().GetDonotDisturbProfile(atoi(profileId.c_str()), userId, profile) != diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index d4c7bcde6..5aa62462d 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -487,6 +487,10 @@ ErrCode NotificationPreferences::AddDoNotDisturbProfiles( { ANS_LOGE("Called."); for (auto profile : profiles) { + if (profile == nullptr) { + ANS_LOGE("The profile is nullptr."); + return ERR_ANS_INVALID_PARAM; + } if (!CheckDoNotDisturbProfileID(profile->GetProfileId())) { return ERR_ANS_INVALID_PARAM; } @@ -494,6 +498,9 @@ ErrCode NotificationPreferences::AddDoNotDisturbProfiles( std::lock_guard lock(preferenceMutex_); NotificationPreferencesInfo preferencesInfo = preferencesInfo_; preferencesInfo.AddDoNotDisturbProfiles(userId, profiles); + if (preferncesDB_ == nullptr) { + return ERR_ANS_SERVICE_NOT_READY; + } if (!preferncesDB_->AddDoNotDisturbProfiles(userId, profiles)) { return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; } @@ -506,6 +513,10 @@ ErrCode NotificationPreferences::RemoveDoNotDisturbProfiles( { ANS_LOGE("Called."); for (auto profile : profiles) { + if (profile == nullptr) { + ANS_LOGE("The profile is nullptr."); + return ERR_ANS_INVALID_PARAM; + } if (!CheckDoNotDisturbProfileID(profile->GetProfileId())) { return ERR_ANS_INVALID_PARAM; } @@ -513,6 +524,9 @@ ErrCode NotificationPreferences::RemoveDoNotDisturbProfiles( std::lock_guard lock(preferenceMutex_); NotificationPreferencesInfo preferencesInfo = preferencesInfo_; preferencesInfo.RemoveDoNotDisturbProfiles(userId, profiles); + if (preferncesDB_ == nullptr) { + return ERR_ANS_SERVICE_NOT_READY; + } if (!preferncesDB_->RemoveDoNotDisturbProfiles(userId, profiles)) { return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; } -- Gitee