1 /*
2 * Copyright (c) 2024-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_clone_bundle_info.h"
17
18 #include "ans_log_wrapper.h"
19
20 namespace OHOS {
21 namespace Notification {
22
23 namespace {
24 constexpr const char *BUNDLE_INFO_NAME = "name";
25 constexpr const char *BUNDLE_INFO_APP_INDEX = "index";
26 constexpr const char *BUNDLE_INFO_SLOT_FLAGS = "slotFlags";
27 constexpr const char *BUNDLE_INFO_SHOW_BADGE = "badge";
28 constexpr const char *BUNDLE_INFO_ENABLE_NOTIFICATION = "enable";
29 constexpr const char *BUNDLE_INFO_SLOT_LIST = "slotList";
30 constexpr const char *BUNDLE_INFO_SLOT_TYPE = "slotType";
31 constexpr const char *BUNDLE_INFO_SLOT_ENABLE = "slotEnable";
32 constexpr const char *BUNDLE_INFO_SLOT_CONTROL = "slotControl";
33 constexpr int32_t CONST_ENABLE_INT = 1;
34 }
SetBundleName(const std::string & name)35 void NotificationCloneBundleInfo::SetBundleName(const std::string &name)
36 {
37 bundleName_ = name;
38 }
39
GetBundleName() const40 std::string NotificationCloneBundleInfo::GetBundleName() const
41 {
42 return bundleName_;
43 }
44
SetAppIndex(const int32_t & appIndex)45 void NotificationCloneBundleInfo::SetAppIndex(const int32_t &appIndex)
46 {
47 appIndex_ = appIndex;
48 }
49
GetAppIndex() const50 int32_t NotificationCloneBundleInfo::GetAppIndex() const
51 {
52 return appIndex_;
53 }
54
SetUid(const int32_t & uid)55 void NotificationCloneBundleInfo::SetUid(const int32_t &uid)
56 {
57 uid_ = uid;
58 }
59
GetUid() const60 int32_t NotificationCloneBundleInfo::GetUid() const
61 {
62 return uid_;
63 }
64
SetSlotFlags(const uint32_t & slotFlags)65 void NotificationCloneBundleInfo::SetSlotFlags(const uint32_t &slotFlags)
66 {
67 slotFlags_ = slotFlags;
68 }
69
GetSlotFlags() const70 uint32_t NotificationCloneBundleInfo::GetSlotFlags() const
71 {
72 return slotFlags_;
73 }
74
SetIsShowBadge(const bool & isShowBadge)75 void NotificationCloneBundleInfo::SetIsShowBadge(const bool &isShowBadge)
76 {
77 isShowBadge_ = isShowBadge;
78 }
79
GetIsShowBadge() const80 bool NotificationCloneBundleInfo::GetIsShowBadge() const
81 {
82 return isShowBadge_;
83 }
84
SetEnableNotification(const bool & enable)85 void NotificationCloneBundleInfo::SetEnableNotification(const bool &enable)
86 {
87 isEnabledNotification_ = enable;
88 }
89
GetEnableNotification() const90 bool NotificationCloneBundleInfo::GetEnableNotification() const
91 {
92 return isEnabledNotification_;
93 }
94
AddSlotInfo(const SlotInfo & slotInfo)95 void NotificationCloneBundleInfo::AddSlotInfo(const SlotInfo &slotInfo)
96 {
97 for (auto& item : slotsInfo_) {
98 if (item.slotType_ == slotInfo.slotType_) {
99 item.enable_ = slotInfo.enable_;
100 item.isForceControl_ = slotInfo.isForceControl_;
101 return;
102 }
103 }
104 slotsInfo_.push_back(slotInfo);
105 }
106
GetSlotInfo() const107 std::vector<NotificationCloneBundleInfo::SlotInfo> NotificationCloneBundleInfo::GetSlotInfo() const
108 {
109 return slotsInfo_;
110 }
111
ToJson(nlohmann::json & jsonObject) const112 void NotificationCloneBundleInfo::ToJson(nlohmann::json &jsonObject) const
113 {
114 if (!slotsInfo_.empty()) {
115 nlohmann::json jsonNodes = nlohmann::json::array();
116 for (size_t index = 0; index < slotsInfo_.size(); index++) {
117 nlohmann::json jsonNode;
118 jsonNode[BUNDLE_INFO_SLOT_TYPE] = static_cast<int32_t>(slotsInfo_[index].slotType_);
119 jsonNode[BUNDLE_INFO_SLOT_ENABLE] = slotsInfo_[index].enable_ ? 1 : 0;
120 jsonNode[BUNDLE_INFO_SLOT_CONTROL] = slotsInfo_[index].isForceControl_ ? 1 : 0;
121 jsonNodes.emplace_back(jsonNode);
122 }
123 jsonObject[BUNDLE_INFO_SLOT_LIST] = jsonNodes;
124 }
125
126 jsonObject[BUNDLE_INFO_NAME] = bundleName_;
127 jsonObject[BUNDLE_INFO_APP_INDEX] = appIndex_;
128 jsonObject[BUNDLE_INFO_SLOT_FLAGS] = slotFlags_;
129 jsonObject[BUNDLE_INFO_SHOW_BADGE] = isShowBadge_ ? 1 : 0;
130 jsonObject[BUNDLE_INFO_ENABLE_NOTIFICATION] = isEnabledNotification_ ? 1 : 0;
131 }
132
SlotsFromJson(const nlohmann::json & jsonObject)133 void NotificationCloneBundleInfo::SlotsFromJson(const nlohmann::json &jsonObject)
134 {
135 if (!jsonObject.contains(BUNDLE_INFO_SLOT_LIST) || !jsonObject[BUNDLE_INFO_SLOT_LIST].is_array()) {
136 return;
137 }
138
139 for (auto &slotJson : jsonObject.at(BUNDLE_INFO_SLOT_LIST)) {
140 SlotInfo slotInfo;
141 if (slotJson.contains(BUNDLE_INFO_SLOT_TYPE) && slotJson[BUNDLE_INFO_SLOT_TYPE].is_number()) {
142 slotInfo.slotType_ = static_cast<NotificationConstant::SlotType>(
143 slotJson.at(BUNDLE_INFO_SLOT_TYPE).get<int32_t>());
144 }
145 if (slotJson.contains(BUNDLE_INFO_SLOT_ENABLE) && slotJson[BUNDLE_INFO_SLOT_ENABLE].is_number()) {
146 int32_t slotEnable = slotJson.at(BUNDLE_INFO_SLOT_ENABLE).get<int32_t>();
147 slotInfo.enable_ = (slotEnable == CONST_ENABLE_INT);
148 }
149 if (slotJson.contains(BUNDLE_INFO_SLOT_CONTROL) && slotJson[BUNDLE_INFO_SLOT_CONTROL].is_number()) {
150 int32_t forceControl = slotJson.at(BUNDLE_INFO_SLOT_CONTROL).get<int32_t>();
151 slotInfo.isForceControl_ = (forceControl == CONST_ENABLE_INT);
152 }
153 slotsInfo_.emplace_back(slotInfo);
154 }
155 }
156
FromJson(const nlohmann::json & jsonObject)157 void NotificationCloneBundleInfo::FromJson(const nlohmann::json &jsonObject)
158 {
159 if (jsonObject.is_null() || !jsonObject.is_object()) {
160 ANS_LOGE("Invalid JSON object");
161 return;
162 }
163 if (jsonObject.is_discarded()) {
164 ANS_LOGE("Failed to parse json string.");
165 return;
166 }
167
168 if (jsonObject.contains(BUNDLE_INFO_NAME) && jsonObject[BUNDLE_INFO_NAME].is_string()) {
169 bundleName_ = jsonObject.at(BUNDLE_INFO_NAME).get<std::string>();
170 }
171 if (jsonObject.contains(BUNDLE_INFO_APP_INDEX) && jsonObject[BUNDLE_INFO_APP_INDEX].is_number()) {
172 appIndex_ = jsonObject.at(BUNDLE_INFO_APP_INDEX).get<int32_t>();
173 }
174 if (jsonObject.contains(BUNDLE_INFO_SLOT_FLAGS) && jsonObject[BUNDLE_INFO_SLOT_FLAGS].is_number()) {
175 slotFlags_ = jsonObject.at(BUNDLE_INFO_SLOT_FLAGS).get<uint32_t>();
176 }
177 if (jsonObject.contains(BUNDLE_INFO_SHOW_BADGE) && jsonObject[BUNDLE_INFO_SHOW_BADGE].is_number()) {
178 int32_t showBadge = jsonObject.at(BUNDLE_INFO_SHOW_BADGE).get<int32_t>();
179 isShowBadge_ = (showBadge == CONST_ENABLE_INT);
180 }
181 if (jsonObject.contains(BUNDLE_INFO_ENABLE_NOTIFICATION) &&
182 jsonObject[BUNDLE_INFO_ENABLE_NOTIFICATION].is_number()) {
183 int32_t enabledNotification = jsonObject.at(BUNDLE_INFO_ENABLE_NOTIFICATION).get<int32_t>();
184 isEnabledNotification_ = (enabledNotification == CONST_ENABLE_INT);
185 }
186 SlotsFromJson(jsonObject);
187 }
Dump() const188 std::string NotificationCloneBundleInfo::SlotInfo::Dump() const
189 {
190 return "type: " + std::to_string(slotType_) + " " + std::to_string(enable_) + " "
191 + std::to_string(isForceControl_);
192 }
193
Dump() const194 std::string NotificationCloneBundleInfo::Dump() const
195 {
196 std::string slotDump = "{";
197 for (auto& slot : slotsInfo_) {
198 slotDump += slot.Dump();
199 slotDump += ",";
200 }
201 slotDump += "}";
202 return "CloneBundle{ name = " + bundleName_ +
203 ", index = " + std::to_string(appIndex_) +
204 ", uid = " + std::to_string(uid_) +
205 ", slotFlags = " + std::to_string(slotFlags_) +
206 ", ShowBadge = " + std::to_string(isShowBadge_) +
207 ", isEnabled = " + std::to_string(isEnabledNotification_) +
208 ", slotsInfo = " + slotDump +
209 " }";
210 }
211 }
212 }
213