From 02ebaabca21f37a09f49a97ef626c44d63779087 Mon Sep 17 00:00:00 2001 From: fangjinliang Date: Sat, 12 Feb 2022 15:56:39 +0800 Subject: [PATCH] notification flags modify Signed-off-by: fangjinliang --- .../ans/native/src/notification_flags.cpp | 26 ++++----- .../ans/native/src/notification_request.cpp | 2 +- .../ans_innerkits_module_publish_test.cpp | 53 +++++++++++++++++-- .../native/include/notification_constant.h | 6 +++ .../ans/native/include/notification_flags.h | 14 ++--- .../kits/js/notification/notificationFlags.ts | 29 ++++++++-- interfaces/kits/napi/ans/include/common.h | 6 +++ interfaces/kits/napi/ans/src/common.cpp | 8 +-- interfaces/kits/napi/ans/src/constant.cpp | 20 +++++++ 9 files changed, 134 insertions(+), 30 deletions(-) diff --git a/frameworks/ans/native/src/notification_flags.cpp b/frameworks/ans/native/src/notification_flags.cpp index 901964bc7..dd6461767 100644 --- a/frameworks/ans/native/src/notification_flags.cpp +++ b/frameworks/ans/native/src/notification_flags.cpp @@ -18,30 +18,30 @@ namespace OHOS { namespace Notification { -void NotificationFlags::SetSoundEnabled(bool soundEnabled) +void NotificationFlags::SetSoundEnabled(NotificationConstant::FlagStatus soundEnabled) { soundEnabled_ = soundEnabled; } -bool NotificationFlags::IsSoundEnabled() const +NotificationConstant::FlagStatus NotificationFlags::IsSoundEnabled() const { return soundEnabled_; } -void NotificationFlags::SetVibrationEnabled(bool vibrationEnabled) +void NotificationFlags::SetVibrationEnabled(NotificationConstant::FlagStatus vibrationEnabled) { vibrationEnabled_ = vibrationEnabled; } -bool NotificationFlags::IsVibrationEnabled() const +NotificationConstant::FlagStatus NotificationFlags::IsVibrationEnabled() const { return vibrationEnabled_; } std::string NotificationFlags::Dump() { - return "soundEnabled = " + std::string(soundEnabled_ ? "true" : "false") + - ", vibrationEnabled = " + std::string(vibrationEnabled_ ? "true" : "false"); + return "soundEnabled = " + std::to_string(static_cast(soundEnabled_)) + + ", vibrationEnabled = " + std::to_string(static_cast(vibrationEnabled_)); } bool NotificationFlags::ToJson(nlohmann::json &jsonObject) const @@ -67,11 +67,13 @@ NotificationFlags *NotificationFlags::FromJson(const nlohmann::json &jsonObject) const auto &jsonEnd = jsonObject.cend(); if (jsonObject.find("soundEnabled") != jsonEnd) { - pFlags->soundEnabled_ = jsonObject.at("soundEnabled").get(); + auto soundEnabled = jsonObject.at("soundEnabled").get(); + pFlags->soundEnabled_ = static_cast(soundEnabled); } if (jsonObject.find("vibrationEnabled") != jsonEnd) { - pFlags->vibrationEnabled_ = jsonObject.at("vibrationEnabled").get(); + auto vibrationEnabled = jsonObject.at("vibrationEnabled").get(); + pFlags->vibrationEnabled_ = static_cast(vibrationEnabled); } return pFlags; @@ -79,12 +81,12 @@ NotificationFlags *NotificationFlags::FromJson(const nlohmann::json &jsonObject) bool NotificationFlags::Marshalling(Parcel &parcel) const { - if (!parcel.WriteBool(soundEnabled_)) { + if (!parcel.WriteUint8(static_cast(soundEnabled_))) { ANS_LOGE("Failed to write flag sound enable for the notification"); return false; } - if (!parcel.WriteBool(vibrationEnabled_)) { + if (!parcel.WriteUint8(static_cast(vibrationEnabled_))) { ANS_LOGE("Failed to write flag vibration enable for the notification"); return false; } @@ -105,8 +107,8 @@ NotificationFlags *NotificationFlags::Unmarshalling(Parcel &parcel) bool NotificationFlags::ReadFromParcel(Parcel &parcel) { - soundEnabled_ = parcel.ReadBool(); - vibrationEnabled_ = parcel.ReadBool(); + soundEnabled_ = static_cast(parcel.ReadUint8()); + vibrationEnabled_ = static_cast(parcel.ReadUint8()); return true; } diff --git a/frameworks/ans/native/src/notification_request.cpp b/frameworks/ans/native/src/notification_request.cpp index 9deace200..e08144147 100644 --- a/frameworks/ans/native/src/notification_request.cpp +++ b/frameworks/ans/native/src/notification_request.cpp @@ -854,7 +854,7 @@ bool NotificationRequest::Marshalling(Parcel &parcel) const } if (!parcel.WriteInt32(static_cast(receiverUserId_))) { - ANS_LOGE("Failed to write creator userId"); + ANS_LOGE("Failed to write receiver userId"); return false; } diff --git a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp index a8ac9f307..25be115ad 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -56,6 +56,7 @@ const int32_t CASE_TWELVE = 12; const int32_t CASE_THIRTEEN = 13; const int32_t CASE_FOURTEEN = 14; const int32_t CASE_FIFTEEN = 15; +const int32_t CASE_SIXTEEN = 16; const int32_t CALLING_UID = 9999; const int32_t PIXEL_MAP_TEST_WIDTH = 32; @@ -139,6 +140,8 @@ public: CheckCaseFourteenResult(notificationRequest); } else if (CASE_FIFTEEN == notificationRequest.GetNotificationId()) { CheckCaseFifteenResult(notificationRequest); + } else if (CASE_SIXTEEN == notificationRequest.GetNotificationId()) { + CheckCaseSixteenResult(notificationRequest); } else { GTEST_LOG_(INFO) << "ANS_Interface_MT_Publish::OnConsumed do nothing!!!!!"; } @@ -377,8 +380,17 @@ private: { std::shared_ptr notiFlags = notificationRequest.GetFlags(); if (notiFlags != nullptr) { - EXPECT_EQ(false, notiFlags->IsSoundEnabled()); - EXPECT_EQ(true, notiFlags->IsVibrationEnabled()); + EXPECT_EQ(NotificationConstant::FlagStatus::CLOSE, notiFlags->IsSoundEnabled()); + EXPECT_EQ(NotificationConstant::FlagStatus::OPEN, notiFlags->IsVibrationEnabled()); + } + } + + void CheckCaseSixteenResult(NotificationRequest notificationRequest) + { + std::shared_ptr notiFlags = notificationRequest.GetFlags(); + if (notiFlags != nullptr) { + EXPECT_EQ(NotificationConstant::FlagStatus::NONE, notiFlags->IsSoundEnabled()); + EXPECT_EQ(NotificationConstant::FlagStatus::NONE, notiFlags->IsVibrationEnabled()); } } }; @@ -1359,8 +1371,8 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_05000, Function std::shared_ptr notiFlags = std::make_shared(); EXPECT_NE(notiFlags, nullptr); - notiFlags->SetSoundEnabled(false); - notiFlags->SetVibrationEnabled(true); + notiFlags->SetSoundEnabled(NotificationConstant::FlagStatus::CLOSE); + notiFlags->SetVibrationEnabled(NotificationConstant::FlagStatus::OPEN); GTEST_LOG_(INFO) << "ANS_Interface_MT_Publish_04000::flags::" << notiFlags->Dump(); std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -1378,5 +1390,38 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_05000, Function EXPECT_EQ(0, NotificationHelper::UnSubscribeNotification(subscriber, info)); WaitOnUnsubscribeResult(); } + +/** + * @tc.number : ANS_Interface_MT_Publish_06000 + * @tc.name : Publish_06000 + * @tc.desc : Add notification slot(type is OTHER), make a subscriber and publish a flags notification. + * @tc.expected : Add notification slot success, make a subscriber and publish default flags notification success. + */ +HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_06000, Function | MediumTest | Level1) +{ + NotificationSlot slot(NotificationConstant::OTHER); + EXPECT_EQ(0, NotificationHelper::AddNotificationSlot(slot)); + auto subscriber = TestAnsSubscriber(); + NotificationSubscribeInfo info = NotificationSubscribeInfo(); + info.AddAppName("bundleName"); + g_subscribe_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); + WaitOnSubscribeResult(); + + std::shared_ptr normalContent = std::make_shared(); + EXPECT_NE(normalContent, nullptr); + std::shared_ptr content = std::make_shared(normalContent); + EXPECT_NE(content, nullptr); + NotificationRequest req; + req.SetContent(content); + req.SetSlotType(NotificationConstant::OTHER); + req.SetNotificationId(CASE_SIXTEEN); + g_consumed_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); + WaitOnConsumed(); + g_unsubscribe_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::UnSubscribeNotification(subscriber, info)); + WaitOnUnsubscribeResult(); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/ans/native/include/notification_constant.h b/interfaces/innerkits/ans/native/include/notification_constant.h index b4e7bec4c..c9dd27dc6 100644 --- a/interfaces/innerkits/ans/native/include/notification_constant.h +++ b/interfaces/innerkits/ans/native/include/notification_constant.h @@ -120,6 +120,12 @@ public: TYPE_TIMER = 0x00000002 }; + enum class FlagStatus { + NONE, + OPEN, + CLOSE + }; + /** * Indicates that a notification is deleted because it is clicked. */ diff --git a/interfaces/innerkits/ans/native/include/notification_flags.h b/interfaces/innerkits/ans/native/include/notification_flags.h index 09664ed4d..5a53ab178 100644 --- a/interfaces/innerkits/ans/native/include/notification_flags.h +++ b/interfaces/innerkits/ans/native/include/notification_flags.h @@ -18,6 +18,8 @@ #include #include "parcel.h" + +#include "notification_constant.h" #include "notification_json_convert.h" namespace OHOS { @@ -38,25 +40,25 @@ public: * Sets the notification whether enable sound. * @param soundEnabled whether enable sound. */ - void SetSoundEnabled(bool soundEnabled); + void SetSoundEnabled(NotificationConstant::FlagStatus soundEnabled); /** * Checks whether enable sound. * @return sound enable. */ - bool IsSoundEnabled() const; + NotificationConstant::FlagStatus IsSoundEnabled() const; /** * Sets the notification whether enable vibration. * @param vibrationEnabled whether enable vibration. */ - void SetVibrationEnabled(bool vibrationEnabled); + void SetVibrationEnabled(NotificationConstant::FlagStatus vibrationEnabled); /** * Checks whether enable vibration. * @return vibration enable. */ - bool IsVibrationEnabled() const; + NotificationConstant::FlagStatus IsVibrationEnabled() const; /** * Returns a string representation of the object. @@ -97,8 +99,8 @@ private: bool ReadFromParcel(Parcel &parcel); private: - bool soundEnabled_ {true}; - bool vibrationEnabled_ {false}; + NotificationConstant::FlagStatus soundEnabled_ {NotificationConstant::FlagStatus::NONE}; + NotificationConstant::FlagStatus vibrationEnabled_ {NotificationConstant::FlagStatus::NONE}; }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/kits/js/notification/notificationFlags.ts b/interfaces/kits/js/notification/notificationFlags.ts index 58c4a6831..e89f2cdb8 100644 --- a/interfaces/kits/js/notification/notificationFlags.ts +++ b/interfaces/kits/js/notification/notificationFlags.ts @@ -13,12 +13,35 @@ * limitations under the License. */ +/** + * The status of the notification flag. + * + * @since 8 + * @systemapi Hide this for inner system use. + * @sysCap SystemCapability.Notification.Notification + */ + export enum NotificationFlagStatus { + /** + * notification flag default value + */ + TYPE_NONE = 0, + + /** + * notification flag open + */ + TYPE_OPEN = 1, + + /** + * notification flag close + */ + TYPE_CLOSE = 2, +} + /** * Describes a NotificationFlags instance. * * @name NotificationFlags * @since 8 - * @devices phone, tablet, tv, wearable, car * @permission N/A * @sysCap SystemCapability.Notification.ANS */ @@ -26,10 +49,10 @@ /** * Whether to enable sound reminder. */ - readonly soundEnabled?: boolean; + readonly soundEnabled?: NotificationFlagStatus; /** * Whether to enable vibration reminder. */ - readonly vibrationEnabled?: boolean; + readonly vibrationEnabled?: NotificationFlagStatus; } diff --git a/interfaces/kits/napi/ans/include/common.h b/interfaces/kits/napi/ans/include/common.h index ba4cd1aad..b998da774 100644 --- a/interfaces/kits/napi/ans/include/common.h +++ b/interfaces/kits/napi/ans/include/common.h @@ -120,6 +120,12 @@ enum class DeviceRemindType { ACTIVE_REMIND }; +enum class NotificationFlagStatus { + TYPE_NONE, + TYPE_OPEN, + TYPE_CLOSE +}; + struct NotificationSubscribeInfo { std::vector bundleNames; int userId = 0; diff --git a/interfaces/kits/napi/ans/src/common.cpp b/interfaces/kits/napi/ans/src/common.cpp index d9c7607c3..76576b565 100644 --- a/interfaces/kits/napi/ans/src/common.cpp +++ b/interfaces/kits/napi/ans/src/common.cpp @@ -4120,12 +4120,12 @@ napi_value Common::SetNotificationFlags( napi_value value = nullptr; - // readonly soundEnabled?: boolean - napi_get_boolean(env, flags->IsSoundEnabled(), &value); + int32_t soundEnabled = static_cast(flags->IsSoundEnabled()); + napi_create_int32(env, soundEnabled, &value); napi_set_named_property(env, result, "soundEnabled", value); - // readonly vibrationEnabled?: boolean - napi_get_boolean(env, flags->IsVibrationEnabled(), &value); + int32_t vibrationEnabled = static_cast(flags->IsVibrationEnabled()); + napi_create_int32(env, vibrationEnabled, &value); napi_set_named_property(env, result, "vibrationEnabled", value); return NapiGetBoolean(env, true); diff --git a/interfaces/kits/napi/ans/src/constant.cpp b/interfaces/kits/napi/ans/src/constant.cpp index 96443b732..5443dd1dc 100644 --- a/interfaces/kits/napi/ans/src/constant.cpp +++ b/interfaces/kits/napi/ans/src/constant.cpp @@ -279,6 +279,25 @@ napi_value SourceTypeInit(napi_env env, napi_value exports) return exports; } +napi_value NotificationFlagTypeInit(napi_env env, napi_value exports) +{ + ANS_LOGI("%{public}s, called", __func__); + + napi_value obj = nullptr; + napi_create_object(env, &obj); + + SetNamedPropertyByInteger(env, obj, (int32_t)NotificationFlagStatus::TYPE_NONE, "TYPE_NONE"); + SetNamedPropertyByInteger(env, obj, (int32_t)NotificationFlagStatus::TYPE_OPEN, "TYPE_OPEN"); + SetNamedPropertyByInteger(env, obj, (int32_t)NotificationFlagStatus::TYPE_CLOSE, "TYPE_CLOSE"); + + napi_property_descriptor exportFuncs[] = { + DECLARE_NAPI_PROPERTY("NotificationFlagStatus", obj), + }; + + napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs); + return exports; +} + napi_value ConstantInit(napi_env env, napi_value exports) { NotificationReasonInit(env, exports); @@ -292,6 +311,7 @@ napi_value ConstantInit(napi_env env, napi_value exports) SourceTypeInit(env, exports); DoNotDisturbTypeInit(env, exports); DeviceRemindTypeInit(env, exports); + NotificationFlagTypeInit(env, exports); return exports; } } // namespace NotificationNapi -- Gitee