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 
SetLockScreenVisblenessEnabled(bool visblenessEnabled)62 void NotificationFlags::SetLockScreenVisblenessEnabled(bool visblenessEnabled)
63 {
64     if (visblenessEnabled) {
65         reminderFlags_ |= NotificationConstant::ReminderFlag::LOCKSCREEN_FLAG;
66     } else {
67         reminderFlags_ &= ~(NotificationConstant::ReminderFlag::LOCKSCREEN_FLAG);
68     }
69 }
70 
IsLockScreenVisblenessEnabled()71 bool NotificationFlags::IsLockScreenVisblenessEnabled()
72 {
73     if ((reminderFlags_ & NotificationConstant::ReminderFlag::LOCKSCREEN_FLAG) != 0) {
74         return true;
75     }
76     return false;
77 }
78 
SetBannerEnabled(bool bannerEnabled)79 void NotificationFlags::SetBannerEnabled(bool bannerEnabled)
80 {
81     if (bannerEnabled) {
82         reminderFlags_ |= NotificationConstant::ReminderFlag::BANNER_FLAG;
83     } else {
84         reminderFlags_ &= ~(NotificationConstant::ReminderFlag::BANNER_FLAG);
85     }
86 }
87 
IsBannerEnabled()88 bool NotificationFlags::IsBannerEnabled()
89 {
90     if ((reminderFlags_ & NotificationConstant::ReminderFlag::BANNER_FLAG) != 0) {
91         return true;
92     }
93     return false;
94 }
95 
SetLightScreenEnabled(bool lightScreenEnabled)96 void NotificationFlags::SetLightScreenEnabled(bool lightScreenEnabled)
97 {
98     if (lightScreenEnabled) {
99         reminderFlags_ |= NotificationConstant::ReminderFlag::LIGHTSCREEN_FLAG;
100     } else {
101         reminderFlags_ &= ~(NotificationConstant::ReminderFlag::LIGHTSCREEN_FLAG);
102     }
103 }
104 
IsLightScreenEnabled()105 bool NotificationFlags::IsLightScreenEnabled()
106 {
107     if ((reminderFlags_ & NotificationConstant::ReminderFlag::LIGHTSCREEN_FLAG) != 0) {
108         return true;
109     }
110     return false;
111 }
112 
SetStatusIconEnabled(bool statusIconEnabled)113 void NotificationFlags::SetStatusIconEnabled(bool statusIconEnabled)
114 {
115     if (statusIconEnabled) {
116         reminderFlags_ |= NotificationConstant::ReminderFlag::STATUSBAR_ICON_FLAG;
117     } else {
118         reminderFlags_ &= ~(NotificationConstant::ReminderFlag::STATUSBAR_ICON_FLAG);
119     }
120 }
121 
IsStatusIconEnabled()122 bool NotificationFlags::IsStatusIconEnabled()
123 {
124     if ((reminderFlags_ & NotificationConstant::ReminderFlag::STATUSBAR_ICON_FLAG) != 0) {
125         return true;
126     }
127     return false;
128 }
129 
Dump()130 std::string NotificationFlags::Dump()
131 {
132     return "soundEnabled = " + std::to_string(static_cast<uint8_t>(soundEnabled_)) +
133            ", vibrationEnabled = " + std::to_string(static_cast<uint8_t>(vibrationEnabled_)) +
134            ", reminderFlags = " + std::to_string(reminderFlags_);
135 }
136 
ToJson(nlohmann::json & jsonObject) const137 bool NotificationFlags::ToJson(nlohmann::json &jsonObject) const
138 {
139     jsonObject["soundEnabled"]     = soundEnabled_;
140     jsonObject["vibrationEnabled"] = vibrationEnabled_;
141     jsonObject["reminderFlags"] = reminderFlags_;
142 
143     return true;
144 }
145 
FromJson(const nlohmann::json & jsonObject)146 NotificationFlags *NotificationFlags::FromJson(const nlohmann::json &jsonObject)
147 {
148     if (jsonObject.is_null() or !jsonObject.is_object()) {
149         ANS_LOGE("Invalid JSON object");
150         return nullptr;
151     }
152 
153     auto pFlags = new (std::nothrow) NotificationFlags();
154     if (pFlags == nullptr) {
155         ANS_LOGE("Failed to create notificationFlags instance");
156         return nullptr;
157     }
158 
159     const auto &jsonEnd = jsonObject.cend();
160     if (jsonObject.find("soundEnabled") != jsonEnd && jsonObject.at("soundEnabled").is_number_integer()) {
161         auto soundEnabled  = jsonObject.at("soundEnabled").get<uint8_t>();
162         pFlags->soundEnabled_ = static_cast<NotificationConstant::FlagStatus>(soundEnabled);
163     }
164 
165     if (jsonObject.find("vibrationEnabled") != jsonEnd && jsonObject.at("vibrationEnabled").is_number_integer()) {
166         auto vibrationEnabled = jsonObject.at("vibrationEnabled").get<uint8_t>();
167         pFlags->vibrationEnabled_ = static_cast<NotificationConstant::FlagStatus>(vibrationEnabled);
168     }
169 
170     if (jsonObject.find("reminderFlags") != jsonEnd && jsonObject.at("reminderFlags").is_number_integer()) {
171         auto reminderFlags = jsonObject.at("reminderFlags").get<uint32_t>();
172         pFlags->reminderFlags_ = reminderFlags;
173     }
174 
175     return pFlags;
176 }
177 
Marshalling(Parcel & parcel) const178 bool NotificationFlags::Marshalling(Parcel &parcel) const
179 {
180     if (!parcel.WriteUint8(static_cast<uint8_t>(soundEnabled_))) {
181         ANS_LOGE("Failed to write flag sound enable for the notification");
182         return false;
183     }
184 
185     if (!parcel.WriteUint8(static_cast<uint8_t>(vibrationEnabled_))) {
186         ANS_LOGE("Failed to write flag vibration enable for the notification");
187         return false;
188     }
189 
190     if (!parcel.WriteUint32(reminderFlags_)) {
191         ANS_LOGE("Failed to write reminder flags for the notification.");
192         return false;
193     }
194 
195     return true;
196 }
197 
Unmarshalling(Parcel & parcel)198 NotificationFlags *NotificationFlags::Unmarshalling(Parcel &parcel)
199 {
200     auto templ = new (std::nothrow) NotificationFlags();
201     if (templ == nullptr) {
202         ANS_LOGE("Failed to create NotificationFlags instance");
203         return nullptr;
204     }
205     if (!templ->ReadFromParcel(parcel)) {
206         delete templ;
207         templ = nullptr;
208     }
209 
210     return templ;
211 }
212 
ReadFromParcel(Parcel & parcel)213 bool NotificationFlags::ReadFromParcel(Parcel &parcel)
214 {
215     soundEnabled_ = static_cast<NotificationConstant::FlagStatus>(parcel.ReadUint8());
216     vibrationEnabled_ = static_cast<NotificationConstant::FlagStatus>(parcel.ReadUint8());
217     reminderFlags_ = parcel.ReadUint32();
218 
219     return true;
220 }
221 
GetReminderFlagsByString(const std::string & strReminderFlags,std::shared_ptr<NotificationFlags> & reminderFlags)222 bool NotificationFlags::GetReminderFlagsByString(
223     const std::string &strReminderFlags, std::shared_ptr<NotificationFlags> &reminderFlags)
224 {
225     if (strReminderFlags.size() <= SOUND_ENABLED_SEQ) {
226         ANS_LOGE("GetReminderFlagsByString failed as Invalid reminderFlags size.");
227         return false;
228     }
229     for (int32_t seq = 0; seq < strReminderFlags.size(); seq++) {
230         if (!ValidCharReminderFlag(strReminderFlags[seq], seq)) {
231             return false;
232         }
233     }
234     if (reminderFlags == nullptr) {
235         reminderFlags = std::make_shared<NotificationFlags>();
236     }
237     reminderFlags->SetSoundEnabled(
238         static_cast<NotificationConstant::FlagStatus>(strReminderFlags[SOUND_ENABLED_SEQ] - '0'));
239     reminderFlags->SetLockScreenVisblenessEnabled(
240         static_cast<bool>(strReminderFlags[LOCK_SCREEN_VISIBLENESS_ENABLED_SEQ] - '0'));
241     reminderFlags->SetBannerEnabled(static_cast<bool>(strReminderFlags[BANNER_ENABLED_SEQ] - '0'));
242     reminderFlags->SetLightScreenEnabled(static_cast<bool>(strReminderFlags[LIGHT_SCREEN_ENABLED_SEQ] - '0'));
243     reminderFlags->SetVibrationEnabled(
244         static_cast<NotificationConstant::FlagStatus>(strReminderFlags[VIBRATION_ENABLED_SEQ] - '0'));
245     reminderFlags->SetStatusIconEnabled(static_cast<bool>(strReminderFlags[ICON_ENABLED_SEQ] - '0'));
246     return true;
247 }
248 
ValidCharReminderFlag(const char & charReminderFlag,const int32_t & seq)249 bool NotificationFlags::ValidCharReminderFlag(const char &charReminderFlag, const int32_t &seq)
250 {
251     if (charReminderFlag == CHAR_REMIND_DISABLE || charReminderFlag == CHAR_REMIND_ENABLE) {
252         return true;
253     }
254     if ((seq == SOUND_ENABLED_SEQ || seq == VIBRATION_ENABLED_SEQ) && charReminderFlag == CHAR_FLAG_STATUS_CLOSE) {
255         return true;
256     }
257     return false;
258 }
259 }  // namespace Notification
260 }  // namespace OHOS
261