• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const char *BUNDLE_INFO_SILENT_REMINDER = "enabledSilentReminder";
34 constexpr int32_t CONST_ENABLE_INT = 1;
35 }
SetBundleName(const std::string & name)36 void NotificationCloneBundleInfo::SetBundleName(const std::string &name)
37 {
38     bundleName_ = name;
39 }
40 
GetBundleName() const41 std::string NotificationCloneBundleInfo::GetBundleName() const
42 {
43     return bundleName_;
44 }
45 
SetAppIndex(const int32_t & appIndex)46 void NotificationCloneBundleInfo::SetAppIndex(const int32_t &appIndex)
47 {
48     appIndex_ = appIndex;
49 }
50 
GetAppIndex() const51 int32_t NotificationCloneBundleInfo::GetAppIndex() const
52 {
53     return appIndex_;
54 }
55 
SetUid(const int32_t & uid)56 void NotificationCloneBundleInfo::SetUid(const int32_t &uid)
57 {
58     uid_ = uid;
59 }
60 
GetUid() const61 int32_t NotificationCloneBundleInfo::GetUid() const
62 {
63     return uid_;
64 }
65 
SetSlotFlags(const uint32_t & slotFlags)66 void NotificationCloneBundleInfo::SetSlotFlags(const uint32_t &slotFlags)
67 {
68     slotFlags_ = slotFlags;
69 }
70 
GetSlotFlags() const71 uint32_t NotificationCloneBundleInfo::GetSlotFlags() const
72 {
73     return slotFlags_;
74 }
75 
SetIsShowBadge(const bool & isShowBadge)76 void NotificationCloneBundleInfo::SetIsShowBadge(const bool &isShowBadge)
77 {
78     isShowBadge_ = isShowBadge;
79 }
80 
GetIsShowBadge() const81 bool NotificationCloneBundleInfo::GetIsShowBadge() const
82 {
83     return isShowBadge_;
84 }
85 
SetEnableNotification(const NotificationConstant::SWITCH_STATE & state)86 void NotificationCloneBundleInfo::SetEnableNotification(const NotificationConstant::SWITCH_STATE &state)
87 {
88     isEnabledNotification_ = state;
89 }
90 
GetEnableNotification() const91 NotificationConstant::SWITCH_STATE NotificationCloneBundleInfo::GetEnableNotification() const
92 {
93     return isEnabledNotification_;
94 }
95 
SetSilentReminderEnabled(const NotificationConstant::SWITCH_STATE & silentReminderEnabled)96 void NotificationCloneBundleInfo::SetSilentReminderEnabled(
97     const NotificationConstant::SWITCH_STATE &silentReminderEnabled)
98 {
99     silentReminderEnabled_ = silentReminderEnabled;
100 }
101 
GetSilentReminderEnabled() const102 NotificationConstant::SWITCH_STATE NotificationCloneBundleInfo::GetSilentReminderEnabled() const
103 {
104     return silentReminderEnabled_;
105 }
106 
AddSlotInfo(const SlotInfo & slotInfo)107 void NotificationCloneBundleInfo::AddSlotInfo(const SlotInfo &slotInfo)
108 {
109     for (auto& item : slotsInfo_) {
110         if (item.slotType_ == slotInfo.slotType_) {
111             item.enable_ = slotInfo.enable_;
112             item.isForceControl_ = slotInfo.isForceControl_;
113             return;
114         }
115     }
116     slotsInfo_.push_back(slotInfo);
117 }
118 
GetSlotInfo() const119 std::vector<NotificationCloneBundleInfo::SlotInfo> NotificationCloneBundleInfo::GetSlotInfo() const
120 {
121     return slotsInfo_;
122 }
123 
ToJson(nlohmann::json & jsonObject) const124 void NotificationCloneBundleInfo::ToJson(nlohmann::json &jsonObject) const
125 {
126     if (!slotsInfo_.empty()) {
127         nlohmann::json jsonNodes = nlohmann::json::array();
128         for (size_t index = 0; index < slotsInfo_.size(); index++) {
129             nlohmann::json jsonNode;
130             jsonNode[BUNDLE_INFO_SLOT_TYPE] = static_cast<int32_t>(slotsInfo_[index].slotType_);
131             jsonNode[BUNDLE_INFO_SLOT_ENABLE] = slotsInfo_[index].enable_ ? 1 : 0;
132             jsonNode[BUNDLE_INFO_SLOT_CONTROL] = slotsInfo_[index].isForceControl_ ? 1 : 0;
133             jsonNodes.emplace_back(jsonNode);
134         }
135         jsonObject[BUNDLE_INFO_SLOT_LIST] = jsonNodes;
136     }
137 
138     jsonObject[BUNDLE_INFO_NAME] =  bundleName_;
139     jsonObject[BUNDLE_INFO_APP_INDEX] =  appIndex_;
140     jsonObject[BUNDLE_INFO_SLOT_FLAGS] =  slotFlags_;
141     jsonObject[BUNDLE_INFO_SHOW_BADGE] =  isShowBadge_ ? 1 : 0;
142     jsonObject[BUNDLE_INFO_ENABLE_NOTIFICATION] =  static_cast<int32_t>(isEnabledNotification_);
143     jsonObject[BUNDLE_INFO_SILENT_REMINDER] =  static_cast<int32_t>(silentReminderEnabled_);
144 }
145 
SlotsFromJson(const nlohmann::json & jsonObject)146 void NotificationCloneBundleInfo::SlotsFromJson(const nlohmann::json &jsonObject)
147 {
148     if (!jsonObject.contains(BUNDLE_INFO_SLOT_LIST) || !jsonObject[BUNDLE_INFO_SLOT_LIST].is_array()) {
149         return;
150     }
151 
152     for (auto &slotJson : jsonObject.at(BUNDLE_INFO_SLOT_LIST)) {
153         SlotInfo slotInfo;
154         if (slotJson.contains(BUNDLE_INFO_SLOT_TYPE) && slotJson[BUNDLE_INFO_SLOT_TYPE].is_number()) {
155             slotInfo.slotType_ = static_cast<NotificationConstant::SlotType>(
156                 slotJson.at(BUNDLE_INFO_SLOT_TYPE).get<int32_t>());
157         }
158         if (slotJson.contains(BUNDLE_INFO_SLOT_ENABLE) && slotJson[BUNDLE_INFO_SLOT_ENABLE].is_number()) {
159             int32_t slotEnable = slotJson.at(BUNDLE_INFO_SLOT_ENABLE).get<int32_t>();
160             slotInfo.enable_ = (slotEnable == CONST_ENABLE_INT);
161         }
162         if (slotJson.contains(BUNDLE_INFO_SLOT_CONTROL) && slotJson[BUNDLE_INFO_SLOT_CONTROL].is_number()) {
163             int32_t forceControl = slotJson.at(BUNDLE_INFO_SLOT_CONTROL).get<int32_t>();
164             slotInfo.isForceControl_ = (forceControl == CONST_ENABLE_INT);
165         }
166         slotsInfo_.emplace_back(slotInfo);
167     }
168 }
169 
FromJson(const nlohmann::json & jsonObject)170 void NotificationCloneBundleInfo::FromJson(const nlohmann::json &jsonObject)
171 {
172     if (jsonObject.is_null() || !jsonObject.is_object()) {
173         ANS_LOGE("Invalid JSON object");
174         return;
175     }
176     if (jsonObject.is_discarded()) {
177         ANS_LOGE("Failed to parse json string.");
178         return;
179     }
180 
181     if (jsonObject.contains(BUNDLE_INFO_NAME) && jsonObject[BUNDLE_INFO_NAME].is_string()) {
182         bundleName_ = jsonObject.at(BUNDLE_INFO_NAME).get<std::string>();
183     }
184     if (jsonObject.contains(BUNDLE_INFO_APP_INDEX) && jsonObject[BUNDLE_INFO_APP_INDEX].is_number()) {
185         appIndex_ = jsonObject.at(BUNDLE_INFO_APP_INDEX).get<int32_t>();
186     }
187     if (jsonObject.contains(BUNDLE_INFO_SLOT_FLAGS) && jsonObject[BUNDLE_INFO_SLOT_FLAGS].is_number()) {
188         slotFlags_ = jsonObject.at(BUNDLE_INFO_SLOT_FLAGS).get<uint32_t>();
189     }
190     if (jsonObject.contains(BUNDLE_INFO_SHOW_BADGE) && jsonObject[BUNDLE_INFO_SHOW_BADGE].is_number()) {
191         int32_t showBadge = jsonObject.at(BUNDLE_INFO_SHOW_BADGE).get<int32_t>();
192         isShowBadge_ = (showBadge == CONST_ENABLE_INT);
193     }
194     if (jsonObject.contains(BUNDLE_INFO_ENABLE_NOTIFICATION) &&
195         jsonObject[BUNDLE_INFO_ENABLE_NOTIFICATION].is_number()) {
196         int32_t enabledNotification = jsonObject.at(BUNDLE_INFO_ENABLE_NOTIFICATION).get<int32_t>();
197         if (enabledNotification >= static_cast<int32_t>(NotificationConstant::SWITCH_STATE::USER_MODIFIED_OFF) &&
198             enabledNotification <= static_cast<int32_t>(NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON)) {
199             isEnabledNotification_ = static_cast<NotificationConstant::SWITCH_STATE>(enabledNotification);
200         }
201     }
202     if (jsonObject.contains(BUNDLE_INFO_SILENT_REMINDER) && jsonObject[BUNDLE_INFO_SILENT_REMINDER].is_number()) {
203         int32_t silentReminderEnabled = jsonObject.at(BUNDLE_INFO_SILENT_REMINDER).get<int32_t>();
204         if (silentReminderEnabled >= static_cast<int32_t>(NotificationConstant::SWITCH_STATE::USER_MODIFIED_OFF) &&
205             silentReminderEnabled <= static_cast<int32_t>(NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON)) {
206             silentReminderEnabled_ = static_cast<NotificationConstant::SWITCH_STATE>(silentReminderEnabled);
207         }
208     }
209     SlotsFromJson(jsonObject);
210 }
Dump() const211 std::string NotificationCloneBundleInfo::SlotInfo::Dump() const
212 {
213     return "type: " + std::to_string(slotType_) + " " + std::to_string(enable_) + " "
214         + std::to_string(isForceControl_);
215 }
216 
Dump() const217 std::string NotificationCloneBundleInfo::Dump() const
218 {
219     std::string slotDump = "{";
220     for (auto& slot : slotsInfo_) {
221         slotDump += slot.Dump();
222         slotDump += ",";
223     }
224     slotDump += "}";
225     return "CloneBundle{ name = " + bundleName_ +
226             ", index = " + std::to_string(appIndex_) +
227             ", uid = " + std::to_string(uid_) +
228             ", slotFlags = " + std::to_string(slotFlags_) +
229             ", ShowBadge = " + std::to_string(isShowBadge_) +
230             ", isEnabled = " + std::to_string(static_cast<int32_t>(isEnabledNotification_)) +
231             ", slotsInfo = " + slotDump +
232             ", silentReminderEnabled = " + std::to_string(static_cast<int32_t>(silentReminderEnabled_)) +
233             " }";
234 }
235 }
236 }
237