1 /*
2 * Copyright (c) 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_do_not_disturb_profile.h"
17
18 #include "ans_const_define.h"
19 #include "nlohmann/json.hpp"
20
21 namespace OHOS {
22 namespace Notification {
23 namespace {
24 constexpr const char *DO_NOT_DISTURB_PROFILE_ID = "id";
25 constexpr const char *DO_NOT_DISTURB_PROFILE_NAME = "name";
26 constexpr const char *DO_NOT_DISTURB_PROFILE_TRUSTLIST = "trustlist";
27 } // namespace
NotificationDoNotDisturbProfile(int32_t id,const std::string & name,const std::vector<NotificationBundleOption> & trustList)28 NotificationDoNotDisturbProfile::NotificationDoNotDisturbProfile(
29 int32_t id, const std::string &name, const std::vector<NotificationBundleOption> &trustList)
30 : id_(id), name_(name), trustList_(trustList)
31 {}
32
SetProfileId(int32_t id)33 void NotificationDoNotDisturbProfile::SetProfileId(int32_t id)
34 {
35 id_ = id;
36 }
37
SetProfileName(const std::string & name)38 void NotificationDoNotDisturbProfile::SetProfileName(const std::string &name)
39 {
40 name_ = name;
41 }
42
SetProfileTrustList(const std::vector<NotificationBundleOption> & trustList)43 void NotificationDoNotDisturbProfile::SetProfileTrustList(const std::vector<NotificationBundleOption> &trustList)
44 {
45 trustList_ = trustList;
46 }
47
GetProfileId() const48 int32_t NotificationDoNotDisturbProfile::GetProfileId() const
49 {
50 return id_;
51 }
52
GetProfileName() const53 std::string NotificationDoNotDisturbProfile::GetProfileName() const
54 {
55 return name_;
56 }
57
GetProfileTrustList() const58 std::vector<NotificationBundleOption> NotificationDoNotDisturbProfile::GetProfileTrustList() const
59 {
60 return trustList_;
61 }
62
Marshalling(Parcel & parcel) const63 bool NotificationDoNotDisturbProfile::Marshalling(Parcel &parcel) const
64 {
65 if (!parcel.WriteInt32(id_)) {
66 ANS_LOGE("Failed to write do not disturb id.");
67 return false;
68 }
69 if (!parcel.WriteString(name_)) {
70 ANS_LOGE("Failed to write do not disturb name.");
71 return false;
72 }
73 auto size = trustList_.size();
74 if (size > MAX_PARCELABLE_VECTOR_NUM) {
75 ANS_LOGE("Size exceeds the range.");
76 return false;
77 }
78 if (!parcel.WriteInt32(size)) {
79 ANS_LOGE("Failed to write do not disturb trust list size.");
80 return false;
81 }
82 for (uint32_t index = 0; index < size; ++index) {
83 if (!parcel.WriteParcelable(&trustList_[index])) {
84 ANS_LOGE("Failed to write do not disturb trust list.");
85 return false;
86 }
87 }
88 return true;
89 }
90
Unmarshalling(Parcel & parcel)91 NotificationDoNotDisturbProfile *NotificationDoNotDisturbProfile::Unmarshalling(Parcel &parcel)
92 {
93 auto objptr = new (std::nothrow) NotificationDoNotDisturbProfile();
94 if ((objptr != nullptr) && !objptr->ReadFromParcel(parcel)) {
95 delete objptr;
96 objptr = nullptr;
97 }
98 return objptr;
99 }
100
ReadFromParcel(Parcel & parcel)101 bool NotificationDoNotDisturbProfile::ReadFromParcel(Parcel &parcel)
102 {
103 id_ = parcel.ReadInt32();
104 name_ = parcel.ReadString();
105 auto size = parcel.ReadUint32();
106 if (size > MAX_PARCELABLE_VECTOR_NUM) {
107 ANS_LOGE("Size exceeds the range.");
108 return false;
109 }
110 for (uint32_t index = 0; index < size; ++index) {
111 sptr<NotificationBundleOption> bundleOption = parcel.ReadParcelable<NotificationBundleOption>();
112 if (bundleOption == nullptr) {
113 ANS_LOGE("Failed to read bundle option.");
114 return false;
115 }
116 trustList_.emplace_back(*bundleOption);
117 }
118 return true;
119 }
120
ToJson()121 std::string NotificationDoNotDisturbProfile::ToJson()
122 {
123 nlohmann::json jsonObject;
124 GetProfileJson(jsonObject);
125 return jsonObject.dump();
126 }
127
GetProfileJson(nlohmann::json & jsonObject) const128 void NotificationDoNotDisturbProfile::GetProfileJson(nlohmann::json &jsonObject) const
129 {
130 nlohmann::json jsonNodes = nlohmann::json::array();
131 for (size_t index = 0; index < trustList_.size(); index++) {
132 nlohmann::json jsonNode;
133 if (trustList_[index].ToJson(jsonNode)) {
134 jsonNodes.emplace_back(jsonNode);
135 }
136 }
137
138 jsonObject[DO_NOT_DISTURB_PROFILE_ID] = id_;
139 jsonObject[DO_NOT_DISTURB_PROFILE_NAME] = name_;
140 jsonObject[DO_NOT_DISTURB_PROFILE_TRUSTLIST] = jsonNodes;
141 }
142
FromJson(const std::string & jsonObj)143 void NotificationDoNotDisturbProfile::FromJson(const std::string &jsonObj)
144 {
145 nlohmann::json jsonObject = nlohmann::json::parse(jsonObj, nullptr, false);
146 if (jsonObject.is_null() or !jsonObject.is_object()) {
147 ANS_LOGE("Invalid JSON object");
148 return;
149 }
150 if (jsonObject.is_discarded()) {
151 ANS_LOGE("Failed to parse json string.");
152 return;
153 }
154 if (jsonObject.contains(DO_NOT_DISTURB_PROFILE_ID) && jsonObject[DO_NOT_DISTURB_PROFILE_ID].is_number()) {
155 id_ = jsonObject.at(DO_NOT_DISTURB_PROFILE_ID).get<int32_t>();
156 }
157 if (jsonObject.contains(DO_NOT_DISTURB_PROFILE_NAME) && jsonObject[DO_NOT_DISTURB_PROFILE_NAME].is_string()) {
158 name_ = jsonObject.at(DO_NOT_DISTURB_PROFILE_NAME).get<std::string>();
159 }
160 if (jsonObject.contains(DO_NOT_DISTURB_PROFILE_TRUSTLIST) &&
161 jsonObject[DO_NOT_DISTURB_PROFILE_TRUSTLIST].is_array()) {
162 for (auto &trust : jsonObject.at(DO_NOT_DISTURB_PROFILE_TRUSTLIST)) {
163 auto bundleOption = NotificationBundleOption::FromJson(trust);
164 if (bundleOption == nullptr) {
165 continue;
166 }
167 trustList_.push_back(*bundleOption);
168 delete bundleOption;
169 }
170 }
171 }
172 } // namespace Notification
173 } // namespace OHOS
174