diff --git a/frameworks/ans/core/BUILD.gn b/frameworks/ans/core/BUILD.gn index 346ddd631b302e5e9b14e54aee4d76d76fd96c5f..f099706bda0790b8a86d92883933b4d022cd8434 100644 --- a/frameworks/ans/core/BUILD.gn +++ b/frameworks/ans/core/BUILD.gn @@ -78,6 +78,7 @@ ohos_shared_library("ans_core") { "${frameworks_path}/ans/native/src/reminder_request.cpp", "${frameworks_path}/ans/native/src/reminder_request_alarm.cpp", "${frameworks_path}/ans/native/src/reminder_request_timer.cpp", + "${frameworks_path}/ans/native/src/notification_flags.cpp", ] configs = [ ":private_config" ] diff --git a/frameworks/ans/native/BUILD.gn b/frameworks/ans/native/BUILD.gn index 831a1d1f9996eec56d057cbee42ea0123e182887..2e26d7604491b360a3a3a5e3630fa764d4956c2e 100644 --- a/frameworks/ans/native/BUILD.gn +++ b/frameworks/ans/native/BUILD.gn @@ -71,6 +71,7 @@ ohos_shared_library("ans_innerkits") { "src/notification_subscriber.cpp", "src/notification_template.cpp", "src/notification_user_input.cpp", + "src/notification_flags.cpp", ] configs = [ diff --git a/frameworks/ans/native/src/notification_flags.cpp b/frameworks/ans/native/src/notification_flags.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00a08f9f9392cd9ea5f105199b8677ff14a8d52a --- /dev/null +++ b/frameworks/ans/native/src/notification_flags.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2021 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_flags.h" +#include "ans_log_wrapper.h" + +namespace OHOS { +namespace Notification { +void NotificationFlags::SetSoundEnabled(bool soundEnabled) +{ + soundEnabled_ = soundEnabled; +} + +bool NotificationFlags::IsSoundEnabled() const +{ + return soundEnabled_; +} + +void NotificationFlags::SetVibrationEnabled(bool vibrationEnabled) +{ + vibrationEnabled_ = vibrationEnabled; +} + +bool NotificationFlags::IsVibrationEnabled() const +{ + return vibrationEnabled_; +} + +std::string NotificationFlags::Dump() +{ + return "soundEnabled = " + std::string(soundEnabled_ ? "true" : "false") + + ", vibrationEnabled = " + std::string(vibrationEnabled_ ? "true" : "false"); +} + +bool NotificationFlags::ToJson(nlohmann::json &jsonObject) const +{ + jsonObject["soundEnabled"] = soundEnabled_; + jsonObject["vibrationEnabled"] = vibrationEnabled_; + + return true; +} + +NotificationFlags *NotificationFlags::FromJson(const nlohmann::json &jsonObject) +{ + if (jsonObject.is_null() or !jsonObject.is_object()) { + ANS_LOGE("Invalid JSON object"); + return nullptr; + } + + auto pFlags = new (std::nothrow) NotificationFlags(); + if (pFlags == nullptr) { + ANS_LOGE("Failed to create notificationFlags instance"); + return nullptr; + } + + const auto &jsonEnd = jsonObject.cend(); + if (jsonObject.find("soundEnabled") != jsonEnd) { + pFlags->soundEnabled_ = jsonObject.at("soundEnabled").get(); + } + + if (jsonObject.find("vibrationEnabled") != jsonEnd) { + pFlags->vibrationEnabled_ = jsonObject.at("vibrationEnabled").get(); + } + + return pFlags; +} + +bool NotificationFlags::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteBool(soundEnabled_)) { + ANS_LOGE("Failed to write flag sound enable for the notification"); + return false; + } + + if (!parcel.WriteBool(vibrationEnabled_)) { + ANS_LOGE("Failed to write flag vibration enable for the notification"); + return false; + } + + return true; +} + +NotificationFlags *NotificationFlags::Unmarshalling(Parcel &parcel) +{ + auto templ = new NotificationFlags(); + if ((templ != nullptr) && !templ->ReadFromParcel(parcel)) { + delete templ; + templ = nullptr; + } + + return templ; +} + +bool NotificationFlags::ReadFromParcel(Parcel &parcel) +{ + soundEnabled_ = parcel.ReadBool(); + vibrationEnabled_ = parcel.ReadBool(); + + return true; +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/ans/native/src/notification_request.cpp b/frameworks/ans/native/src/notification_request.cpp index 463ffe7dfe639e759833baaf6de59bb5d4647d7b..a486bb337ee257f14c16170255f391749259a95c 100644 --- a/frameworks/ans/native/src/notification_request.cpp +++ b/frameworks/ans/native/src/notification_request.cpp @@ -679,6 +679,7 @@ std::string NotificationRequest::Dump() ", messageUsers = " + (!messageUsers_.empty() ? messageUsers_.at(0)->Dump() : "empty") + ", userInputHistory = " + (!userInputHistory_.empty() ? userInputHistory_.at(0) : "empty") + ", distributedOptions = " + distributedOptions_.Dump() + + ", notificationFlags = " + (notificationFlags_ ? "not null" : "null") + " }"; } @@ -777,6 +778,12 @@ NotificationRequest *NotificationRequest::FromJson(const nlohmann::json &jsonObj return nullptr; } + if (!ConvertJsonToNotificationFlags(pRequest, jsonObject)) { + delete pRequest; + pRequest = nullptr; + return nullptr; + } + return pRequest; } @@ -1127,6 +1134,19 @@ bool NotificationRequest::Marshalling(Parcel &parcel) const } } + valid = notificationFlags_ ? true : false; + if (!parcel.WriteBool(valid)) { + ANS_LOGE("Failed to write flags for the notification"); + return false; + } + + if (valid) { + if (!parcel.WriteParcelable(notificationFlags_.get())) { + ANS_LOGE("Failed to write notification flags"); + return false; + } + } + return true; } @@ -1337,6 +1357,15 @@ bool NotificationRequest::ReadFromParcel(Parcel &parcel) } } + valid = parcel.ReadBool(); + if (valid) { + notificationFlags_ = std::shared_ptr(parcel.ReadParcelable()); + if (!notificationFlags_) { + ANS_LOGE("Failed to read notificationFlags"); + return false; + } + } + return true; } @@ -1359,6 +1388,16 @@ std::shared_ptr NotificationRequest::GetTemplate() const return notificationTemplate_; } +void NotificationRequest::SetFlags(const std::shared_ptr &flags) +{ + notificationFlags_ = flags; +} + +std::shared_ptr NotificationRequest::GetFlags() const +{ + return notificationFlags_; +} + void NotificationRequest::CopyBase(const NotificationRequest &other) { this->notificationId_ = other.notificationId_; @@ -1422,6 +1461,7 @@ void NotificationRequest::CopyOther(const NotificationRequest &other) this->distributedOptions_ = other.distributedOptions_; this->notificationTemplate_ = other.notificationTemplate_; + this->notificationFlags_ = other.notificationFlags_; } bool NotificationRequest::ConvertObjectsToJson(nlohmann::json &jsonObject) const @@ -1470,6 +1510,13 @@ bool NotificationRequest::ConvertObjectsToJson(nlohmann::json &jsonObject) const } jsonObject["distributedOptions"] = optObj; + nlohmann::json flagsObj; + if (!NotificationJsonConverter::ConvertToJosn(notificationFlags_.get(), flagsObj)) { + ANS_LOGE("Cannot convert notificationFlags to JSON"); + return false; + } + jsonObject["notificationFlags"] = flagsObj; + return true; } @@ -1692,5 +1739,31 @@ bool NotificationRequest::ConvertJsonToNotificationDistributedOptions( return true; } + +bool NotificationRequest::ConvertJsonToNotificationFlags( + NotificationRequest *target, const nlohmann::json &jsonObject) +{ + if (target == nullptr) { + ANS_LOGE("Invalid input parameter"); + return false; + } + + const auto &jsonEnd = jsonObject.cend(); + + if (jsonObject.find("notificationFlags") != jsonEnd) { + auto flagsObj = jsonObject.at("notificationFlags"); + if (!flagsObj.is_null()) { + auto pFlags = NotificationJsonConverter::ConvertFromJosn(flagsObj); + if (pFlags == nullptr) { + ANS_LOGE("Failed to parse notificationFlags!"); + return false; + } + + target->notificationFlags_ = std::shared_ptr(pFlags); + } + } + + return true; +} } // namespace Notification } // namespace OHOS 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 b9115de5681a661087cc68d8cdab314c0d8eccd8..67cad5bd2eb566749d9dfb3f1a70fce188c1e13f 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -55,6 +55,7 @@ const int32_t CASE_ELEVEN = 11; 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 CALLING_UID = 9999; const int32_t PIXEL_MAP_TEST_WIDTH = 32; @@ -136,6 +137,8 @@ public: EXPECT_EQ(true, notificationRequest.IsGroupOverview()); } else if (CASE_FOURTEEN == notificationRequest.GetNotificationId()) { CheckCaseFourteenResult(notificationRequest); + } else if (CASE_FIFTEEN == notificationRequest.GetNotificationId()) { + CheckCaseFIFteenResult(notificationRequest); } else { GTEST_LOG_(INFO) << "ANS_Interface_MT_Publish::OnConsumed do nothing!!!!!"; } @@ -369,6 +372,14 @@ private: } EXPECT_EQ(NotificationConstant::OTHER, notificationRequest.GetSlotType()); } + + void CheckCaseFIFTEENResult(NotificationRequest notificationRequest) + { + std::shared_ptr notiFlags = notificationRequest.GetFlags(); + EXPECT_EQ(false, notiFlags.IsSoundEnabled()); + EXPECT_EQ(true, notiFlags.IsVibrationEnabled()); + + } }; class CompletedCallbackTest : public WantAgent::CompletedCallback { @@ -1327,5 +1338,44 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_08000, Function CheckJsonConverter(&req); } + +/** + * @tc.number : ANS_Interface_MT_Publish_05000 + * @tc.name : Publish_05000 + * @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 a flags notification success. + */ +HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_05000, 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 notiFlags = std::make_shared(); + EXPECT_NE(notiFlags, nullptr); + notiFlags->SetSoundEnabled(false); + notiFlags->SetVibrationEnabled(true); + GTEST_LOG_(INFO) << "ANS_Interface_MT_Publish_04000::flags::" << notiFlags->Dump(); + 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.SetFlags(notiFlags); + req.SetSlotType(NotificationConstant::OTHER); + req.SetNotificationId(CASE_FIFTEEN); + 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_flags.h b/interfaces/innerkits/ans/native/include/notification_flags.h new file mode 100644 index 0000000000000000000000000000000000000000..097f2f1b063000b027a6349e8ae2d159816e3aa8 --- /dev/null +++ b/interfaces/innerkits/ans/native/include/notification_flags.h @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_FLAGS_H +#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_FLAGS_H + +#include +#include "parcel.h" +#include "notification_json_convert.h" + +namespace OHOS { +namespace Notification { +class NotificationFlags : public Parcelable, public NotificationJsonConvertionBase { +public: + /** + * Default constructor used to create an empty NotificationFlags instance. + */ + NotificationFlags() = default; + + /** + * Default deconstructor used to deconstruct. + */ + ~NotificationFlags() = default; + + /** + * Sets the notification whether enable sound. + * @param soundEnabled whether enable sound. + */ + void SetSoundEnabled(bool soundEnabled); + + /** + * Checks whether enable sound. + * @return sound enable. + */ + bool IsSoundEnabled() const; + + /** + * Sets the notification whether enable vibration. + * @param vibrationEnabled whether enable vibration. + */ + void SetVibrationEnabled(bool vibrationEnabled); + + /** + * Checks whether enable vibration. + * @return vibration enable. + */ + bool IsVibrationEnabled() const; + + /** + * Returns a string representation of the object. + * @return a string representation of the object. + */ + std::string Dump(); + + /** + * Converts a NotificationFlags object into a Json. + * @param jsonObject Indicates the Json object. + */ + bool ToJson(nlohmann::json &jsonObject) const override; + + /** + * Creates a NotificationFlags object from a Json. + * @param jsonObject Indicates the Json object. + * @return the NotificationFlags. + */ + static NotificationFlags *FromJson(const nlohmann::json &jsonObject); + + /** + * Marshal a object into a Parcel. + * @param parcel the object into the parcel + */ + virtual bool Marshalling(Parcel &parcel) const override; + + /** + * Unmarshal object from a Parcel. + * @return the NotificationFlags + */ + static NotificationFlags *Unmarshalling(Parcel &parcel); + +private: + /** + * Read a NotificationFlags object from a Parcel. + * @param parcel the parcel + */ + bool ReadFromParcel(Parcel &parcel); + +private: + bool soundEnabled_ {true}; + bool vibrationEnabled_ {false}; +}; +} // namespace Notification +} // namespace OHOS + +#endif // #define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_FLAGS_H + diff --git a/interfaces/innerkits/ans/native/include/notification_request.h b/interfaces/innerkits/ans/native/include/notification_request.h index 93b5f2d9a8f233e70d17ddac8b1c7ac0df83b2c6..e2f6190f8ec06c8b690ba0f72a2355336bd9ce9c 100644 --- a/interfaces/innerkits/ans/native/include/notification_request.h +++ b/interfaces/innerkits/ans/native/include/notification_request.h @@ -20,6 +20,7 @@ #include "notification_action_button.h" #include "notification_content.h" #include "notification_distributed_options.h" +#include "notification_flags.h" #include "notification_json_convert.h" #include "notification_template.h" #include "ohos/aafwk/content/want_params.h" @@ -915,6 +916,18 @@ public: */ std::shared_ptr GetTemplate() const; + /** + * Sets the flags of this notification. + * @param flags the flags of this notification. + */ + void SetFlags(const std::shared_ptr &flags); + + /** + * Obtains the flags of the notification. + * @return the flags of the notification. + */ + std::shared_ptr GetFlags() const; + private: /** * Indicates the color mask, used for calculation with the ARGB value set by setColor(int32_t). @@ -958,6 +971,7 @@ private: static bool ConvertJsonToNotificationActionButton(NotificationRequest *target, const nlohmann::json &jsonObject); static bool ConvertJsonToNotificationDistributedOptions( NotificationRequest *target, const nlohmann::json &jsonObject); + static bool ConvertJsonToNotificationFlags(NotificationRequest *target, const nlohmann::json &jsonObject); private: int32_t notificationId_ {0}; @@ -1019,6 +1033,7 @@ private: NotificationDistributedOptions distributedOptions_; std::shared_ptr notificationTemplate_ {}; + std::shared_ptr notificationFlags_ {}; }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/kits/js/notification/notificationFlags.ts b/interfaces/kits/js/notification/notificationFlags.ts new file mode 100644 index 0000000000000000000000000000000000000000..35e1df8079fd49769f98a47a75904674f2c0d376 --- /dev/null +++ b/interfaces/kits/js/notification/notificationFlags.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 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. + */ + +/** + * Describes a NotificationFlags instance. + * + * @name NotificationFlags + * @since 8 + * @devices phone, tablet, tv, wearable, car + * @permission N/A + * @sysCap SystemCapability.Notification.ANS + */ + export interface NotificationFlags { + /** + * Whether to enable sound reminder. + */ + readonly soundEnabled?: boolean; + + /** + * Whether to enable vibration reminder. + */ + readonly vibrationEnabled?: boolean; +} diff --git a/interfaces/kits/js/notification/notificationRequest.d.ts b/interfaces/kits/js/notification/notificationRequest.d.ts index 6525bf19ea3640ac643504b838db1111e35a4218..971ecb0c744167e6b9bd77deb0acd6b178627c7b 100644 --- a/interfaces/kits/js/notification/notificationRequest.d.ts +++ b/interfaces/kits/js/notification/notificationRequest.d.ts @@ -19,6 +19,7 @@ import { WantAgent } from '../@ohos.wantAgent'; import { NotificationContent } from './notificationContent'; import { NotificationActionButton } from './notificationActionButton'; import { NotificationTemplate } from './notificationTemplate'; +import { NotificationFlags } from './notificationFlags'; /** * Defines a NotificationRequest instance. @@ -197,4 +198,11 @@ export interface NotificationRequest { * @since 8 */ template?: NotificationTemplate; + + /** + * Obtains the set of identifiers for the notification. + * + * @since 8 + */ + readonly notificationFlags?: NotificationFlags; } diff --git a/interfaces/kits/napi/ans/include/common.h b/interfaces/kits/napi/ans/include/common.h index f77e712a0739059ea6506b38e9943992c1cba99e..ba4cd1aadaee35e5170f873578c06b42e05a6729 100644 --- a/interfaces/kits/napi/ans/include/common.h +++ b/interfaces/kits/napi/ans/include/common.h @@ -429,6 +429,9 @@ public: static napi_value SetNotificationTemplateInfo( const napi_env &env, const std::shared_ptr &templ, napi_value &result); + static napi_value SetNotificationFlags( + const napi_env &env, const std::shared_ptr &flags, napi_value &result); + private: static const int ARGS_ONE = 1; static const int ARGS_TWO = 2; diff --git a/interfaces/kits/napi/ans/src/common.cpp b/interfaces/kits/napi/ans/src/common.cpp index 2097b51d3ebeb6bc36fa9c5dca059ba1179810c9..1e71f2bd7d53cb6120479d8908896d2933459b93 100644 --- a/interfaces/kits/napi/ans/src/common.cpp +++ b/interfaces/kits/napi/ans/src/common.cpp @@ -548,7 +548,7 @@ napi_value Common::SetNotificationRequestByCustom( } napi_set_named_property(env, result, "actionButtons", arr); - // template?: NotificationTemplate; + // template?: NotificationTemplate std::shared_ptr templ = request->GetTemplate(); if (templ) { napi_value templateResult = nullptr; @@ -560,6 +560,18 @@ napi_value Common::SetNotificationRequestByCustom( napi_set_named_property(env, result, "template", templateResult); } + // readonly notificationFlags?: NotificationFlags + std::shared_ptr flags = request->GetFlags(); + if (flags) { + napi_value flagsResult = nullptr; + napi_create_object(env, &flagsResult); + if (!SetNotificationFlags(env, flags, flagsResult)) { + ANS_LOGE("SetNotificationFlags call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "notificationFlags", flagsResult); + } + return NapiGetBoolean(env, true); } @@ -4074,5 +4086,28 @@ napi_value Common::SetNotificationTemplateInfo( return NapiGetBoolean(env, true); } + +napi_value Common::SetNotificationFlags( + const napi_env &env, const std::shared_ptr &flags, napi_value &result) +{ + ANS_LOGI("enter"); + + if (flags == nullptr) { + ANS_LOGE("flags is null"); + return NapiGetBoolean(env, false); + } + + napi_value value = nullptr; + + // readonly soundEnabled?: boolean + napi_get_boolean(env, flags->IsSoundEnabled(), &value); + napi_set_named_property(env, result, "soundEnabled", value); + + // readonly vibrationEnabled?: boolean + napi_get_boolean(env, flags->IsVibrationEnabled(), &value); + napi_set_named_property(env, result, "vibrationEnabled", value); + + return NapiGetBoolean(env, true); +} } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file