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_distributed_options.h"
17 #include "ans_log_wrapper.h"
18
19 namespace OHOS {
20 namespace Notification {
NotificationDistributedOptions(bool distribute,const std::vector<std::string> & dvsDisplay,const std::vector<std::string> & dvsOperate)21 NotificationDistributedOptions::NotificationDistributedOptions(
22 bool distribute, const std::vector<std::string> &dvsDisplay, const std::vector<std::string> &dvsOperate)
23 : isDistributed_(distribute), devicesSupportDisplay_(dvsDisplay), devicesSupportOperate_(dvsOperate)
24 {}
25
SetDistributed(bool distribute)26 void NotificationDistributedOptions::SetDistributed(bool distribute)
27 {
28 isDistributed_ = distribute;
29 }
30
IsDistributed() const31 bool NotificationDistributedOptions::IsDistributed() const
32 {
33 return isDistributed_;
34 }
35
SetDevicesSupportDisplay(const std::vector<std::string> & devices)36 void NotificationDistributedOptions::SetDevicesSupportDisplay(const std::vector<std::string> &devices)
37 {
38 devicesSupportDisplay_ = devices;
39 }
40
GetDevicesSupportDisplay() const41 std::vector<std::string> NotificationDistributedOptions::GetDevicesSupportDisplay() const
42 {
43 return devicesSupportDisplay_;
44 }
45
SetDevicesSupportOperate(const std::vector<std::string> & devices)46 void NotificationDistributedOptions::SetDevicesSupportOperate(const std::vector<std::string> &devices)
47 {
48 devicesSupportOperate_ = devices;
49 }
50
GetDevicesSupportOperate() const51 std::vector<std::string> NotificationDistributedOptions::GetDevicesSupportOperate() const
52 {
53 return devicesSupportOperate_;
54 }
55
Dump()56 std::string NotificationDistributedOptions::Dump()
57 {
58 std::string devicesSupportDisplay = "";
59 for (const auto &device : devicesSupportDisplay_) {
60 devicesSupportDisplay += device;
61 devicesSupportDisplay += ", ";
62 }
63
64 std::string devicesSupportOperate = "";
65 for (const auto &device : devicesSupportOperate_) {
66 devicesSupportOperate += device;
67 devicesSupportOperate += ", ";
68 }
69
70 return "NotificationDistributedOptions{ "
71 "isDistributed = " + std::string((isDistributed_ ? "true" : "false")) +
72 ", devicesSupportDisplay = [" + devicesSupportDisplay + "]" +
73 ", devicesSupportOperate = [" + devicesSupportOperate + "]" +
74 " }";
75 }
76
ToJson(nlohmann::json & jsonObject) const77 bool NotificationDistributedOptions::ToJson(nlohmann::json &jsonObject) const
78 {
79 jsonObject["isDistributed"] = isDistributed_;
80 jsonObject["devicesSupportDisplay"] = nlohmann::json(devicesSupportDisplay_);
81 jsonObject["devicesSupportOperate"] = nlohmann::json(devicesSupportOperate_);
82
83 return true;
84 }
85
FromJson(const nlohmann::json & jsonObject)86 NotificationDistributedOptions *NotificationDistributedOptions::FromJson(const nlohmann::json &jsonObject)
87 {
88 if (jsonObject.is_null() or !jsonObject.is_object()) {
89 ANS_LOGE("Invalid JSON object");
90 return nullptr;
91 }
92
93 auto pOpt = new (std::nothrow) NotificationDistributedOptions();
94 if (pOpt == nullptr) {
95 ANS_LOGE("Failed to create distributedOptions instance");
96 return nullptr;
97 }
98
99 const auto &jsonEnd = jsonObject.cend();
100 if (jsonObject.find("isDistributed") != jsonEnd && jsonObject.at("isDistributed").is_boolean()) {
101 pOpt->isDistributed_ = jsonObject.at("isDistributed").get<bool>();
102 }
103
104 if (jsonObject.find("devicesSupportDisplay") != jsonEnd && jsonObject.at("devicesSupportDisplay").is_array()) {
105 pOpt->devicesSupportDisplay_ = jsonObject.at("devicesSupportDisplay").get<std::vector<std::string>>();
106 }
107
108 if (jsonObject.find("devicesSupportOperate") != jsonEnd && jsonObject.at("devicesSupportOperate").is_array()) {
109 pOpt->devicesSupportOperate_ = jsonObject.at("devicesSupportOperate").get<std::vector<std::string>>();
110 }
111
112 return pOpt;
113 }
114
Marshalling(Parcel & parcel) const115 bool NotificationDistributedOptions::Marshalling(Parcel &parcel) const
116 {
117 if (!parcel.WriteBool(isDistributed_)) {
118 ANS_LOGE("Failed to write flag isdistributed");
119 return false;
120 }
121
122 if (!parcel.WriteStringVector(devicesSupportDisplay_)) {
123 ANS_LOGE("Failed to write devicesSupportDisplay");
124 return false;
125 }
126
127 if (!parcel.WriteStringVector(devicesSupportOperate_)) {
128 ANS_LOGE("Failed to write devicesSupportOperate");
129 return false;
130 }
131
132 return true;
133 }
134
Unmarshalling(Parcel & parcel)135 NotificationDistributedOptions *NotificationDistributedOptions::Unmarshalling(Parcel &parcel)
136 {
137 auto objptr = new (std::nothrow) NotificationDistributedOptions();
138 if ((objptr != nullptr) && !objptr->ReadFromParcel(parcel)) {
139 delete objptr;
140 objptr = nullptr;
141 }
142
143 return objptr;
144 }
145
ReadFromParcel(Parcel & parcel)146 bool NotificationDistributedOptions::ReadFromParcel(Parcel &parcel)
147 {
148 isDistributed_ = parcel.ReadBool();
149
150 if (!parcel.ReadStringVector(&devicesSupportDisplay_)) {
151 ANS_LOGE("Failed to read devicesSupportDisplay");
152 return false;
153 }
154
155 if (!parcel.ReadStringVector(&devicesSupportOperate_)) {
156 ANS_LOGE("Failed to read devicesSupportOperate");
157 return false;
158 }
159
160 return true;
161 }
162 } // namespace Notification
163 } // namespace OHOS