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_subscribe_info.h"
17
18 #include <string> // for basic_string, operator+
19 #include <vector> // for vector
20
21 #include "ans_log_wrapper.h"
22 #include "parcel.h" // for Parcel
23
24 namespace OHOS {
25 namespace Notification {
26 constexpr uint32_t MAX_SLOT_SIZE = 1000;
NotificationSubscribeInfo()27 NotificationSubscribeInfo::NotificationSubscribeInfo()
28 {}
29
~NotificationSubscribeInfo()30 NotificationSubscribeInfo::~NotificationSubscribeInfo()
31 {}
32
NotificationSubscribeInfo(const NotificationSubscribeInfo & subscribeInfo)33 NotificationSubscribeInfo::NotificationSubscribeInfo(const NotificationSubscribeInfo &subscribeInfo)
34 {
35 appNames_ = subscribeInfo.GetAppNames();
36 deviceType_ = subscribeInfo.GetDeviceType();
37 userId_ = subscribeInfo.GetAppUserId();
38 subscriberUid_ = subscribeInfo.GetSubscriberUid();
39 slotTypes_ = subscribeInfo.GetSlotTypes();
40 filterType_ = subscribeInfo.GetFilterType();
41 }
42
AddAppName(const std::string appName)43 void NotificationSubscribeInfo::AddAppName(const std::string appName)
44 {
45 appNames_.push_back(appName);
46 }
47
AddAppNames(const std::vector<std::string> & appNames)48 void NotificationSubscribeInfo::AddAppNames(const std::vector<std::string> &appNames)
49 {
50 appNames_.insert(appNames_.end(), appNames.begin(), appNames.end());
51 }
52
GetAppNames() const53 std::vector<std::string> NotificationSubscribeInfo::GetAppNames() const
54 {
55 return appNames_;
56 }
57
AddAppUserId(const int32_t userId)58 void NotificationSubscribeInfo::AddAppUserId(const int32_t userId)
59 {
60 userId_ = userId;
61 }
62
GetAppUserId() const63 int32_t NotificationSubscribeInfo::GetAppUserId() const
64 {
65 return userId_;
66 }
67
AddDeviceType(const std::string deviceType)68 void NotificationSubscribeInfo::AddDeviceType(const std::string deviceType)
69 {
70 deviceType_ = deviceType;
71 }
72
GetDeviceType() const73 std::string NotificationSubscribeInfo::GetDeviceType() const
74 {
75 return deviceType_;
76 }
77
Marshalling(Parcel & parcel) const78 bool NotificationSubscribeInfo::Marshalling(Parcel &parcel) const
79 {
80 // write appNames_
81 if (!parcel.WriteStringVector(appNames_)) {
82 ANS_LOGE("Can't write appNames_");
83 return false;
84 }
85 // write deviceType_
86 if (!parcel.WriteString(deviceType_)) {
87 ANS_LOGE("Can't write deviceType_");
88 return false;
89 }
90 // write userId_
91 if (!parcel.WriteInt32(userId_)) {
92 ANS_LOGE("Can't write userId_");
93 return false;
94 }
95 //write slotTypes_
96 if (!parcel.WriteUint32(slotTypes_.size())) {
97 ANS_LOGE("Failed to write slotTypes_ size.");
98 return false;
99 }
100 for (auto slotType : slotTypes_) {
101 if (!parcel.WriteInt32(static_cast<int32_t>(slotType))) {
102 ANS_LOGE("Failed to write slotType");
103 return false;
104 }
105 }
106 // write filterType_
107 if (!parcel.WriteUint32(filterType_)) {
108 ANS_LOGE("Can't write filterType_");
109 return false;
110 }
111
112 // write needNotifyApplicationChanged_
113 if (!parcel.WriteBool(needNotifyApplicationChanged_)) {
114 ANS_LOGE("Can't write needNotifyApplicationChanged");
115 return false;
116 }
117
118 // write needNotifyResponse
119 if (!parcel.WriteBool(needNotifyResponse_)) {
120 ANS_LOGE("Can't write needNotifyResponse");
121 return false;
122 }
123 return true;
124 }
125
Unmarshalling(Parcel & parcel)126 NotificationSubscribeInfo *NotificationSubscribeInfo::Unmarshalling(Parcel &parcel)
127 {
128 NotificationSubscribeInfo *info = new (std::nothrow) NotificationSubscribeInfo();
129 if (info && !info->ReadFromParcel(parcel)) {
130 delete info;
131 info = nullptr;
132 }
133
134 return info;
135 }
136
ReadFromParcel(Parcel & parcel)137 bool NotificationSubscribeInfo::ReadFromParcel(Parcel &parcel)
138 {
139 // read appNames_
140 if (!parcel.ReadStringVector(&appNames_)) {
141 ANS_LOGE("Can't read appNames_");
142 return false;
143 }
144 //read deviceType_
145 if (!parcel.ReadString(deviceType_)) {
146 ANS_LOGE("Can't read deviceType_");
147 return false;
148 }
149 //read userId_
150 if (!parcel.ReadInt32(userId_)) {
151 ANS_LOGE("Can't read userId_");
152 return false;
153 }
154 //read slotTypes_
155 uint32_t size = 0;
156 if (!parcel.ReadUint32(size)) {
157 ANS_LOGE("read slotType_ size failed.");
158 return false;
159 }
160 if (size > MAX_SLOT_SIZE) {
161 ANS_LOGE("slotType_ size over 1000.");
162 return false;
163 }
164 for (uint32_t index = 0; index < size; index++) {
165 int32_t slotType = -1;
166 if (!parcel.ReadInt32(slotType)) {
167 ANS_LOGE("read Parcelable slotType failed.");
168 return false;
169 }
170 slotTypes_.emplace_back(static_cast<NotificationConstant::SlotType>(slotType));
171 }
172 // read filterType_
173 if (!parcel.ReadUint32(filterType_)) {
174 ANS_LOGE("Can't read filterType_");
175 return false;
176 }
177
178 // read needNotifyApplicationChanged_
179 needNotifyApplicationChanged_ = parcel.ReadBool();
180 // read needNotifyResponse
181 needNotifyResponse_ = parcel.ReadBool();
182 return true;
183 }
184
Dump()185 std::string NotificationSubscribeInfo::Dump()
186 {
187 std::string appNames = "";
188 for (auto name : appNames_) {
189 appNames += name;
190 appNames += ", ";
191 }
192 std::string slotTypes = "";
193 for (auto slotType : slotTypes_) {
194 slotTypes += std::to_string(static_cast<int32_t>(slotType));
195 slotTypes += ", ";
196 }
197 return "NotificationSubscribeInfo{ "
198 "appNames = [" + appNames + "]" +
199 "deviceType = " + deviceType_ +
200 "userId = " + std::to_string(userId_) +
201 "slotTypes = [" + slotTypes + "]" +
202 "needNotify = " + std::to_string(needNotifyApplicationChanged_) +
203 "filterType = " + std::to_string(filterType_) +
204 "needResponse = " + std::to_string(needNotifyResponse_) +
205 " }";
206 }
207
SetSubscriberUid(const int32_t uid)208 void NotificationSubscribeInfo::SetSubscriberUid(const int32_t uid)
209 {
210 subscriberUid_ = uid;
211 }
212
GetSubscriberUid() const213 int32_t NotificationSubscribeInfo::GetSubscriberUid() const
214 {
215 return subscriberUid_;
216 }
217
SetSlotTypes(const std::vector<NotificationConstant::SlotType> slotTypes)218 void NotificationSubscribeInfo::SetSlotTypes(const std::vector<NotificationConstant::SlotType> slotTypes)
219 {
220 slotTypes_ = slotTypes;
221 }
222
GetSlotTypes() const223 std::vector<NotificationConstant::SlotType> NotificationSubscribeInfo::GetSlotTypes() const
224 {
225 return slotTypes_;
226 }
227
SetFilterType(const uint32_t filterType)228 void NotificationSubscribeInfo::SetFilterType(const uint32_t filterType)
229 {
230 filterType_ = filterType;
231 }
232
GetFilterType() const233 uint32_t NotificationSubscribeInfo::GetFilterType() const
234 {
235 return filterType_;
236 }
237
GetNeedNotifyApplication() const238 bool NotificationSubscribeInfo::GetNeedNotifyApplication() const
239 {
240 return needNotifyApplicationChanged_;
241 }
242
SetNeedNotifyApplication(bool isNeed)243 void NotificationSubscribeInfo::SetNeedNotifyApplication(bool isNeed)
244 {
245 needNotifyApplicationChanged_ = isNeed;
246 }
247
GetNeedNotifyResponse() const248 bool NotificationSubscribeInfo::GetNeedNotifyResponse() const
249 {
250 return needNotifyResponse_;
251 }
252
SetNeedNotifyResponse(bool isNeed)253 void NotificationSubscribeInfo::SetNeedNotifyResponse(bool isNeed)
254 {
255 needNotifyResponse_ = isNeed;
256 }
257 } // namespace Notification
258 } // namespace OHOS
259