1 /*
2 * Copyright (c) 2021 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_bundle_option.h"
17
18 #include <string> // for operator+, basic_string, to_...
19
20 #include "ans_log_wrapper.h"
21 #include "parcel.h" // for Parcel
22
23 namespace OHOS {
24 namespace Notification {
NotificationBundleOption(const std::string & bundleName,const int32_t uid)25 NotificationBundleOption::NotificationBundleOption(const std::string &bundleName, const int32_t uid)
26 : bundleName_(bundleName), uid_(uid)
27 {}
28
~NotificationBundleOption()29 NotificationBundleOption::~NotificationBundleOption()
30 {}
31
SetBundleName(const std::string & bundleName)32 void NotificationBundleOption::SetBundleName(const std::string &bundleName)
33 {
34 bundleName_ = bundleName;
35 }
36
GetBundleName() const37 std::string NotificationBundleOption::GetBundleName() const
38 {
39 return bundleName_;
40 }
41
SetUid(const int32_t uid)42 void NotificationBundleOption::SetUid(const int32_t uid)
43 {
44 uid_ = uid;
45 }
46
GetUid() const47 int32_t NotificationBundleOption::GetUid() const
48 {
49 return uid_;
50 }
51
SetInstanceKey(const int32_t key)52 void NotificationBundleOption::SetInstanceKey(const int32_t key)
53 {
54 instanceKey_ = key;
55 }
56
GetInstanceKey() const57 int32_t NotificationBundleOption::GetInstanceKey() const
58 {
59 return instanceKey_;
60 }
61
SetAppInstanceKey(const std::string & key)62 void NotificationBundleOption::SetAppInstanceKey(const std::string &key)
63 {
64 appInstanceKey_ = key;
65 }
66
GetAppInstanceKey() const67 std::string NotificationBundleOption::GetAppInstanceKey() const
68 {
69 return appInstanceKey_;
70 }
71
SetAppIndex(const int32_t appIndex)72 void NotificationBundleOption::SetAppIndex(const int32_t appIndex)
73 {
74 appIndex_ = appIndex;
75 }
76
GetAppIndex() const77 int32_t NotificationBundleOption::GetAppIndex() const
78 {
79 return appIndex_;
80 }
81
Dump()82 std::string NotificationBundleOption::Dump()
83 {
84 return "NotificationBundleOption{ "
85 "bundleName = " + bundleName_ +
86 ", uid = " + std::to_string(uid_) +
87 ", instanceKey = " + std::to_string(instanceKey_) +
88 ", appIndex = " + std::to_string(appIndex_) +
89 " }";
90 }
91
Marshalling(Parcel & parcel) const92 bool NotificationBundleOption::Marshalling(Parcel &parcel) const
93 {
94 if (!parcel.WriteString(bundleName_)) {
95 ANS_LOGE("Failed to write bundle name");
96 return false;
97 }
98
99 if (!parcel.WriteInt32(uid_)) {
100 ANS_LOGE("Failed to write uid");
101 return false;
102 }
103
104 if (!parcel.WriteInt32(instanceKey_)) {
105 ANS_LOGE("Failed to write instance key");
106 return false;
107 }
108
109 return true;
110 }
111
Unmarshalling(Parcel & parcel)112 NotificationBundleOption *NotificationBundleOption::Unmarshalling(Parcel &parcel)
113 {
114 auto pbundleOption = new (std::nothrow) NotificationBundleOption();
115 if (pbundleOption && !pbundleOption->ReadFromParcel(parcel)) {
116 delete pbundleOption;
117 pbundleOption = nullptr;
118 }
119
120 return pbundleOption;
121 }
122
ReadFromParcel(Parcel & parcel)123 bool NotificationBundleOption::ReadFromParcel(Parcel &parcel)
124 {
125 if (!parcel.ReadString(bundleName_)) {
126 ANS_LOGE("Failed to read bundle name");
127 return false;
128 }
129
130 uid_ = parcel.ReadInt32();
131
132 instanceKey_ = parcel.ReadInt32();
133
134 return true;
135 }
136
ToJson(nlohmann::json & jsonObject) const137 bool NotificationBundleOption::ToJson(nlohmann::json &jsonObject) const
138 {
139 jsonObject["uid"] = uid_;
140 jsonObject["bundleName"] = bundleName_;
141 jsonObject["instanceKey"] = instanceKey_;
142 jsonObject["appIndex"] = appIndex_;
143 return true;
144 }
145
FromJson(const nlohmann::json & jsonObject)146 NotificationBundleOption *NotificationBundleOption::FromJson(const nlohmann::json &jsonObject)
147 {
148 if (jsonObject.is_null() or !jsonObject.is_object()) {
149 ANS_LOGE("Invalid JSON object");
150 return nullptr;
151 }
152
153 auto *pBundle = new (std::nothrow) NotificationBundleOption();
154 if (pBundle == nullptr) {
155 ANS_LOGE("null pBundle");
156 return nullptr;
157 }
158
159 const auto &jsonEnd = jsonObject.cend();
160
161 if (jsonObject.find("uid") != jsonEnd && jsonObject.at("uid").is_number_integer()) {
162 pBundle->uid_ = jsonObject.at("uid").get<int32_t>();
163 }
164
165 if (jsonObject.find("bundleName") != jsonEnd && jsonObject.at("bundleName").is_string()) {
166 pBundle->bundleName_ = jsonObject.at("bundleName").get<std::string>();
167 }
168
169 if (jsonObject.find("instanceKey") != jsonEnd && jsonObject.at("instanceKey").is_number_integer()) {
170 pBundle->instanceKey_ = jsonObject.at("instanceKey").get<int32_t>();
171 }
172
173 if (jsonObject.find("appIndex") != jsonEnd && jsonObject.at("appIndex").is_number_integer()) {
174 pBundle->appIndex_ = jsonObject.at("appIndex").get<int32_t>();
175 }
176 return pBundle;
177 }
178
179 } // namespace Notification
180 } // namespace OHOS
181