• 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 
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 }
31 
IsSoundEnabled() const32 NotificationConstant::FlagStatus NotificationFlags::IsSoundEnabled() const
33 {
34     return soundEnabled_;
35 }
36 
SetVibrationEnabled(NotificationConstant::FlagStatus vibrationEnabled)37 void NotificationFlags::SetVibrationEnabled(NotificationConstant::FlagStatus vibrationEnabled)
38 {
39     vibrationEnabled_ = vibrationEnabled;
40 }
41 
IsVibrationEnabled() const42 NotificationConstant::FlagStatus NotificationFlags::IsVibrationEnabled() const
43 {
44     return vibrationEnabled_;
45 }
46 
Dump()47 std::string NotificationFlags::Dump()
48 {
49     return "soundEnabled = " + std::to_string(static_cast<uint8_t>(soundEnabled_)) +
50         ", vibrationEnabled = " + std::to_string(static_cast<uint8_t>(vibrationEnabled_));
51 }
52 
ToJson(nlohmann::json & jsonObject) const53 bool NotificationFlags::ToJson(nlohmann::json &jsonObject) const
54 {
55     jsonObject["soundEnabled"]     = soundEnabled_;
56     jsonObject["vibrationEnabled"] = vibrationEnabled_;
57 
58     return true;
59 }
60 
FromJson(const nlohmann::json & jsonObject)61 NotificationFlags *NotificationFlags::FromJson(const nlohmann::json &jsonObject)
62 {
63     if (jsonObject.is_null() or !jsonObject.is_object()) {
64         ANS_LOGE("Invalid JSON object");
65         return nullptr;
66     }
67 
68     auto pFlags = new (std::nothrow) NotificationFlags();
69     if (pFlags == nullptr) {
70         ANS_LOGE("Failed to create notificationFlags instance");
71         return nullptr;
72     }
73 
74     const auto &jsonEnd = jsonObject.cend();
75     if (jsonObject.find("soundEnabled") != jsonEnd && jsonObject.at("soundEnabled").is_number_integer()) {
76         auto soundEnabled  = jsonObject.at("soundEnabled").get<uint8_t>();
77         pFlags->soundEnabled_ = static_cast<NotificationConstant::FlagStatus>(soundEnabled);
78     }
79 
80     if (jsonObject.find("vibrationEnabled") != jsonEnd && jsonObject.at("vibrationEnabled").is_number_integer()) {
81         auto vibrationEnabled = jsonObject.at("vibrationEnabled").get<uint8_t>();
82         pFlags->vibrationEnabled_ = static_cast<NotificationConstant::FlagStatus>(vibrationEnabled);
83     }
84 
85     return pFlags;
86 }
87 
Marshalling(Parcel & parcel) const88 bool NotificationFlags::Marshalling(Parcel &parcel) const
89 {
90     if (!parcel.WriteUint8(static_cast<uint8_t>(soundEnabled_))) {
91         ANS_LOGE("Failed to write flag sound enable for the notification");
92         return false;
93     }
94 
95     if (!parcel.WriteUint8(static_cast<uint8_t>(vibrationEnabled_))) {
96         ANS_LOGE("Failed to write flag vibration enable for the notification");
97         return false;
98     }
99 
100     return true;
101 }
102 
Unmarshalling(Parcel & parcel)103 NotificationFlags *NotificationFlags::Unmarshalling(Parcel &parcel)
104 {
105     auto templ = new NotificationFlags();
106     if ((templ != nullptr) && !templ->ReadFromParcel(parcel)) {
107         delete templ;
108         templ = nullptr;
109     }
110 
111     return templ;
112 }
113 
ReadFromParcel(Parcel & parcel)114 bool NotificationFlags::ReadFromParcel(Parcel &parcel)
115 {
116     soundEnabled_ = static_cast<NotificationConstant::FlagStatus>(parcel.ReadUint8());
117     vibrationEnabled_ = static_cast<NotificationConstant::FlagStatus>(parcel.ReadUint8());
118 
119     return true;
120 }
121 }  // namespace Notification
122 }  // namespace OHOS
123