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