1 /*
2  * Copyright (c) 2024 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_unified_group_Info.h"
17 
18 #include "ans_inner_errors.h"
19 #include "ans_log_wrapper.h"
20 #include "errors.h"
21 #include "want_agent_helper.h"
22 #include "want_params_wrapper.h"
23 #include <memory>
24 
25 namespace OHOS {
26 namespace Notification {
GetKey() const27 std::string NotificationUnifiedGroupInfo::GetKey() const
28 {
29     return key_;
30 }
31 
SetKey(const std::string & key)32 void NotificationUnifiedGroupInfo::SetKey(const std::string &key)
33 {
34     key_ = key;
35 }
36 
GetTitle() const37 std::string NotificationUnifiedGroupInfo::GetTitle() const
38 {
39     return title_;
40 }
41 
SetTitle(const std::string & title)42 void NotificationUnifiedGroupInfo::SetTitle(const std::string &title)
43 {
44     title_ = title;
45 }
46 
GetContent() const47 std::string NotificationUnifiedGroupInfo::GetContent() const
48 {
49     return content_;
50 }
51 
SetContent(const std::string & content)52 void NotificationUnifiedGroupInfo::SetContent(const std::string &content)
53 {
54     content_ = content;
55 }
56 
GetSceneName() const57 std::string NotificationUnifiedGroupInfo::GetSceneName() const
58 {
59     return sceneName_;
60 }
61 
SetSceneName(const std::string & sceneName)62 void NotificationUnifiedGroupInfo::SetSceneName(const std::string &sceneName)
63 {
64     sceneName_ = sceneName;
65 }
66 
67 
GetExtraInfo() const68 std::shared_ptr<AAFwk::WantParams> NotificationUnifiedGroupInfo::GetExtraInfo() const
69 {
70     return extraInfo_;
71 }
72 
SetExtraInfo(const std::shared_ptr<AAFwk::WantParams> & extras)73 void NotificationUnifiedGroupInfo::SetExtraInfo(const std::shared_ptr<AAFwk::WantParams> &extras)
74 {
75     extraInfo_ = extras;
76 }
77 
Dump()78 std::string NotificationUnifiedGroupInfo::Dump()
79 {
80     std::string extraStr{"null"};
81     if (extraInfo_ != nullptr) {
82         AAFwk::WantParamWrapper wWrapper(*extraInfo_);
83         extraStr = wWrapper.ToString();
84     }
85 
86     return "NotificationUnifiedGroupInfo{ key = " + key_ + ", title = " + title_ + ", content = " + content_ +
87             ", sceneName = " + sceneName_ + ", extraInfo = " + extraStr +" }";
88 }
89 
Marshalling(Parcel & parcel) const90 bool NotificationUnifiedGroupInfo::Marshalling(Parcel &parcel) const
91 {
92     if (!parcel.WriteString(key_)) {
93         ANS_LOGE("Failed to write key");
94         return false;
95     }
96 
97     if (!parcel.WriteString(title_)) {
98         ANS_LOGE("Failed to write title");
99         return false;
100     }
101 
102     if (!parcel.WriteString(content_)) {
103         ANS_LOGE("Failed to write content");
104         return false;
105     }
106 
107     if (!parcel.WriteString(sceneName_)) {
108         ANS_LOGE("Failed to write sceneName");
109         return false;
110     }
111 
112     bool valid = extraInfo_ ? true : false;
113     if (!parcel.WriteBool(valid)) {
114         ANS_LOGE("Failed to write the flag which indicate whether extraInfo is null");
115         return false;
116     }
117 
118     if (valid) {
119         if (!parcel.WriteParcelable(extraInfo_.get())) {
120             ANS_LOGE("Failed to write extraInfo");
121             return false;
122         }
123     }
124 
125     return true;
126 }
127 
Unmarshalling(Parcel & parcel)128 NotificationUnifiedGroupInfo *NotificationUnifiedGroupInfo::Unmarshalling(Parcel &parcel)
129 {
130     auto unifiedGroupInfo = new (std::nothrow) NotificationUnifiedGroupInfo();
131     if ((unifiedGroupInfo != nullptr) && !unifiedGroupInfo->ReadFromParcel(parcel)) {
132         delete unifiedGroupInfo;
133         unifiedGroupInfo = nullptr;
134     }
135 
136     return unifiedGroupInfo;
137 }
138 
ReadFromParcel(Parcel & parcel)139 bool NotificationUnifiedGroupInfo::ReadFromParcel(Parcel &parcel)
140 {
141     if (!parcel.ReadString(key_)) {
142         ANS_LOGE("Failed to read key");
143         return false;
144     }
145 
146     if (!parcel.ReadString(title_)) {
147         ANS_LOGE("Failed to read title");
148         return false;
149     }
150 
151     if (!parcel.ReadString(content_)) {
152         ANS_LOGE("Failed to read content");
153         return false;
154     }
155 
156     if (!parcel.ReadString(sceneName_)) {
157         ANS_LOGE("Failed to read sceneName");
158         return false;
159     }
160 
161     auto valid = parcel.ReadBool();
162     if (valid) {
163         extraInfo_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
164         if (!extraInfo_) {
165             ANS_LOGE("Failed to read extraInfo");
166             return false;
167         }
168     }
169 
170     return true;
171 }
172 }
173 }
174