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_template.h"
17 
18 #include "ans_log_wrapper.h"
19 #include "parcel.h"                 // for Parcel
20 #include "want_params.h"            // for WantParams
21 
22 namespace OHOS {
23 namespace Notification {
SetTemplateName(const std::string & name)24 void NotificationTemplate::SetTemplateName(const std::string &name)
25 {
26     templateName_ = name;
27 }
28 
GetTemplateName() const29 std::string NotificationTemplate::GetTemplateName() const
30 {
31     return templateName_;
32 }
33 
SetTemplateData(const std::shared_ptr<AAFwk::WantParams> & data)34 void NotificationTemplate::SetTemplateData(const std::shared_ptr<AAFwk::WantParams> &data)
35 {
36     templateData_ = data;
37 }
38 
GetTemplateData() const39 std::shared_ptr<AAFwk::WantParams> NotificationTemplate::GetTemplateData() const
40 {
41     return templateData_;
42 }
43 
Dump()44 std::string NotificationTemplate::Dump()
45 {
46     return "templateName = " + templateName_ +
47         ", templateData = " + (templateData_ ? "not null" : "null");
48 }
49 
Marshalling(Parcel & parcel) const50 bool NotificationTemplate::Marshalling(Parcel &parcel) const
51 {
52     if (!parcel.WriteString(templateName_)) {
53         ANS_LOGE("Failed to write text");
54         return false;
55     }
56 
57     bool valid = templateData_ ? true : false;
58     if (!parcel.WriteBool(valid)) {
59         ANS_LOGE("Failed to write the flag which indicate whether templateData is null");
60         return false;
61     }
62 
63     if (valid) {
64         if (!parcel.WriteParcelable(templateData_.get())) {
65             ANS_LOGE("Failed to write templateData");
66             return false;
67         }
68     }
69 
70     return true;
71 }
72 
Unmarshalling(Parcel & parcel)73 NotificationTemplate *NotificationTemplate::Unmarshalling(Parcel &parcel)
74 {
75     auto templ = new (std::nothrow) NotificationTemplate();
76     if (templ == nullptr) {
77         ANS_LOGE("Failed to create NotificationTemplate instance");
78         return nullptr;
79     }
80     if (!templ->ReadFromParcel(parcel)) {
81         delete templ;
82         templ = nullptr;
83     }
84 
85     return templ;
86 }
87 
ReadFromParcel(Parcel & parcel)88 bool NotificationTemplate::ReadFromParcel(Parcel &parcel)
89 {
90     if (!parcel.ReadString(templateName_)) {
91         ANS_LOGE("Failed to read template name");
92         return false;
93     }
94 
95     bool valid = parcel.ReadBool();
96     if (valid) {
97         templateData_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
98         if (!templateData_) {
99             ANS_LOGE("Failed to read template data");
100             return false;
101         }
102     }
103 
104     return true;
105 }
106 }  // namespace Notification
107 }  // namespace OHOS
108