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
SetAppIndex(const int32_t appIndex)62 void NotificationBundleOption::SetAppIndex(const int32_t appIndex)
63 {
64 appIndex_ = appIndex;
65 }
66
GetAppIndex() const67 int32_t NotificationBundleOption::GetAppIndex() const
68 {
69 return appIndex_;
70 }
71
Dump()72 std::string NotificationBundleOption::Dump()
73 {
74 return "NotificationBundleOption{ "
75 "bundleName = " + bundleName_ +
76 ", uid = " + std::to_string(uid_) +
77 ", instanceKey = " + std::to_string(instanceKey_) +
78 ", appIndex = " + std::to_string(appIndex_) +
79 " }";
80 }
81
Marshalling(Parcel & parcel) const82 bool NotificationBundleOption::Marshalling(Parcel &parcel) const
83 {
84 if (!parcel.WriteString(bundleName_)) {
85 ANS_LOGE("Failed to write bundle name");
86 return false;
87 }
88
89 if (!parcel.WriteInt32(uid_)) {
90 ANS_LOGE("Failed to write uid");
91 return false;
92 }
93
94 if (!parcel.WriteInt32(instanceKey_)) {
95 ANS_LOGE("Failed to write instance key");
96 return false;
97 }
98
99 return true;
100 }
101
Unmarshalling(Parcel & parcel)102 NotificationBundleOption *NotificationBundleOption::Unmarshalling(Parcel &parcel)
103 {
104 auto pbundleOption = new (std::nothrow) NotificationBundleOption();
105 if (pbundleOption && !pbundleOption->ReadFromParcel(parcel)) {
106 delete pbundleOption;
107 pbundleOption = nullptr;
108 }
109
110 return pbundleOption;
111 }
112
ReadFromParcel(Parcel & parcel)113 bool NotificationBundleOption::ReadFromParcel(Parcel &parcel)
114 {
115 if (!parcel.ReadString(bundleName_)) {
116 ANS_LOGE("Failed to read bundle name");
117 return false;
118 }
119
120 uid_ = parcel.ReadInt32();
121
122 instanceKey_ = parcel.ReadInt32();
123
124 return true;
125 }
126
ToJson(nlohmann::json & jsonObject) const127 bool NotificationBundleOption::ToJson(nlohmann::json &jsonObject) const
128 {
129 jsonObject["uid"] = uid_;
130 jsonObject["bundleName"] = bundleName_;
131 jsonObject["instanceKey"] = instanceKey_;
132 jsonObject["appIndex"] = appIndex_;
133 return true;
134 }
135
FromJson(const nlohmann::json & jsonObject)136 NotificationBundleOption *NotificationBundleOption::FromJson(const nlohmann::json &jsonObject)
137 {
138 if (jsonObject.is_null() or !jsonObject.is_object()) {
139 ANS_LOGE("Invalid JSON object");
140 return nullptr;
141 }
142
143 auto *pBundle = new (std::nothrow) NotificationBundleOption();
144 if (pBundle == nullptr) {
145 ANS_LOGE("Failed to create bundle option instance");
146 return nullptr;
147 }
148
149 const auto &jsonEnd = jsonObject.cend();
150
151 if (jsonObject.find("uid") != jsonEnd && jsonObject.at("uid").is_number_integer()) {
152 pBundle->uid_ = jsonObject.at("uid").get<int32_t>();
153 }
154
155 if (jsonObject.find("bundleName") != jsonEnd && jsonObject.at("bundleName").is_string()) {
156 pBundle->bundleName_ = jsonObject.at("bundleName").get<std::string>();
157 }
158
159 if (jsonObject.find("instanceKey") != jsonEnd && jsonObject.at("instanceKey").is_number_integer()) {
160 pBundle->instanceKey_ = jsonObject.at("instanceKey").get<int32_t>();
161 }
162
163 if (jsonObject.find("appIndex") != jsonEnd && jsonObject.at("appIndex").is_number_integer()) {
164 pBundle->appIndex_ = jsonObject.at("appIndex").get<int32_t>();
165 }
166 return pBundle;
167 }
168
169 } // namespace Notification
170 } // namespace OHOS
171