• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
124     // write isSubscribeSelf
125     if (!parcel.WriteBool(isSubscribeSelf_)) {
126         ANS_LOGE("Can't write isSubscribeSelf");
127         return false;
128     }
129     return true;
130 }
131 
Unmarshalling(Parcel & parcel)132 NotificationSubscribeInfo *NotificationSubscribeInfo::Unmarshalling(Parcel &parcel)
133 {
134     NotificationSubscribeInfo *info = new (std::nothrow) NotificationSubscribeInfo();
135     if (info && !info->ReadFromParcel(parcel)) {
136         delete info;
137         info = nullptr;
138     }
139 
140     return info;
141 }
142 
ReadFromParcel(Parcel & parcel)143 bool NotificationSubscribeInfo::ReadFromParcel(Parcel &parcel)
144 {
145     // read appNames_
146     if (!parcel.ReadStringVector(&appNames_)) {
147         ANS_LOGE("Can't read appNames_");
148         return false;
149     }
150     //read deviceType_
151     if (!parcel.ReadString(deviceType_)) {
152         ANS_LOGE("Can't read deviceType_");
153         return false;
154     }
155     //read userId_
156     if (!parcel.ReadInt32(userId_)) {
157         ANS_LOGE("Can't read userId_");
158         return false;
159     }
160     //read slotTypes_
161     uint32_t size = 0;
162     if (!parcel.ReadUint32(size)) {
163         ANS_LOGE("Can't read size");
164         return false;
165     }
166     if (size > MAX_SLOT_SIZE) {
167         ANS_LOGE("slotType_ size over 1000.");
168         return false;
169     }
170     for (uint32_t index = 0; index < size; index++) {
171         int32_t slotType = -1;
172         if (!parcel.ReadInt32(slotType)) {
173             ANS_LOGE("Can't read slotType");
174             return false;
175         }
176         slotTypes_.emplace_back(static_cast<NotificationConstant::SlotType>(slotType));
177         }
178     // read filterType_
179     if (!parcel.ReadUint32(filterType_)) {
180         ANS_LOGE("Can't read filterType_");
181         return false;
182     }
183 
184     // read needNotifyApplicationChanged_
185     needNotifyApplicationChanged_ = parcel.ReadBool();
186     // read needNotifyResponse
187     needNotifyResponse_ = parcel.ReadBool();
188     // read isSubscribeSelf_
189     isSubscribeSelf_ = parcel.ReadBool();
190     return true;
191 }
192 
Dump()193 std::string NotificationSubscribeInfo::Dump()
194 {
195     std::string appNames = "";
196     for (auto name : appNames_) {
197         appNames += name;
198         appNames += ", ";
199     }
200     std::string slotTypes = "";
201     for (auto slotType : slotTypes_) {
202         slotTypes += std::to_string(static_cast<int32_t>(slotType));
203         slotTypes += ", ";
204     }
205     return "NotificationSubscribeInfo{ "
206             "appNames = [" + appNames + "]" +
207             "deviceType = " + deviceType_ +
208             "userId = " + std::to_string(userId_) +
209             "slotTypes = [" + slotTypes + "]" +
210             "needNotify = " + std::to_string(needNotifyApplicationChanged_) +
211             "filterType = " + std::to_string(filterType_) +
212             "needResponse = " + std::to_string(needNotifyResponse_) +
213             "isSubscribeSelf = " + std::to_string(isSubscribeSelf_) +
214             " }";
215 }
216 
SetSubscriberUid(const int32_t uid)217 void NotificationSubscribeInfo::SetSubscriberUid(const int32_t uid)
218 {
219     subscriberUid_ = uid;
220 }
221 
GetSubscriberUid() const222 int32_t NotificationSubscribeInfo::GetSubscriberUid() const
223 {
224     return subscriberUid_;
225 }
226 
SetSlotTypes(const std::vector<NotificationConstant::SlotType> slotTypes)227 void NotificationSubscribeInfo::SetSlotTypes(const std::vector<NotificationConstant::SlotType> slotTypes)
228 {
229     slotTypes_ = slotTypes;
230 }
231 
GetSlotTypes() const232 std::vector<NotificationConstant::SlotType> NotificationSubscribeInfo::GetSlotTypes() const
233 {
234     return slotTypes_;
235 }
236 
SetFilterType(const uint32_t filterType)237 void NotificationSubscribeInfo::SetFilterType(const uint32_t filterType)
238 {
239     filterType_ = filterType;
240 }
241 
GetFilterType() const242 uint32_t NotificationSubscribeInfo::GetFilterType() const
243 {
244     return filterType_;
245 }
246 
GetNeedNotifyApplication() const247 bool NotificationSubscribeInfo::GetNeedNotifyApplication() const
248 {
249     return needNotifyApplicationChanged_;
250 }
251 
SetNeedNotifyApplication(bool isNeed)252 void NotificationSubscribeInfo::SetNeedNotifyApplication(bool isNeed)
253 {
254     needNotifyApplicationChanged_ = isNeed;
255 }
256 
GetNeedNotifyResponse() const257 bool NotificationSubscribeInfo::GetNeedNotifyResponse() const
258 {
259     return needNotifyResponse_;
260 }
261 
SetNeedNotifyResponse(bool isNeed)262 void NotificationSubscribeInfo::SetNeedNotifyResponse(bool isNeed)
263 {
264     needNotifyResponse_ = isNeed;
265 }
266 
GetIsSubscribeSelf() const267 bool NotificationSubscribeInfo::GetIsSubscribeSelf() const
268 {
269     return isSubscribeSelf_;
270 }
271 
SetIsSubscribeSelf(bool isSubscribeSelf)272 void NotificationSubscribeInfo::SetIsSubscribeSelf(bool isSubscribeSelf)
273 {
274     isSubscribeSelf_ = isSubscribeSelf;
275 }
276 }  // namespace Notification
277 }  // namespace OHOS
278