1 /*
2 * Copyright (c) 2023 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 #include "notification_check_info.h"
16 #include "ans_log_wrapper.h"
17 #include "want_params_wrapper.h"
18 #include "nlohmann/json.hpp"
19
20 namespace OHOS {
21 namespace Notification {
22
NotificationCheckInfo(std::string pkgName,int32_t notifyId,int32_t contentType,int32_t creatorUserId,int32_t slotType,std::shared_ptr<AAFwk::WantParams> extraInfo)23 NotificationCheckInfo::NotificationCheckInfo(std::string pkgName, int32_t notifyId, int32_t contentType,
24 int32_t creatorUserId, int32_t slotType, std::shared_ptr<AAFwk::WantParams> extraInfo)
25 : pkgName_(pkgName), notifyId_(notifyId), contentType_(contentType),
26 creatorUserId_(creatorUserId), slotType_(slotType), extraInfo_(extraInfo)
27 {}
28
~NotificationCheckInfo()29 NotificationCheckInfo::~NotificationCheckInfo()
30 {}
31
GetPkgName() const32 std::string NotificationCheckInfo::GetPkgName() const
33 {
34 return pkgName_;
35 }
36
SetPkgName(std::string pkgName)37 void NotificationCheckInfo::SetPkgName(std::string pkgName)
38 {
39 pkgName_ = pkgName;
40 }
41
GetNotifyId() const42 int32_t NotificationCheckInfo::GetNotifyId() const
43 {
44 return notifyId_;
45 }
46
SetNotifyId(int32_t notifyId)47 void NotificationCheckInfo::SetNotifyId(int32_t notifyId)
48 {
49 notifyId_ = notifyId;
50 }
51
52
GetContentType() const53 int32_t NotificationCheckInfo::GetContentType() const
54 {
55 return contentType_;
56 }
57
SetContentType(int32_t contentType)58 void NotificationCheckInfo::SetContentType(int32_t contentType)
59 {
60 contentType_ = contentType;
61 }
62
63
GetCreatorUserId() const64 int32_t NotificationCheckInfo::GetCreatorUserId() const
65 {
66 return creatorUserId_;
67 }
68
SetCreatorUserId(int32_t creatorUserId)69 void NotificationCheckInfo::SetCreatorUserId(int32_t creatorUserId)
70 {
71 creatorUserId_ = creatorUserId;
72 }
73
GetSlotType() const74 int32_t NotificationCheckInfo::GetSlotType() const
75 {
76 return slotType_;
77 }
78
SetSlotType(int32_t slotType)79 void NotificationCheckInfo::SetSlotType(int32_t slotType)
80 {
81 slotType_ = slotType;
82 }
83
GetLabel() const84 std::string NotificationCheckInfo::GetLabel() const
85 {
86 return label_;
87 }
88
SetLabel(std::string label)89 void NotificationCheckInfo::SetLabel(std::string label)
90 {
91 label_ = label;
92 }
93
GetExtraInfo() const94 std::shared_ptr<AAFwk::WantParams> NotificationCheckInfo::GetExtraInfo() const
95 {
96 return extraInfo_;
97 }
98
SetExtraInfo(std::shared_ptr<AAFwk::WantParams> extraInfo)99 void NotificationCheckInfo::SetExtraInfo(std::shared_ptr<AAFwk::WantParams> extraInfo)
100 {
101 extraInfo_ = extraInfo;
102 }
103
ConvertJsonExtraInfoToValue(nlohmann::json & jsonobj)104 void NotificationCheckInfo::ConvertJsonExtraInfoToValue(nlohmann::json &jsonobj)
105 {
106 const auto &jsonEnd = jsonobj.cend();
107 if (jsonobj.find("extraInfo") == jsonEnd) {
108 return;
109 }
110
111 if (!jsonobj.at("extraInfo").is_string()) {
112 ANS_LOGE("Invalid JSON object extraInfo");
113 return;
114 }
115 auto extraInfoStr = jsonobj.at("extraInfo").get<std::string>();
116 if (!extraInfoStr.empty()) {
117 AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(extraInfoStr);
118 extraInfo_ = std::make_shared<AAFwk::WantParams>(params);
119 }
120 }
121
ConvertJsonStringToValue(const std::string & notificationData)122 void NotificationCheckInfo::ConvertJsonStringToValue(const std::string ¬ificationData)
123 {
124 nlohmann::json jsonobj = nlohmann::json::parse(notificationData);
125 if (jsonobj.is_null() || !jsonobj.is_object()) {
126 ANS_LOGE("Invalid JSON object");
127 return;
128 }
129
130 const auto &jsonEnd = jsonobj.cend();
131 if (jsonobj.find("pkgName") != jsonEnd && jsonobj.at("pkgName").is_string()) {
132 pkgName_ = jsonobj.at("pkgName").get<std::string>();
133 }
134 if (jsonobj.find("notifyId") != jsonEnd && jsonobj.at("notifyId").is_number()) {
135 notifyId_ = jsonobj.at("notifyId").get<int32_t>();
136 }
137 if (jsonobj.find("contentType") != jsonEnd && jsonobj.at("contentType").is_number()) {
138 contentType_ = jsonobj.at("contentType").get<int32_t>();
139 }
140 if (jsonobj.find("creatorUserId") != jsonEnd && jsonobj.at("creatorUserId").is_number()) {
141 creatorUserId_ = jsonobj.at("creatorUserId").get<int32_t>();
142 }
143 if (jsonobj.find("slotType") != jsonEnd && jsonobj.at("slotType").is_number()) {
144 slotType_ = jsonobj.at("slotType").get<int32_t>();
145 }
146 if (jsonobj.find("label") != jsonEnd && jsonobj.at("label").is_string()) {
147 label_ = jsonobj.at("label").get<std::string>();
148 }
149 ConvertJsonExtraInfoToValue(jsonobj);
150 }
151 } // namespace Notification
152 } // namespace OHOS
153