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 {
NotificationSubscribeInfo()26 NotificationSubscribeInfo::NotificationSubscribeInfo()
27 {}
28 
~NotificationSubscribeInfo()29 NotificationSubscribeInfo::~NotificationSubscribeInfo()
30 {}
31 
NotificationSubscribeInfo(const NotificationSubscribeInfo & subscribeInfo)32 NotificationSubscribeInfo::NotificationSubscribeInfo(const NotificationSubscribeInfo &subscribeInfo)
33 {
34     appNames_ = subscribeInfo.GetAppNames();
35     deviceType_ = subscribeInfo.GetDeviceType();
36     userId_ = subscribeInfo.GetAppUserId();
37     subscriberUid_ = subscribeInfo.GetSubscriberUid();
38 }
39 
AddAppName(const std::string appName)40 void NotificationSubscribeInfo::AddAppName(const std::string appName)
41 {
42     appNames_.push_back(appName);
43 }
44 
AddAppNames(const std::vector<std::string> & appNames)45 void NotificationSubscribeInfo::AddAppNames(const std::vector<std::string> &appNames)
46 {
47     appNames_.insert(appNames_.end(), appNames.begin(), appNames.end());
48 }
49 
GetAppNames() const50 std::vector<std::string> NotificationSubscribeInfo::GetAppNames() const
51 {
52     return appNames_;
53 }
54 
AddAppUserId(const int32_t userId)55 void NotificationSubscribeInfo::AddAppUserId(const int32_t userId)
56 {
57     userId_ = userId;
58 }
59 
GetAppUserId() const60 int32_t NotificationSubscribeInfo::GetAppUserId() const
61 {
62     return userId_;
63 }
64 
AddDeviceType(const std::string deviceType)65 void NotificationSubscribeInfo::AddDeviceType(const std::string deviceType)
66 {
67     deviceType_ = deviceType;
68 }
69 
GetDeviceType() const70 std::string NotificationSubscribeInfo::GetDeviceType() const
71 {
72     return deviceType_;
73 }
74 
Marshalling(Parcel & parcel) const75 bool NotificationSubscribeInfo::Marshalling(Parcel &parcel) const
76 {
77     // write appNames_
78     if (!parcel.WriteStringVector(appNames_)) {
79         ANS_LOGE("Can't write appNames_");
80         return false;
81     }
82     // write deviceType_
83     if (!parcel.WriteString(deviceType_)) {
84         ANS_LOGE("Can't write deviceType_");
85         return false;
86     }
87     // write userId_
88     if (!parcel.WriteInt32(userId_)) {
89         ANS_LOGE("Can't write userId_");
90         return false;
91     }
92     return true;
93 }
94 
Unmarshalling(Parcel & parcel)95 NotificationSubscribeInfo *NotificationSubscribeInfo::Unmarshalling(Parcel &parcel)
96 {
97     NotificationSubscribeInfo *info = new (std::nothrow) NotificationSubscribeInfo();
98     if (info && !info->ReadFromParcel(parcel)) {
99         delete info;
100         info = nullptr;
101     }
102 
103     return info;
104 }
105 
ReadFromParcel(Parcel & parcel)106 bool NotificationSubscribeInfo::ReadFromParcel(Parcel &parcel)
107 {
108     // read appNames_
109     if (!parcel.ReadStringVector(&appNames_)) {
110         ANS_LOGE("Can't read appNames_");
111         return false;
112     }
113     //read deviceType_
114     if (!parcel.ReadString(deviceType_)) {
115         ANS_LOGE("Can't read deviceType_");
116         return false;
117     }
118     //read userId_
119     if (!parcel.ReadInt32(userId_)) {
120         ANS_LOGE("Can't read userId_");
121         return false;
122     }
123     return true;
124 }
125 
Dump()126 std::string NotificationSubscribeInfo::Dump()
127 {
128     std::string appNames = "";
129     for (auto name : appNames_) {
130         appNames += name;
131         appNames += ", ";
132     }
133     return "NotificationSubscribeInfo{ "
134             "appNames = [" + appNames + "]" +
135             "deviceType = " + deviceType_ +
136             "userId = " + std::to_string(userId_) +
137             " }";
138 }
139 
SetSubscriberUid(const int32_t uid)140 void NotificationSubscribeInfo::SetSubscriberUid(const int32_t uid)
141 {
142     subscriberUid_ = uid;
143 }
144 
GetSubscriberUid() const145 int32_t NotificationSubscribeInfo::GetSubscriberUid() const
146 {
147     return subscriberUid_;
148 }
149 }  // namespace Notification
150 }  // namespace OHOS
151