1 /*
2 * Copyright (c) 2022-2024 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_flags.h"
17
18 #include <string> // for operator+, to_string, basic_string
19
20 #include "ans_log_wrapper.h"
21 #include "nlohmann/json.hpp" // for json, basic_json<>::object_t, bas...
22 #include "notification_constant.h" // for NotificationConstant::FlagStatus
23 #include "parcel.h" // for Parcel
24
25 namespace OHOS {
26 namespace Notification {
SetSoundEnabled(NotificationConstant::FlagStatus soundEnabled)27 void NotificationFlags::SetSoundEnabled(NotificationConstant::FlagStatus soundEnabled)
28 {
29 soundEnabled_ = soundEnabled;
30 if (soundEnabled == NotificationConstant::FlagStatus::OPEN) {
31 reminderFlags_ |= NotificationConstant::ReminderFlag::SOUND_FLAG;
32 } else {
33 reminderFlags_ &= ~(NotificationConstant::ReminderFlag::SOUND_FLAG);
34 }
35 }
36
IsSoundEnabled() const37 NotificationConstant::FlagStatus NotificationFlags::IsSoundEnabled() const
38 {
39 return soundEnabled_;
40 }
41
SetVibrationEnabled(NotificationConstant::FlagStatus vibrationEnabled)42 void NotificationFlags::SetVibrationEnabled(NotificationConstant::FlagStatus vibrationEnabled)
43 {
44 vibrationEnabled_ = vibrationEnabled;
45 if (vibrationEnabled == NotificationConstant::FlagStatus::OPEN) {
46 reminderFlags_ |= NotificationConstant::ReminderFlag::VIBRATION_FLAG;
47 } else {
48 reminderFlags_ &= ~(NotificationConstant::ReminderFlag::VIBRATION_FLAG);
49 }
50 }
51
IsVibrationEnabled() const52 NotificationConstant::FlagStatus NotificationFlags::IsVibrationEnabled() const
53 {
54 return vibrationEnabled_;
55 }
56
GetReminderFlags()57 uint32_t NotificationFlags::GetReminderFlags()
58 {
59 return reminderFlags_;
60 }
61
SetReminderFlags(const uint32_t reminderFlag)62 void NotificationFlags::SetReminderFlags(const uint32_t reminderFlag)
63 {
64 reminderFlags_ = reminderFlag;
65 if (reminderFlags_ & NotificationConstant::ReminderFlag::VIBRATION_FLAG) {
66 vibrationEnabled_ = NotificationConstant::FlagStatus::OPEN;
67 } else {
68 vibrationEnabled_ = NotificationConstant::FlagStatus::CLOSE;
69 }
70
71 if (reminderFlags_ & NotificationConstant::ReminderFlag::SOUND_FLAG) {
72 soundEnabled_ = NotificationConstant::FlagStatus::OPEN;
73 } else {
74 soundEnabled_ = NotificationConstant::FlagStatus::CLOSE;
75 }
76 }
77
SetLockScreenVisblenessEnabled(bool visblenessEnabled)78 void NotificationFlags::SetLockScreenVisblenessEnabled(bool visblenessEnabled)
79 {
80 if (visblenessEnabled) {
81 reminderFlags_ |= NotificationConstant::ReminderFlag::LOCKSCREEN_FLAG;
82 } else {
83 reminderFlags_ &= ~(NotificationConstant::ReminderFlag::LOCKSCREEN_FLAG);
84 }
85 }
86
IsLockScreenVisblenessEnabled()87 bool NotificationFlags::IsLockScreenVisblenessEnabled()
88 {
89 if ((reminderFlags_ & NotificationConstant::ReminderFlag::LOCKSCREEN_FLAG) != 0) {
90 return true;
91 }
92 return false;
93 }
94
SetBannerEnabled(bool bannerEnabled)95 void NotificationFlags::SetBannerEnabled(bool bannerEnabled)
96 {
97 if (bannerEnabled) {
98 reminderFlags_ |= NotificationConstant::ReminderFlag::BANNER_FLAG;
99 } else {
100 reminderFlags_ &= ~(NotificationConstant::ReminderFlag::BANNER_FLAG);
101 }
102 }
103
IsBannerEnabled()104 bool NotificationFlags::IsBannerEnabled()
105 {
106 if ((reminderFlags_ & NotificationConstant::ReminderFlag::BANNER_FLAG) != 0) {
107 return true;
108 }
109 return false;
110 }
111
SetLightScreenEnabled(bool lightScreenEnabled)112 void NotificationFlags::SetLightScreenEnabled(bool lightScreenEnabled)
113 {
114 if (lightScreenEnabled) {
115 reminderFlags_ |= NotificationConstant::ReminderFlag::LIGHTSCREEN_FLAG;
116 } else {
117 reminderFlags_ &= ~(NotificationConstant::ReminderFlag::LIGHTSCREEN_FLAG);
118 }
119 }
120
IsLightScreenEnabled()121 bool NotificationFlags::IsLightScreenEnabled()
122 {
123 if ((reminderFlags_ & NotificationConstant::ReminderFlag::LIGHTSCREEN_FLAG) != 0) {
124 return true;
125 }
126 return false;
127 }
128
SetStatusIconEnabled(bool statusIconEnabled)129 void NotificationFlags::SetStatusIconEnabled(bool statusIconEnabled)
130 {
131 if (statusIconEnabled) {
132 reminderFlags_ |= NotificationConstant::ReminderFlag::STATUSBAR_ICON_FLAG;
133 } else {
134 reminderFlags_ &= ~(NotificationConstant::ReminderFlag::STATUSBAR_ICON_FLAG);
135 }
136 }
137
IsStatusIconEnabled()138 bool NotificationFlags::IsStatusIconEnabled()
139 {
140 if ((reminderFlags_ & NotificationConstant::ReminderFlag::STATUSBAR_ICON_FLAG) != 0) {
141 return true;
142 }
143 return false;
144 }
145
Dump()146 std::string NotificationFlags::Dump()
147 {
148 return "soundEnabled = " + std::to_string(static_cast<uint8_t>(soundEnabled_)) +
149 ", vibrationEnabled = " + std::to_string(static_cast<uint8_t>(vibrationEnabled_)) +
150 ", reminderFlags = " + std::to_string(reminderFlags_);
151 }
152
ToJson(nlohmann::json & jsonObject) const153 bool NotificationFlags::ToJson(nlohmann::json &jsonObject) const
154 {
155 jsonObject["soundEnabled"] = soundEnabled_;
156 jsonObject["vibrationEnabled"] = vibrationEnabled_;
157 jsonObject["reminderFlags"] = reminderFlags_;
158
159 return true;
160 }
161
FromJson(const nlohmann::json & jsonObject)162 NotificationFlags *NotificationFlags::FromJson(const nlohmann::json &jsonObject)
163 {
164 if (jsonObject.is_null() or !jsonObject.is_object()) {
165 ANS_LOGE("Invalid JSON object");
166 return nullptr;
167 }
168
169 auto pFlags = new (std::nothrow) NotificationFlags();
170 if (pFlags == nullptr) {
171 ANS_LOGE("Failed to create notificationFlags instance");
172 return nullptr;
173 }
174
175 const auto &jsonEnd = jsonObject.cend();
176 if (jsonObject.find("soundEnabled") != jsonEnd && jsonObject.at("soundEnabled").is_number_integer()) {
177 auto soundEnabled = jsonObject.at("soundEnabled").get<uint8_t>();
178 pFlags->soundEnabled_ = static_cast<NotificationConstant::FlagStatus>(soundEnabled);
179 }
180
181 if (jsonObject.find("vibrationEnabled") != jsonEnd && jsonObject.at("vibrationEnabled").is_number_integer()) {
182 auto vibrationEnabled = jsonObject.at("vibrationEnabled").get<uint8_t>();
183 pFlags->vibrationEnabled_ = static_cast<NotificationConstant::FlagStatus>(vibrationEnabled);
184 }
185
186 if (jsonObject.find("reminderFlags") != jsonEnd && jsonObject.at("reminderFlags").is_number_integer()) {
187 auto reminderFlags = jsonObject.at("reminderFlags").get<uint32_t>();
188 pFlags->reminderFlags_ = reminderFlags;
189 }
190
191 return pFlags;
192 }
193
Marshalling(Parcel & parcel) const194 bool NotificationFlags::Marshalling(Parcel &parcel) const
195 {
196 if (!parcel.WriteUint8(static_cast<uint8_t>(soundEnabled_))) {
197 ANS_LOGE("Failed to write flag sound enable for the notification");
198 return false;
199 }
200
201 if (!parcel.WriteUint8(static_cast<uint8_t>(vibrationEnabled_))) {
202 ANS_LOGE("Failed to write flag vibration enable for the notification");
203 return false;
204 }
205
206 if (!parcel.WriteUint32(reminderFlags_)) {
207 ANS_LOGE("Failed to write reminder flags for the notification.");
208 return false;
209 }
210
211 return true;
212 }
213
Unmarshalling(Parcel & parcel)214 NotificationFlags *NotificationFlags::Unmarshalling(Parcel &parcel)
215 {
216 auto templ = new (std::nothrow) NotificationFlags();
217 if (templ == nullptr) {
218 ANS_LOGE("Failed to create NotificationFlags instance");
219 return nullptr;
220 }
221 if (!templ->ReadFromParcel(parcel)) {
222 delete templ;
223 templ = nullptr;
224 }
225
226 return templ;
227 }
228
ReadFromParcel(Parcel & parcel)229 bool NotificationFlags::ReadFromParcel(Parcel &parcel)
230 {
231 soundEnabled_ = static_cast<NotificationConstant::FlagStatus>(parcel.ReadUint8());
232 vibrationEnabled_ = static_cast<NotificationConstant::FlagStatus>(parcel.ReadUint8());
233 reminderFlags_ = parcel.ReadUint32();
234
235 return true;
236 }
237
GetReminderFlagsByString(const std::string & strReminderFlags,std::shared_ptr<NotificationFlags> & reminderFlags)238 bool NotificationFlags::GetReminderFlagsByString(
239 const std::string &strReminderFlags, std::shared_ptr<NotificationFlags> &reminderFlags)
240 {
241 if (strReminderFlags.size() <= SOUND_ENABLED_SEQ) {
242 ANS_LOGE("GetReminderFlagsByString failed as Invalid reminderFlags size.");
243 return false;
244 }
245 for (int32_t seq = 0; seq < strReminderFlags.size(); seq++) {
246 if (!ValidCharReminderFlag(strReminderFlags[seq], seq)) {
247 return false;
248 }
249 }
250 if (reminderFlags == nullptr) {
251 reminderFlags = std::make_shared<NotificationFlags>();
252 }
253 reminderFlags->SetSoundEnabled(
254 static_cast<NotificationConstant::FlagStatus>(strReminderFlags[SOUND_ENABLED_SEQ] - '0'));
255 reminderFlags->SetLockScreenVisblenessEnabled(
256 static_cast<bool>(strReminderFlags[LOCK_SCREEN_VISIBLENESS_ENABLED_SEQ] - '0'));
257 reminderFlags->SetBannerEnabled(static_cast<bool>(strReminderFlags[BANNER_ENABLED_SEQ] - '0'));
258 reminderFlags->SetLightScreenEnabled(static_cast<bool>(strReminderFlags[LIGHT_SCREEN_ENABLED_SEQ] - '0'));
259 reminderFlags->SetVibrationEnabled(
260 static_cast<NotificationConstant::FlagStatus>(strReminderFlags[VIBRATION_ENABLED_SEQ] - '0'));
261 reminderFlags->SetStatusIconEnabled(static_cast<bool>(strReminderFlags[ICON_ENABLED_SEQ] - '0'));
262 return true;
263 }
264
ValidCharReminderFlag(const char & charReminderFlag,const int32_t & seq)265 bool NotificationFlags::ValidCharReminderFlag(const char &charReminderFlag, const int32_t &seq)
266 {
267 if (charReminderFlag == CHAR_REMIND_DISABLE || charReminderFlag == CHAR_REMIND_ENABLE) {
268 return true;
269 }
270 if ((seq == SOUND_ENABLED_SEQ || seq == VIBRATION_ENABLED_SEQ) && charReminderFlag == CHAR_FLAG_STATUS_CLOSE) {
271 return true;
272 }
273 return false;
274 }
275 } // namespace Notification
276 } // namespace OHOS
277