diff --git a/frameworks/ans/BUILD.gn b/frameworks/ans/BUILD.gn index e6fbcc821c305fa963b01c528f3297f473954dc8..065b0da5792a826cf8b0528c9d95521385c0af0a 100644 --- a/frameworks/ans/BUILD.gn +++ b/frameworks/ans/BUILD.gn @@ -91,6 +91,7 @@ ohos_shared_library("ans_innerkits") { "${frameworks_module_ans_path}/src/notification_subscribe_info.cpp", "${frameworks_module_ans_path}/src/notification_subscriber.cpp", "${frameworks_module_ans_path}/src/notification_template.cpp", + "${frameworks_module_ans_path}/src/notification_time.cpp", "${frameworks_module_ans_path}/src/notification_user_input.cpp", "${frameworks_module_ans_path}/src/push_callback_stub.cpp", "${frameworks_module_ans_path}/src/reminder_helper.cpp", diff --git a/frameworks/ans/src/notification_local_live_view_content.cpp b/frameworks/ans/src/notification_local_live_view_content.cpp index 321425c9d49736bd1dba369609d07cb20a240604..12d770e8e91f54b04a1d125e0862886ed2b9bc17 100644 --- a/frameworks/ans/src/notification_local_live_view_content.cpp +++ b/frameworks/ans/src/notification_local_live_view_content.cpp @@ -27,6 +27,7 @@ #include "notification_json_convert.h" #include "notification_progress.h" #include "notification_local_live_view_button.h" +#include "notification_time.h" #include "parcel.h" // for Parcel namespace OHOS { @@ -72,6 +73,16 @@ NotificationProgress NotificationLocalLiveViewContent::GetProgress() return progress_; } +void NotificationLocalLiveViewContent::SetTime(NotificationTime time) +{ + time_ = time; +} + +NotificationTime NotificationLocalLiveViewContent::GetTime() +{ + return time_; +} + std::string NotificationLocalLiveViewContent::Dump() { return "NotificationLocalLiveViewContent{ " + NotificationBasicContent::Dump() + @@ -79,6 +90,7 @@ std::string NotificationLocalLiveViewContent::Dump() ", capsule = " + capsule_.Dump() + ", button = " + button_.Dump() + ", progress = " + progress_.Dump() + + ", time = " + time_.Dump() + " }"; } @@ -107,10 +119,17 @@ bool NotificationLocalLiveViewContent::ToJson(nlohmann::json &jsonObject) const return false; } + nlohmann::json timeObj; + if (!NotificationJsonConverter::ConvertToJson(&time_, timeObj)) { + ANS_LOGE("Cannot convert time to JSON"); + return false; + } + jsonObject["type"] = type_; jsonObject["capsule"] = capsuleObj; jsonObject["button"] = buttonObj; jsonObject["progress"] = progressObj; + jsonObject["time"] = timeObj; return true; } @@ -165,6 +184,16 @@ NotificationLocalLiveViewContent *NotificationLocalLiveViewContent::FromJson(con } } + if (jsonObject.find("time") != jsonEnd) { + auto timeObj = jsonObject.at("time"); + auto pTime = NotificationJsonConverter::ConvertFromJson(timeObj); + if (pTime != nullptr) { + pContent->time_ = *pTime; + delete pTime; + pTime = nullptr; + } + } + return pContent; } @@ -195,6 +224,11 @@ bool NotificationLocalLiveViewContent::Marshalling(Parcel &parcel) const return false; } + if (!parcel.WriteParcelable(&time_)) { + ANS_LOGE("Failed to write time"); + return false; + } + return true; } @@ -241,13 +275,22 @@ bool NotificationLocalLiveViewContent::ReadFromParcel(Parcel &parcel) auto pProgress = parcel.ReadParcelable(); if (pProgress == nullptr) { - ANS_LOGE("Failed to read capsule"); + ANS_LOGE("Failed to read progress"); return false; } progress_ = *pProgress; delete pProgress; pProgress = nullptr; + auto pTime = parcel.ReadParcelable(); + if (pTime == nullptr) { + ANS_LOGE("Failed to read time"); + return false; + } + time_ = *pTime; + delete pTime; + pTime = nullptr; + return true; } } // namespace Notification diff --git a/frameworks/ans/src/notification_time.cpp b/frameworks/ans/src/notification_time.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41231df724ae9f326ad2572f9c08e51bbc2b1793 --- /dev/null +++ b/frameworks/ans/src/notification_time.cpp @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2023 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_time.h" + +#include +#include // for basic_string, operator+, basic_string<>... +#include // for shared_ptr, shared_ptr<>::element_type + + +#include "ans_log_wrapper.h" +#include "nlohmann/json.hpp" // for json, basic_json<>::object_t, basic_json +#include "parcel.h" // for Parcel + +namespace OHOS { +namespace Notification { + +int32_t NotificationTime::GetInitialTime() const +{ + return initialTime_; +} + +void NotificationTime::SetInitialTime(int32_t time) +{ + initialTime_ = time; +} + +bool NotificationTime::GetIsCountDown() const +{ + return isCountDown_; +} + +void NotificationTime::SetIsCountDown(bool flag) +{ + isCountDown_ = flag; +} + +bool NotificationTime::GetIsPaused() const +{ + return isPaused_; +} + +void NotificationTime::SetIsPaused(bool flag) +{ + isPaused_ = flag; +} + +bool NotificationTime::GetIsInTitle() const +{ + return isInTitle_; +} + +void NotificationTime::SetIsInTitle(bool flag) +{ + isInTitle_ = flag; +} + +std::string NotificationTime::Dump() +{ + return "Time{ " + "initialTime = " + std::to_string(initialTime_) + + ", isCountDown = " + std::to_string(isCountDown_) + + ", isPaused = " + std::to_string(isPaused_) + + ", isInTitle = " + std::to_string(isInTitle_) + + " }"; +} + +bool NotificationTime::ToJson(nlohmann::json &jsonObject) const +{ + jsonObject["initialTime"] = initialTime_; + jsonObject["isCountDown"] = isCountDown_; + jsonObject["isPaused"] = isPaused_; + jsonObject["isInTitle"] = isInTitle_; + + return true; +} + +NotificationTime *NotificationTime::FromJson(const nlohmann::json &jsonObject) +{ + if (jsonObject.is_null() or !jsonObject.is_object()) { + ANS_LOGE("Invalid JSON object"); + return nullptr; + } + + NotificationTime *time = new (std::nothrow) NotificationTime(); + if (time == nullptr) { + ANS_LOGE("Failed to create time instance"); + return nullptr; + } + + const auto &jsonEnd = jsonObject.cend(); + if (jsonObject.find("initialTime") != jsonEnd && jsonObject.at("initialTime").is_number_integer()) { + time->initialTime_ = jsonObject.at("initialTime").get(); + } + + if (jsonObject.find("isCountDown") != jsonEnd && jsonObject.at("isCountDown").is_boolean()) { + time->isCountDown_ = jsonObject.at("isCountDown").get(); + } + + if (jsonObject.find("isPaused") != jsonEnd && jsonObject.at("isPaused").is_boolean()) { + time->isPaused_ = jsonObject.at("isPaused").get(); + } + + if (jsonObject.find("isInTitle") != jsonEnd && jsonObject.at("isInTitle").is_boolean()) { + time->isInTitle_ = jsonObject.at("isInTitle").get(); + } + + return time; +} + +bool NotificationTime::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteInt32(initialTime_)) { + ANS_LOGE("Failed to write initialTime"); + return false; + } + + if (!parcel.WriteBool(isCountDown_)) { + ANS_LOGE("Failed to write isCountDown"); + return false; + } + + if (!parcel.WriteBool(isPaused_)) { + ANS_LOGE("Failed to write isPaused"); + return false; + } + + if (!parcel.WriteBool(isInTitle_)) { + ANS_LOGE("Failed to write isInTitle"); + return false; + } + + return true; +} + +bool NotificationTime::ReadFromParcel(Parcel &parcel) +{ + initialTime_ = parcel.ReadInt32(); + isCountDown_ = parcel.ReadBool(); + isPaused_ = parcel.ReadBool(); + isInTitle_ = parcel.ReadBool(); + + return true; +} + +NotificationTime *NotificationTime::Unmarshalling(Parcel &parcel) +{ + NotificationTime *time = new (std::nothrow) NotificationTime(); + + if (time && !time->ReadFromParcel(parcel)) { + delete time; + time = nullptr; + } + + return time; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/include/common.h b/frameworks/js/napi/include/common.h index d395c55ee6a05eccd74815a8094b2057f29f1d26..fc47bdd5bfc54e8745cf4d5ccbf539df58a4bd7f 100644 --- a/frameworks/js/napi/include/common.h +++ b/frameworks/js/napi/include/common.h @@ -22,6 +22,7 @@ #include "notification_helper.h" #include "notification_local_live_view_button.h" #include "notification_progress.h" +#include "notification_time.h" namespace OHOS { namespace NotificationNapi { @@ -521,6 +522,16 @@ public: */ static napi_value SetProgress(const napi_env &env, const NotificationProgress &progress, napi_value &result); + /** + * @brief Sets a js object by specified NotificationTime object + * + * @param env Indicates the environment that the API is invoked under + * @param time Indicates a NotificationTime object to be converted + * @param result Indicates a js object to be set + * @return Returns the null object if success, returns the null value otherwise + */ + static napi_value SetTime(const napi_env &env, const NotificationTime &time, napi_value &result); + /** * @brief Sets a js object by specified MessageUser object * diff --git a/frameworks/js/napi/src/common.cpp b/frameworks/js/napi/src/common.cpp index 19569a74f76d46008ae9a764bd60065500c59474..51d454aeebab32f0e25f9ebfe8cc52e3f4802b94 100644 --- a/frameworks/js/napi/src/common.cpp +++ b/frameworks/js/napi/src/common.cpp @@ -19,6 +19,7 @@ #include "notification_action_button.h" #include "notification_capsule.h" #include "notification_progress.h" +#include "notification_time.h" #include "pixel_map_napi.h" namespace OHOS { @@ -1190,7 +1191,7 @@ napi_value Common::SetNotificationLocalLiveViewContent( napi_value capsule = nullptr; napi_create_object(env, &capsule); if (!SetCapsule(env, localLiveViewContent->GetCapsule(), capsule)) { - ANS_LOGE("SetMessageUser call failed"); + ANS_LOGE("SetCapsule call failed"); return NapiGetBoolean(env, false); } napi_set_named_property(env, result, "capsule", capsule); @@ -1199,7 +1200,7 @@ napi_value Common::SetNotificationLocalLiveViewContent( napi_value button = nullptr; napi_create_object(env, &button); if (!SetButton(env, localLiveViewContent->GetButton(), button)) { - ANS_LOGE("SetMessageUser call failed"); + ANS_LOGE("SetButton call failed"); return NapiGetBoolean(env, false); } napi_set_named_property(env, result, "button", button); @@ -1208,11 +1209,20 @@ napi_value Common::SetNotificationLocalLiveViewContent( napi_value progress = nullptr; napi_create_object(env, &progress); if (!SetProgress(env, localLiveViewContent->GetProgress(), progress)) { - ANS_LOGE("SetMessageUser call failed"); + ANS_LOGE("SetProgress call failed"); return NapiGetBoolean(env, false); } napi_set_named_property(env, result, "progress", progress); + // time: NotificationTime + napi_value time = nullptr; + napi_create_object(env, &time); + if (!SetTime(env, localLiveViewContent->GetTime(), time)) { + ANS_LOGE("SetMessageUser call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "time", time); + return NapiGetBoolean(env, true); } @@ -1304,6 +1314,30 @@ napi_value Common::SetProgress(const napi_env &env, const NotificationProgress & return NapiGetBoolean(env, true); } +napi_value Common::SetTime(const napi_env &env, const NotificationTime &time, napi_value &result) +{ + ANS_LOGI("enter"); + + napi_value value = nullptr; + // initialTime: int32_t + napi_create_int32(env, time.GetInitialTime(), &value); + napi_set_named_property(env, result, "initialTime", value); + + // isCountDown: bool + napi_get_boolean(env, time.GetIsCountDown(), &value); + napi_set_named_property(env, result, "isCountDown", value); + + // isPaused: bool + napi_get_boolean(env, time.GetIsPaused(), &value); + napi_set_named_property(env, result, "isPaused", value); + + // isInTitle: bool + napi_get_boolean(env, time.GetIsInTitle(), &value); + napi_set_named_property(env, result, "isInTitle", value); + + return NapiGetBoolean(env, true); +} + napi_value Common::SetMessageUser(const napi_env &env, const MessageUser &messageUser, napi_value &result) { ANS_LOGI("enter"); @@ -4441,6 +4475,83 @@ napi_value Common::GetNotificationLocalLiveViewProgress(const napi_env &env, con return NapiGetNull(env); } +napi_value Common::GetNotificationLocalLiveViewTime(const napi_env &env, const napi_value &contentResult, + std::shared_ptr content) +{ + napi_value result = nullptr; + napi_valuetype valuetype = napi_undefined; + bool hasProperty = false; + int32_t intValue = -1; + bool boolValue = false; + napi_value timeResult = nullptr; + + ANS_LOGI("enter"); + + napi_get_named_property(env, contentResult, "time", &timeResult); + NAPI_CALL(env, napi_typeof(env, timeResult, &valuetype)); + if (valuetype != napi_object) { + ANS_LOGE("Wrong argument type. Object expected."); + return nullptr; + } + + NotificationTime time; + + NAPI_CALL(env, napi_has_named_property(env, timeResult, "initialTime", &hasProperty)); + if (hasProperty) { + napi_get_named_property(env, timeResult, "initialTime", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + if (valuetype != napi_number) { + ANS_LOGE("Wrong argument type. Number expected."); + return nullptr; + } + napi_get_value_int32(env, result, &intValue); + time.SetInitialTime(intValue); + ANS_LOGD("time initialTime = %{public}d", intValue); + } + + NAPI_CALL(env, napi_has_named_property(env, timeResult, "isCountDown", &hasProperty)); + if (hasProperty) { + napi_get_named_property(env, timeResult, "isCountDown", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + if (valuetype != napi_boolean) { + ANS_LOGE("Wrong argument type. bool expected."); + return nullptr; + } + napi_get_value_bool(env, result, &boolValue); + time.SetIsCountDown(boolValue); + ANS_LOGD("time isCountDown = %{public}d", boolValue); + } + + NAPI_CALL(env, napi_has_named_property(env, timeResult, "isPaused", &hasProperty)); + if (hasProperty) { + napi_get_named_property(env, timeResult, "isPaused", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + if (valuetype != napi_boolean) { + ANS_LOGE("Wrong argument type. bool expected."); + return nullptr; + } + napi_get_value_bool(env, result, &boolValue); + time.SetIsPaused(boolValue); + ANS_LOGD("time isPaused = %{public}d", boolValue); + } + + NAPI_CALL(env, napi_has_named_property(env, timeResult, "isInTitle", &hasProperty)); + if (hasProperty) { + napi_get_named_property(env, timeResult, "isInTitle", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + if (valuetype != napi_boolean) { + ANS_LOGE("Wrong argument type. bool expected."); + return nullptr; + } + napi_get_value_bool(env, result, &boolValue); + time.SetIsInTitle(boolValue); + ANS_LOGD("time isInTitle = %{public}d", boolValue); + } + + content->SetTime(time); + return NapiGetNull(env); +} + napi_value Common::GetNotificationLocalLiveViewContentDetailed( const napi_env &env, const napi_value &contentResult, std::shared_ptr content) @@ -4492,6 +4603,12 @@ napi_value Common::GetNotificationLocalLiveViewContentDetailed( return nullptr; } + //time? + NAPI_CALL(env, napi_has_named_property(env, contentResult, "time", &hasProperty)); + if (hasProperty && GetNotificationLocalLiveViewTime(env, contentResult, content) == nullptr) { + return nullptr; + } + return NapiGetNull(env); } diff --git a/interfaces/inner_api/notification_local_live_view_content.h b/interfaces/inner_api/notification_local_live_view_content.h index e0c88b61a16dc66e8ca89cf010a45483573ebaec..7807c1118b06ace876c7d7a4e9e6b5db6ea44eb5 100644 --- a/interfaces/inner_api/notification_local_live_view_content.h +++ b/interfaces/inner_api/notification_local_live_view_content.h @@ -23,6 +23,7 @@ #include "notification_basic_content.h" #include "notification_conversational_message.h" #include "notification_json_convert.h" +#include "notification_time.h" #include "parcel.h" namespace OHOS { @@ -85,6 +86,20 @@ public: */ NotificationProgress GetProgress(); + /* + * @brief Sets the time to be included in a local live view notification. + * + * @param time Indicates the type to be included. + + */ + void SetTime(NotificationTime time); + + /* + * @brief Get the time of a local live view notification. + * + */ + NotificationTime GetTime(); + /** * @brief Returns a string representation of the object. * @@ -138,6 +153,7 @@ private: NotificationCapsule capsule_ {}; NotificationLocalLiveViewButton button_ {}; NotificationProgress progress_ {}; + NotificationTime time_ {}; }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/inner_api/notification_time.h b/interfaces/inner_api/notification_time.h new file mode 100644 index 0000000000000000000000000000000000000000..37a117aabca3f27f92db97728b7d68ad073caba3 --- /dev/null +++ b/interfaces/inner_api/notification_time.h @@ -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. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_TIME_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_TIME_H + +#include "notification_json_convert.h" +#include "parcel.h" +#include +#include + +namespace OHOS { +namespace Notification { +class NotificationTime : public Parcelable, public NotificationJsonConvertionBase { +public: + NotificationTime() = default; + + ~NotificationTime() = default; + + /** + * @brief Obtains the initialTime. + * + * @return Returns the initialTime. + */ + int32_t GetInitialTime() const; + + void SetInitialTime(int32_t time); + + /** + * @brief Obtains isCountDown flag. + * + * @return Returns the isCountDown flag. + */ + bool GetIsCountDown() const; + + void SetIsCountDown(bool flag); + + /** + * @brief Obtains isPaused flag. + * + * @return Returns the isPaused flag. + */ + bool GetIsPaused() const; + + void SetIsPaused(bool flag); + + /** + * @brief Obtains isInTitle flag. + * + * @return Returns the isInTitle flag. + */ + bool GetIsInTitle() const; + + void SetIsInTitle(bool flag); + + /** + * @brief Returns a string representation of the object. + * + * @return Returns a string representation of the object. + */ + std::string Dump(); + + /** + * @brief Converts a NotificationTime object into a Json. + * + * @param jsonObject Indicates the Json object. + * @return Returns true if succeed; returns false otherwise. + */ + bool ToJson(nlohmann::json &jsonObject) const override; + + /** + * @brief Creates a NotificationTime object from a Json. + * + * @param jsonObject Indicates the Json object. + * @return Returns the NotificationConversationalMessage. + */ + static NotificationTime *FromJson(const nlohmann::json &jsonObject); + + /** + * @brief Marshal a object into a Parcel. + * + * @param parcel Indicates the object into the parcel. + * @return Returns true if succeed; returns false otherwise. + */ + virtual bool Marshalling(Parcel &parcel) const override; + + /** + * @brief Unmarshal object from a Parcel. + * + * @param parcel Indicates the parcel object. + * @return Returns the NotificationTime. + */ + static NotificationTime *Unmarshalling(Parcel &parcel); + +private: + /** + * @brief Read a NotificationTime object from a Parcel. + * + * @param parcel Indicates the parcel object. + * @return Returns true if succeed; returns false otherwise. + */ + bool ReadFromParcel(Parcel &parcel); + +private: + int32_t initialTime_ {0}; + bool isCountDown_ {false}; + bool isPaused_ {false}; + bool isInTitle_ {false}; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_TIME_H \ No newline at end of file