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_sorting.h"
17 
18 #include "string_ex.h"
19 
20 namespace OHOS {
21 namespace Notification {
NotificationSorting()22 NotificationSorting::NotificationSorting()
23 {}
24 
~NotificationSorting()25 NotificationSorting::~NotificationSorting()
26 {}
27 
NotificationSorting(const NotificationSorting & sorting)28 NotificationSorting::NotificationSorting(const NotificationSorting &sorting)
29 {
30     key_ = sorting.key_;
31     ranking_ = sorting.ranking_;
32     importance_ = sorting.importance_;
33     isDisplayBadge_ = sorting.isDisplayBadge_;
34     isHiddenNotification_ = sorting.isHiddenNotification_;
35     visiblenessOverride_ = sorting.visiblenessOverride_;
36     groupKeyOverride_ = sorting.groupKeyOverride_;
37     slot_ = sorting.slot_;
38 }
39 
SetSlot(const sptr<NotificationSlot> & slot)40 void NotificationSorting::SetSlot(const sptr<NotificationSlot> &slot)
41 {
42     if (slot == nullptr) {
43         slot_ = new (std::nothrow) NotificationSlot(NotificationConstant::SlotType::OTHER);
44         return;
45     }
46     slot_ = slot;
47 }
48 
SetGroupKeyOverride(const std::string & groupKeyOverride)49 void NotificationSorting::SetGroupKeyOverride(const std::string &groupKeyOverride)
50 {
51     groupKeyOverride_ = groupKeyOverride;
52 }
53 
Dump() const54 std::string NotificationSorting::Dump() const
55 {
56     return "NotificationSorting{ "
57             "key = " + key_ +
58             ", ranking = " + std::to_string(ranking_) +
59             ", importance = " + std::to_string(importance_) +
60             ", visiblenessOverride = " + std::to_string(visiblenessOverride_) +
61             ", isDisplayBadge = " + (isDisplayBadge_ ? "true" : "false") +
62             ", isHiddenNotification = " + (isHiddenNotification_ ? "true" : "false") +
63             ", groupKeyOverride = " + groupKeyOverride_ +
64             ", slot = " + (slot_ != nullptr ? slot_->Dump() : "nullptr") +
65             " }";
66 }
67 
Marshalling(Parcel & parcel) const68 bool NotificationSorting::Marshalling(Parcel &parcel) const
69 {
70     if (!parcel.WriteString(key_)) {
71         ANS_LOGE("Can't write key");
72         return false;
73     }
74 
75     if (!parcel.WriteUint64(ranking_)) {
76         ANS_LOGE("Can't write ranking");
77         return false;
78     }
79 
80     if (!parcel.WriteInt32(importance_)) {
81         ANS_LOGE("Can't write importance");
82         return false;
83     }
84 
85     if (!parcel.WriteInt32(visiblenessOverride_)) {
86         ANS_LOGE("Can't write importance");
87         return false;
88     }
89 
90     if (!parcel.WriteBool(isDisplayBadge_)) {
91         ANS_LOGE("Can't write isDisplayBadge");
92         return false;
93     }
94 
95     if (!parcel.WriteBool(isHiddenNotification_)) {
96         ANS_LOGE("Can't write is HiddenNotification");
97         return false;
98     }
99 
100     if (!parcel.WriteString(groupKeyOverride_)) {
101         ANS_LOGE("Can't write groupKey groupKeyOverride");
102         return false;
103     }
104 
105     if (!parcel.WriteStrongParcelable(slot_)) {
106         ANS_LOGE("Can't write slot");
107         return false;
108     }
109 
110     return true;
111 }
112 
ReadFromParcel(Parcel & parcel)113 bool NotificationSorting::ReadFromParcel(Parcel &parcel)
114 {
115     // read key_
116     key_ = parcel.ReadString();
117 
118     // read ranking_
119     ranking_ = parcel.ReadUint64();
120 
121     // read importance_
122     importance_ = parcel.ReadInt32();
123 
124     // read visiblenessOverride_
125     visiblenessOverride_ = parcel.ReadInt32();
126 
127     // read isDisplayBadge_
128     isDisplayBadge_ = parcel.ReadBool();
129 
130     // read isHiddenNotification_
131     isHiddenNotification_ = parcel.ReadBool();
132 
133     // read groupKeyOverride_
134     groupKeyOverride_ = parcel.ReadString();
135 
136     // read slot_
137     slot_ = parcel.ReadStrongParcelable<NotificationSlot>();
138     if (!slot_) {
139         ANS_LOGE("read slot failed.");
140         return false;
141     }
142 
143     return true;
144 }
145 
Unmarshalling(Parcel & parcel)146 NotificationSorting *NotificationSorting::Unmarshalling(Parcel &parcel)
147 {
148     NotificationSorting *sorting = new (std::nothrow) NotificationSorting();
149     if (sorting && !sorting->ReadFromParcel(parcel)) {
150         ANS_LOGE("NotificationSorting Unmarshalling failed.");
151         delete sorting;
152         sorting = nullptr;
153     }
154     return sorting;
155 }
156 
SetKey(const std::string & key)157 void NotificationSorting::SetKey(const std::string &key)
158 {
159     key_ = key;
160 }
161 
SetImportance(const int32_t & importance)162 void NotificationSorting::SetImportance(const int32_t &importance)
163 {
164     importance_ = importance;
165 }
166 
SetRanking(const uint64_t & ranking)167 void NotificationSorting::SetRanking(const uint64_t &ranking)
168 {
169     ranking_ = ranking;
170 }
171 
SetVisiblenessOverride(const int32_t & visibleness)172 void NotificationSorting::SetVisiblenessOverride(const int32_t &visibleness)
173 {
174     visiblenessOverride_ = visibleness;
175 }
176 
SetDisplayBadge(const bool & isDisplayBadge)177 void NotificationSorting::SetDisplayBadge(const bool &isDisplayBadge)
178 {
179     isDisplayBadge_ = isDisplayBadge;
180 }
181 
SetHiddenNotification(const bool & isHiddenNotification)182 void NotificationSorting::SetHiddenNotification(const bool &isHiddenNotification)
183 {
184     isHiddenNotification_ = isHiddenNotification;
185 }
186 }  // namespace Notification
187 }  // namespace OHOS
188