• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "ans_log_wrapper.h"
18 
19 namespace OHOS {
20 namespace Notification {
SetSoundEnabled(NotificationConstant::FlagStatus soundEnabled)21 void NotificationFlags::SetSoundEnabled(NotificationConstant::FlagStatus soundEnabled)
22 {
23     soundEnabled_ = soundEnabled;
24 }
25 
IsSoundEnabled() const26 NotificationConstant::FlagStatus NotificationFlags::IsSoundEnabled() const
27 {
28     return soundEnabled_;
29 }
30 
SetVibrationEnabled(NotificationConstant::FlagStatus vibrationEnabled)31 void NotificationFlags::SetVibrationEnabled(NotificationConstant::FlagStatus vibrationEnabled)
32 {
33     vibrationEnabled_ = vibrationEnabled;
34 }
35 
IsVibrationEnabled() const36 NotificationConstant::FlagStatus NotificationFlags::IsVibrationEnabled() const
37 {
38     return vibrationEnabled_;
39 }
40 
Dump()41 std::string NotificationFlags::Dump()
42 {
43     return "soundEnabled = " + std::to_string(static_cast<uint8_t>(soundEnabled_)) +
44         ", vibrationEnabled = " + std::to_string(static_cast<uint8_t>(vibrationEnabled_));
45 }
46 
ToJson(nlohmann::json & jsonObject) const47 bool NotificationFlags::ToJson(nlohmann::json &jsonObject) const
48 {
49     jsonObject["soundEnabled"]     = soundEnabled_;
50     jsonObject["vibrationEnabled"] = vibrationEnabled_;
51 
52     return true;
53 }
54 
FromJson(const nlohmann::json & jsonObject)55 NotificationFlags *NotificationFlags::FromJson(const nlohmann::json &jsonObject)
56 {
57     if (jsonObject.is_null() or !jsonObject.is_object()) {
58         ANS_LOGE("Invalid JSON object");
59         return nullptr;
60     }
61 
62     auto pFlags = new (std::nothrow) NotificationFlags();
63     if (pFlags == nullptr) {
64         ANS_LOGE("Failed to create notificationFlags instance");
65         return nullptr;
66     }
67 
68     const auto &jsonEnd = jsonObject.cend();
69     if (jsonObject.find("soundEnabled") != jsonEnd) {
70         auto soundEnabled  = jsonObject.at("soundEnabled").get<uint8_t>();
71         pFlags->soundEnabled_ = static_cast<NotificationConstant::FlagStatus>(soundEnabled);
72     }
73 
74     if (jsonObject.find("vibrationEnabled") != jsonEnd) {
75         auto vibrationEnabled = jsonObject.at("vibrationEnabled").get<uint8_t>();
76         pFlags->vibrationEnabled_ = static_cast<NotificationConstant::FlagStatus>(vibrationEnabled);
77     }
78 
79     return pFlags;
80 }
81 
Marshalling(Parcel & parcel) const82 bool NotificationFlags::Marshalling(Parcel &parcel) const
83 {
84     if (!parcel.WriteUint8(static_cast<uint8_t>(soundEnabled_))) {
85         ANS_LOGE("Failed to write flag sound enable for the notification");
86         return false;
87     }
88 
89     if (!parcel.WriteUint8(static_cast<uint8_t>(vibrationEnabled_))) {
90         ANS_LOGE("Failed to write flag vibration enable for the notification");
91         return false;
92     }
93 
94     return true;
95 }
96 
Unmarshalling(Parcel & parcel)97 NotificationFlags *NotificationFlags::Unmarshalling(Parcel &parcel)
98 {
99     auto templ = new NotificationFlags();
100     if ((templ != nullptr) && !templ->ReadFromParcel(parcel)) {
101         delete templ;
102         templ = nullptr;
103     }
104 
105     return templ;
106 }
107 
ReadFromParcel(Parcel & parcel)108 bool NotificationFlags::ReadFromParcel(Parcel &parcel)
109 {
110     soundEnabled_ = static_cast<NotificationConstant::FlagStatus>(parcel.ReadUint8());
111     vibrationEnabled_ = static_cast<NotificationConstant::FlagStatus>(parcel.ReadUint8());
112 
113     return true;
114 }
115 }  // namespace Notification
116 }  // namespace OHOS
117