1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "notification_time.h"
17
18 #include <cstdint>
19 #include <string> // for basic_string, operator+, basic_string<>...
20 #include <memory> // for shared_ptr, shared_ptr<>::element_type
21
22
23 #include "ans_log_wrapper.h"
24 #include "nlohmann/json.hpp" // for json, basic_json<>::object_t, basic_json
25 #include "parcel.h" // for Parcel
26
27 namespace OHOS {
28 namespace Notification {
29
GetInitialTime() const30 int32_t NotificationTime::GetInitialTime() const
31 {
32 return initialTime_;
33 }
34
SetInitialTime(int32_t time)35 void NotificationTime::SetInitialTime(int32_t time)
36 {
37 initialTime_ = time;
38 }
39
GetIsCountDown() const40 bool NotificationTime::GetIsCountDown() const
41 {
42 return isCountDown_;
43 }
44
SetIsCountDown(bool flag)45 void NotificationTime::SetIsCountDown(bool flag)
46 {
47 isCountDown_ = flag;
48 }
49
GetIsPaused() const50 bool NotificationTime::GetIsPaused() const
51 {
52 return isPaused_;
53 }
54
SetIsPaused(bool flag)55 void NotificationTime::SetIsPaused(bool flag)
56 {
57 isPaused_ = flag;
58 }
59
GetIsInTitle() const60 bool NotificationTime::GetIsInTitle() const
61 {
62 return isInTitle_;
63 }
64
SetIsInTitle(bool flag)65 void NotificationTime::SetIsInTitle(bool flag)
66 {
67 isInTitle_ = flag;
68 }
69
Dump()70 std::string NotificationTime::Dump()
71 {
72 return "Time{ "
73 "initialTime = " + std::to_string(initialTime_) +
74 ", isCountDown = " + std::to_string(isCountDown_) +
75 ", isPaused = " + std::to_string(isPaused_) +
76 ", isInTitle = " + std::to_string(isInTitle_) +
77 " }";
78 }
79
ToJson(nlohmann::json & jsonObject) const80 bool NotificationTime::ToJson(nlohmann::json &jsonObject) const
81 {
82 jsonObject["initialTime"] = initialTime_;
83 jsonObject["isCountDown"] = isCountDown_;
84 jsonObject["isPaused"] = isPaused_;
85 jsonObject["isInTitle"] = isInTitle_;
86
87 return true;
88 }
89
FromJson(const nlohmann::json & jsonObject)90 NotificationTime *NotificationTime::FromJson(const nlohmann::json &jsonObject)
91 {
92 if (jsonObject.is_null() or !jsonObject.is_object()) {
93 ANS_LOGE("Invalid JSON object");
94 return nullptr;
95 }
96
97 NotificationTime *time = new (std::nothrow) NotificationTime();
98 if (time == nullptr) {
99 ANS_LOGE("Failed to create time instance");
100 return nullptr;
101 }
102
103 const auto &jsonEnd = jsonObject.cend();
104 if (jsonObject.find("initialTime") != jsonEnd && jsonObject.at("initialTime").is_number_integer()) {
105 time->initialTime_ = jsonObject.at("initialTime").get<int32_t>();
106 }
107
108 if (jsonObject.find("isCountDown") != jsonEnd && jsonObject.at("isCountDown").is_boolean()) {
109 time->isCountDown_ = jsonObject.at("isCountDown").get<bool>();
110 }
111
112 if (jsonObject.find("isPaused") != jsonEnd && jsonObject.at("isPaused").is_boolean()) {
113 time->isPaused_ = jsonObject.at("isPaused").get<bool>();
114 }
115
116 if (jsonObject.find("isInTitle") != jsonEnd && jsonObject.at("isInTitle").is_boolean()) {
117 time->isInTitle_ = jsonObject.at("isInTitle").get<bool>();
118 }
119
120 return time;
121 }
122
Marshalling(Parcel & parcel) const123 bool NotificationTime::Marshalling(Parcel &parcel) const
124 {
125 if (!parcel.WriteInt32(initialTime_)) {
126 ANS_LOGE("Failed to write initialTime");
127 return false;
128 }
129
130 if (!parcel.WriteBool(isCountDown_)) {
131 ANS_LOGE("Failed to write isCountDown");
132 return false;
133 }
134
135 if (!parcel.WriteBool(isPaused_)) {
136 ANS_LOGE("Failed to write isPaused");
137 return false;
138 }
139
140 if (!parcel.WriteBool(isInTitle_)) {
141 ANS_LOGE("Failed to write isInTitle");
142 return false;
143 }
144
145 return true;
146 }
147
ReadFromParcel(Parcel & parcel)148 bool NotificationTime::ReadFromParcel(Parcel &parcel)
149 {
150 initialTime_ = parcel.ReadInt32();
151 isCountDown_ = parcel.ReadBool();
152 isPaused_ = parcel.ReadBool();
153 isInTitle_ = parcel.ReadBool();
154
155 return true;
156 }
157
Unmarshalling(Parcel & parcel)158 NotificationTime *NotificationTime::Unmarshalling(Parcel &parcel)
159 {
160 NotificationTime *time = new (std::nothrow) NotificationTime();
161
162 if (time && !time->ReadFromParcel(parcel)) {
163 delete time;
164 time = nullptr;
165 }
166
167 return time;
168 }
169 } // namespace Notification
170 } // namespace OHOS