From d6fe3a59f2713cafafcaa9e0e1680ce113cb2953 Mon Sep 17 00:00:00 2001 From: dy_study Date: Tue, 4 Jan 2022 14:32:34 +0800 Subject: [PATCH 1/3] Update Notification ans code. Signed-off-by: dy_study --- frameworks/ans/core/BUILD.gn | 1 + .../core/common/include/ans_const_define.h | 3 + .../core/common/include/ans_inner_errors.h | 1 + .../ans/core/include/ans_manager_interface.h | 2 + .../ans/core/include/ans_manager_proxy.h | 1 + .../ans/core/include/ans_manager_stub.h | 2 + .../ans/core/include/ans_notification.h | 8 ++ frameworks/ans/core/src/ans_manager_proxy.cpp | 34 +++++ frameworks/ans/core/src/ans_manager_stub.cpp | 34 +++++ frameworks/ans/core/src/ans_notification.cpp | 10 ++ frameworks/ans/native/BUILD.gn | 1 + .../ans/native/src/notification_helper.cpp | 5 + .../src/notification_long_text_content.cpp | 3 +- .../native/src/notification_media_content.cpp | 3 +- .../src/notification_multiline_content.cpp | 4 +- .../ans/native/src/notification_request.cpp | 40 +++++- .../ans/native/src/notification_template.cpp | 93 +++++++++++++ frameworks/ans/test/moduletest/BUILD.gn | 1 + .../ans_innerkits_module_publish_test.cpp | 61 +++++++++ .../ans_innerkits_module_setting_test.cpp | 43 ++++++ .../wantagent/src/want_agent_helper.cpp | 3 +- .../ans/native/include/notification_helper.h | 10 +- .../ans/native/include/notification_request.h | 14 ++ .../native/include/notification_template.h | 95 +++++++++++++ interfaces/kits/js/@ohos.notification.d.ts | 23 ++++ .../notificationActionButton.d.ts | 2 + .../js/notification/notificationRequest.d.ts | 13 ++ .../notification/notificationSubscriber.d.ts | 6 + .../js/notification/notificationTemplate.d.ts | 35 +++++ interfaces/kits/napi/ans/BUILD.gn | 1 + .../kits/napi/ans/include/ans_template.h | 29 ++++ interfaces/kits/napi/ans/include/common.h | 9 ++ interfaces/kits/napi/ans/src/ans_template.cpp | 125 ++++++++++++++++++ interfaces/kits/napi/ans/src/common.cpp | 114 +++++++++++++++- interfaces/kits/napi/ans/src/init.cpp | 2 + interfaces/kits/napi/ans/src/subscribe.cpp | 3 +- .../kits/napi/wantagent/napi_want_agent.h | 17 ++- mgit.info | 1 + .../include/advanced_notification_service.h | 3 +- .../ans/include/notification_preferences.h | 1 + .../ans/src/advanced_notification_service.cpp | 11 ++ services/ans/src/notification_preferences.cpp | 34 +++++ services/ans/test/unittest/BUILD.gn | 1 + services/test/moduletest/BUILD.gn | 1 + test/BUILD.gn | 6 +- tools/dump/src/main.cpp | 2 +- 46 files changed, 888 insertions(+), 23 deletions(-) create mode 100644 frameworks/ans/native/src/notification_template.cpp create mode 100644 interfaces/innerkits/ans/native/include/notification_template.h create mode 100644 interfaces/kits/js/notification/notificationTemplate.d.ts create mode 100644 interfaces/kits/napi/ans/include/ans_template.h create mode 100644 interfaces/kits/napi/ans/src/ans_template.cpp create mode 100644 mgit.info diff --git a/frameworks/ans/core/BUILD.gn b/frameworks/ans/core/BUILD.gn index e2ba42989..4b035ea19 100644 --- a/frameworks/ans/core/BUILD.gn +++ b/frameworks/ans/core/BUILD.gn @@ -68,6 +68,7 @@ ohos_shared_library("ans_core") { "${frameworks_path}/ans/native/src/notification_subscribe_info.cpp", "${frameworks_path}/ans/native/src/notification_subscriber.cpp", "${frameworks_path}/ans/native/src/notification_user_input.cpp", + "${frameworks_path}/ans/native/src/notification_template.cpp", "${frameworks_path}/ans/native/src/reminder_helper.cpp", "${frameworks_path}/ans/native/src/reminder_request.cpp", "${frameworks_path}/ans/native/src/reminder_request_alarm.cpp", diff --git a/frameworks/ans/core/common/include/ans_const_define.h b/frameworks/ans/core/common/include/ans_const_define.h index 8568a2b3e..e06699976 100644 --- a/frameworks/ans/core/common/include/ans_const_define.h +++ b/frameworks/ans/core/common/include/ans_const_define.h @@ -37,6 +37,9 @@ constexpr uint32_t SYSTEM_SERVICE_UID = 1000; const static Uri DEFAULT_NOTIFICATION_SOUND("file://system/etc/Light.ogg"); const static std::vector DEFAULT_NOTIFICATION_VIBRATION = {200}; +// Default path for template +const static std::string DEFAULT_TEMPLATE_PATH("/system/etc/external.json"); + } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/common/include/ans_inner_errors.h b/frameworks/ans/core/common/include/ans_inner_errors.h index 8e59ee723..63d35c83f 100644 --- a/frameworks/ans/core/common/include/ans_inner_errors.h +++ b/frameworks/ans/core/common/include/ans_inner_errors.h @@ -69,6 +69,7 @@ enum ErrorCode : uint32_t { ERR_ANS_PREFERENCES_NOTIFICATION_SLOTGROUP_NOT_EXIST, ERR_ANS_PREFERENCES_NOTIFICATION_SLOTGROUP_ID_INVALID, ERR_ANS_PREFERENCES_NOTIFICATION_SLOTGROUP_EXCEED_MAX_NUM, + ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED, }; } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/include/ans_manager_interface.h b/frameworks/ans/core/include/ans_manager_interface.h index 9657c2b34..c8d126c03 100644 --- a/frameworks/ans/core/include/ans_manager_interface.h +++ b/frameworks/ans/core/include/ans_manager_interface.h @@ -118,6 +118,7 @@ public: virtual ErrCode CancelReminder(const int32_t reminderId) = 0; virtual ErrCode GetValidReminders(std::vector> &reminders) = 0; virtual ErrCode CancelAllReminders() = 0; + virtual ErrCode IsSupportTemplate(const std::string &templateName, bool &support) = 0; protected: enum TransactId : uint32_t { @@ -181,6 +182,7 @@ protected: CANCEL_REMINDER, CANCEL_ALL_REMINDERS, GET_ALL_VALID_REMINDERS + IS_SUPPORT_TEMPLATE, }; }; } // namespace Notification diff --git a/frameworks/ans/core/include/ans_manager_proxy.h b/frameworks/ans/core/include/ans_manager_proxy.h index d5bd993e1..0c58d0b84 100644 --- a/frameworks/ans/core/include/ans_manager_proxy.h +++ b/frameworks/ans/core/include/ans_manager_proxy.h @@ -101,6 +101,7 @@ public: ErrCode ShellDump(const std::string &dumpOption, std::vector &dumpInfo) override; ErrCode PublishContinuousTaskNotification(const sptr &request) override; ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId) override; + ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override; ErrCode PublishReminder(sptr &reminder) override; ErrCode CancelReminder(const int32_t reminderId) override; diff --git a/frameworks/ans/core/include/ans_manager_stub.h b/frameworks/ans/core/include/ans_manager_stub.h index 5366ed950..0316ea1bd 100644 --- a/frameworks/ans/core/include/ans_manager_stub.h +++ b/frameworks/ans/core/include/ans_manager_stub.h @@ -110,6 +110,7 @@ public: virtual ErrCode CancelReminder(const int32_t reminderId) override; virtual ErrCode GetValidReminders(std::vector> &reminders) override; virtual ErrCode CancelAllReminders() override; + virtual ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override; private: static const std::map> @@ -175,6 +176,7 @@ private: ErrCode HandleCancelReminder(MessageParcel &data, MessageParcel &reply); ErrCode HandleGetValidReminders(MessageParcel &data, MessageParcel &reply); ErrCode HandleCancelAllReminders(MessageParcel &data, MessageParcel &reply); + ErrCode HandleIsSupportTemplate(MessageParcel &data, MessageParcel &reply); template bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &reply, ErrCode &result); diff --git a/frameworks/ans/core/include/ans_notification.h b/frameworks/ans/core/include/ans_notification.h index bb5480207..7ea2f0d6d 100644 --- a/frameworks/ans/core/include/ans_notification.h +++ b/frameworks/ans/core/include/ans_notification.h @@ -629,6 +629,14 @@ public: */ ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId); + /** + * Obtains whether the template is supported by the system. + * + * @param support whether is it a system supported template. + * @return Returns check result. + */ + ErrCode IsSupportTemplate(const std::string &templateName, bool &support); + /** * Reset ans manager proxy when OnRemoteDied called. */ diff --git a/frameworks/ans/core/src/ans_manager_proxy.cpp b/frameworks/ans/core/src/ans_manager_proxy.cpp index 9614eb978..e06b9e28f 100644 --- a/frameworks/ans/core/src/ans_manager_proxy.cpp +++ b/frameworks/ans/core/src/ans_manager_proxy.cpp @@ -2151,5 +2151,39 @@ bool AnsManagerProxy::ReadParcelableVector(std::vector> &parcelableInfos return true; } + +ErrCode AnsManagerProxy::IsSupportTemplate(const std::string &templateName, bool &support) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGW("[IsSupportTemplate] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteString(templateName)) { + ANS_LOGW("[IsSupportTemplate] fail: write template name failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(IS_SUPPORT_TEMPLATE, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[IsSupportTemplate] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGW("[IsSupportTemplate] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!reply.ReadBool(support)) { + ANS_LOGW("[IsSupportTemplate] fail: read support failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/src/ans_manager_stub.cpp b/frameworks/ans/core/src/ans_manager_stub.cpp index b6352240e..534428779 100644 --- a/frameworks/ans/core/src/ans_manager_stub.cpp +++ b/frameworks/ans/core/src/ans_manager_stub.cpp @@ -210,6 +210,10 @@ const std::map> &parcelableInfos, return true; } +ErrCode AnsManagerStub::HandleIsSupportTemplate(MessageParcel &data, MessageParcel &reply) +{ + std::string templateName; + if (!data.ReadString(templateName)) { + ANS_LOGW("[HandleIsSupportTemplate] fail: read template name failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + bool support = false; + ErrCode result = IsSupportTemplate(templateName, support); + + if (!reply.WriteInt32(result)) { + ANS_LOGW("[HandleIsSupportTemplate] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!reply.WriteBool(support)) { + ANS_LOGW("[HandleIsSupportTemplate] fail: write support failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + return ERR_OK; +} + ErrCode AnsManagerStub::Publish(const std::string &label, const sptr ¬ification) { ANS_LOGW("AnsManagerStub::Publish called!"); @@ -1733,6 +1760,7 @@ ErrCode AnsManagerStub::CancelContinuousTaskNotification(const std::string &labe ANS_LOGW("AnsManagerStub::CancelContinuousTaskNotification called!"); return ERR_INVALID_OPERATION; } + ErrCode AnsManagerStub::PublishReminder(sptr &reminder) { ANS_LOGW("AnsManagerStub::PublishReminder called!"); @@ -1756,5 +1784,11 @@ ErrCode AnsManagerStub::CancelAllReminders() ANS_LOGW("AnsManagerStub::cancelAllReminders called!"); return ERR_INVALID_OPERATION; } + +ErrCode AnsManagerStub::IsSupportTemplate(const std::string &templateName, bool &support) +{ + ANS_LOGW("AnsManagerStub::IsSupportTemplate called!"); + return ERR_INVALID_OPERATION; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/src/ans_notification.cpp b/frameworks/ans/core/src/ans_notification.cpp index 9f2214e90..8087f3b11 100644 --- a/frameworks/ans/core/src/ans_notification.cpp +++ b/frameworks/ans/core/src/ans_notification.cpp @@ -1099,5 +1099,15 @@ ErrCode AnsNotification::CheckImageSize(const NotificationRequest &request) return ERR_OK; } + +ErrCode AnsNotification::IsSupportTemplate(const std::string &templateName, bool &support) +{ + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return ansManagerProxy_->IsSupportTemplate(templateName, support); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/frameworks/ans/native/BUILD.gn b/frameworks/ans/native/BUILD.gn index f83983bd0..3a3de9de8 100644 --- a/frameworks/ans/native/BUILD.gn +++ b/frameworks/ans/native/BUILD.gn @@ -63,6 +63,7 @@ ohos_shared_library("ans_innerkits") { "src/notification_subscribe_info.cpp", "src/notification_subscriber.cpp", "src/notification_user_input.cpp", + "src/notification_template.cpp", ] configs = [ diff --git a/frameworks/ans/native/src/notification_helper.cpp b/frameworks/ans/native/src/notification_helper.cpp index 038e5f916..7f0bc3619 100644 --- a/frameworks/ans/native/src/notification_helper.cpp +++ b/frameworks/ans/native/src/notification_helper.cpp @@ -331,5 +331,10 @@ ErrCode NotificationHelper::CancelContinuousTaskNotification(const std::string & { return DelayedSingleton::GetInstance()->CancelContinuousTaskNotification(label, notificationId); } + +ErrCode NotificationHelper::IsSupportTemplate(const std::string &templateName, bool &support) +{ + return DelayedSingleton::GetInstance()->IsSupportTemplate(templateName, support); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/frameworks/ans/native/src/notification_long_text_content.cpp b/frameworks/ans/native/src/notification_long_text_content.cpp index fb305300d..0e242feee 100644 --- a/frameworks/ans/native/src/notification_long_text_content.cpp +++ b/frameworks/ans/native/src/notification_long_text_content.cpp @@ -13,9 +13,10 @@ * limitations under the License. */ -#include "ans_log_wrapper.h" #include "notification_long_text_content.h" +#include "ans_log_wrapper.h" + namespace OHOS { namespace Notification { const std::size_t NotificationLongTextContent::MAX_LONGTEXT_LENGTH {1024}; diff --git a/frameworks/ans/native/src/notification_media_content.cpp b/frameworks/ans/native/src/notification_media_content.cpp index e1c303d89..6c3862c50 100644 --- a/frameworks/ans/native/src/notification_media_content.cpp +++ b/frameworks/ans/native/src/notification_media_content.cpp @@ -13,9 +13,10 @@ * limitations under the License. */ -#include "ans_log_wrapper.h" #include "notification_media_content.h" +#include "ans_log_wrapper.h" + namespace OHOS { namespace Notification { void NotificationMediaContent::SetAVToken(const std::shared_ptr &avToken) diff --git a/frameworks/ans/native/src/notification_multiline_content.cpp b/frameworks/ans/native/src/notification_multiline_content.cpp index 320c23bd5..299ac9059 100644 --- a/frameworks/ans/native/src/notification_multiline_content.cpp +++ b/frameworks/ans/native/src/notification_multiline_content.cpp @@ -13,9 +13,11 @@ * limitations under the License. */ +#include "notification_multiline_content.h" + #include + #include "ans_log_wrapper.h" -#include "notification_multiline_content.h" namespace OHOS { namespace Notification { diff --git a/frameworks/ans/native/src/notification_request.cpp b/frameworks/ans/native/src/notification_request.cpp index bbf4e1303..0bce17ed3 100644 --- a/frameworks/ans/native/src/notification_request.cpp +++ b/frameworks/ans/native/src/notification_request.cpp @@ -13,9 +13,10 @@ * limitations under the License. */ -#include "ans_log_wrapper.h" #include "notification_request.h" +#include "ans_log_wrapper.h" + namespace OHOS { namespace Notification { const std::string NotificationRequest::CLASSIFICATION_ALARM {"alarm"}; @@ -111,6 +112,8 @@ NotificationRequest::NotificationRequest(const NotificationRequest &other) this->actionButtons_ = other.actionButtons_; this->messageUsers_ = other.messageUsers_; this->userInputHistory_ = other.userInputHistory_; + + this->notificationTemplate_ = other.notificationTemplate_; } NotificationRequest &NotificationRequest::operator=(const NotificationRequest &other) @@ -171,6 +174,8 @@ NotificationRequest &NotificationRequest::operator=(const NotificationRequest &o this->messageUsers_ = other.messageUsers_; this->userInputHistory_ = other.userInputHistory_; + this->notificationTemplate_ = other.notificationTemplate_; + return *this; } @@ -754,6 +759,7 @@ std::string NotificationRequest::Dump() ", bigIcon = " + (bigIcon_ ? "not null" : "null") + ", notificationContent = " + (notificationContent_ ? notificationContent_->Dump() : "null") + ", publicNotification = " + (publicNotification_ ? "not null" : "null") + + ", notificationTemplate = " + (notificationTemplate_ ? "not null" : "null") + ", actionButtons = " + (!actionButtons_.empty() ? actionButtons_.at(0)->Dump() : "empty") + ", messageUsers = " + (!messageUsers_.empty() ? messageUsers_.at(0)->Dump() : "empty") + ", userInputHistory = " + (!userInputHistory_.empty() ? userInputHistory_.at(0) : "empty") + @@ -1089,6 +1095,19 @@ bool NotificationRequest::Marshalling(Parcel &parcel) const return false; } + valid = notificationTemplate_ ? true : false; + if (!parcel.WriteBool(valid)) { + ANS_LOGE("Failed to write the flag which indicate whether publicNotification is null"); + return false; + } + + if (valid) { + if (!parcel.WriteParcelable(notificationTemplate_.get())) { + ANS_LOGE("Failed to write notificationTemplate"); + return false; + } + } + return true; } @@ -1283,6 +1302,15 @@ bool NotificationRequest::ReadFromParcel(Parcel &parcel) return false; } + valid = parcel.ReadBool(); + if (valid) { + notificationTemplate_ = std::shared_ptr(parcel.ReadParcelable()); + if (!notificationTemplate_) { + ANS_LOGE("Failed to read notificationTemplate"); + return false; + } + } + return true; } @@ -1294,5 +1322,15 @@ int64_t NotificationRequest::GetNowSysTime() int64_t duration = value.count(); return duration; } + +void NotificationRequest::SetTemplate(const std::shared_ptr &templ) +{ + notificationTemplate_ = templ; +} + +std::shared_ptr NotificationRequest::GetTemplate() const +{ + return notificationTemplate_; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/native/src/notification_template.cpp b/frameworks/ans/native/src/notification_template.cpp new file mode 100644 index 000000000..6bc3b1ed5 --- /dev/null +++ b/frameworks/ans/native/src/notification_template.cpp @@ -0,0 +1,93 @@ +/* + * 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_template.h" +#include "ans_log_wrapper.h" + +namespace OHOS { +namespace Notification { +void NotificationTemplate::SetTemplateName(const std::string &name) +{ + templateName_ = name; +} + +std::string NotificationTemplate::GetTemplateName() const +{ + return templateName_; +} + +void NotificationTemplate::SetTemplateData(const std::shared_ptr &data) +{ + templateData_ = data; +} + +std::shared_ptr NotificationTemplate::GetTemplateData() const +{ + return templateData_; +} + +std::string NotificationTemplate::Dump() +{ + return "templateName = " + templateName_ + + ", templateData = " + (templateData_ ? "not null" : "null"); +} + +bool NotificationTemplate::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteString(templateName_)) { + ANS_LOGE("Failed to write text"); + return false; + } + + bool valid = templateData_ ? true : false; + if (!parcel.WriteBool(valid)) { + ANS_LOGE("Failed to write the flag which indicate whether templateData is null"); + return false; + } + + return true; +} + +NotificationTemplate *NotificationTemplate::Unmarshalling(Parcel &parcel) +{ + auto templ = new NotificationTemplate(); + if ((templ != nullptr) && !templ->ReadFromParcel(parcel)) { + delete templ; + templ = nullptr; + } + + return templ; +} + +bool NotificationTemplate::ReadFromParcel(Parcel &parcel) +{ + if (!parcel.ReadString(templateName_)) { + ANS_LOGE("Failed to read template name"); + return false; + } + + bool valid = parcel.ReadBool(); + if (valid) { + templateData_ = std::shared_ptr(parcel.ReadParcelable()); + if (!templateData_) { + ANS_LOGE("Failed to read template data"); + return false; + } + } + + return true; +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/ans/test/moduletest/BUILD.gn b/frameworks/ans/test/moduletest/BUILD.gn index f2edf4096..7c5a0ec15 100644 --- a/frameworks/ans/test/moduletest/BUILD.gn +++ b/frameworks/ans/test/moduletest/BUILD.gn @@ -173,6 +173,7 @@ ohos_moduletest("ans_innerkits_module_publish_test") { "${frameworks_path}/wantagent:wantagent_innerkits", "${services_path}/ans:libans", "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/interfaces/innerkits/base:base", "//foundation/aafwk/standard/interfaces/innerkits/want:want", "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", 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 2c14f23f9..5f3f3428f 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -28,6 +28,8 @@ #include "system_ability_definition.h" #include "want_agent_info.h" #include "want_agent_helper.h" +#include "want_params.h" +#include "ohos/aafwk/base/int_wrapper.h" using namespace testing::ext; namespace OHOS { @@ -52,6 +54,7 @@ const int32_t CASE_TEN = 10; 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 CALLING_UID = 9999; const int32_t PIXEL_MAP_TEST_WIDTH = 32; @@ -131,6 +134,8 @@ public: } else if (CASE_THIRTEEN == notificationRequest.GetNotificationId()) { EXPECT_EQ(NotificationRequest::GroupAlertType::ALL, notificationRequest.GetGroupAlertType()); EXPECT_EQ(true, notificationRequest.IsGroupOverview()); + } else if (CASE_FOURTEEN == notificationRequest.GetNotificationId()) { + CheckCaseFourteenResult(notificationRequest); } else { GTEST_LOG_(INFO) << "ANS_Interface_MT_Publish::OnConsumed do nothing!!!!!"; } @@ -352,6 +357,18 @@ private: EXPECT_EQ(0, notificationRequest.GetProgressValue()); EXPECT_EQ(false, notificationRequest.IsProgressIndeterminate()); } + + void CheckCaseFourteenResult(const NotificationRequest& notificationRequest) const + { + std::shared_ptr notiTemplate = notificationRequest.GetTemplate(); + if (notiTemplate != nullptr) { + EXPECT_EQ("process", notiTemplate->GetTemplateName()); + std::shared_ptr param = notiTemplate->GetTemplateData(); + int value = AAFwk::Integer::Unbox(AAFwk::IInteger::Query(param->GetParam("process"))); + EXPECT_EQ(20, value); // 20 test input + } + EXPECT_EQ(NotificationConstant::OTHER, notificationRequest.GetSlotType()); + } }; class CompletedCallbackTest : public WantAgent::CompletedCallback { @@ -1159,5 +1176,49 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_CancelGroup_10100, Func EXPECT_EQ(0, NotificationHelper::UnSubscribeNotification(subscriber, info)); WaitOnUnsubscribeResult(); } + +/** + * @tc.number : ANS_Interface_MT_Publish_04000 + * @tc.name : Publish_04000 + * @tc.desc : Add notification slot(type is OTHER), make a subscriber and publish a template notification. + * @tc.expected : Add notification slot success, make a subscriber and publish a ltemplate notification success. + */ +HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_04000, 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 notiTemplate = std::make_shared(); + EXPECT_NE(notiTemplate, nullptr); + notiTemplate->SetTemplateName("process"); + // [{'process':20}] + AAFwk::WantParams wantParams; + std::string key("process"); + int resultValue = 20; + wantParams.SetParam(key, AAFwk::Integer::Box(resultValue)); + notiTemplate->SetTemplateData(std::make_shared(wantParams)); + GTEST_LOG_(INFO) << "ANS_Interface_MT_Publish_04000::notiTemplate::" << notiTemplate->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.SetTemplate(notiTemplate); + req.SetSlotType(NotificationConstant::OTHER); + req.SetNotificationId(CASE_FOURTEEN); + 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/frameworks/ans/test/moduletest/ans_innerkits_module_setting_test.cpp b/frameworks/ans/test/moduletest/ans_innerkits_module_setting_test.cpp index 4a16ba62f..a34cdb20e 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_setting_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_setting_test.cpp @@ -134,5 +134,48 @@ HWTEST_F(AnsInterfaceModuleSettingTest, ANS_Interface_MT_NotificationSetting_004 EXPECT_EQ("bundlename", bundleOption.GetBundleName()); EXPECT_EQ(CALLING_UID, bundleOption.GetUid()); } + +/** + * @tc.number : ANS_Interface_MT_NotificationSetting_00500 + * @tc.name : NotificationSetting_00500 + * @tc.desc : If the template configuration file does not exist, query whether the template exists. + * @tc.expected : Query return failed. + */ +HWTEST_F(AnsInterfaceModuleSettingTest, ANS_Interface_MT_NotificationSetting_00500, Function | MediumTest | Level1) +{ + std::string templateName("process"); + bool support = false; + EXPECT_EQ((int)ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED, + NotificationHelper::IsSupportTemplate(templateName, support)); + EXPECT_EQ(false, support); +} + +/** + * @tc.number : ANS_Interface_MT_NotificationSetting_00600 + * @tc.name : NotificationSetting_00600 + * @tc.desc : The template exists in the system, query whether the template exists. + * @tc.expected : Query return success. + */ +HWTEST_F(AnsInterfaceModuleSettingTest, ANS_Interface_MT_NotificationSetting_00600, Function | MediumTest | Level1) +{ + std::string templateName("process"); + bool support = false; + EXPECT_EQ(0, NotificationHelper::IsSupportTemplate(templateName, support)); + EXPECT_EQ(true, support); +} + +/** + * @tc.number : ANS_Interface_MT_NotificationSetting_00700 + * @tc.name : NotificationSetting_00700 + * @tc.desc : The template does not exist in the system, query whether the template exists. + * @tc.expected : Query return failed. + */ +HWTEST_F(AnsInterfaceModuleSettingTest, ANS_Interface_MT_NotificationSetting_00700, Function | MediumTest | Level1) +{ + std::string templateName("template123"); + bool support = false; + EXPECT_EQ(0, NotificationHelper::IsSupportTemplate(templateName, support)); + EXPECT_EQ(false, support); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/frameworks/wantagent/src/want_agent_helper.cpp b/frameworks/wantagent/src/want_agent_helper.cpp index 33135ef63..ba702512b 100644 --- a/frameworks/wantagent/src/want_agent_helper.cpp +++ b/frameworks/wantagent/src/want_agent_helper.cpp @@ -14,12 +14,13 @@ */ #include "want_agent_helper.h" + #include "ability_manager_client.h" #include "hilog_wrapper.h" #include "pending_want.h" #include "want_agent_log_wrapper.h" -#include "want_sender_interface.h" #include "want_sender_info.h" +#include "want_sender_interface.h" using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; diff --git a/interfaces/innerkits/ans/native/include/notification_helper.h b/interfaces/innerkits/ans/native/include/notification_helper.h index a5d94f43e..fcf245989 100644 --- a/interfaces/innerkits/ans/native/include/notification_helper.h +++ b/interfaces/innerkits/ans/native/include/notification_helper.h @@ -524,7 +524,7 @@ public: * only be null or an empty string, indicating the current device. * @param enabled Specifies whether to allow the current application to publish notifications. The value * true indicates that notifications are allowed, and the value false indicates that - * notifications are not allowed. + * notifications are not allowed. * @return Returns set notifications enabled for default bundle result. */ static ErrCode SetNotificationsEnabledForDefaultBundle(const std::string &deviceId, bool enabled); @@ -629,6 +629,14 @@ public: * @return Returns cancel continuous task notification result. */ static ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId); + + /** + * Obtains whether the template is supported by the system. + * + * @param support whether is it a system supported template. + * @return Returns check result. + */ + static ErrCode IsSupportTemplate(const std::string &templateName, bool &support); }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/innerkits/ans/native/include/notification_request.h b/interfaces/innerkits/ans/native/include/notification_request.h index 1e8a22b52..74b269550 100644 --- a/interfaces/innerkits/ans/native/include/notification_request.h +++ b/interfaces/innerkits/ans/native/include/notification_request.h @@ -20,6 +20,7 @@ #include "message_user.h" #include "notification_action_button.h" #include "notification_content.h" +#include "notification_template.h" #include "ohos/aafwk/content/want_params.h" #include "parcel.h" #include "pixel_map.h" @@ -860,6 +861,18 @@ public: */ static NotificationRequest *Unmarshalling(Parcel &parcel); + /** + * Sets the template of this notification. + * @param template the template of this notification. + */ + void SetTemplate(const std::shared_ptr &templ); + + /** + * Obtains the Template of the notification. + * @return the Template of the notification. + */ + std::shared_ptr GetTemplate() const; + private: /** * Indicates the color mask, used for calculation with the ARGB value set by setColor(int32_t). @@ -945,6 +958,7 @@ private: std::vector> actionButtons_ {}; std::vector> messageUsers_ {}; std::vector userInputHistory_ {}; + std::shared_ptr notificationTemplate_ {}; }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/innerkits/ans/native/include/notification_template.h b/interfaces/innerkits/ans/native/include/notification_template.h new file mode 100644 index 000000000..0b56be337 --- /dev/null +++ b/interfaces/innerkits/ans/native/include/notification_template.h @@ -0,0 +1,95 @@ +/* + * 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_TEMPLATE_H +#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_TEMPLATE_H + +#include +#include +#include "parcel.h" +#include "ohos/aafwk/content/want_params.h" + +namespace OHOS { +namespace Notification { +class NotificationTemplate : public Parcelable { +public: + /** + * Default constructor used to create an empty NotificationTemplate instance. + */ + NotificationTemplate() = default; + + /** + * Default deconstructor used to deconstruct. + */ + ~NotificationTemplate() = default; + + /** + * Sets the template name to be included in a template notification. + * @param name template name. + */ + void SetTemplateName(const std::string &name); + + /** + * Obtains the template name to be included in a template notification. + * @return template name. + */ + std::string GetTemplateName() const; + + /** + * Sets the template data to be included in a template notification. + * @param data template data. + */ + void SetTemplateData(const std::shared_ptr &data); + + /** + * Obtains the template data to be included in a template notification. + * @return template data. + */ + std::shared_ptr GetTemplateData() const; + + /** + * Returns a string representation of the object. + * @return a string representation of the object. + */ + std::string Dump(); + + /** + * Marshal a object into a Parcel. + * @param parcel the object into the parcel + */ + virtual bool Marshalling(Parcel &parcel) const override; + + /** + * Unmarshal object from a Parcel. + * @return the NotificationTemplate + */ + static NotificationTemplate *Unmarshalling(Parcel &parcel); + +private: + /** + * Read a NotificationTemplate object from a Parcel. + * @param parcel the parcel + */ + bool ReadFromParcel(Parcel &parcel); + +private: + std::string templateName_; + std::shared_ptr templateData_ {}; +}; +} // namespace Notification +} // namespace OHOS + +#endif // #define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_TEMPLATE_H + diff --git a/interfaces/kits/js/@ohos.notification.d.ts b/interfaces/kits/js/@ohos.notification.d.ts index aed59524f..79467c48a 100644 --- a/interfaces/kits/js/@ohos.notification.d.ts +++ b/interfaces/kits/js/@ohos.notification.d.ts @@ -497,6 +497,8 @@ declare namespace notification { /** * Cancel the notification of a specified group for this application. + * + * @since 8 */ function cancelGroup(groupName: string, callback: AsyncCallback): void; function cancelGroup(groupName: string): Promise; @@ -504,6 +506,7 @@ declare namespace notification { /** * Delete the notification of a specified group for this application. * + * @since 8 * @systemapi Hide this for inner system use. * @permission ohos.permission.NOTIFICATION_CONTROLLER */ @@ -513,6 +516,7 @@ declare namespace notification { /** * Set the Do Not Disturb date. * + * @since 8 * @systemapi Hide this for inner system use. * @permission ohos.permission.NOTIFICATION_CONTROLLER */ @@ -522,6 +526,7 @@ declare namespace notification { /** * Obtains the Do Not Disturb date. * + * @since 8 * @systemapi Hide this for inner system use. * @permission ohos.permission.NOTIFICATION_CONTROLLER */ @@ -531,12 +536,22 @@ declare namespace notification { /** * Obtains whether to support the Do Not Disturb mode. * + * @since 8 * @systemapi Hide this for inner system use. * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function supportDoNotDisturbMode(callback: AsyncCallback): void; function supportDoNotDisturbMode(): Promise; + /** + * Obtains whether the template is supported by the system. + * + * @since 8 + * @param plateName Name of template to be Obtained + */ + function isSupportTemplate(plateName: string, callback: AsyncCallback): void; + function isSupportTemplate(plateName: string): Promise; + /** * Describes a BundleOption. */ @@ -587,6 +602,7 @@ declare namespace notification { /** * The type of the Do Not Disturb. * + * @since 8 * @systemapi Hide this for inner system use. */ export enum DoNotDisturbType { @@ -619,16 +635,22 @@ declare namespace notification { export interface DoNotDisturbDate { /** * the type of the Do Not Disturb. + * + * @since 8 */ type: DoNotDisturbType; /** * the start time of the Do Not Disturb. + * + * @since 8 */ begin: Date; /** * the end time of the Do Not Disturb. + * + * @since 8 */ end: Date; } @@ -636,6 +658,7 @@ declare namespace notification { /** * Notification source type * + * @since 8 * @systemapi Hide this for inner system use. */ export enum SourceType { diff --git a/interfaces/kits/js/notification/notificationActionButton.d.ts b/interfaces/kits/js/notification/notificationActionButton.d.ts index b2967c462..9428cc4f0 100644 --- a/interfaces/kits/js/notification/notificationActionButton.d.ts +++ b/interfaces/kits/js/notification/notificationActionButton.d.ts @@ -42,6 +42,8 @@ export interface NotificationActionButton { /** * User input + * + * @since 8 */ userInput?: NotificationUserInput; } diff --git a/interfaces/kits/js/notification/notificationRequest.d.ts b/interfaces/kits/js/notification/notificationRequest.d.ts index 6cefef386..6525bf19e 100644 --- a/interfaces/kits/js/notification/notificationRequest.d.ts +++ b/interfaces/kits/js/notification/notificationRequest.d.ts @@ -18,6 +18,7 @@ import image from '../@ohos.multimedia.image'; import { WantAgent } from '../@ohos.wantAgent'; import { NotificationContent } from './notificationContent'; import { NotificationActionButton } from './notificationActionButton'; +import { NotificationTemplate } from './notificationTemplate'; /** * Defines a NotificationRequest instance. @@ -141,6 +142,8 @@ export interface NotificationRequest { /** * The group information for this notification. + * + * @since 8 */ groupName?: string; @@ -174,6 +177,8 @@ export interface NotificationRequest { /** * Whether the notification can be remove. * + * @default true + * @since 8 * @systemapi Hide this for inner system use. */ readonly isRemoveAllowed?: boolean; @@ -181,7 +186,15 @@ export interface NotificationRequest { /** * Notification source. enum SourceType * + * @since 8 * @systemapi Hide this for inner system use. */ readonly source?: number; + + /** + * Obtains the template of this notification. + * + * @since 8 + */ + template?: NotificationTemplate; } diff --git a/interfaces/kits/js/notification/notificationSubscriber.d.ts b/interfaces/kits/js/notification/notificationSubscriber.d.ts index 2b3f57ee9..d8e2277bc 100644 --- a/interfaces/kits/js/notification/notificationSubscriber.d.ts +++ b/interfaces/kits/js/notification/notificationSubscriber.d.ts @@ -36,6 +36,12 @@ export interface NotificationSubscriber { onDisconnect?:() => void; onDestroy?:() => void; onDisturbModeChange?:(mode: notification.DoNotDisturbMode) => void; + + /** + * Callback when the Do Not Disturb setting changed. + * + * @since 8 + */ onDoNotDisturbDateChange?:(mode: notification.DoNotDisturbDate) => void; } diff --git a/interfaces/kits/js/notification/notificationTemplate.d.ts b/interfaces/kits/js/notification/notificationTemplate.d.ts new file mode 100644 index 000000000..d131ed5cb --- /dev/null +++ b/interfaces/kits/js/notification/notificationTemplate.d.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 NotificationTemplate instance. + * + * @name NotificationTemplate + * @since 8 + * @devices phone, tablet, tv, wearable, car + * @permission N/A + * @sysCap SystemCapability.Notification.ANS + */ +export interface NotificationTemplate { + /** + * Obtains the type of a notification template. + */ + name: string; + + /** + * Obtains the data of a notification template. + */ + data: {[key: string]: Object}; +} diff --git a/interfaces/kits/napi/ans/BUILD.gn b/interfaces/kits/napi/ans/BUILD.gn index 2d7176e35..dde4e0bc0 100644 --- a/interfaces/kits/napi/ans/BUILD.gn +++ b/interfaces/kits/napi/ans/BUILD.gn @@ -59,6 +59,7 @@ ohos_shared_library("notification") { "src/slot.cpp", "src/subscribe.cpp", "src/unsubscribe.cpp", + "src/ans_template.cpp", ] deps = [ diff --git a/interfaces/kits/napi/ans/include/ans_template.h b/interfaces/kits/napi/ans/include/ans_template.h new file mode 100644 index 000000000..2919b6233 --- /dev/null +++ b/interfaces/kits/napi/ans/include/ans_template.h @@ -0,0 +1,29 @@ +/* + * 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_NAPI_INCLUDE_TEMPLATE_H +#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NAPI_INCLUDE_TEMPLATE_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value IsSupportTemplate(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS + +#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NAPI_INCLUDE_TEMPLATE_H \ No newline at end of file diff --git a/interfaces/kits/napi/ans/include/common.h b/interfaces/kits/napi/ans/include/common.h index ca2751f8f..4f202eb0b 100644 --- a/interfaces/kits/napi/ans/include/common.h +++ b/interfaces/kits/napi/ans/include/common.h @@ -404,6 +404,15 @@ public: static napi_value CreateWantAgentByJS(const napi_env &env, const std::shared_ptr &agent); + static napi_value GetNotificationTemplate( + const napi_env &env, const napi_value &value, NotificationRequest &request); + + static napi_value GetNotificationTemplateInfo(const napi_env &env, const napi_value &value, + std::shared_ptr &templ); + + static napi_value SetNotificationTemplateInfo( + const napi_env &env, const std::shared_ptr &templ, napi_value &result); + private: static const int ARGS_ONE = 1; static const int ARGS_TWO = 2; diff --git a/interfaces/kits/napi/ans/src/ans_template.cpp b/interfaces/kits/napi/ans/src/ans_template.cpp new file mode 100644 index 000000000..1efa21250 --- /dev/null +++ b/interfaces/kits/napi/ans/src/ans_template.cpp @@ -0,0 +1,125 @@ +/* + * 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 "ans_template.h" +#include "cancel.h" + +namespace OHOS { +namespace NotificationNapi { +const int IS_TEMPLATE_MAX_PARA = 2; + +struct TemplateName { + std::string templateName = ""; + bool support = false; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoTemplate { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + TemplateName params; + CallbackPromiseInfo info; +}; + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, TemplateName& params) +{ + ANS_LOGI("enter"); + + size_t argc = IS_TEMPLATE_MAX_PARA; + napi_value argv[IS_TEMPLATE_MAX_PARA] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + NAPI_ASSERT(env, argc < IS_TEMPLATE_MAX_PARA, "Wrong number of arguments"); + + napi_valuetype valuetype = napi_undefined; + // argv[0]: name: string + NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); + NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type. String expected."); + char str[STR_MAX_SIZE] = {0}; + size_t strLen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], str, STR_MAX_SIZE - 1, &strLen)); + params.templateName = str; + + // argv[1]: callback + if (argc >= IS_TEMPLATE_MAX_PARA) { + NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype)); + NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + napi_create_reference(env, argv[1], 1, ¶ms.callback); + } + + return Common::NapiGetNull(env); +} + +napi_value IsSupportTemplate(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + TemplateName params; + if (ParseParameters(env, info, params) == nullptr) { + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoTemplate *asyncCallbackinfo = new (std::nothrow) + AsyncCallbackInfoTemplate {.env = env, .asyncWork = nullptr, .params = params}; + if (!asyncCallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asyncCallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "IsSupportTemplate", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsSupportTemplate napi_create_async_work start"); + AsyncCallbackInfoTemplate *asyncCallbackinfo = (AsyncCallbackInfoTemplate *)data; + + asyncCallbackinfo->info.errorCode = NotificationHelper::CancelNotification( + asyncCallbackinfo->params.templateName, asyncCallbackinfo->params.support); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("IsSupportTemplate napi_create_async_work end"); + AsyncCallbackInfoTemplate *asyncCallbackinfo = (AsyncCallbackInfoTemplate *)data; + + napi_value result = nullptr; + napi_get_boolean(env, asyncCallbackinfo->params.support, &result); + Common::ReturnCallbackPromise(env, asyncCallbackinfo->info, result); + + if (asyncCallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asyncCallbackinfo->info.callback); + } + + napi_delete_async_work(env, asyncCallbackinfo->asyncWork); + if (asyncCallbackinfo) { + delete asyncCallbackinfo; + asyncCallbackinfo = nullptr; + } + }, + (void *)asyncCallbackinfo, + &asyncCallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackinfo->asyncWork)); + + if (asyncCallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/ans/src/common.cpp b/interfaces/kits/napi/ans/src/common.cpp index c995c0e62..2864281d6 100644 --- a/interfaces/kits/napi/ans/src/common.cpp +++ b/interfaces/kits/napi/ans/src/common.cpp @@ -400,7 +400,7 @@ napi_value Common::SetNotificationRequestByPixelMap( return NapiGetBoolean(env, false); } - // smallIcon?: image.PixelMap + // smallIcon?: image.PixelMap std::shared_ptr littleIcon = request->GetLittleIcon(); if (littleIcon) { napi_value smallIconResult = nullptr; @@ -482,6 +482,18 @@ napi_value Common::SetNotificationRequestByCustom( } napi_set_named_property(env, result, "actionButtons", arr); + // template?: NotificationTemplate; + std::shared_ptr templ = request->GetTemplate(); + if (templ) { + napi_value templateResult = nullptr; + napi_create_object(env, &templateResult); + if (!SetNotificationTemplateInfo(env, templ, templateResult)) { + ANS_LOGE("SetNotificationTemplate call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "template", templateResult); + } + return NapiGetBoolean(env, true); } @@ -1408,6 +1420,10 @@ napi_value Common::GetNotificationRequestByCustom( if (GetNotificationLargeIcon(env, value, request) == nullptr) { return nullptr; } + // template?: NotificationTemplate + if (GetNotificationTemplate(env, value, request) == nullptr) { + return nullptr; + } return NapiGetNull(env); } @@ -2823,7 +2839,7 @@ napi_value Common::GetNotificationConversationalContentMessages( napi_value conversationalContentResult = nullptr; bool hasProperty = false; - // messages: Array + // messages: Array NAPI_CALL(env, napi_has_named_property(env, contentResult, "messages", &hasProperty)); NAPI_ASSERT(env, hasProperty, "Property messages expected."); napi_get_named_property(env, contentResult, "messages", &conversationalContentResult); @@ -3751,5 +3767,99 @@ napi_value Common::CreateWantAgentByJS(const napi_env &env, const std::shared_pt return wantAgent; } + +napi_value Common::GetNotificationTemplate(const napi_env &env, const napi_value &value, NotificationRequest &request) +{ + ANS_LOGI("enter"); + + napi_valuetype valuetype = napi_undefined; + napi_value result = nullptr; + bool hasProperty = false; + + NAPI_CALL(env, napi_has_named_property(env, value, "template", &hasProperty)); + if (hasProperty) { + napi_get_named_property(env, value, "template", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + NAPI_ASSERT(env, valuetype == napi_object, "Wrong argument type. Object expected."); + + std::shared_ptr templ = std::make_shared(); + if (templ == nullptr) { + ANS_LOGE("template is null"); + return nullptr; + } + if (GetNotificationTemplateInfo(env, value, templ) == nullptr) { + return nullptr; + } + + request.SetTemplate(templ); + } + + return NapiGetNull(env); +} + +napi_value Common::GetNotificationTemplateInfo(const napi_env &env, const napi_value &value, + std::shared_ptr &templ) +{ + ANS_LOGI("enter"); + + napi_valuetype valuetype = napi_undefined; + napi_value result = nullptr; + bool hasProperty = false; + char str[STR_MAX_SIZE] = {0}; + size_t strLen = 0; + + // name: string + NAPI_CALL(env, napi_has_named_property(env, value, "name", &hasProperty)); + NAPI_ASSERT(env, hasProperty, "Property text expected."); + napi_get_named_property(env, value, "name", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type. String expected."); + NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen)); + std::string strInput = str; + templ->SetTemplateName(strInput); + + // data?: {[key: string]: object} + NAPI_CALL(env, napi_has_named_property(env, value, "data", &hasProperty)); + if (hasProperty) { + napi_get_named_property(env, value, "data", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + NAPI_ASSERT(env, valuetype == napi_object, "Wrong argument type. Object expected."); + AAFwk::WantParams wantParams; + if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) { + return nullptr; + } + + std::shared_ptr data = std::make_shared(wantParams); + templ->SetTemplateData(data); + } + + return NapiGetNull(env); +} + +napi_value Common::SetNotificationTemplateInfo( + const napi_env &env, const std::shared_ptr &templ, napi_value &result) +{ + ANS_LOGI("enter"); + + if (templ == nullptr) { + ANS_LOGE("templ is null"); + return NapiGetBoolean(env, false); + } + + napi_value value = nullptr; + + // name: string; + napi_create_string_utf8(env, templ->GetTemplateName().c_str(), NAPI_AUTO_LENGTH, &value); + napi_set_named_property(env, result, "name", value); + + // data?: {[key: string]: object}; + std::shared_ptr data = templ->GetTemplateData(); + if (data) { + value = OHOS::AppExecFwk::WrapWantParams(env, *data); + napi_set_named_property(env, result, "data", value); + } + + return NapiGetBoolean(env, true); +} } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/ans/src/init.cpp b/interfaces/kits/napi/ans/src/init.cpp index 2a27497cb..c3cfdbb1d 100644 --- a/interfaces/kits/napi/ans/src/init.cpp +++ b/interfaces/kits/napi/ans/src/init.cpp @@ -14,6 +14,7 @@ */ #include "init.h" +#include "ans_template.h" #include "cancel.h" #include "constant.h" #include "display_badge.h" @@ -66,6 +67,7 @@ napi_value NotificationInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("setDoNotDisturbDate", SetDoNotDisturbDate), DECLARE_NAPI_FUNCTION("getDoNotDisturbDate", GetDoNotDisturbDate), DECLARE_NAPI_FUNCTION("supportDoNotDisturbMode", SupportDoNotDisturbMode), + DECLARE_NAPI_FUNCTION("isSupportTemplate", IsSupportTemplate), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); diff --git a/interfaces/kits/napi/ans/src/subscribe.cpp b/interfaces/kits/napi/ans/src/subscribe.cpp index 8573dcb04..e0083a426 100644 --- a/interfaces/kits/napi/ans/src/subscribe.cpp +++ b/interfaces/kits/napi/ans/src/subscribe.cpp @@ -13,9 +13,10 @@ * limitations under the License. */ +#include "subscribe.h" + #include #include -#include "subscribe.h" namespace OHOS { namespace NotificationNapi { diff --git a/interfaces/kits/napi/wantagent/napi_want_agent.h b/interfaces/kits/napi/wantagent/napi_want_agent.h index e79f2e93d..6a33753c7 100644 --- a/interfaces/kits/napi/wantagent/napi_want_agent.h +++ b/interfaces/kits/napi/wantagent/napi_want_agent.h @@ -16,22 +16,21 @@ #ifndef NAPI_WANT_AGENT_H #define NAPI_WANT_AGENT_H -#include -#include #include +#include +#include #include -#include "napi/native_common.h" -#include "napi/native_node_api.h" - -#include "want_agent_constant.h" +#include "ability.h" #include "completed_callback.h" #include "context.h" +#include "napi/native_common.h" +#include "napi/native_node_api.h" +#include "trigger_info.h" #include "want.h" -#include "want_params.h" -#include "ability.h" #include "want_agent.h" -#include "trigger_info.h" +#include "want_agent_constant.h" +#include "want_params.h" namespace OHOS { using namespace OHOS::AppExecFwk; diff --git a/mgit.info b/mgit.info new file mode 100644 index 000000000..bba4d813d --- /dev/null +++ b/mgit.info @@ -0,0 +1 @@ +hmf/notification/ans_standard diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 847942150..b53211b49 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -112,6 +112,7 @@ public: ErrCode CancelReminder(const int32_t reminderId) override; ErrCode GetValidReminders(std::vector> &reminders) override; ErrCode CancelAllReminders() override; + ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override; // SystemEvent void OnBundleRemoved(const sptr &bundleOption); @@ -175,4 +176,4 @@ private: } // namespace Notification } // namespace OHOS -#endif // ADVANCED_NOTIFICATION_SERVICE_H \ No newline at end of file +#endif // ADVANCED_NOTIFICATION_SERVICE_H diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 3104edf45..ef0af0a2f 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -68,6 +68,7 @@ public: ErrCode SetNotificationsEnabled(const bool &enabled); ErrCode GetDoNotDisturbDate(sptr &date); ErrCode SetDoNotDisturbDate(const sptr date); + ErrCode GetTemplateSupported(const std::string& templateName, bool &support); ErrCode ClearNotificationInRestoreFactorySettings(); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 1a1652fd4..b29831f2c 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -2088,5 +2088,16 @@ ErrCode AdvancedNotificationService::PrepereContinuousTaskNotificationRequest( ErrCode result = CheckPictureSize(request); return result; } + +ErrCode AdvancedNotificationService::IsSupportTemplate(const std::string& templateName, bool &support) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + ErrCode result = ERR_OK; + handler_->PostSyncTask(std::bind([&]() { + support = false; + result = NotificationPreferences::GetInstance().GetTemplateSupported(templateName, support); + })); + return result; +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 04c2ce8d6..a8fcbaf9b 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -15,9 +15,12 @@ #include "notification_preferences.h" +#include + #include "ans_const_define.h" #include "ans_inner_errors.h" #include "ans_log_wrapper.h" +#include "nlohmann/json.hpp" namespace OHOS { namespace Notification { @@ -773,5 +776,36 @@ void NotificationPreferences::OnDistributedKvStoreDeathRecipient() } } } + +ErrCode NotificationPreferences::GetTemplateSupported(const std::string& templateName, bool &support) +{ + if (templateName.length() == 0) { + ANS_LOGE("template name is null."); + return ERR_ANS_INVALID_PARAM; + } + + std::ifstream inFile; + inFile.open(DEFAULT_TEMPLATE_PATH.c_str(), std::ios::in); + if (!inFile.is_open()) { + ANS_LOGE("read template config error."); + return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED; + } + + nlohmann::json jsonObj; + inFile >> jsonObj; + if (jsonObj.is_discarded()) { + ANS_LOGE("template json discarded error."); + inFile.close(); + return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED; + } + + if (jsonObj.contains(templateName)) { + support = true; + } + + jsonObj.clear(); + inFile.close(); + return ERR_OK; +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 80690341e..fbb643750 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -92,6 +92,7 @@ ohos_unittest("ans_unit_test") { "appexecfwk_standard:appexecfwk_core", "appexecfwk_standard:libeventhandler", "ces_standard:cesfwk_innerkits", + "hitrace_native:libhitrace", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", diff --git a/services/test/moduletest/BUILD.gn b/services/test/moduletest/BUILD.gn index f33bd2002..93dbc3c82 100644 --- a/services/test/moduletest/BUILD.gn +++ b/services/test/moduletest/BUILD.gn @@ -90,6 +90,7 @@ ohos_moduletest("ans_module_test") { "appexecfwk_standard:appexecfwk_core", "appexecfwk_standard:libeventhandler", "ces_standard:cesfwk_innerkits", + "hitrace_native:libhitrace", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", diff --git a/test/BUILD.gn b/test/BUILD.gn index 1cd6c473b..9692d01f9 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -25,8 +25,8 @@ group("systemtest") { testonly = true deps = [ - "common\acts:actsnotificationtest", - "resource\ansSTSlotGroupTest:ansSTSlotGroupTest", - "systemtest\acts\ansDump:ansDumpTest", + #"common\acts:actsnotificationtest", + #"resource\ansSTSlotGroupTest:ansSTSlotGroupTest", + #"systemtest\acts\ansDump:ansDumpTest", ] } diff --git a/tools/dump/src/main.cpp b/tools/dump/src/main.cpp index b5ea1058d..668d39161 100644 --- a/tools/dump/src/main.cpp +++ b/tools/dump/src/main.cpp @@ -20,4 +20,4 @@ int main(int argc, char *argv[]) OHOS::Notification::NotificationShellCommand cmd(argc, argv); std::cout << cmd.ExecCommand(); return 0; -} \ No newline at end of file +} -- Gitee From e6177853b2488a0e97afc2bf52e1231e879fb491 Mon Sep 17 00:00:00 2001 From: dy_study Date: Tue, 4 Jan 2022 14:46:24 +0800 Subject: [PATCH 2/3] Fix the compile error. Signed-off-by: dy_study --- frameworks/ans/core/include/ans_manager_interface.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/ans/core/include/ans_manager_interface.h b/frameworks/ans/core/include/ans_manager_interface.h index c8d126c03..f43d0df46 100644 --- a/frameworks/ans/core/include/ans_manager_interface.h +++ b/frameworks/ans/core/include/ans_manager_interface.h @@ -181,8 +181,8 @@ protected: PUBLISH_REMINDER, CANCEL_REMINDER, CANCEL_ALL_REMINDERS, - GET_ALL_VALID_REMINDERS - IS_SUPPORT_TEMPLATE, + GET_ALL_VALID_REMINDERS, + IS_SUPPORT_TEMPLATE }; }; } // namespace Notification -- Gitee From f7679107907883cbcd63e237f25d5a4ef8fb6ce5 Mon Sep 17 00:00:00 2001 From: dy_study Date: Tue, 4 Jan 2022 15:35:53 +0800 Subject: [PATCH 3/3] Fix gn format error. Signed-off-by: dy_study Change-Id: Id1db6c59c49d99207114add3586d8af487bec252 --- frameworks/ans/core/BUILD.gn | 2 +- frameworks/ans/native/BUILD.gn | 2 +- interfaces/kits/napi/ans/BUILD.gn | 2 +- mgit.info | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) delete mode 100644 mgit.info diff --git a/frameworks/ans/core/BUILD.gn b/frameworks/ans/core/BUILD.gn index 4b035ea19..cf945217a 100644 --- a/frameworks/ans/core/BUILD.gn +++ b/frameworks/ans/core/BUILD.gn @@ -67,8 +67,8 @@ ohos_shared_library("ans_core") { "${frameworks_path}/ans/native/src/notification_sorting_map.cpp", "${frameworks_path}/ans/native/src/notification_subscribe_info.cpp", "${frameworks_path}/ans/native/src/notification_subscriber.cpp", - "${frameworks_path}/ans/native/src/notification_user_input.cpp", "${frameworks_path}/ans/native/src/notification_template.cpp", + "${frameworks_path}/ans/native/src/notification_user_input.cpp", "${frameworks_path}/ans/native/src/reminder_helper.cpp", "${frameworks_path}/ans/native/src/reminder_request.cpp", "${frameworks_path}/ans/native/src/reminder_request_alarm.cpp", diff --git a/frameworks/ans/native/BUILD.gn b/frameworks/ans/native/BUILD.gn index 3a3de9de8..390903624 100644 --- a/frameworks/ans/native/BUILD.gn +++ b/frameworks/ans/native/BUILD.gn @@ -62,8 +62,8 @@ ohos_shared_library("ans_innerkits") { "src/notification_sorting_map.cpp", "src/notification_subscribe_info.cpp", "src/notification_subscriber.cpp", - "src/notification_user_input.cpp", "src/notification_template.cpp", + "src/notification_user_input.cpp", ] configs = [ diff --git a/interfaces/kits/napi/ans/BUILD.gn b/interfaces/kits/napi/ans/BUILD.gn index dde4e0bc0..01c5cfad5 100644 --- a/interfaces/kits/napi/ans/BUILD.gn +++ b/interfaces/kits/napi/ans/BUILD.gn @@ -46,6 +46,7 @@ ohos_shared_library("notification") { configs = [ ":native_module_config" ] sources = [ + "src/ans_template.cpp", "src/cancel.cpp", "src/common.cpp", "src/constant.cpp", @@ -59,7 +60,6 @@ ohos_shared_library("notification") { "src/slot.cpp", "src/subscribe.cpp", "src/unsubscribe.cpp", - "src/ans_template.cpp", ] deps = [ diff --git a/mgit.info b/mgit.info deleted file mode 100644 index bba4d813d..000000000 --- a/mgit.info +++ /dev/null @@ -1 +0,0 @@ -hmf/notification/ans_standard -- Gitee