• 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_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 NotificationTemplate();
76     if ((templ != nullptr) && !templ->ReadFromParcel(parcel)) {
77         delete templ;
78         templ = nullptr;
79     }
80 
81     return templ;
82 }
83 
ReadFromParcel(Parcel & parcel)84 bool NotificationTemplate::ReadFromParcel(Parcel &parcel)
85 {
86     if (!parcel.ReadString(templateName_)) {
87         ANS_LOGE("Failed to read template name");
88         return false;
89     }
90 
91     bool valid = parcel.ReadBool();
92     if (valid) {
93         templateData_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
94         if (!templateData_) {
95             ANS_LOGE("Failed to read template data");
96             return false;
97         }
98     }
99 
100     return true;
101 }
102 }  // namespace Notification
103 }  // namespace OHOS
104